package in.twizmwaz.cardinal.module.modules.killReward;

import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.ModuleCollection;
import in.twizmwaz.cardinal.module.modules.filter.FilterModule;
import in.twizmwaz.cardinal.module.modules.filter.FilterState;
import in.twizmwaz.cardinal.module.modules.kit.Kit;
import in.twizmwaz.cardinal.module.modules.tntTracker.TntTracker;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent;

public class KillReward implements Module {

    private final Kit kit;
    private final ModuleCollection<FilterModule> filters;

    @Override
    public void unload() {
        HandlerList.unregisterAll(this);
    }

    protected KillReward(final Kit kit, final ModuleCollection<FilterModule> filters) {
        this.kit = kit;
        this.filters = filters;
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        if (event.getEntity().getKiller() != null) {
            Player killer = event.getEntity().getKiller();
            boolean proceed = true;
            for (FilterModule filter : this.filters) {
                if (filter.evaluate(killer).equals(FilterState.DENY)) {
                    proceed = false;
                }
            }
            if (proceed)
                this.kit.apply(killer);
        } else {
            try {
                EntityDamageEvent.DamageCause cause = event.getEntity().getLastDamageCause().getCause();
                if (cause.equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) || cause.equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) {
                    if (event.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent) {
                        EntityDamageByEntityEvent damageByEntityEvent = (EntityDamageByEntityEvent) event.getEntity().getLastDamageCause();
                        if (TntTracker.getWhoPlaced(damageByEntityEvent.getDamager()) != null) {
                            if (Bukkit.getOfflinePlayer(TntTracker.getWhoPlaced(damageByEntityEvent.getDamager())).isOnline()) {
                                Player source = Bukkit.getPlayer(TntTracker.getWhoPlaced(damageByEntityEvent.getDamager()));
                                if (!source.equals(event.getEntity())) {
                                    boolean proceed = true;
                                    for (FilterModule filter : this.filters) {
                                        if (filter.evaluate(source).equals(FilterState.DENY)) {
                                            proceed = false;
                                        }
                                    }
                                    if (proceed)
                                        this.kit.apply(source);
                                }
                            }
                        }
                    }
                }
            } catch (NullPointerException e) {
            }
        }
    }

}

@@ -50,16 +50,70 @@
 eward;%0A%0A
+import in.twizmwaz.cardinal.event.CardinalDeathEvent;%0A
 import i
@@ -139,32 +139,32 @@
 .module.Module;%0A
-
 import in.twizmw
@@ -1148,26 +1148,30 @@
 d on
-PlayerDeath(Player
+CardinalDeath(Cardinal
 Deat
@@ -1203,36 +1203,24 @@
 f (event.get
-Entity().get
 Killer() != 
@@ -1215,32 +1215,69 @@
 Killer() != null
+ && event.getKiller().getHealth() %3E 0
 ) %7B%0A            
@@ -1298,28 +1298,16 @@
 = event.
-getEntity().
 getKille
@@ -1559,32 +1559,34 @@
     if (proceed)
+ %7B
 %0A               
@@ -1613,1628 +1613,8 @@
 er);
-%0A        %7D else %7B%0A            try %7B%0A                EntityDamageEvent.DamageCause cause = event.getEntity().getLastDamageCause().getCause();%0A                if (cause.equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) %7C%7C cause.equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) %7B%0A                    if (event.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent) %7B%0A                        EntityDamageByEntityEvent damageByEntityEvent = (EntityDamageByEntityEvent) event.getEntity().getLastDamageCause();%0A                        if (TntTracker.getWhoPlaced(damageByEntityEvent.getDamager()) != null) %7B%0A                            if (Bukkit.getOfflinePlayer(TntTracker.getWhoPlaced(damageByEntityEvent.getDamager())).isOnline()) %7B%0A                                Player source = Bukkit.getPlayer(TntTracker.getWhoPlaced(damageByEntityEvent.getDamager()));%0A                                if (!source.equals(event.getEntity())) %7B%0A                                    boolean proceed = true;%0A                                    for (FilterModule filter : this.filters) %7B%0A                                        if (filter.evaluate(source).equals(FilterState.DENY)) %7B%0A                                            proceed = false;%0A                                        %7D%0A                                    %7D%0A                                    if (proceed)%0A                                        this.kit.apply(source);%0A                                %7D%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D catch (NullPointerException e) %7B
 %0A   

Make sure that the player isn't dead when giving a kill reward, fixes #447