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

import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;

import com.elmakers.mine.bukkit.api.entity.EntityData;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.magic.MageController;

public class MagicMobDeathEvent extends Event {
    private static final HandlerList handlers = new HandlerList();

    private final EntityDeathEvent deathEvent;
    private final EntityData entityData;
    private final MageController controller;
    private Mage mage;
    private Player player;

    public MagicMobDeathEvent(MageController controller, EntityData entityData, EntityDeathEvent deathEvent) {
        this.controller = controller;
        this.entityData = entityData;
        this.deathEvent = deathEvent;

        LivingEntity killed = deathEvent.getEntity();
        EntityDamageEvent damageEvent = killed.getLastDamageCause();
        if (damageEvent instanceof EntityDamageByEntityEvent)
        {
            EntityDamageByEntityEvent damageByEvent = (EntityDamageByEntityEvent)damageEvent;
            Entity damagingEntity = damageByEvent.getDamager();
            if (damagingEntity instanceof Projectile) {
                Projectile projectile = (Projectile)damagingEntity;
                damagingEntity = (LivingEntity)projectile.getShooter();
            }
            if (damagingEntity != null && damagingEntity instanceof Player)
            {
                player = (Player)damagingEntity;
                mage = controller.getRegisteredMage(player.getUniqueId().toString());
            }
        }
    }

    @Override
    public HandlerList getHandlers() {
        return handlers;
    }

    public static HandlerList getHandlerList() {
        return handlers;
    }

    public EntityDeathEvent getDeathEvent() {
        return deathEvent;
    }

    public EntityData getEntityData() {
        return entityData;
    }

    public MageController getController() {
        return controller;
    }

    public Mage getMage() {
        return mage;
    }

    public Player getPlayer() {
        return player;
    }
}

@@ -400,24 +400,72 @@
 yDeathEvent;
+%0Aimport org.bukkit.projectiles.ProjectileSource;
 %0A%0Aimport com
@@ -1611,32 +1611,157 @@
 damagingEntity;%0A
+                ProjectileSource source = projectile.getShooter();%0A                if (source instanceof LivingEntity) %7B%0A    
                 
@@ -1795,32 +1795,33 @@
 ity)
-projectile.getShooter();
+source;%0A                %7D
 %0A   

Fix error when a magic mob is killed by a projectile launched by a non-entity