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);
}
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;
}
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);
}
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);
}
Aggregations