ItemStackBuilder.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package me.km.utils;
  2. import java.util.ArrayList;
  3. import me.km.utils.ItemStackUtils.ItemFlag;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.init.Enchantments;
  6. import net.minecraft.item.Item;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.util.math.BlockPos;
  9. import net.minecraft.world.World;
  10. public class ItemStackBuilder
  11. {
  12. private final ItemStack stack;
  13. public ItemStackBuilder(Block b, int amount)
  14. {
  15. stack = new ItemStack(b, amount);
  16. }
  17. public ItemStackBuilder(Block b, int amount, int meta)
  18. {
  19. stack = new ItemStack(b, amount, meta);
  20. }
  21. public ItemStackBuilder(Item i, int amount)
  22. {
  23. stack = new ItemStack(i, amount);
  24. }
  25. public ItemStackBuilder(Item i, int amount, int meta)
  26. {
  27. stack = new ItemStack(i, amount, meta);
  28. }
  29. public ItemStackBuilder(ItemStack stack, int amount)
  30. {
  31. this.stack = stack.copy();
  32. this.stack.setCount(amount);
  33. }
  34. public ItemStack build()
  35. {
  36. return stack;
  37. }
  38. public void drop(World w, BlockPos l)
  39. {
  40. Block.spawnAsEntity(w, l, stack);
  41. }
  42. public ItemStackBuilder setName(String name)
  43. {
  44. stack.setStackDisplayName(name);
  45. return this;
  46. }
  47. public ItemStackBuilder addToName(String name)
  48. {
  49. stack.setStackDisplayName(stack.getDisplayName() + name);
  50. return this;
  51. }
  52. public ItemStackBuilder addLore(String line)
  53. {
  54. ItemStackUtils.addLore(stack, line);
  55. return this;
  56. }
  57. public ItemStackBuilder addLimitedLore(String whole, int limit, String addition)
  58. {
  59. ItemStackUtils.setLore(stack, buildLimitedLore(whole, limit, addition));
  60. return this;
  61. }
  62. public ItemStackBuilder addLimitedLore(String whole, String addition)
  63. {
  64. return addLimitedLore(whole, 25, addition);
  65. }
  66. public static ArrayList<String> buildLimitedLore(String whole, int limit, String addition)
  67. {
  68. ArrayList<String> list = new ArrayList<>();
  69. int pos = 0;
  70. int space;
  71. while(pos + limit < whole.length())
  72. {
  73. space = whole.lastIndexOf(" ", pos + limit);
  74. if(space == -1 || pos > space)
  75. {
  76. space = whole.indexOf(" ", pos + limit);
  77. }
  78. list.add(addition + whole.substring(pos, space));
  79. pos = space + 1;
  80. }
  81. list.add(addition + whole.substring(pos));
  82. return list;
  83. }
  84. public ItemStackBuilder hideTags()
  85. {
  86. ItemStackUtils.addItemFlag(stack, ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ENCHANTS);
  87. return this;
  88. }
  89. public ItemStackBuilder addGlow()
  90. {
  91. stack.addEnchantment(Enchantments.PROTECTION, 1);
  92. return this;
  93. }
  94. }