Advertisement
Guest User

Untitled

a guest
May 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. private ItemStack item;
  2.  
  3. public ItemBuilder(Material material, int amount) {
  4. item = new ItemStack(material, amount);
  5. }
  6.  
  7. public ItemBuilder(Material material, int amount, int data) {
  8. item = new ItemStack(material, amount, (short) data);
  9. }
  10.  
  11. public ItemBuilder(ItemStack item) {
  12. this.item = item;
  13. }
  14.  
  15. public ItemBuilder setData(int data) {
  16. item.setDurability((short) data);
  17. return this;
  18. }
  19.  
  20. public ItemBuilder setMaterial(Material m) {
  21. item.setType(m);
  22. return this;
  23. }
  24.  
  25. public ItemBuilder setAmount(int amount) {
  26. item.setAmount(amount);
  27. return this;
  28. }
  29.  
  30. public ItemBuilder setName(String name) {
  31. ItemMeta m = item.getItemMeta();
  32. m.setDisplayName(name);
  33. item.setItemMeta(m);
  34. return this;
  35. }
  36.  
  37. public ItemBuilder setLore(String... lore) {
  38. ItemMeta m = item.getItemMeta();
  39. m.setLore(Arrays.asList(lore));
  40. item.setItemMeta(m);
  41. return this;
  42. }
  43.  
  44. public ItemBuilder enchant(Enchantment ench, int lvl) {
  45. item.addUnsafeEnchantment(ench, lvl);
  46. return this;
  47. }
  48.  
  49. public ItemBuilder addFlags(ItemFlag... flag) {
  50. ItemMeta m = item.getItemMeta();
  51. m.addItemFlags(flag);
  52. item.setItemMeta(m);
  53. return this;
  54. }
  55.  
  56. public ItemBuilder setLeatherColor(Color color) {
  57. LeatherArmorMeta m = (LeatherArmorMeta) item.getItemMeta();
  58. m.setColor(color);
  59. item.setItemMeta(m);
  60. return this;
  61. }
  62.  
  63. public ItemBuilder setSkullOwner(String owner) {
  64. SkullMeta m = (SkullMeta) item.getItemMeta();
  65. m.setOwner(owner);
  66. item.setItemMeta(m);
  67. return this;
  68. }
  69.  
  70. public ItemBuilder setPotionType(PotionEffectType type) {
  71. PotionMeta m = (PotionMeta) item.getItemMeta();
  72. m.setMainEffect(type);
  73. item.setItemMeta(m);
  74. return this;
  75. }
  76.  
  77. public ItemBuilder setBookAuthor(String author) {
  78. BookMeta m = (BookMeta) item.getItemMeta();
  79. m.setAuthor(author);
  80. item.setItemMeta(m);
  81. return this;
  82. }
  83.  
  84. public ItemBuilder setBookContent(String... pages) {
  85. BookMeta m = (BookMeta) item.getItemMeta();
  86. m.setPages(pages);
  87. item.setItemMeta(m);
  88. return this;
  89. }
  90.  
  91. public ItemBuilder setBookTitle(String title) {
  92. BookMeta m = (BookMeta) item.getItemMeta();
  93. m.setTitle(title);
  94. item.setItemMeta(m);
  95. return this;
  96. }
  97.  
  98. public ItemBuilder setBookMeta(String title, String author, String... pages) {
  99. BookMeta m = (BookMeta) item.getItemMeta();
  100. m.setTitle(title);
  101. m.setAuthor(author);
  102. m.setPages(pages);
  103. item.setItemMeta(m);
  104. return this;
  105. }
  106.  
  107. public ItemBuilder setEggType(EntityType type) {
  108. if ((item != null) && (item.getType() == Material.MONSTER_EGG) && (type != null) && (type.getName() != null)) {
  109. try {
  110. String version = Bukkit.getServer().getClass().toString().split("\\.")[3];
  111. Class<?> craftItemStack = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
  112. Object nmsItemStack = craftItemStack.getDeclaredMethod("asNMSCopy", ItemStack.class).invoke(null, item);
  113. Object nbtTagCompound = Class.forName("net.minecraft.server." + version + ".NBTTagCompound").newInstance();
  114. Field nbtTagCompoundField = nmsItemStack.getClass().getDeclaredField("tag");
  115. nbtTagCompoundField.setAccessible(true);
  116. nbtTagCompound.getClass().getMethod("setString", String.class, String.class).invoke(nbtTagCompound, "id", type.getName());
  117. nbtTagCompound.getClass().getMethod("set", String.class, Class.forName("net.minecraft.server." + version + ".NBTBase")).invoke(nbtTagCompoundField.get(nmsItemStack), "EntityTag", nbtTagCompound);
  118. item = (ItemStack) craftItemStack.getDeclaredMethod("asCraftMirror", nmsItemStack.getClass()).invoke(null, nmsItemStack);
  119. } catch (Exception ex) {
  120. ex.printStackTrace();
  121. }
  122. }
  123. return this;
  124. }
  125.  
  126. public ItemBuilder setSkullTexture(String base64) {
  127. ItemMeta m = item.getItemMeta();
  128. GameProfile profile = new GameProfile(UUID.randomUUID(), null);
  129. profile.getProperties().put("textures", new Property("textures", base64));
  130. Field profileField = null;
  131. try {
  132. profileField = m.getClass().getDeclaredField("profile");
  133. profileField.setAccessible(true);
  134. profileField.set(m, profile);
  135. } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) {
  136. e1.printStackTrace();
  137. }
  138. item.setItemMeta(m);
  139. return this;
  140. }
  141.  
  142. public ItemStack build() {
  143. return item;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement