Advertisement
Guest User

Untitled

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