package com.elmakers.mine.bukkit.api.magic;

import javax.annotation.Nonnull;

import org.bukkit.configuration.ConfigurationSection;

public class Trigger {
    @Nonnull
    private final String trigger;
    private final int interval;
    private final double maxHealth;
    private final double minHealth;
    private final double maxHealthPercentage;
    private final double minHealthPercentage;
    private final double maxDamage;
    private final double minDamage;
    private final double maxBowPull;
    private final double minBowPull;
    private final boolean isCancelLaunch;
    private final String damageType;

    private long lastTrigger;

    public Trigger(ConfigurationSection configuration) {
        this(configuration, "");
    }

    public Trigger(ConfigurationSection configuration, String defaultType) {
        trigger = configuration.getString("trigger", configuration.getString("type", defaultType)).toLowerCase();
        interval = configuration.getInt("interval");
        maxHealth = configuration.getDouble("max_health");
        minHealth = configuration.getDouble("min_health");
        maxHealthPercentage = configuration.getDouble("max_health_percentage");
        minHealthPercentage = configuration.getDouble("min_health_percentage");
        maxDamage = configuration.getDouble("max_damage");
        minDamage = configuration.getDouble("min_damage");
        isCancelLaunch = configuration.getBoolean("cancel_launch", true);
        maxBowPull = configuration.getDouble("max_bowpull");
        minBowPull = configuration.getDouble("min_bowpull");
        damageType = configuration.getString("damage_type");
    }

    public int getInterval() {
        return interval;
    }

    @Nonnull
    public String getTrigger() {
        return trigger;
    }

    public boolean isValid(Mage mage) {
        String lastDamageType = null;
        double damage = 0;
        if (trigger.equalsIgnoreCase("damage_dealt")) {
            lastDamageType = mage.getLastDamageDealtType();
            damage = mage.getLastDamage();
        } else {
            lastDamageType = mage.getLastDamageType();
            damage = mage.getLastDamageDealt();
        }

        if (minDamage > 0 && damage < minDamage) return false;
        if (maxDamage > 0 && damage > maxDamage) return false;

        if (minHealth > 0 && (mage.getHealth() < minHealth)) return false;
        if (maxHealth > 0 && (mage.getHealth() > maxHealth)) return false;
        if (minHealthPercentage > 0 && (mage.getHealth() * 100 / mage.getMaxHealth() < minHealthPercentage)) return false;
        if (maxHealthPercentage > 0 && (mage.getHealth() * 100 / mage.getMaxHealth() > maxHealthPercentage)) return false;

        if (minBowPull > 0 && (mage.getLastBowPull() < minBowPull)) return false;
        if (maxBowPull > 0 && (mage.getLastBowPull() > maxBowPull)) return false;

        if (interval > 0 && System.currentTimeMillis() < lastTrigger + interval) return false;

        if (damageType != null && !damageType.isEmpty()) {
            if (lastDamageType == null || !lastDamageType.equalsIgnoreCase(damageType)) return false;
        }

        return true;
    }

    public void triggered() {
        lastTrigger = System.currentTimeMillis();
    }

    public boolean isCancelLaunch() {
        return isCancelLaunch;
    }
}

@@ -38,16 +38,87 @@
 magic;%0A%0A
+import java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A
 import j
@@ -142,16 +142,16 @@
 onnull;%0A
-
 %0Aimport 
@@ -688,16 +688,59 @@
 ageType;
+%0A    private final Set%3CString%3E damageTypes;
 %0A%0A    pr
@@ -1754,24 +1754,250 @@
 age_type%22);%0A
+        List%3CString%3E damageTypeList = configuration.getStringList(%22damage_types%22);%0A        damageTypeList.replaceAll(String::toLowerCase);%0A        damageTypes = damageTypeList.isEmpty() ? null : new HashSet%3C%3E(damageTypeList);%0A
     %7D%0A%0A    p
@@ -3464,32 +3464,32 @@
 ) return false;%0A
-
         %7D%0A%0A     
@@ -3473,32 +3473,162 @@
 false;%0A        %7D
+%0A        if (damageTypes != null && (lastDamageType == null %7C%7C !damageTypes.contains(lastDamageType.toLowerCase()))) return false;
 %0A%0A        return

Add damage_types property to Trigger