Advertisement
lNockl

ItemStack Builder

Dec 7th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.20 KB | None | 0 0
  1. package me.filipenock.cypherapi.Utils;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.lang.reflect.Field;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import java.util.UUID;
  11.  
  12. import org.apache.commons.codec.binary.Base64;
  13. import org.bukkit.Color;
  14. import org.bukkit.DyeColor;
  15. import org.bukkit.Material;
  16. import org.bukkit.block.banner.Pattern;
  17. import org.bukkit.block.banner.PatternType;
  18. import org.bukkit.enchantments.Enchantment;
  19. import org.bukkit.inventory.ItemFlag;
  20. import org.bukkit.inventory.ItemStack;
  21. import org.bukkit.inventory.meta.BannerMeta;
  22. import org.bukkit.inventory.meta.ItemMeta;
  23. import org.bukkit.inventory.meta.LeatherArmorMeta;
  24. import org.bukkit.inventory.meta.PotionMeta;
  25. import org.bukkit.inventory.meta.SkullMeta;
  26. import org.bukkit.potion.Potion;
  27. import org.bukkit.potion.PotionEffect;
  28. import org.bukkit.potion.PotionEffectType;
  29. import org.bukkit.potion.PotionType;
  30. import org.bukkit.util.io.BukkitObjectInputStream;
  31. import org.bukkit.util.io.BukkitObjectOutputStream;
  32. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  33.  
  34. import com.mojang.authlib.GameProfile;
  35. import com.mojang.authlib.properties.Property;
  36.  
  37. @SuppressWarnings("all")
  38. public class ItemBuilder {
  39.  
  40.     private ItemStack is;
  41.  
  42.     public ItemBuilder(Material s, int data) {
  43.         is = new ItemStack(s, 1, (short) 1, (byte) data);
  44.     }
  45.  
  46.     public ItemBuilder(Material s) {
  47.         is = new ItemStack(s);
  48.     }
  49.  
  50.     public ItemBuilder setName(String name) {
  51.         ItemMeta ism = is.getItemMeta();
  52.         ism.setDisplayName(name.replace("&", "§"));
  53.         is.setItemMeta(ism);
  54.         return this;
  55.     }
  56.    
  57.     public ItemBuilder addPattern(PatternType type, DyeColor color) {
  58.         BannerMeta meta = (BannerMeta) is.getItemMeta();
  59.         meta.addPattern(new Pattern(color, type));
  60.         is.setItemMeta(meta);
  61.         return this;
  62.        
  63.     }
  64.    
  65.     public ItemBuilder setBasecolor(DyeColor color) {
  66.         BannerMeta meta = (BannerMeta) is.getItemMeta();
  67.         meta.setBaseColor(color);
  68.         is.setItemMeta(meta);
  69.         return this;
  70.     }
  71.  
  72.     public ItemBuilder setSkullOwner(String owner) {
  73.         try {
  74.             SkullMeta im = (SkullMeta) is.getItemMeta();
  75.             im.setOwner(owner);
  76.             is.setItemMeta(im);
  77.         } catch (ClassCastException expected) {
  78.         }
  79.         return this;
  80.     }
  81.    
  82.     public ItemBuilder setLatherColor(Color color) {
  83.         LeatherArmorMeta im = (LeatherArmorMeta) is.getItemMeta();
  84.         im.setColor(color);
  85.         is.setItemMeta(im);
  86.         return this;
  87.     }
  88.    
  89.     public ItemBuilder removeFlags() {
  90.         ItemMeta im = is.getItemMeta();
  91.         im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
  92.         im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  93.         im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
  94.         im.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
  95.         is.setItemMeta(im);
  96.         return this;
  97.     }
  98.  
  99.     public ItemBuilder addEnchant(Enchantment ench, int level) {
  100.         ItemMeta im = is.getItemMeta();
  101.         im.addEnchant(ench, level, true);
  102.         is.setItemMeta(im);
  103.         return this;
  104.     }
  105.  
  106.     public ItemBuilder addLoreLine(String line) {
  107.         ItemMeta im = is.getItemMeta();
  108.         List<String> lore = new ArrayList<>();
  109.         if (im.hasLore())
  110.             lore = new ArrayList<>(im.getLore());
  111.         lore.add(line);
  112.         im.setLore(lore);
  113.         is.setItemMeta(im);
  114.         return this;
  115.     }
  116.  
  117.     public ItemBuilder addLoreLine(String line, int pos) {
  118.         ItemMeta im = is.getItemMeta();
  119.         List<String> lore = new ArrayList<>(im.getLore());
  120.         lore.set(pos, line);
  121.         im.setLore(lore);
  122.         is.setItemMeta(im);
  123.         return this;
  124.     }
  125.  
  126.     public ItemBuilder addPotionEffect(PotionEffectType type, int duration, int amplifier) {
  127.         PotionMeta meta = (PotionMeta) is.getItemMeta();
  128.         meta.addCustomEffect(new PotionEffect(type, duration, amplifier), true);
  129.         is.setItemMeta(meta);
  130.         return this;
  131.     }
  132.  
  133.     public ItemStack build() {
  134.         return is;
  135.     }
  136.  
  137.     public static boolean isEqual(ItemStack item1, ItemStack item2) {
  138.         if (item1 == null)
  139.             return false;
  140.         if (item2 == null)
  141.             return false;
  142.         if (item1.getType() == null)
  143.             return false;
  144.         if (item2.getType() == null)
  145.             return false;
  146.         if (item1.getType() == item2.getType()) {
  147.             if (item1.getItemMeta().hasDisplayName()) {
  148.                 if (item2.getItemMeta().hasDisplayName()) {
  149.                     if (item1.getItemMeta().getDisplayName().equals(item2.getItemMeta().getDisplayName())) {
  150.                         if (item1.getData().getData() == item2.getData().getData()) {
  151.                             return true;
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.         return false;
  158.     }
  159.  
  160.     public static String DeSerialize(ItemStack item) {
  161.         String serialized = "";
  162.         StringBuilder builder = new StringBuilder(serialized);
  163.         builder.append("id=" + item.getType().name() + " : ");
  164.         builder.append("data=" + item.getData().getData() + " : ");
  165.         if (item.getItemMeta().hasDisplayName()) {
  166.             builder.append("name=" + item.getItemMeta().getDisplayName().replace("§", "&") + " : ");
  167.         }
  168.         if (item.getItemMeta().hasLore()) {
  169.             List<String> lore = item.getItemMeta().getLore();
  170.             List<String> formated = new ArrayList<>();
  171.             for (String l : lore) {
  172.                 formated.add(l.replace("§", "&"));
  173.             }
  174.             builder.append("lore=" + String.join("/-/", formated) + " : ");
  175.         }
  176.         if (item.getItemMeta().hasEnchants()) {
  177.             for (Enchantment en : item.getItemMeta().getEnchants().keySet()) {
  178.                 builder.append("enchant=" + en.getName() + ";" + item.getItemMeta().getEnchants().get(en) + " : ");
  179.             }
  180.         }
  181.         if (item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)
  182.                 || item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)
  183.                 || item.getItemMeta().hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)
  184.                 || item.getItemMeta().hasItemFlag(ItemFlag.HIDE_UNBREAKABLE)) {
  185.             builder.append("hideflags=true" + " : ");
  186.         }
  187.  
  188.         return builder.toString();
  189.     }
  190.  
  191.     public static ItemStack Serialize(String fromtext) {
  192.         // id=TEXT/NUMBER : data=NUMBER : amount=16 : name=TEXT : lore=TEXT/-/TEXT :
  193.         // enchant=DAMAGE_ALL;5 : hideflags=BOOLEAN : potioneffect=SPEED;10;2;true : texture=TEXT
  194.         ItemStack item = null;
  195.         String[] textparts = fromtext.split(" : ");
  196.         boolean hideflags = false;
  197.         int material = 0;
  198.         Material materialreal = null;
  199.         byte data = 0;
  200.         String name = null;
  201.         List<String> lore = null;
  202.         List<String> enchants = new ArrayList<>();
  203.         int amount = 0;
  204.         List<String> potions = new ArrayList<>();
  205.         String headtexture = null;
  206.         for (String part : Arrays.asList(textparts)) {
  207.             if (part.contains("id=")) {
  208.                 try {
  209.                     String cc = part.replace("id=", "");
  210.                     material = Integer.valueOf(cc);
  211.                 } catch (Exception e) {
  212.                     String cc = part.replace("id=", "");
  213.                     materialreal = Material.valueOf(cc.toUpperCase());
  214.                 }
  215.             }
  216.             if (part.contains("data=")) {
  217.                 try {
  218.                     String cc = part.replace("data=", "");
  219.                     data = Byte.valueOf(cc);
  220.                 } catch (Exception e) {
  221.                     data = 0;
  222.                 }
  223.             }
  224.             if (part.contains("name=")) {
  225.                 String cc = part.replace("name=", "").replace("&", "§");
  226.                 name = cc;
  227.             }
  228.             if (part.contains("lore=")) {
  229.                 String[] lorelines = part.replace("&", "§").replace("lore=", "").split("/-/");
  230.                 lore = Arrays.asList(lorelines);
  231.             }
  232.             if (part.contains("enchant=")) {
  233.                 String replaced = part.replace("enchant=", "");
  234.                 enchants.add(replaced);
  235.             }
  236.             if (part.contains("hideflags=true")) {
  237.                 hideflags = true;
  238.             }
  239.             if (part.contains("amount=")) {
  240.                 amount = Integer.valueOf(part.replace("amount=", ""));
  241.             }
  242.             if (part.contains("potioneffect=")) {
  243.                 potions.add(part.replace("potioneffect=", ""));
  244.             }
  245.             if (part.contains("texture=")) {
  246.                 headtexture = part.replace("texture=", "");
  247.             }
  248.         }
  249.         if (material != 0) {
  250.             if (materialreal == null) {
  251.                 if (data == 0) {
  252.                     item = new ItemStack(material);
  253.                 } else {
  254.                     item = new ItemStack(material, 1, (short) 0, data);
  255.                 }
  256.             }
  257.         } else {
  258.             if (materialreal != null) {
  259.                 if (data == 0) {
  260.                     item = new ItemStack(materialreal);
  261.                 } else {
  262.                     item = new ItemStack(materialreal, 1, (short) 0, data);
  263.                 }
  264.             }
  265.         }
  266.         if (name != null) {
  267.             ItemMeta meta = item.getItemMeta();
  268.             meta.setDisplayName(name);
  269.             item.setItemMeta(meta);
  270.         }
  271.         if (lore != null) {
  272.             ItemMeta meta = item.getItemMeta();
  273.             meta.setLore(lore);
  274.             item.setItemMeta(meta);
  275.         }
  276.         if (enchants.size() > 0) {
  277.             ItemMeta meta = item.getItemMeta();
  278.             for (String ec : enchants) {
  279.                 String[] splitter = ec.split(";");
  280.                 Enchantment enchantter = null;
  281.                 try {
  282.                     enchantter = Enchantment.getById(Integer.valueOf(splitter[0]));
  283.                 } catch (Exception e) {
  284.                     String encname = splitter[0];
  285.                     enchantter = Enchantment.getByName(encname);
  286.                 }
  287.                 try {
  288.                     meta.addEnchant(enchantter, Integer.parseInt(splitter[1]), true);
  289.                 } catch (Exception e) {
  290.                 }
  291.             }
  292.             item.setItemMeta(meta);
  293.         }
  294.         if (hideflags) {
  295.             ItemMeta meta = item.getItemMeta();
  296.             meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  297.             meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
  298.             meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
  299.             meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
  300.             item.setItemMeta(meta);
  301.         }
  302.         if (amount != 0) {
  303.             item.setAmount(amount);
  304.         }
  305.         if (potions.size() > 0) {
  306.             PotionMeta meta = (PotionMeta) item.getItemMeta();
  307.             boolean splash = false;
  308.             for (String st : potions) {
  309.                 String[] split = st.split(";");
  310.                 meta.addCustomEffect(new PotionEffect(PotionEffectType.getByName(split[0].toUpperCase()),
  311.                         20 * Integer.valueOf(split[1]), Integer.valueOf(split[2])), true);
  312.                 if (split[3] != null) {
  313.                     splash = Boolean.valueOf(split[3]);
  314.                 }
  315.             }
  316.             item.setItemMeta(meta);
  317.             Potion potion = Potion.fromItemStack(item);
  318.             potion.setSplash(splash);
  319.             potion.apply(item);
  320.         }
  321.         if (headtexture != null) {
  322.             String skinURL = "http://textures.minecraft.net/texture/"+headtexture;
  323.             ItemMeta headMeta = item.getItemMeta();
  324.             GameProfile profile = new GameProfile(UUID.randomUUID(), null);
  325.             byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", skinURL).getBytes());
  326.             profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
  327.             Field profileField = null;
  328.             try {
  329.                 profileField = headMeta.getClass().getDeclaredField("profile");
  330.             } catch (NoSuchFieldException | SecurityException e) {
  331.             }
  332.             profileField.setAccessible(true);
  333.             try {
  334.                 profileField.set(headMeta, profile);
  335.             } catch (IllegalArgumentException | IllegalAccessException e) {
  336.             }
  337.             item.setItemMeta(headMeta);
  338.         }
  339.         return item;
  340.     }
  341.    
  342.     public static ItemStack getSkull(String skin) {
  343.         String skinURL = "http://textures.minecraft.net/texture/"+skin;
  344.         ItemStack stack = new ItemBuilder(Material.SKULL_ITEM, 3).build();
  345.         ItemMeta headMeta = stack.getItemMeta();
  346.         GameProfile profile = new GameProfile(UUID.randomUUID(), null);
  347.         byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", skinURL).getBytes());
  348.         profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
  349.         Field profileField = null;
  350.         try {
  351.             profileField = headMeta.getClass().getDeclaredField("profile");
  352.         } catch (NoSuchFieldException | SecurityException e) {
  353.         }
  354.         profileField.setAccessible(true);
  355.         try {
  356.             profileField.set(headMeta, profile);
  357.         } catch (IllegalArgumentException | IllegalAccessException e) {
  358.         }
  359.         stack.setItemMeta(headMeta);
  360.         return stack;
  361.        
  362.     }
  363.    
  364.     public static String itemto64(ItemStack stack) {
  365.         try {
  366.             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  367.             BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
  368.             dataOutput.writeObject(stack);
  369.  
  370.             dataOutput.close();
  371.             return Base64Coder.encodeLines(outputStream.toByteArray());
  372.         } catch (Exception e) {
  373.             throw new IllegalStateException("Nao foi possivel.", e);
  374.         }
  375.     }
  376.  
  377.     public static ItemStack itemFrom64(String data) {
  378.         try {
  379.             ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
  380.             BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
  381.             try {
  382.                 return (ItemStack) dataInput.readObject();
  383.             } finally {
  384.                 dataInput.close();
  385.             }
  386.         } catch (Exception e) {
  387.             throw new IllegalStateException("Nao foi possivel.", e);
  388.         }
  389.     }
  390.  
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement