package me.km.utils; import java.util.ArrayList; import me.km.exception.IllegalItemStackStringException; import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.item.ItemStack; import net.minecraft.nbt.JsonToNBT; import net.minecraft.nbt.NBTException; import net.minecraft.nbt.NBTTagCompound; 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(s); } } // ----------------------------------------------------------------------------------- // Misc // ----------------------------------------------------------------------------------- public static void setUnbreakable(ItemStack stack) { // TODO } public static void setLore(ItemStack stack, ArrayList list) { // TODO } }