package me.km.utils; import java.util.ArrayList; import java.util.List; import me.km.exception.IllegalItemStackStringException; import net.minecraft.block.Block; import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.JsonToNBT; import net.minecraft.nbt.NBTException; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; public class ItemStackUtils { // ----------------------------------------------------------------------------------- // Attribute // ----------------------------------------------------------------------------------- public enum Attribute { /** * value from 0 to 30; */ ARMOR("generic.armor"), /** * value from 0 to 20; */ ARMOR_TOUGHNESS("generic.armorToughness"), /** * value from 0 to 1.7E308; */ ATTACK_DAMAGE("generic.attackDamage"), /** * value from 0 to 1; */ KNOCKBACK_RESISTANCE("generic.knockbackResistance"), /** * value from 0 to 1.7E308; */ MAX_HEALTH("generic.maxHealth"), /** * value from 0 to 1.7E308; */ MOVEMENT_SPEED("generic.movementSpeed"), /** * value from 0 to 1024; */ ATTACK_SPEED("generic.attackSpeed"), /** * value from -1024 to 1024; */ LUCK("generic.luck"); private final String name; Attribute(String name) { this.name = name; } public String getName() { return name; } } public enum Operation { ADD, MUL, MUL_CHANGED } public static void addAttribute(ItemStack stack, Attribute a, EntityEquipmentSlot slot, double amount, Operation op) { stack.addAttributeModifier(a.getName(), new AttributeModifier("modifier", amount, op.ordinal()), slot); } // ----------------------------------------------------------------------------------- // Item-Flags // ----------------------------------------------------------------------------------- public enum ItemFlag { /** * Setting to show/hide enchants */ HIDE_ENCHANTS, /** * Setting to show/hide Attributes like Damage */ HIDE_ATTRIBUTES, /** * Setting to show/hide the unbreakable State */ HIDE_UNBREAKABLE, /** * Setting to show/hide what the ItemStack can break/destroy */ HIDE_DESTROYS, /** * Setting to show/hide where this ItemStack can be build/placed on */ HIDE_PLACED_ON, /** * Setting to show/hide potion effects on this ItemStack */ HIDE_POTION_EFFECTS; public byte getBit() { return (byte) (1 << this.ordinal()); } } public static void addItemFlag(ItemStack stack, ItemFlag... flags) { NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound(); if(com == null) // this will never happen, but the compiler wants it { return; } int i = com.getInteger("HideFlags"); for (ItemFlag f : flags) { i |= f.getBit(); // bitwise 'or' } com.setInteger("HideFlags", i); stack.setTagCompound(com); } public static void removeItemFlags(ItemStack stack, ItemFlag... flags) { NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound(); if(com == null) // this will never happen, but the compiler wants it { return; } int i = com.getInteger("HideFlags"); for (ItemFlag f : flags) { i &= ~f.getBit(); // bitwise 'and' with inversed bits } com.setInteger("HideFlags", i); stack.setTagCompound(com); } public static boolean hasItemFlag(ItemStack stack, ItemFlag flag) { if(!stack.hasTagCompound()) { return false; } NBTTagCompound com = stack.getTagCompound(); if(com == null) // this will never happen, but the compiler wants it { return false; } byte b = flag.getBit(); return (com.getInteger("HideFlags") & b) == b; } // ----------------------------------------------------------------------------------- // Converter // ----------------------------------------------------------------------------------- public static String getNbtString(ItemStack stack) { return stack.writeToNBT(new NBTTagCompound()).toString(); } public static ItemStack getStackFromNbtString(String s) throws IllegalItemStackStringException { try { NBTTagCompound c = JsonToNBT.getTagFromJson(s); return new ItemStack(c); } catch(NBTException ex) { throw new IllegalItemStackStringException(ex.getMessage()); } } // ----------------------------------------------------------------------------------- // Misc // ----------------------------------------------------------------------------------- public static void setLore(ItemStack stack, List list) { NBTTagCompound com = stack.getOrCreateSubCompound("display"); NBTTagList nbtList = new NBTTagList(); list.forEach(s -> nbtList.appendTag(new NBTTagString(s))); com.setTag("Lore", nbtList); } public static void addLore(ItemStack stack, String s) { NBTTagCompound com = stack.getOrCreateSubCompound("display"); NBTTagList nbtList = com.getTagList("Lore", 8); nbtList.appendTag(new NBTTagString(s)); com.setTag("Lore", nbtList); } public static List getLore(ItemStack stack) { NBTTagCompound com = stack.getOrCreateSubCompound("display"); NBTTagList nbtList = com.getTagList("Lore", 8); ArrayList list = new ArrayList<>(); for(int i = 0; i < nbtList.tagCount(); i++) { list.add(nbtList.getStringTagAt(i)); } return list; } public static void addLore(ItemStack stack, String s, int i) { NBTTagCompound com = stack.getOrCreateSubCompound("display"); NBTTagList list = com.getTagList("Lore", 8); if(i >= list.tagCount()) { list.appendTag(new NBTTagString(s)); } else { list.set(i, new NBTTagString(s)); } com.setTag("Lore", list); } public static void drop(World w, BlockPos pos, ItemStack stack) { Block.spawnAsEntity(w, pos, stack); } public static Item getItem(String stack) throws IllegalItemStackStringException { Item item = Item.getByNameOrId(stack); if(item == null) { Block b = Block.getBlockFromName(stack); if(b == null) { throw new IllegalItemStackStringException(stack); } return Item.getItemFromBlock(b); } return item; } }