Advertisement
Guest User

Untitled

a guest
Aug 18th, 2015
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1.     private static final List<Material> LEATHER_ARMOR = Lists.newArrayList(Material.LEATHER_BOOTS, Material.LEATHER_LEGGINGS, Material.LEATHER_CHESTPLATE, Material.LEATHER_HELMET);
  2.    
  3. public static ItemStack newItemStack(ConfigurationSection item) throws ParseException {
  4.         if (!item.getKeys(false).contains("material"))
  5.             throw new ParseException("Unable to parse ItemStack. No material field found!", 0);
  6.         return new ItemStack(Material.getMaterial((String) item.get("material"))) {
  7.             {
  8.                 ItemMeta itemMeta = getItemMeta();
  9.                 for (String key : item.getKeys(false)) {
  10.                     switch (key) {
  11.                         case "name":
  12.                             itemMeta.setDisplayName(item.getString(key));
  13.                             break;
  14.                         case "amount":
  15.                             setAmount(item.getInt(key));
  16.                             break;
  17.                         case "data":
  18.                             setDurability((short) item.getInt(key));
  19.                             break;
  20.                         case "lore":
  21.                             itemMeta.setLore((item.getStringList(key)));
  22.                             break;
  23.                         case "enchants":
  24.                             Map<Enchantment, Integer> enchantsMap = Maps.newHashMap();
  25.                             for (Map.Entry enchantEntry : item.getConfigurationSection("enchants").getValues(false).entrySet())
  26.                                 itemMeta.addEnchant(Enchantment.getByName((String) enchantEntry.getKey()), (Integer) enchantEntry.getValue(), true);
  27.                             break;
  28.                         case "color":
  29.                             if (LEATHER_ARMOR.contains(getType())) {
  30.                                 ConfigurationSection color = item.getConfigurationSection("color");
  31.                                 ((LeatherArmorMeta) itemMeta).setColor(Color.fromRGB(color.getInt("R"), color.getInt("G"), color.getInt("B")));
  32.                             }
  33.                             break;
  34.                     }
  35.                     setItemMeta(itemMeta);
  36.                 }
  37.             }
  38.         };
  39.     }
  40.  
  41.  
  42.     public static Map<String, Object> toStringMap(ItemStack itemStack) {
  43.         Map map = Maps.newHashMap();
  44.         map.put("material", itemStack.getType().toString());
  45.         map.put("amount", itemStack.getAmount());
  46.         map.put("data", itemStack.getDurability());
  47.         if (itemStack.hasItemMeta()) {
  48.             ItemMeta itemMeta = itemStack.getItemMeta();
  49.  
  50.             if (itemMeta.hasDisplayName()) {
  51.                 map.put("name", itemMeta.getDisplayName());
  52.             }
  53.             if (itemMeta.hasLore()) {
  54.                 map.put("lore", itemMeta.getLore());
  55.             }
  56.             if (itemMeta.hasEnchants()) {
  57.                 Map<String, Integer> enchants = Maps.newHashMap();
  58.                 for (Map.Entry<Enchantment, Integer> entry : itemMeta.getEnchants().entrySet()) {
  59.                     enchants.put(entry.getKey().getName(), entry.getValue());
  60.                 }
  61.                 map.put("enchants", enchants);
  62.             }
  63.         }
  64.         if (LEATHER_ARMOR.contains(itemStack.getType())) {
  65.             LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemStack.getItemMeta();
  66.             Map<String, Integer> color = Maps.newHashMap();
  67.             color.put("R", leatherArmorMeta.getColor().getRed());
  68.             color.put("G", leatherArmorMeta.getColor().getGreen());
  69.             color.put("B", leatherArmorMeta.getColor().getBlue());
  70.             map.put("color", color);
  71.         }
  72.         return map;
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement