ItemStackUtils.java 7.0 KB

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