Advertisement
DaCurse

Spigot/Bukkit ItemStack Converter

Jul 20th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5.  
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.Material;
  8. import org.bukkit.enchantments.Enchantment;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.inventory.meta.ItemMeta;
  11.  
  12. public class ItemStackConverter {
  13.  
  14.     public static String serializeItemStack(ItemStack is) {
  15.         String serializedItemStack = new String();
  16.  
  17.         String isType = String.valueOf(is.getType().getId());
  18.         serializedItemStack += "t@" + isType;
  19.  
  20.         if (is.getDurability() != 0) {
  21.             String isDurability = String.valueOf(is.getDurability());
  22.             serializedItemStack += ":d@" + isDurability;
  23.         }
  24.  
  25.         if (is.getAmount() != 1) {
  26.             String isAmount = String.valueOf(is.getAmount());
  27.             serializedItemStack += ":a@" + isAmount;
  28.         }
  29.  
  30.         Map<Enchantment, Integer> isEnch = is.getEnchantments();
  31.         if (isEnch.size() > 0) {
  32.             for (Entry<Enchantment, Integer> ench : isEnch.entrySet()) {
  33.                 serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
  34.             }
  35.         }
  36.  
  37.         ItemMeta im = is.getItemMeta();
  38.  
  39.         serializedItemStack += ":n@" + im.getDisplayName().replaceAll("ยง", "&");
  40.  
  41.         if(im.getLore() != null)
  42.             for (String l : im.getLore())
  43.                 serializedItemStack += ":l@" + l.replaceAll("ยง", "&");
  44.  
  45.         return serializedItemStack;
  46.  
  47.     }
  48.  
  49.     public static ItemStack deserializeItemStack(String itemString) {
  50.  
  51.         ItemStack is = null;
  52.         ItemMeta im = null;
  53.         List<String> lore = new ArrayList<String>();
  54.  
  55.         String[] serializedItemStack = itemString.split(":");
  56.  
  57.         for (String itemInfo : serializedItemStack) {
  58.             String[] itemAttribute = itemInfo.split("@");
  59.             if (itemAttribute[0].equals("t")) {
  60.                 is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
  61.                 im = is.getItemMeta();
  62.             } else if (itemAttribute[0].equals("d"))
  63.                 is.setDurability(Short.valueOf(itemAttribute[1]));
  64.             else if (itemAttribute[0].equals("a"))
  65.                 is.setAmount(Integer.valueOf(itemAttribute[1]));
  66.             else if (itemAttribute[0].equals("e"))
  67.                 im.addEnchant(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]),
  68.                         true);
  69.             else if (itemAttribute[0].equals("n"))
  70.                 im.setDisplayName(ChatColor.translateAlternateColorCodes('&', itemAttribute[1]));
  71.             else if (itemAttribute[0].equals("l"))
  72.                 lore.add(ChatColor.translateAlternateColorCodes('&', itemAttribute[1]));
  73.  
  74.         }
  75.  
  76.         im.setLore(lore);
  77.         is.setItemMeta(im);
  78.  
  79.         return is;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement