package com.legit2.Demigods.Listeners;

import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;

import com.legit2.Demigods.DSouls;
import com.legit2.Demigods.DUtil;
import com.legit2.Demigods.Demigods;

public class DEntityListener implements Listener
{
	static Demigods plugin;
	
	public DEntityListener(Demigods instance)
	{
		plugin = instance;
	}
	
	@EventHandler(priority = EventPriority.HIGHEST)
	public static void entityDeath(EntityDeathEvent event)
	{
		// Define variables
		LivingEntity attackedEntity;
		if(event.getEntityType().equals(EntityType.PLAYER)) // IF IT'S A PLAYER
		{
			// Cancel soul drop if player kills themselves
			if(event.getEntity().getKiller() == null) return;

			// Define entity as player and other variables
			attackedEntity = event.getEntity();
			Player attackedPlayer = (Player) attackedEntity;
			EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) attackedEntity.getLastDamageCause();
			Entity attacker = damageEvent.getDamager();
			
			if(attacker instanceof Player)
			{
				
				// For player deaths, we first check their opponent for # of souls and determine soul drops from there...
				if(DUtil.getNumberOfSouls((attackedPlayer)) == 0) // If they have no souls then we know to drop a new soul on death
				{
					attackedEntity.getLocation().getWorld().dropItemNaturally(attackedEntity.getLocation(), DSouls.getSoulFromEntity(attackedEntity));
				}
				else // Else we cancel their death and subtract a soul
				{
					if(DUtil.getNumberOfSouls(attackedPlayer) > 0)
					{
						ItemStack usedSoul = DUtil.useSoul(attackedPlayer);
					
						DUtil.serverMsg("TEMP: " + attackedPlayer.getName() + " just lost 1 " + usedSoul.getType().name().toLowerCase() + "!");
						
						// Cancel it
						if(damageEvent.getDamage() > attackedPlayer.getHealth())
						{
							DUtil.serverMsg("TEMP: Attempting to cancel death...");
							damageEvent.setCancelled(true);
						}
					}
				}
			}
		}
		else if(event.getEntityType().equals(EntityType.VILLAGER)) // IF IT'S A VILLAGER
		{
			// Define villager
			LivingEntity villager = event.getEntity();
			EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) villager.getLastDamageCause();
			
			// Define attacker and name
			Player attacker = (Player) damageEvent.getDamager();
			//String attackerName = attacker.getName();

			villager.getLocation().getWorld().dropItemNaturally(villager.getLocation(), DSouls.getSoulFromEntity(villager));
			attacker.sendMessage(ChatColor.GRAY + "One weaker than you has been slain by your hand.");
		}
	}

}

@@ -376,32 +376,83 @@
 ByEntityEvent;%0D%0A
+import org.bukkit.event.entity.EntityDamageEvent;%0D%0A
 import org.bukki
@@ -871,27 +871,27 @@
 oid 
-entityDeath
+damageEvent
 (EntityD
 eath
@@ -882,28 +882,29 @@
 vent(EntityD
-eath
+amage
 Event event)
@@ -1097,24 +1097,40 @@
 lves%0D%0A%09%09%09if(
+((LivingEntity) 
 event.getEnt
@@ -1134,16 +1134,17 @@
 Entity()
+)
 .getKill
@@ -1238,16 +1238,31 @@
 Entity =
+ (LivingEntity)
  event.g
@@ -2618,16 +2618,31 @@
 llager =
+ (LivingEntity)
  event.g
@@ -3121,14 +3121,134 @@
 %09%09%7D%0D%0A%09%7D%0D
+%0A%09%0D%0A%09@EventHandler(priority = EventPriority.HIGHEST)%0D%0A%09public static void entityDeath(EntityDeathEvent event)%0D%0A%09%7B%0D%0A%0D%0A%09%7D%0D
 %0A%0D%0A%7D%0D%0A

Update for Souls.