package net.thereturningvoid.bloodmoney.listeners;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.permission.Permission;
import net.thereturningvoid.bloodmoney.BloodMoney;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;

import java.util.Map;

public class PlayerDeathListener implements Listener {

    private BloodMoney plugin;
    private Economy econ;
    private Permission perm;

    public PlayerDeathListener(BloodMoney instance) {
        plugin = instance;
        econ = instance.economy;
        perm = instance.permission;
    }

    @EventHandler
    public void onKillPlayer(PlayerDeathEvent e) throws InvalidConfigurationException {
        if (e.getEntityType() == EntityType.PLAYER) {
            Player pKilled = e.getEntity();
            if (pKilled.getKiller() != null && getRanks() != null) {
                Player pKilledBy = pKilled.getKiller();
                if (plugin.getConfig().getBoolean("bloodmoney.sendMessageToDeadPlayer") && !plugin.getConfig().getBoolean("bloodmoney.loseMoneyOnDeath")) {
                    String dMessage = plugin.getConfig().getString("bloodmoney.deathMessage").replace("%p", pKilledBy.getName());
                    pKilled.sendMessage(BloodMoney.CHAT_PREFIX + dMessage);
                }
                for (Map.Entry rank : getRanks().entrySet()) {
                    String rankName = (String) rank.getKey();
                    if (perm.has(pKilledBy, "bloodmoney.rank." + rank.getKey())) {
                        EconomyResponse creditPlayer = econ.depositPlayer(pKilledBy, plugin.getConfig().getDouble("bloodmoney.ranks." + rankName + ".gain"));
                        if (creditPlayer.transactionSuccess()) {
                            String kMessage = plugin.getConfig().getString("bloodmoney.killMessage").replace("%m", econ.format(creditPlayer.amount)).replace("%p", pKilled.getName());
                            pKilledBy.sendMessage(BloodMoney.CHAT_PREFIX + kMessage);
                        } else {
                            pKilledBy.sendMessage(BloodMoney.CHAT_PREFIX + "Something went wrong and the transaction was unsuccessful.");
                        }
                    }
                    if (plugin.getConfig().getBoolean("bloodmoney.loseMoneyOnDeath")) {
                        if (perm.has(pKilled, "bloodmoney.rank." + rank.getKey())) {
                            EconomyResponse takeFromPlayer = econ.withdrawPlayer(pKilled, plugin.getConfig().getDouble("bloodmoney.ranks." + rankName + ".loss"));
                            if (takeFromPlayer.transactionSuccess()) {
                                String kMessage2 = plugin.getConfig().getString("bloodmoney.deathMessage").replace("%p", pKilledBy.getName()).replace("%m", econ.format(takeFromPlayer.amount));
                                pKilled.sendMessage(BloodMoney.CHAT_PREFIX + kMessage2);
                            }
                        }
                    }
                }
            }
        }
    }

    private Map<String, Object> getRanks() {
        if (plugin.getConfig().isConfigurationSection("bloodmoney.ranks")) {
            return plugin.getConfig().getConfigurationSection("bloodmoney.ranks").getValues(false);
        } else return null;
    }

}

@@ -1017,24 +1017,95 @@
 etEntity();%0A
+            if (pKilledBy.getName().equals(pKilled.getName())) return;%0A
             
@@ -1157,24 +1157,24 @@
  != null) %7B%0A
-
             
@@ -1656,24 +1656,86 @@
 trySet()) %7B%0A
+                    if (!(pKilledBy instanceof Player)) %7B%0A    
             
@@ -1780,24 +1780,28 @@
 k.getKey();%0A
+    
             
@@ -1863,32 +1863,36 @@
 nk.getKey())) %7B%0A
+    
                 
@@ -2053,24 +2053,28 @@
             
+    
 if (creditPl
@@ -2122,32 +2122,36 @@
                 
+ 
+   
 String kMessage 
@@ -2309,32 +2309,36 @@
                 
+    
 pKilledBy.sendMe
@@ -2399,24 +2399,28 @@
             
+    
 %7D else %7B%0A   
@@ -2416,16 +2416,20 @@
  else %7B%0A
+    
         
@@ -2558,16 +2558,46 @@
 ful.%22);%0A
+                            %7D%0A
         

Update PlayerDeathListener.java