Search in sources :

Example 26 with EntityDamageByEntityEvent

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

the class PrismEntityEvents method onEntityDeath.

/**
 * EntityDeathEvent.
 * @param event EntityDeathEvent
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(final EntityDeathEvent event) {
    final LivingEntity entity = event.getEntity();
    // Mob Death
    if (!(entity instanceof Player)) {
        // Log item drops
        if (Prism.getIgnore().event("item-drop", entity.getWorld())) {
            String name = entity.getType().name().toLowerCase();
            // Inventory
            if (entity instanceof InventoryHolder) {
                final InventoryHolder holder = (InventoryHolder) entity;
                for (final ItemStack i : holder.getInventory().getContents()) {
                    if (checkNotNullorAir(i)) {
                        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
                    }
                }
            }
            // Equipment
            EntityEquipment equipment = entity.getEquipment();
            if (equipment != null) {
                for (final ItemStack i : equipment.getArmorContents()) {
                    if (checkNotNullorAir(i)) {
                        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
                    }
                }
            }
            // Hand items not stored in "getArmorContents"
            ItemStack main = entity.getEquipment().getItemInMainHand();
            ItemStack off = entity.getEquipment().getItemInOffHand();
            if (checkNotNullorAir(main)) {
                RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", main, main.getAmount(), -1, null, entity.getLocation(), name));
            }
            if (checkNotNullorAir(off)) {
                RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", off, off.getAmount(), -1, null, entity.getLocation(), name));
            }
        }
        EntityDamageEvent damageEvent = entity.getLastDamageCause();
        Entity entitySource = null;
        Block blockSource = null;
        // Resolve source
        if (damageEvent != null && !damageEvent.isCancelled()) {
            if (damageEvent instanceof EntityDamageByEntityEvent) {
                entitySource = ((EntityDamageByEntityEvent) damageEvent).getDamager();
                if (entitySource instanceof Projectile) {
                    ProjectileSource ps = ((Projectile) entitySource).getShooter();
                    if (ps instanceof BlockProjectileSource) {
                        entitySource = null;
                        blockSource = ((BlockProjectileSource) ps).getBlock();
                    } else {
                        entitySource = (Entity) ps;
                    }
                }
            } else if (damageEvent instanceof EntityDamageByBlockEvent) {
                blockSource = ((EntityDamageByBlockEvent) damageEvent).getDamager();
            }
        }
        // Create handlers
        if (entitySource instanceof Player) {
            Player player = (Player) entitySource;
            if (!Prism.getIgnore().event("player-kill", player)) {
                return;
            }
            RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity, player));
        } else if (entitySource != null) {
            if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
                return;
            }
            String name = entitySource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
        } else if (blockSource != null) {
            if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
                return;
            }
            String name = "block:" + blockSource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
        } else {
            if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
                return;
            }
            String name = "unknown";
            if (damageEvent != null && !damageEvent.isCancelled()) {
                name = damageEvent.getCause().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            }
            RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
        }
    /*
             * if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) { final
             * EntityDamageByEntityEvent entityDamageByEntityEvent =
             * (EntityDamageByEntityEvent) entity .getLastDamageCause();
             *
             * // Mob killed by player if (entityDamageByEntityEvent.getDamager() instanceof
             * Player) { final Player player = (Player)
             * entityDamageByEntityEvent.getDamager(); if
             * (!Prism.getIgnore().event("player-kill", player)) return;
             * RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity,
             * player));
             *
             * } // Mob shot by an arrow from a player else if
             * (entityDamageByEntityEvent.getDamager() instanceof Arrow) { final Arrow arrow
             * = (Arrow) entityDamageByEntityEvent.getDamager();
             *
             * if (arrow.getShooter() instanceof Player) { final Player player = (Player)
             * arrow.getShooter(); if (!Prism.getIgnore().event("player-kill", player))
             * return; RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill",
             * entity, player));
             *
             * } else if (arrow.getShooter() instanceof LivingEntity) { final Entity damager
             * = (Entity) arrow.getShooter(); String name = "unknown"; if (damager != null)
             * { name = damager.getType().name().toLowerCase(); }
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * name)); } else if (arrow.getShooter() instanceof BlockProjectileSource) {
             *
             * final Block damager = (Block)
             * ((BlockProjectileSource)arrow.getShooter()).getBlock();
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             *
             * String name = "block:" + damager.getType().name().toLowerCase();
             *
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * name)); } } else { // Mob died by another mob final Entity damager =
             * entityDamageByEntityEvent.getDamager(); String name = "unknown"; if (damager
             * != null) { name = damager.getType().name().toLowerCase(); }
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * name)); } } else {
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             *
             * String killer = "unknown"; final EntityDamageEvent damage =
             * entity.getLastDamageCause(); if (damage != null) { final DamageCause cause =
             * damage.getCause(); if (cause != null) { killer = cause.name().toLowerCase();
             * } }
             *
             * // Record the death as natural
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * killer));
             *
             * }
             */
    } else {
        // Determine who died and what the exact cause was
        final Player p = (Player) event.getEntity();
        if (Prism.getIgnore().event("player-death", p)) {
            final String cause = DeathUtils.getCauseNiceName(p);
            String attacker = DeathUtils.getAttackerName(p);
            if (attacker.equals("pvpwolf")) {
                final String owner = DeathUtils.getTameWolfOwner(event);
                attacker = owner + "'s wolf";
            }
            RecordingQueue.addToQueue(ActionFactory.createPlayerDeath("player-death", p, cause, attacker));
        }
        // Log item drops
        if (Prism.getIgnore().event("item-drop", p)) {
            if (!event.getDrops().isEmpty()) {
                for (final ItemStack i : event.getDrops()) {
                    RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, p.getLocation(), p));
                }
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) BlockProjectileSource(org.bukkit.projectiles.BlockProjectileSource) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityEquipment(org.bukkit.inventory.EntityEquipment) Block(org.bukkit.block.Block) ProjectileSource(org.bukkit.projectiles.ProjectileSource) BlockProjectileSource(org.bukkit.projectiles.BlockProjectileSource) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 27 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project NoCheatPlus by NoCheatPlus.

the class FightListener method onEntityDamageMonitor.

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onEntityDamageMonitor(final EntityDamageEvent event) {
    final Entity damaged = event.getEntity();
    if (damaged instanceof Player) {
        final Player damagedPlayer = (Player) damaged;
        final IPlayerData damagedPData = DataManager.getPlayerData(damagedPlayer);
        final FightData damagedData = damagedPData.getGenericInstance(FightData.class);
        final int ndt = damagedPlayer.getNoDamageTicks();
        if (damagedData.lastDamageTick == TickTask.getTick() && damagedData.lastNoDamageTicks != ndt) {
            // Plugin compatibility thing.
            damagedData.lastNoDamageTicks = ndt;
        }
        // Knock-back calculation (1.8: events only fire if they would count by ndt).
        switch(event.getCause()) {
            case ENTITY_ATTACK:
                if (event instanceof EntityDamageByEntityEvent) {
                    final Entity entity = ((EntityDamageByEntityEvent) event).getDamager();
                    if ((entity instanceof Player) && !damagedPlayer.isInsideVehicle() && damagedPData.getGenericInstance(FightConfig.class).knockBackVelocityPvP) {
                        // TODO: Use the velocity event that is sent anyway and replace x/z if 0 (queue max. values).
                        applyKnockBack((Player) entity, damagedPlayer, damagedData, damagedPData);
                    }
                }
            default:
                break;
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) IPlayerData(fr.neatmonster.nocheatplus.players.IPlayerData) EventHandler(org.bukkit.event.EventHandler)

Example 28 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project AuthMeReloaded by AuthMe.

the class EntityListenerTest method shouldNotCancelEntityDamageByEntityEvent.

@Test
public void shouldNotCancelEntityDamageByEntityEvent() {
    // given
    EntityDamageByEntityEvent event = mock(EntityDamageByEntityEvent.class);
    Entity player = mock(Player.class);
    given(event.getDamager()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(false);
    // when
    listener.onAttack(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(event, only()).getDamager();
}
Also used : Entity(org.bukkit.entity.Entity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Test(org.junit.Test)

Example 29 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelper method fireFakeDamageEvent.

public static EntityDamageEvent fireFakeDamageEvent(Entity target, Entity source, EntityDamageEvent.DamageCause cause, float amount) {
    EntityDamageEvent ede = source == null ? new EntityDamageEvent(target, cause, amount) : new EntityDamageByEntityEvent(source, target, cause, amount);
    Bukkit.getPluginManager().callEvent(ede);
    return ede;
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 30 with EntityDamageByEntityEvent

use of org.bukkit.event.entity.EntityDamageByEntityEvent in project Denizen-For-Bukkit by DenizenScript.

the class NPCTagBase method onDeath.

// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
// 
// @Triggers when the NPC dies.
// 
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
// <context.damage> returns the last amount of damage applied (if any)
// <context.death_cause> returns the last damage cause (if any)
// 
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(EntityDeathEvent deathEvent) {
    NPC citizen = CitizensAPI.getNPCRegistry().getNPC(deathEvent.getEntity());
    if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(citizen);
    EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
    String deathCause = event == null ? "unknown" : CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event == null ? 0 : event.getDamage()));
    context.put("death_cause", new ElementTag(deathCause));
    PlayerTag player = null;
    if (event instanceof EntityDamageByEntityEvent) {
        Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
        context.put("killer", new EntityTag(killerEntity).getDenizenObject());
        if (killerEntity instanceof Player) {
            player = PlayerTag.mirrorBukkitPlayer((Player) killerEntity);
        } else if (killerEntity instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
            if (shooter instanceof LivingEntity) {
                context.put("shooter", new EntityTag((LivingEntity) shooter).getDenizenObject());
                if (shooter instanceof Player) {
                    player = PlayerTag.mirrorBukkitPlayer((Player) shooter);
                }
                npc.action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
            }
        }
        npc.action("death by entity", player, context);
        npc.action("death by " + killerEntity.getType().toString(), player, context);
    } else if (event instanceof EntityDamageByBlockEvent) {
        npc.action("death by block", null, context);
    }
    npc.action("death", player, context);
    npc.action("death by " + deathCause, player, context);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

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