ItemStackUtils.java 8.4 KB

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