Search in sources :

Example 21 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Citizens2 by CitizensDev.

the class EventListen method onEntityDamage.

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
    NPC npc = npcRegistry.getNPC(event.getEntity());
    if (npc == null) {
        if (event instanceof EntityDamageByEntityEvent) {
            npc = npcRegistry.getNPC(((EntityDamageByEntityEvent) event).getDamager());
            if (npc == null)
                return;
            event.setCancelled(!npc.data().get(NPC.DAMAGE_OTHERS_METADATA, true));
            NPCDamageEntityEvent damageEvent = new NPCDamageEntityEvent(npc, (EntityDamageByEntityEvent) event);
            Bukkit.getPluginManager().callEvent(damageEvent);
        }
        return;
    }
    event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
    if (event instanceof EntityDamageByEntityEvent) {
        NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, (EntityDamageByEntityEvent) event);
        Bukkit.getPluginManager().callEvent(damageEvent);
        if (!damageEvent.isCancelled() || !(damageEvent.getDamager() instanceof Player))
            return;
        Player damager = (Player) damageEvent.getDamager();
        NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager);
        Bukkit.getPluginManager().callEvent(leftClickEvent);
    } else if (event instanceof EntityDamageByBlockEvent) {
        Bukkit.getPluginManager().callEvent(new NPCDamageByBlockEvent(npc, (EntityDamageByBlockEvent) event));
    } else {
        Bukkit.getPluginManager().callEvent(new NPCDamageEvent(npc, event));
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) NPCDamageByEntityEvent(net.citizensnpcs.api.event.NPCDamageByEntityEvent) NPCLeftClickEvent(net.citizensnpcs.api.event.NPCLeftClickEvent) NPCDamageEvent(net.citizensnpcs.api.event.NPCDamageEvent) Player(org.bukkit.entity.Player) NPCDamageEntityEvent(net.citizensnpcs.api.event.NPCDamageEntityEvent) NPCDamageByBlockEvent(net.citizensnpcs.api.event.NPCDamageByBlockEvent) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) EventHandler(org.bukkit.event.EventHandler)

Example 22 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Statz by Staartvin.

the class KillsMobsListener method onDie.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDie(final EntityDeathEvent event) {
    final PlayerStat stat = PlayerStat.KILLS_MOBS;
    Entity e = event.getEntity();
    if (!(e.getLastDamageCause() instanceof EntityDamageByEntityEvent)) {
        return;
    }
    EntityDamageByEntityEvent nEvent = (EntityDamageByEntityEvent) e.getLastDamageCause();
    if (nEvent.getDamager() instanceof Player) {
        // Entity died because of Player
        // Killer
        final Player player = (Player) nEvent.getDamager();
        // Do general check
        if (!plugin.doGeneralCheck(player, stat))
            return;
        if (e instanceof Player) {
        // Player killed player
        // Handled by other listener
        } else {
            // Player killed mob
            String mobType = StatzUtil.getMobType(e);
            String weapon = "";
            ItemStack item = player.getInventory().getItemInMainHand();
            if (item.getType() == Material.AIR) {
                weapon = "HAND";
            } else {
                weapon = item.getType().toString();
            }
            // Update value to new stat.
            plugin.getDataManager().setPlayerInfo(player.getUniqueId(), stat, StatzUtil.makeQuery("uuid", player.getUniqueId().toString(), "value", 1, "world", player.getWorld().getName(), "mob", mobType, "weapon", weapon));
        }
    } else if (nEvent.getDamager() instanceof Arrow) {
        // Entity was killed by an arrow, now check if it was shot by a player
        Arrow killerArrow = (Arrow) nEvent.getDamager();
        if (killerArrow.getShooter() instanceof Player) {
            Player shooter = (Player) killerArrow.getShooter();
            // Now update database.
            plugin.getDataManager().setPlayerInfo(shooter.getUniqueId(), stat, StatzUtil.makeQuery("uuid", shooter.getUniqueId().toString(), "value", 1, "world", shooter.getWorld().getName(), "mob", StatzUtil.getMobType(e), "weapon", "BOW"));
        }
    }
// 
}
Also used : Arrow(org.bukkit.entity.Arrow) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 23 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Towny by ElgarL.

the class TownyEntityMonitorListener method onEntityDeath.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
    Entity defenderEntity = event.getEntity();
    TownyWorld World = null;
    try {
        World = TownyUniverse.getDataSource().getWorld(defenderEntity.getLocation().getWorld().getName());
        if (!World.isUsingTowny())
            return;
    } catch (NotRegisteredException e) {
        // World not registered with Towny.
        return;
    }
    // Was this a player death?
    if (defenderEntity instanceof Player) {
        // Killed by another entity?
        if (defenderEntity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) defenderEntity.getLastDamageCause();
            Entity attackerEntity = damageEvent.getDamager();
            Player defenderPlayer = (Player) defenderEntity;
            Player attackerPlayer = null;
            Resident attackerResident = null;
            Resident defenderResident = null;
            try {
                defenderResident = TownyUniverse.getDataSource().getResident(defenderPlayer.getName());
            } catch (NotRegisteredException e) {
                return;
            }
            // Was this a missile?
            if (attackerEntity instanceof Projectile) {
                Projectile projectile = (Projectile) attackerEntity;
                if (projectile.getShooter() instanceof Player) {
                    attackerPlayer = (Player) projectile.getShooter();
                    try {
                        attackerResident = TownyUniverse.getDataSource().getResident(attackerPlayer.getName());
                    } catch (NotRegisteredException e) {
                    }
                }
            } else if (attackerEntity instanceof Player) {
                // This was a player kill
                attackerPlayer = (Player) attackerEntity;
                try {
                    attackerResident = TownyUniverse.getDataSource().getResident(attackerPlayer.getName());
                } catch (NotRegisteredException e) {
                }
            }
            /*
				 * If attackerPlayer or attackerResident are null at this point
				 * it was a natural death, not PvP.
				 */
            deathPayment(attackerPlayer, defenderPlayer, attackerResident, defenderResident);
            wartimeDeathPoints(attackerPlayer, defenderPlayer, attackerResident, defenderResident);
            if (TownySettings.isRemovingOnMonarchDeath())
                monarchDeath(attackerPlayer, defenderPlayer, attackerResident, defenderResident);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 24 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Prism-Bukkit by prism.

the class EntitySerializer method serialize.

/**
 * Serialize entity.
 * @param entity Entity.
 */
public final void serialize(Entity entity) {
    entityName = entity.getType().name().toLowerCase();
    // Get custom name
    customName = entity.getCustomName();
    // Get animal age
    if (entity instanceof Ageable) {
        isAdult = ((Ageable) entity).isAdult();
    }
    // Owner
    if (entity instanceof Tameable) {
        final Tameable mob = (Tameable) entity;
        if (mob.getOwner() != null) {
            tamingOwner = mob.getOwner().getUniqueId().toString();
        } else if (mob.isTamed()) {
            tamingOwner = "-none-";
        }
    }
    // Sitting
    if (entity instanceof Sittable) {
        sitting = ((Sittable) entity).isSitting();
    }
    EntityDamageEvent damageEvent = entity.getLastDamageCause();
    // Saves us the null check
    if (damageEvent instanceof EntityDamageByEntityEvent && !damageEvent.isCancelled() && damageEvent.getDamage() > ((LivingEntity) entity).getHealth()) {
        EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) damageEvent;
        if (e.getDamager() instanceof Projectile) {
            customDesc = EntityUtils.getCustomProjectileDescription((Projectile) e.getDamager());
        }
    }
    serializer(entity);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Sittable(org.bukkit.entity.Sittable) Tameable(org.bukkit.entity.Tameable) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Ageable(org.bukkit.entity.Ageable) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Projectile(org.bukkit.entity.Projectile)

Example 25 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Prism-Bukkit by prism.

the class DeathUtils method getTameWolfOwner.

/**
 * Determines the owner of a tamed wolf.
 *
 * @param event  EntityDeathEvent
 * @return String
 */
public static String getTameWolfOwner(EntityDeathEvent event) {
    String owner = "";
    EntityDamageByEntityEvent e = ((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause());
    if (e != null) {
        Entity killer = e.getDamager();
        if (killer instanceof Wolf) {
            Wolf wolf = (Wolf) killer;
            if (wolf.isTamed()) {
                if (wolf.getOwner() instanceof Player) {
                    owner = ((Player) wolf.getOwner()).getName();
                }
                if (wolf.getOwner() instanceof OfflinePlayer) {
                    owner = wolf.getOwner().getName();
                }
            }
        }
    }
    return owner;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) OfflinePlayer(org.bukkit.OfflinePlayer) Wolf(org.bukkit.entity.Wolf)

Aggregations

EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)35 EventHandler (org.bukkit.event.EventHandler)22 Entity (org.bukkit.entity.Entity)19 Player (org.bukkit.entity.Player)17 LivingEntity (org.bukkit.entity.LivingEntity)15 Projectile (org.bukkit.entity.Projectile)12 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)12 ItemStack (org.bukkit.inventory.ItemStack)6 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)5 Element (net.aufdemrand.denizencore.objects.Element)5 ProjectileSource (org.bukkit.projectiles.ProjectileSource)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 Arrow (org.bukkit.entity.Arrow)4 ArrayList (java.util.ArrayList)3 Animals (org.bukkit.entity.Animals)3 Tameable (org.bukkit.entity.Tameable)3 EntityDamageByBlockEvent (org.bukkit.event.entity.EntityDamageByBlockEvent)3 Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)2 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)2