Advertisement
Lisenochek

Untitled

Sep 3rd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. package ru.proempire.lisenochek.pj.anarchy.utils;
  2.  
  3. import com.comphenix.protocol.utility.MinecraftReflection;
  4. import com.comphenix.protocol.wrappers.nbt.NbtFactory;
  5. import com.google.common.collect.Lists;
  6. import org.bukkit.Color;
  7. import org.bukkit.Material;
  8. import org.bukkit.enchantments.Enchantment;
  9. import org.bukkit.inventory.ItemFlag;
  10. import org.bukkit.inventory.ItemStack;
  11. import org.bukkit.inventory.meta.ItemMeta;
  12. import org.bukkit.inventory.meta.LeatherArmorMeta;
  13. import org.bukkit.util.io.BukkitObjectInputStream;
  14. import org.bukkit.util.io.BukkitObjectOutputStream;
  15. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  16.  
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22.  
  23. public class ISBuilder {
  24.  
  25. private ItemStack stack;
  26. private ItemMeta meta;
  27.  
  28. private ISBuilder(ItemStack stack) {
  29. this.stack = MinecraftReflection.isCraftItemStack(stack) ? stack : MinecraftReflection.getBukkitItemStack(stack);
  30. this.meta = stack.getItemMeta();
  31. }
  32.  
  33. public static ISBuilder getBuilder(ItemStack stack) {
  34. return new ISBuilder(stack);
  35. }
  36.  
  37. public static ISBuilder getBuilder(Material material, int data, int count, String name, String... lore) {
  38. return getBuilder(material, data, count, name, Arrays.asList(lore));
  39. }
  40.  
  41. public static ISBuilder getBuilder(Material material, int data, int count, String name, List<String> lore) {
  42. ItemStack stack = new ItemStack(material, count, (short) data);
  43. ItemMeta meta = stack.getItemMeta();
  44. meta.setDisplayName(Utils.stripColor(name));
  45. List<String> l = new ArrayList<>();
  46. for (String s : lore) if (s != null) l.add(Utils.stripColor(s));
  47. meta.setLore(l);
  48. stack.setItemMeta(meta);
  49. return new ISBuilder(stack);
  50. }
  51.  
  52. public static String serialise(List<ItemStack> list) {
  53. try {
  54. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  55. BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
  56. dataOutput.writeInt(list.size());
  57. for (ItemStack stack : list) dataOutput.writeObject(stack);
  58. dataOutput.close();
  59. return Base64Coder.encodeLines(outputStream.toByteArray());
  60. } catch (Exception e) {
  61. throw new IllegalStateException("Unable to save item stacks", e);
  62. }
  63. }
  64.  
  65. public static String serialise(ItemStack stack) {
  66. try {
  67. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  68. BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
  69. dataOutput.writeInt(1);
  70. dataOutput.writeObject(stack);
  71. dataOutput.close();
  72. return Base64Coder.encodeLines(outputStream.toByteArray());
  73. } catch (Exception e) {
  74. throw new IllegalStateException("Unable to save item stack", e);
  75. }
  76. }
  77.  
  78. public static List<ItemStack> deserialiseStacks(String stacksData) {
  79.  
  80. if (stacksData == null) return new ArrayList<>();
  81.  
  82. ItemStack[] stacks = null;
  83.  
  84. try {
  85. ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(stacksData));
  86. BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
  87. stacks = new ItemStack[dataInput.readInt()];
  88. for (int i = 0; i < stacks.length; i++) stacks[i] = (ItemStack) dataInput.readObject();
  89. dataInput.close();
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93.  
  94. return Lists.newArrayList(stacks);
  95. }
  96.  
  97. public static ItemStack deserialiseStack(String stackData) {
  98.  
  99. ItemStack stack = null;
  100.  
  101. try {
  102. ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(stackData));
  103. BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
  104. ItemStack[] s = new ItemStack[dataInput.readInt()];
  105. for (int i = 0; i < s.length; i++) s[i] = (ItemStack) dataInput.readObject();
  106. stack = s[0];
  107. dataInput.close();
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111.  
  112. return stack;
  113. }
  114.  
  115. public ItemStack getStack() {
  116. return stack;
  117. }
  118.  
  119. public ISBuilder setUnbreakable(boolean unbreakable) {
  120. meta.setUnbreakable(unbreakable);
  121. stack.setItemMeta(meta);
  122. return this;
  123. }
  124.  
  125. public ISBuilder setDisplayName(String displayName) {
  126. meta.setDisplayName(Utils.stripColor(displayName));
  127. stack.setItemMeta(meta);
  128. return this;
  129. }
  130.  
  131. public ISBuilder setEnchantGlow() {
  132. meta.addEnchant(Enchantment.ARROW_DAMAGE, 1, true);
  133. stack.setItemMeta(meta);
  134. return this;
  135. }
  136.  
  137. public ISBuilder setTag(String tag, Object value) {
  138. if (stack != null && stack.getType() != Material.AIR) NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).putObject(tag, value);
  139. return this;
  140. }
  141.  
  142. public boolean hasTag(String tag) {
  143. return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getObject(tag) != null;
  144. }
  145.  
  146. public int getIntTag(String tag) {
  147. return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getInteger(tag);
  148. }
  149.  
  150. public String getStringTag(String tag) {
  151. return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getString(tag);
  152. }
  153.  
  154. public double getDoubleTag(String tag) {
  155. return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getDouble(tag);
  156. }
  157.  
  158. public boolean getBooleanTag(String tag) {
  159. return Boolean.parseBoolean(NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getString(tag));
  160. }
  161.  
  162. public ISBuilder removeTag(String tag) {
  163. if (stack != null && stack.getType() != Material.AIR) NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).remove(tag);
  164. return this;
  165. }
  166.  
  167. public ISBuilder addEnchantment(Enchantment enchantment, int level) {
  168. if (enchantment == null) return this;
  169. meta.addEnchant(enchantment, level, true);
  170. stack.setItemMeta(meta);
  171. return this;
  172. }
  173.  
  174. public ISBuilder setAmount(int amount) {
  175. stack.setAmount(amount);
  176. return this;
  177. }
  178.  
  179. public ISBuilder setColor(Color color) {
  180. ((LeatherArmorMeta) meta).setColor(color);
  181. stack.setItemMeta(meta);
  182. return this;
  183. }
  184.  
  185. public ISBuilder formatLore(String oldReplace, Object newReplace) {
  186. List<String> list = new ArrayList<>();
  187. for (String s : meta.getLore()) if (oldReplace != null) list.add(Utils.stripColor(s.replace(oldReplace, String.valueOf(newReplace))));
  188. meta.setLore(list);
  189. stack.setItemMeta(meta);
  190. return this;
  191. }
  192.  
  193. public ISBuilder setLore(String... lore) {
  194. List<String> list = new ArrayList<>();
  195. if (lore != null) for (String s : lore) list.add(Utils.stripColor(s));
  196. meta.setLore(list);
  197. stack.setItemMeta(meta);
  198. return this;
  199. }
  200.  
  201. public ISBuilder addLore(String... lore) {
  202. List<String> list = meta.hasLore() ? meta.getLore() : new ArrayList<>();
  203. if (lore != null) for (String s : lore) list.add(Utils.stripColor(s));
  204. meta.setLore(list);
  205. stack.setItemMeta(meta);
  206. return this;
  207. }
  208.  
  209. public ISBuilder addLore(List<String> lore) {
  210. List<String> list = meta.hasLore() ? meta.getLore() : new ArrayList<>();
  211. if (lore != null) for (String s : lore) list.add(Utils.stripColor(s));
  212. meta.setLore(list);
  213. stack.setItemMeta(meta);
  214. return this;
  215. }
  216.  
  217. public ISBuilder hideFlags() {
  218. for (ItemFlag flag : new ItemFlag[]{ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_POTION_EFFECTS}) meta.addItemFlags(flag);
  219. stack.setItemMeta(meta);
  220. return this;
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement