ItemStackUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package me.km.utils;
  2. import java.util.ArrayList;
  3. import me.km.exception.IllegalItemStackStringException;
  4. import net.minecraft.entity.ai.attributes.AttributeModifier;
  5. import net.minecraft.inventory.EntityEquipmentSlot;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.JsonToNBT;
  8. import net.minecraft.nbt.NBTException;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. public class ItemStackUtils
  11. {
  12. // -----------------------------------------------------------------------------------
  13. // Attribute
  14. // -----------------------------------------------------------------------------------
  15. public enum Attribute
  16. {
  17. /**
  18. * value from 0 to 30;
  19. */
  20. ARMOR("generic.armor"),
  21. /**
  22. * value from 0 to 20;
  23. */
  24. ARMOR_TOUGHNESS("generic.armorToughness"),
  25. /**
  26. * value from 0 to 1.7E308;
  27. */
  28. ATTACK_DAMAGE("generic.attackDamage"),
  29. /**
  30. * value from 0 to 1;
  31. */
  32. KNOCKBACK_RESISTANCE("generic.knockbackResistance"),
  33. /**
  34. * value from 0 to 1.7E308;
  35. */
  36. MAX_HEALTH("generic.maxHealth"),
  37. /**
  38. * value from 0 to 1.7E308;
  39. */
  40. MOVEMENT_SPEED("generic.movementSpeed"),
  41. /**
  42. * value from 0 to 1024;
  43. */
  44. ATTACK_SPEED("generic.attackSpeed"),
  45. /**
  46. * value from -1024 to 1024;
  47. */
  48. LUCK("generic.luck");
  49. private final String name;
  50. Attribute(String name)
  51. {
  52. this.name = name;
  53. }
  54. public String getName()
  55. {
  56. return name;
  57. }
  58. }
  59. public enum Operation
  60. {
  61. ADD, MUL, MUL_CHANGED
  62. }
  63. public static void addAttribute(ItemStack stack, Attribute a, EntityEquipmentSlot slot, double amount, Operation op)
  64. {
  65. stack.addAttributeModifier(a.getName(), new AttributeModifier("modifier", amount, op.ordinal()), slot);
  66. }
  67. // -----------------------------------------------------------------------------------
  68. // Item-Flags
  69. // -----------------------------------------------------------------------------------
  70. public enum ItemFlag
  71. {
  72. /**
  73. * Setting to show/hide enchants
  74. */
  75. HIDE_ENCHANTS,
  76. /**
  77. * Setting to show/hide Attributes like Damage
  78. */
  79. HIDE_ATTRIBUTES,
  80. /**
  81. * Setting to show/hide the unbreakable State
  82. */
  83. HIDE_UNBREAKABLE,
  84. /**
  85. * Setting to show/hide what the ItemStack can break/destroy
  86. */
  87. HIDE_DESTROYS,
  88. /**
  89. * Setting to show/hide where this ItemStack can be build/placed on
  90. */
  91. HIDE_PLACED_ON,
  92. /**
  93. * Setting to show/hide potion effects on this ItemStack
  94. */
  95. HIDE_POTION_EFFECTS;
  96. public byte getBit()
  97. {
  98. return (byte) (1 << this.ordinal());
  99. }
  100. }
  101. public static void addItemFlag(ItemStack stack, ItemFlag... flags)
  102. {
  103. NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
  104. if(com == null) // this will never happen, but the compiler wants it
  105. {
  106. return;
  107. }
  108. int i = com.getInteger("HideFlags");
  109. for (ItemFlag f : flags)
  110. {
  111. i |= f.getBit(); // bitwise 'or'
  112. }
  113. com.setInteger("HideFlags", i);
  114. stack.setTagCompound(com);
  115. }
  116. public static void removeItemFlags(ItemStack stack, ItemFlag... flags)
  117. {
  118. NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
  119. if(com == null) // this will never happen, but the compiler wants it
  120. {
  121. return;
  122. }
  123. int i = com.getInteger("HideFlags");
  124. for (ItemFlag f : flags)
  125. {
  126. i &= ~f.getBit(); // bitwise 'and' with inversed bits
  127. }
  128. com.setInteger("HideFlags", i);
  129. stack.setTagCompound(com);
  130. }
  131. public static boolean hasItemFlag(ItemStack stack, ItemFlag flag)
  132. {
  133. if(!stack.hasTagCompound())
  134. {
  135. return false;
  136. }
  137. NBTTagCompound com = stack.getTagCompound();
  138. if(com == null) // this will never happen, but the compiler wants it
  139. {
  140. return false;
  141. }
  142. byte b = flag.getBit();
  143. return (com.getInteger("HideFlags") & b) == b;
  144. }
  145. // -----------------------------------------------------------------------------------
  146. // Converter
  147. // -----------------------------------------------------------------------------------
  148. public static String getNbtString(ItemStack stack)
  149. {
  150. return stack.writeToNBT(new NBTTagCompound()).toString();
  151. }
  152. public static ItemStack getStackFromNbtString(String s) throws IllegalItemStackStringException
  153. {
  154. try
  155. {
  156. NBTTagCompound c = JsonToNBT.getTagFromJson(s);
  157. return new ItemStack(c);
  158. }
  159. catch(NBTException ex)
  160. {
  161. throw new IllegalItemStackStringException(s);
  162. }
  163. }
  164. // -----------------------------------------------------------------------------------
  165. // Misc
  166. // -----------------------------------------------------------------------------------
  167. public static void setUnbreakable(ItemStack stack)
  168. {
  169. // TODO
  170. }
  171. public static void setLore(ItemStack stack, ArrayList<String> list)
  172. {
  173. // TODO
  174. }
  175. }