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.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;

import com.legit2.Demigods.Demigods;
import com.legit2.Demigods.Utilities.DMiscUtil;

public class DEntityListener implements Listener
{
	static Demigods plugin;
	
	public DEntityListener(Demigods instance)
	{
		plugin = instance;
	}
	
	@EventHandler(priority = EventPriority.MONITOR)
	public static void damageEvent(EntityDamageEvent event)
	{
		// Define variables
		LivingEntity attackedEntity;
		if(event.getEntityType().equals(EntityType.PLAYER)) // IF IT'S A PLAYER
		{
			// Define entity as player and other variables
			attackedEntity = (LivingEntity) event.getEntity();
			Player attackedPlayer = (Player) attackedEntity;
			EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) attackedEntity.getLastDamageCause();
			Entity attacker = damageEvent.getDamager();
			
			if(attacker instanceof Player)
			{
				if(!DMiscUtil.canTarget(attackedPlayer, attackedPlayer.getLocation()))
				{
					((Player) attacker).sendMessage(ChatColor.GRAY + "Stop, you!");
					event.setCancelled(true);
					return;
				}
				
				/*
				if(damageEvent.getDamage() > attackedPlayer.getHealth())
				{
					// 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() + "!");
							
							DUtil.serverMsg("TEMP: Attempting to cancel death...");
							event.setCancelled(true);
						}
					}
				}
				*/
			}
		}
		else if(event.getEntityType().equals(EntityType.VILLAGER)) // IF IT'S A VILLAGER
		{
			// Define villager
			LivingEntity villager = (LivingEntity) event.getEntity();
			EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) villager.getLastDamageCause();
			
			// Define attacker and name
			Player attacker = null;
			if(damageEvent.getDamager() != null) attacker = (Player) damageEvent.getDamager();

			if(damageEvent.getDamager() instanceof Player && damageEvent.getDamage() > villager.getHealth())
			{
				//villager.getLocation().getWorld().dropItemNaturally(villager.getLocation(), DSouls.getSoulFromEntity(villager));
				if(attacker != null) attacker.sendMessage(ChatColor.GRAY + "One weaker than you has been slain by your hand.");
			}
		}
	}
	
	@EventHandler(priority = EventPriority.HIGHEST)
	public static void entityDeath(EntityDeathEvent event)
	{
		// TODO
	}

}

@@ -902,16 +902,65 @@
 ntity;%0D%0A
+%09%09EntityDamageByEntityEvent damageEvent = null;%0D%0A
 %09%09if(eve
@@ -1022,24 +1022,24 @@
 S A PLAYER%0D%0A
-
 %09%09%7B%0D%0A%09%09%09// D
@@ -1189,24 +1189,79 @@
 Entity;%0D%0A%09%09%09
+%0D%0A%09%09%09if(attackedEntity.getLastDamageCause() instanceof 
 EntityDamage
@@ -1265,33 +1265,46 @@
 ageByEntityEvent
- 
+) %0D%0A%09%09%09%7B%0D%0A%09%09%09%09
 damageEvent = (E
@@ -1363,24 +1363,50 @@
 geCause();%0D%0A
+%09%09%09%7D %0D%0A%09%09%09else return;%0D%0A%0D%0A
 %09%09%09Entity at
@@ -2823,34 +2823,8 @@
 %0A%09%09%09
-EntityDamageByEntityEvent 
 dama

Fix for entity damange casting error.