package in.twizmwaz.cardinal.util;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jdom2.Element;

import java.util.ArrayList;

public class ParseUtils {
    public static ItemStack getItem(Element element) {
        int amount = 1;
        if (element.getAttributeValue("amount") != null) {
            try {
                amount = Integer.parseInt(element.getAttributeValue("amount"));
            } catch (NumberFormatException e) {
            }
        }
        ItemStack itemStack = new ItemStack(Material.matchMaterial(element.getText()), amount);
        if (element.getAttributeValue("damage") != null) {
            itemStack.setDurability(Short.parseShort(element.getAttributeValue("damage")));
        }
        try {
            for (String raw : element.getAttributeValue("enchantment").split(";")) {
                String[] enchant = raw.split(":");
                try {
                    itemStack.addUnsafeEnchantment(in.parapengu.commons.utils.StringUtils.convertStringToEnchantment(enchant[0]), Integer.parseInt(enchant[1]));
                } catch (ArrayIndexOutOfBoundsException e) {
                    itemStack.addUnsafeEnchantment(in.parapengu.commons.utils.StringUtils.convertStringToEnchantment(enchant[0]), 1);
                }
            }
        } catch (NullPointerException e) {

        }
        ItemMeta meta = itemStack.getItemMeta();
        if (element.getAttributeValue("name") != null) {
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('`', element.getAttributeValue("name")));
        }
        if (element.getAttributeValue("lore") != null) {
            ArrayList<String> lore = new ArrayList<>();
            for (String raw : element.getAttributeValue("lore").split("\\|")) {
                String colored = ChatColor.translateAlternateColorCodes('`', raw);
                lore.add(colored);
            }
            meta.setLore(lore);
        }
        int slot = element.getAttributeValue("slot") != null ? Integer.parseInt(element.getAttributeValue("slot")) : -1;
        itemStack.setItemMeta(meta);
        return itemStack;
    }
}

@@ -168,16 +168,176 @@
 emMeta;%0A
+import org.bukkit.inventory.meta.PotionMeta;%0Aimport org.bukkit.potion.Potion;%0Aimport org.bukkit.potion.PotionEffect;%0Aimport org.bukkit.potion.PotionEffectType;%0A
 import o
@@ -2306,16 +2306,945 @@
 ) : -1;%0A
+        if (element.getAttributeValue(%22potions%22) != null) %7B%0A            String potions = element.getAttributeValue(%22potions%22);%0A            if (potions.contains(%22;%22)) %7B%0A                for (String potion : potions.split(%22;%22)) %7B%0A                    String%5B%5D parse = potion.split(%22:%22);%0A                    PotionEffect effect = new PotionEffect(PotionEffectType.getByName(parse%5B0%5D.toUpperCase().replaceAll(%22 %22, %22_%22)), Integer.parseInt(parse%5B1%5D) * 20, Integer.parseInt(parse%5B2%5D) - 1);%0A                    ((PotionMeta) meta).addCustomEffect(effect, true);%0A                %7D%0A            %7D else %7B%0A                String%5B%5D parse = potions.split(%22:%22);%0A                PotionEffect effect = new PotionEffect(PotionEffectType.getByName(parse%5B0%5D.toUpperCase().replaceAll(%22 %22, %22_%22)), Integer.parseInt(parse%5B1%5D) * 20, Integer.parseInt(parse%5B2%5D) - 1);%0A                ((PotionMeta) meta).addCustomEffect(effect, true);%0A            %7D%0A        %7D%0A
         

Add potion parsing, fixes #132