Search in sources :

Example 6 with EntityDamageEvent

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

the class EntityDeathScriptEvent method onEntityDeath.

@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
    LivingEntity livingEntity = event.getEntity();
    dEntity.rememberEntity(livingEntity);
    entity = new dEntity(livingEntity);
    dPlayer player = null;
    if (entity.isPlayer()) {
        player = entity.getDenizenPlayer();
    }
    damager = null;
    EntityDamageEvent lastDamage = entity.getBukkitEntity().getLastDamageCause();
    if (lastDamage != null) {
        if (lastDamage instanceof EntityDamageByEntityEvent) {
            damager = new dEntity(((EntityDamageByEntityEvent) lastDamage).getDamager()).getDenizenObject();
        }
    }
    message = null;
    inventory = null;
    PlayerDeathEvent subEvent = null;
    if (event instanceof PlayerDeathEvent) {
        subEvent = (PlayerDeathEvent) event;
        message = new Element(subEvent.getDeathMessage());
        // Null check to prevent NPCs from causing an NPE
        if (player != null) {
            inventory = player.getInventory();
        }
        keep_inv = subEvent.getKeepInventory();
        keep_level = subEvent.getKeepLevel();
    }
    cause = null;
    if (event.getEntity().getLastDamageCause() != null) {
        cause = new Element(event.getEntity().getLastDamageCause().getCause().toString());
    }
    drops = new dList();
    for (ItemStack stack : event.getDrops()) {
        if (stack == null) {
            drops.add("i@air");
        } else {
            drops.add(new dItem(stack).identify());
        }
    }
    changed_drops = false;
    xp = event.getDroppedExp();
    this.event = event;
    fire();
    event.setDroppedExp(xp);
    if (changed_drops) {
        event.getDrops().clear();
        for (String drop : drops) {
            dItem item = dItem.valueOf(drop);
            if (item != null) {
                event.getDrops().add(item.getItemStack());
            }
        }
    }
    if (event instanceof PlayerDeathEvent) {
        ((PlayerDeathEvent) event).setKeepInventory(keep_inv);
        ((PlayerDeathEvent) event).setKeepLevel(keep_level);
    }
    if (message != null && subEvent != null) {
        subEvent.setDeathMessage(message.asString());
    }
    if (cancelled && subEvent != null) {
        subEvent.setDeathMessage(null);
    }
    dEntity.forgetEntity(livingEntity);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) PlayerDeathEvent(org.bukkit.event.entity.PlayerDeathEvent) ItemStack(org.bukkit.inventory.ItemStack) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 7 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project Bukkit by Bukkit.

the class KillCommand method execute.

@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    if (!testPermission(sender))
        return true;
    if (sender instanceof Player) {
        Player player = (Player) sender;
        EntityDamageEvent ede = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.SUICIDE, 1000);
        Bukkit.getPluginManager().callEvent(ede);
        if (ede.isCancelled())
            return true;
        ede.getEntity().setLastDamageCause(ede);
        player.setHealth(0);
        sender.sendMessage("Ouch. That look like it hurt.");
    } else {
        sender.sendMessage("You can only perform this command as a player");
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 8 with EntityDamageEvent

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

the class EntityListenerTest method shouldNotCancelEntityDamageEvent.

@Test
public void shouldNotCancelEntityDamageEvent() {
    // given
    EntityDamageEvent event = mock(EntityDamageEvent.class);
    given(listenerService.shouldCancelEvent(event)).willReturn(false);
    // when
    listener.onDamage(event);
    // then
    verify(listenerService).shouldCancelEvent(event);
    verifyZeroInteractions(event);
}
Also used : EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Test(org.junit.Test)

Example 9 with EntityDamageEvent

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

the class EntityListenerTest method shouldCancelEntityDamageEvent.

@Test
public void shouldCancelEntityDamageEvent() {
    // given
    EntityDamageEvent event = mock(EntityDamageEvent.class);
    Entity entity = mock(Entity.class);
    given(event.getEntity()).willReturn(entity);
    given(listenerService.shouldCancelEvent(event)).willReturn(true);
    // when
    listener.onDamage(event);
    // then
    verify(listenerService).shouldCancelEvent(event);
    verify(event).setCancelled(true);
    verify(event).setDamage(0);
    verify(entity).setFireTicks(0);
}
Also used : Entity(org.bukkit.entity.Entity) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Test(org.junit.Test)

Aggregations

EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)9 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)4 Player (org.bukkit.entity.Player)3 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)2 Element (net.aufdemrand.denizencore.objects.Element)2 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)2 Entity (org.bukkit.entity.Entity)2 ItemStack (org.bukkit.inventory.ItemStack)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 LivingEntity (org.bukkit.entity.LivingEntity)1 EventHandler (org.bukkit.event.EventHandler)1 PlayerDeathEvent (org.bukkit.event.entity.PlayerDeathEvent)1 HawkEvent (uk.co.oliwali.HawkEye.HawkEvent)1 DataEntry (uk.co.oliwali.HawkEye.entry.DataEntry)1