Search in sources :

Example 16 with EntityDamageEvent

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;
}
Also used : EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 17 with EntityDamageEvent

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);
}
Also used : Function(com.google.common.base.Function) DamageModifier(org.bukkit.event.entity.EntityDamageEvent.DamageModifier) EnumMap(java.util.EnumMap) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 18 with EntityDamageEvent

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());
}
Also used : EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with EntityDamageEvent

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());
}
Also used : UUID(java.util.UUID) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with EntityDamageEvent

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);
    }
}
Also used : LightningStrike(org.bukkit.entity.LightningStrike) VillagerEntity(net.minecraft.entity.merchant.villager.VillagerEntity) WaterMobEntity(net.minecraft.entity.passive.WaterMobEntity) ProjectileEntity(net.minecraft.entity.projectile.ProjectileEntity) GolemEntity(net.minecraft.entity.passive.GolemEntity) AbstractRaiderEntity(net.minecraft.entity.monster.AbstractRaiderEntity) SlimeEntity(net.minecraft.entity.monster.SlimeEntity) LivingEntity(org.bukkit.entity.LivingEntity) ExperienceOrbEntity(net.minecraft.entity.item.ExperienceOrbEntity) AreaEffectCloudEntity(net.minecraft.entity.AreaEffectCloudEntity) StriderEntity(net.minecraft.entity.passive.StriderEntity) PotionEntity(net.minecraft.entity.projectile.PotionEntity) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) EnderDragonEntity(net.minecraft.entity.boss.dragon.EnderDragonEntity) MonsterEntity(net.minecraft.entity.monster.MonsterEntity) CraftEntity(org.bukkit.craftbukkit.v1_16_R3.entity.CraftEntity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) SpellcastingIllagerEntity(net.minecraft.entity.monster.SpellcastingIllagerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) FireworkRocketEntity(net.minecraft.entity.projectile.FireworkRocketEntity) MobEntity(net.minecraft.entity.MobEntity) HumanEntity(org.bukkit.entity.HumanEntity) Entity(net.minecraft.entity.Entity) GhastEntity(net.minecraft.entity.monster.GhastEntity) ItemEntity(net.minecraft.entity.item.ItemEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_16_R3.entity.CraftLivingEntity) EnderDragonEntity(net.minecraft.entity.boss.dragon.EnderDragonEntity) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) CraftEntity(org.bukkit.craftbukkit.v1_16_R3.entity.CraftEntity) IndirectEntityDamageSource(net.minecraft.util.IndirectEntityDamageSource) EntityDamageSource(net.minecraft.util.EntityDamageSource) Projectile(org.bukkit.entity.Projectile) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) ThrownPotion(org.bukkit.entity.ThrownPotion) CraftBlock(org.bukkit.craftbukkit.v1_16_R3.block.CraftBlock) Block(org.bukkit.block.Block) DamageCause(org.bukkit.event.entity.EntityDamageEvent.DamageCause) IndirectEntityDamageSource(net.minecraft.util.IndirectEntityDamageSource) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Aggregations

EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)109 Player (org.bukkit.entity.Player)43 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)40 Entity (org.bukkit.entity.Entity)30 LivingEntity (org.bukkit.entity.LivingEntity)25 EventHandler (org.bukkit.event.EventHandler)24 Projectile (org.bukkit.entity.Projectile)15 ItemStack (org.bukkit.inventory.ItemStack)13 Test (org.junit.Test)11 Function (com.google.common.base.Function)8 ProjectileSource (org.bukkit.projectiles.ProjectileSource)8 Location (org.bukkit.Location)7 EntityDamageByBlockEvent (org.bukkit.event.entity.EntityDamageByBlockEvent)7 DamageCause (org.bukkit.event.entity.EntityDamageEvent.DamageCause)7 EntityDeathEvent (org.bukkit.event.entity.EntityDeathEvent)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 World (org.bukkit.World)6 Block (org.bukkit.block.Block)6