ItemStackUtils.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package me.km.utils;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import me.km.exception.IllegalItemStackStringException;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.entity.ai.attributes.AttributeModifier;
  7. import net.minecraft.inventory.EntityEquipmentSlot;
  8. import net.minecraft.item.Item;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.nbt.JsonToNBT;
  11. import net.minecraft.nbt.NBTException;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.nbt.NBTTagList;
  14. import net.minecraft.nbt.NBTTagString;
  15. import net.minecraft.util.math.BlockPos;
  16. import net.minecraft.world.World;
  17. public class ItemStackUtils
  18. {
  19. // -----------------------------------------------------------------------------------
  20. // Attribute
  21. // -----------------------------------------------------------------------------------
  22. public enum Attribute
  23. {
  24. /**
  25. * value from 0 to 30;
  26. */
  27. ARMOR("generic.armor"),
  28. /**
  29. * value from 0 to 20;
  30. */
  31. ARMOR_TOUGHNESS("generic.armorToughness"),
  32. /**
  33. * value from 0 to 1.7E308;
  34. */
  35. ATTACK_DAMAGE("generic.attackDamage"),
  36. /**
  37. * value from 0 to 1;
  38. */
  39. KNOCKBACK_RESISTANCE("generic.knockbackResistance"),
  40. /**
  41. * value from 0 to 1.7E308;
  42. */
  43. MAX_HEALTH("generic.maxHealth"),
  44. /**
  45. * value from 0 to 1.7E308;
  46. */
  47. MOVEMENT_SPEED("generic.movementSpeed"),
  48. /**
  49. * value from 0 to 1024;
  50. */
  51. ATTACK_SPEED("generic.attackSpeed"),
  52. /**
  53. * value from -1024 to 1024;
  54. */
  55. LUCK("generic.luck");
  56. private final String name;
  57. Attribute(String name)
  58. {
  59. this.name = name;
  60. }
  61. public String getName()
  62. {
  63. return name;
  64. }
  65. }
  66. public enum Operation
  67. {
  68. ADD, MUL, MUL_CHANGED
  69. }
  70. public static void addAttribute(ItemStack stack, Attribute a, EntityEquipmentSlot slot, double amount, Operation op)
  71. {
  72. stack.addAttributeModifier(a.getName(), new AttributeModifier("modifier", amount, op.ordinal()), slot);
  73. }
  74. // -----------------------------------------------------------------------------------
  75. // Item-Flags
  76. // -----------------------------------------------------------------------------------
  77. public enum ItemFlag
  78. {
  79. /**
  80. * Setting to show/hide enchants
  81. */
  82. HIDE_ENCHANTS,
  83. /**
  84. * Setting to show/hide Attributes like Damage
  85. */
  86. HIDE_ATTRIBUTES,
  87. /**
  88. * Setting to show/hide the unbreakable State
  89. */
  90. HIDE_UNBREAKABLE,
  91. /**
  92. * Setting to show/hide what the ItemStack can break/destroy
  93. */
  94. HIDE_DESTROYS,
  95. /**
  96. * Setting to show/hide where this ItemStack can be build/placed on
  97. */
  98. HIDE_PLACED_ON,
  99. /**
  100. * Setting to show/hide potion effects on this ItemStack
  101. */
  102. HIDE_POTION_EFFECTS;
  103. public byte getBit()
  104. {
  105. return (byte) (1 << this.ordinal());
  106. }
  107. }
  108. public static void addItemFlag(ItemStack stack, ItemFlag... flags)
  109. {
  110. NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
  111. if(com == null) // this will never happen, but the compiler wants it
  112. {
  113. return;
  114. }
  115. int i = com.getInteger("HideFlags");
  116. for (ItemFlag f : flags)
  117. {
  118. i |= f.getBit(); // bitwise 'or'
  119. }
  120. com.setInteger("HideFlags", i);
  121. stack.setTagCompound(com);
  122. }
  123. public static void removeItemFlags(ItemStack stack, ItemFlag... flags)
  124. {
  125. NBTTagCompound com = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
  126. if(com == null) // this will never happen, but the compiler wants it
  127. {
  128. return;
  129. }
  130. int i = com.getInteger("HideFlags");
  131. for (ItemFlag f : flags)
  132. {
  133. i &= ~f.getBit(); // bitwise 'and' with inversed bits
  134. }
  135. com.setInteger("HideFlags", i);
  136. stack.setTagCompound(com);
  137. }
  138. public static boolean hasItemFlag(ItemStack stack, ItemFlag flag)
  139. {
  140. if(!stack.hasTagCompound())
  141. {
  142. return false;
  143. }
  144. NBTTagCompound com = stack.getTagCompound();
  145. if(com == null) // this will never happen, but the compiler wants it
  146. {
  147. return false;
  148. }
  149. byte b = flag.getBit();
  150. return (com.getInteger("HideFlags") & b) == b;
  151. }
  152. // -----------------------------------------------------------------------------------
  153. // Converter
  154. // -----------------------------------------------------------------------------------
  155. public static String getNbtString(ItemStack stack)
  156. {
  157. return stack.writeToNBT(new NBTTagCompound()).toString();
  158. }
  159. public static ItemStack getStackFromNbtString(String s) throws IllegalItemStackStringException
  160. {
  161. try
  162. {
  163. NBTTagCompound c = JsonToNBT.getTagFromJson(s);
  164. return new ItemStack(c);
  165. }
  166. catch(NBTException ex)
  167. {
  168. throw new IllegalItemStackStringException(ex.getMessage());
  169. }
  170. }
  171. // -----------------------------------------------------------------------------------
  172. // Misc
  173. // -----------------------------------------------------------------------------------
  174. public static void setLore(ItemStack stack, List<String> list)
  175. {
  176. NBTTagCompound com = stack.getOrCreateSubCompound("display");
  177. NBTTagList nbtList = new NBTTagList();
  178. list.forEach(s -> nbtList.appendTag(new NBTTagString(s)));
  179. com.setTag("Lore", nbtList);
  180. }
  181. public static void addLore(ItemStack stack, String s)
  182. {
  183. NBTTagCompound com = stack.getOrCreateSubCompound("display");
  184. NBTTagList nbtList = com.getTagList("Lore", 8);
  185. nbtList.appendTag(new NBTTagString(s));
  186. com.setTag("Lore", nbtList);
  187. }
  188. public static List<String> getLore(ItemStack stack)
  189. {
  190. NBTTagCompound com = stack.getOrCreateSubCompound("display");
  191. NBTTagList nbtList = com.getTagList("Lore", 8);
  192. ArrayList<String> list = new ArrayList<>();
  193. for(int i = 0; i < nbtList.tagCount(); i++)
  194. {
  195. list.add(nbtList.getStringTagAt(i));
  196. }
  197. return list;
  198. }
  199. public static void addLore(ItemStack stack, String s, int i)
  200. {
  201. NBTTagCompound com = stack.getOrCreateSubCompound("display");
  202. NBTTagList list = com.getTagList("Lore", 8);
  203. if(i >= list.tagCount())
  204. {
  205. list.appendTag(new NBTTagString(s));
  206. }
  207. else
  208. {
  209. list.set(i, new NBTTagString(s));
  210. }
  211. com.setTag("Lore", list);
  212. }
  213. public static void drop(World w, BlockPos pos, ItemStack stack)
  214. {
  215. Block.spawnAsEntity(w, pos, stack);
  216. }
  217. public static Item getItem(String stack) throws IllegalItemStackStringException
  218. {
  219. Item item = Item.getByNameOrId(stack);
  220. if(item == null)
  221. {
  222. Block b = Block.getBlockFromName(stack);
  223. if(b == null)
  224. {
  225. throw new IllegalItemStackStringException(stack);
  226. }
  227. return Item.getItemFromBlock(b);
  228. }
  229. return item;
  230. }
  231. }