Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final List<Material> LEATHER_ARMOR = Lists.newArrayList(Material.LEATHER_BOOTS, Material.LEATHER_LEGGINGS, Material.LEATHER_CHESTPLATE, Material.LEATHER_HELMET);
- public static ItemStack newItemStack(ConfigurationSection item) throws ParseException {
- if (!item.getKeys(false).contains("material"))
- throw new ParseException("Unable to parse ItemStack. No material field found!", 0);
- return new ItemStack(Material.getMaterial((String) item.get("material"))) {
- {
- ItemMeta itemMeta = getItemMeta();
- for (String key : item.getKeys(false)) {
- switch (key) {
- case "name":
- itemMeta.setDisplayName(item.getString(key));
- break;
- case "amount":
- setAmount(item.getInt(key));
- break;
- case "data":
- setDurability((short) item.getInt(key));
- break;
- case "lore":
- itemMeta.setLore((item.getStringList(key)));
- break;
- case "enchants":
- Map<Enchantment, Integer> enchantsMap = Maps.newHashMap();
- for (Map.Entry enchantEntry : item.getConfigurationSection("enchants").getValues(false).entrySet())
- itemMeta.addEnchant(Enchantment.getByName((String) enchantEntry.getKey()), (Integer) enchantEntry.getValue(), true);
- break;
- case "color":
- if (LEATHER_ARMOR.contains(getType())) {
- ConfigurationSection color = item.getConfigurationSection("color");
- ((LeatherArmorMeta) itemMeta).setColor(Color.fromRGB(color.getInt("R"), color.getInt("G"), color.getInt("B")));
- }
- break;
- }
- setItemMeta(itemMeta);
- }
- }
- };
- }
- public static Map<String, Object> toStringMap(ItemStack itemStack) {
- Map map = Maps.newHashMap();
- map.put("material", itemStack.getType().toString());
- map.put("amount", itemStack.getAmount());
- map.put("data", itemStack.getDurability());
- if (itemStack.hasItemMeta()) {
- ItemMeta itemMeta = itemStack.getItemMeta();
- if (itemMeta.hasDisplayName()) {
- map.put("name", itemMeta.getDisplayName());
- }
- if (itemMeta.hasLore()) {
- map.put("lore", itemMeta.getLore());
- }
- if (itemMeta.hasEnchants()) {
- Map<String, Integer> enchants = Maps.newHashMap();
- for (Map.Entry<Enchantment, Integer> entry : itemMeta.getEnchants().entrySet()) {
- enchants.put(entry.getKey().getName(), entry.getValue());
- }
- map.put("enchants", enchants);
- }
- }
- if (LEATHER_ARMOR.contains(itemStack.getType())) {
- LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemStack.getItemMeta();
- Map<String, Integer> color = Maps.newHashMap();
- color.put("R", leatherArmorMeta.getColor().getRed());
- color.put("G", leatherArmorMeta.getColor().getGreen());
- color.put("B", leatherArmorMeta.getColor().getBlue());
- map.put("color", color);
- }
- return map;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement