use of org.bukkit.event.entity.EntityDamageEvent in project Mohist by MohistMC.
the class CraftEventFactory method callEntityDamageEvent.
private static EntityDamageEvent callEntityDamageEvent(Entity damager, Entity damagee, DamageCause cause, Map<DamageModifier, Double> modifiers, Map<DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled) {
EntityDamageEvent event;
if (damager != null) {
event = new EntityDamageByEntityEvent(damager.getBukkitEntity(), damagee.getBukkitEntity(), cause, modifiers, modifierFunctions);
} else {
event = new EntityDamageEvent(damagee.getBukkitEntity(), cause, modifiers, modifierFunctions);
}
event.setCancelled(cancelled);
callEvent(event);
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
} else {
// SPIGOT-5339, SPIGOT-6252, SPIGOT-6777: Keep track if the event was canceled
damagee.lastDamageCancelled = true;
}
return event;
}
use of org.bukkit.event.entity.EntityDamageEvent in project Mohist by MohistMC.
the class CraftEventFactory method handleNonLivingEntityDamageEvent.
public static boolean handleNonLivingEntityDamageEvent(Entity entity, DamageSource source, double damage, boolean cancelOnZeroDamage, boolean cancelled) {
final EnumMap<DamageModifier, Double> modifiers = new EnumMap<DamageModifier, Double>(DamageModifier.class);
final EnumMap<DamageModifier, Function<? super Double, Double>> functions = new EnumMap(DamageModifier.class);
modifiers.put(DamageModifier.BASE, damage);
functions.put(DamageModifier.BASE, ZERO);
final EntityDamageEvent event = handleEntityDamageEvent(entity, source, modifiers, functions, cancelled);
if (event == null) {
return false;
}
return event.isCancelled() || (cancelOnZeroDamage && event.getDamage() == 0);
}
use of org.bukkit.event.entity.EntityDamageEvent in project BentoBox by BentoBoxWorld.
the class InvincibleVisitorsListenerTest method testOnVisitorGetDamageNPC.
@Test
public void testOnVisitorGetDamageNPC() {
when(player.hasMetadata(eq("NPC"))).thenReturn(true);
EntityDamageEvent e = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.CRAMMING, 0D);
listener.onVisitorGetDamage(e);
assertFalse(e.isCancelled());
}
use of org.bukkit.event.entity.EntityDamageEvent in project BentoBox by BentoBoxWorld.
the class InvincibleVisitorsListenerTest method testOnVisitorGetDamageVoidNoIslandHerePlayerHasNoIsland.
@Test
public void testOnVisitorGetDamageVoidNoIslandHerePlayerHasNoIsland() {
when(im.getIslandAt(any())).thenReturn(Optional.empty());
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
EntityDamageEvent e = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.VOID, 0D);
// Player should die
listener.onVisitorGetDamage(e);
assertFalse(e.isCancelled());
}
use of org.bukkit.event.entity.EntityDamageEvent in project Magma-1.16.x by magmafoundation.
the class CraftEventFactory method handleEntityDamageEvent.
private static EntityDamageEvent handleEntityDamageEvent(Entity entity, DamageSource source, Map<DamageModifier, Double> modifiers, Map<DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled) {
if (source.isExplosion()) {
DamageCause damageCause;
Entity damager = entityDamage;
entityDamage = null;
EntityDamageEvent event;
if (damager == null) {
event = new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.BLOCK_EXPLOSION, modifiers, modifierFunctions);
} else if (entity instanceof EnderDragonEntity && /*PAIL FIXME ((EnderDragonEntity) entity).target == damager*/
false) {
event = new EntityDamageEvent(entity.getBukkitEntity(), DamageCause.ENTITY_EXPLOSION, modifiers, modifierFunctions);
} else {
if (damager instanceof org.bukkit.entity.TNTPrimed) {
damageCause = DamageCause.BLOCK_EXPLOSION;
} else {
damageCause = DamageCause.ENTITY_EXPLOSION;
}
event = new EntityDamageByEntityEvent(damager.getBukkitEntity(), entity.getBukkitEntity(), damageCause, modifiers, modifierFunctions);
}
event.setCancelled(cancelled);
callEvent(event);
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
}
return event;
} else if (source instanceof EntityDamageSource) {
Entity damager = source.getEntity();
DamageCause cause = (source.isSweep()) ? DamageCause.ENTITY_SWEEP_ATTACK : DamageCause.ENTITY_ATTACK;
if (source instanceof IndirectEntityDamageSource) {
damager = ((IndirectEntityDamageSource) source).getProximateDamageSource();
if (damager.getBukkitEntity() instanceof ThrownPotion) {
cause = DamageCause.MAGIC;
} else if (damager.getBukkitEntity() instanceof Projectile) {
cause = DamageCause.PROJECTILE;
}
} else if ("thorns".equals(source.msgId)) {
cause = DamageCause.THORNS;
}
return callEntityDamageEvent(damager, entity, cause, modifiers, modifierFunctions, cancelled);
} else if (source == DamageSource.OUT_OF_WORLD) {
EntityDamageEvent event = new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.VOID, modifiers, modifierFunctions);
event.setCancelled(cancelled);
callEvent(event);
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
}
return event;
} else if (source == DamageSource.LAVA) {
EntityDamageEvent event = (new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.LAVA, modifiers, modifierFunctions));
event.setCancelled(cancelled);
callEvent(event);
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
}
return event;
} else if (blockDamage != null) {
DamageCause cause = null;
Block damager = blockDamage;
blockDamage = null;
if (source == DamageSource.CACTUS || source == DamageSource.SWEET_BERRY_BUSH) {
cause = DamageCause.CONTACT;
} else if (source == DamageSource.HOT_FLOOR) {
cause = DamageCause.HOT_FLOOR;
} else if (source == DamageSource.MAGIC) {
cause = DamageCause.MAGIC;
} else if (source == DamageSource.FALL) {
cause = DamageCause.FALL;
} else if (source == DamageSource.ANVIL || source == DamageSource.FALLING_BLOCK) {
cause = DamageCause.FALLING_BLOCK;
} else if (source == DamageSource.IN_FIRE) {
cause = DamageCause.FIRE;
} else if (source == DamageSource.ON_FIRE) {
cause = DamageCause.FIRE_TICK;
} else if (source == DamageSource.LAVA) {
cause = DamageCause.LAVA;
} else if (damager instanceof LightningStrike) {
cause = DamageCause.LIGHTNING;
} else if (source == MELTING) {
cause = DamageCause.MELTING;
} else if (source == POISON) {
cause = DamageCause.POISON;
} else if (source == DamageSource.LIGHTNING_BOLT) {
cause = DamageCause.LIGHTNING;
} else if (source == DamageSource.GENERIC) {
cause = DamageCause.CUSTOM;
} else {
cause = DamageCause.CUSTOM;
}
EntityDamageEvent event = new EntityDamageByBlockEvent(damager, entity.getBukkitEntity(), cause, modifiers, modifierFunctions);
event.setCancelled(cancelled);
callEvent(event);
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
}
return event;
} else if (entityDamage != null) {
DamageCause cause = null;
CraftEntity damager = entityDamage.getBukkitEntity();
entityDamage = null;
if (source == DamageSource.ANVIL || source == DamageSource.FALLING_BLOCK) {
cause = DamageCause.FALLING_BLOCK;
} else if (damager instanceof LightningStrike) {
cause = DamageCause.LIGHTNING;
} else if (source == DamageSource.FALL) {
cause = DamageCause.FALL;
} else if (source == DamageSource.DRAGON_BREATH) {
cause = DamageCause.DRAGON_BREATH;
} else if (source == DamageSource.MAGIC) {
cause = DamageCause.MAGIC;
} else if (source == DamageSource.CACTUS) {
cause = DamageCause.CONTACT;
} else if (source == DamageSource.IN_FIRE) {
cause = DamageCause.FIRE;
} else if (source == DamageSource.ON_FIRE) {
cause = DamageCause.FIRE_TICK;
} else if (source == DamageSource.LAVA) {
cause = DamageCause.LAVA;
} else if (source == MELTING) {
cause = DamageCause.MELTING;
} else if (source == POISON) {
cause = DamageCause.POISON;
} else {
cause = DamageCause.CUSTOM;
}
EntityDamageEvent event = new EntityDamageByEntityEvent(damager, entity.getBukkitEntity(), cause, modifiers, modifierFunctions);
event.setCancelled(cancelled);
callEvent(event);
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
}
return event;
}
DamageCause cause = null;
if (source == DamageSource.IN_FIRE) {
cause = DamageCause.FIRE;
} else if (source == DamageSource.STARVE) {
cause = DamageCause.STARVATION;
} else if (source == DamageSource.WITHER) {
cause = DamageCause.WITHER;
} else if (source == DamageSource.IN_WALL) {
cause = DamageCause.SUFFOCATION;
} else if (source == DamageSource.DROWN) {
cause = DamageCause.DROWNING;
} else if (source == DamageSource.ON_FIRE) {
cause = DamageCause.FIRE_TICK;
} else if (source == MELTING) {
cause = DamageCause.MELTING;
} else if (source == POISON) {
cause = DamageCause.POISON;
} else if (source == DamageSource.MAGIC) {
cause = DamageCause.MAGIC;
} else if (source == DamageSource.FALL) {
cause = DamageCause.FALL;
} else if (source == DamageSource.FLY_INTO_WALL) {
cause = DamageCause.FLY_INTO_WALL;
} else if (source == DamageSource.CRAMMING) {
cause = DamageCause.CRAMMING;
} else if (source == DamageSource.DRY_OUT) {
cause = DamageCause.DRYOUT;
} else if (source == DamageSource.GENERIC) {
cause = DamageCause.CUSTOM;
}
if (cause != null) {
return callEntityDamageEvent(null, entity, cause, modifiers, modifierFunctions, cancelled);
} else {
return new EntityDamageEvent(entity.getBukkitEntity(), DamageCause.CUSTOM, modifiers, modifierFunctions);
}
}
Aggregations