Search in sources :

Example 61 with EntityDamageEvent

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

the class EntityHelperImpl method damage.

@Override
public void damage(LivingEntity target, float amount, Entity source, EntityDamageEvent.DamageCause cause) {
    if (target == null) {
        return;
    }
    EntityLiving nmsTarget = ((CraftLivingEntity) target).getHandle();
    net.minecraft.server.v1_16_R3.Entity nmsSource = source == null ? null : ((CraftEntity) source).getHandle();
    CraftEventFactory.entityDamage = nmsSource;
    try {
        DamageSource src = DamageSource.GENERIC;
        if (nmsSource != null) {
            if (nmsSource instanceof EntityHuman) {
                src = DamageSource.playerAttack((EntityHuman) nmsSource);
            } else if (nmsSource instanceof EntityLiving) {
                src = DamageSource.mobAttack((EntityLiving) nmsSource);
            }
        }
        if (cause != null) {
            switch(cause) {
                case CONTACT:
                    src = DamageSource.CACTUS;
                    break;
                case ENTITY_ATTACK:
                    src = DamageSource.mobAttack(nmsSource instanceof EntityLiving ? (EntityLiving) nmsSource : null);
                    break;
                case ENTITY_SWEEP_ATTACK:
                    if (src != DamageSource.GENERIC) {
                        src.sweep();
                    }
                    break;
                case PROJECTILE:
                    src = DamageSource.projectile(nmsSource, source instanceof Projectile && ((Projectile) source).getShooter() instanceof Entity ? ((CraftEntity) ((Projectile) source).getShooter()).getHandle() : null);
                    break;
                case SUFFOCATION:
                    src = DamageSource.STUCK;
                    break;
                case FALL:
                    src = DamageSource.FALL;
                    break;
                case FIRE:
                    src = DamageSource.FIRE;
                    break;
                case FIRE_TICK:
                    src = DamageSource.BURN;
                    break;
                case MELTING:
                    src = CraftEventFactory.MELTING;
                    break;
                case LAVA:
                    src = DamageSource.LAVA;
                    break;
                case DROWNING:
                    src = DamageSource.DROWN;
                    break;
                case BLOCK_EXPLOSION:
                    src = DamageSource.d(nmsSource instanceof TNTPrimed && ((TNTPrimed) nmsSource).getSource() instanceof EntityLiving ? (EntityLiving) ((TNTPrimed) nmsSource).getSource() : null);
                    break;
                case ENTITY_EXPLOSION:
                    src = DamageSource.d(nmsSource instanceof EntityLiving ? (EntityLiving) nmsSource : null);
                    break;
                case VOID:
                    src = DamageSource.OUT_OF_WORLD;
                    break;
                case LIGHTNING:
                    src = DamageSource.LIGHTNING;
                    break;
                case STARVATION:
                    src = DamageSource.STARVE;
                    break;
                case POISON:
                    src = CraftEventFactory.POISON;
                    break;
                case MAGIC:
                    src = DamageSource.MAGIC;
                    break;
                case WITHER:
                    src = DamageSource.WITHER;
                    break;
                case FALLING_BLOCK:
                    src = DamageSource.FALLING_BLOCK;
                    break;
                case THORNS:
                    src = DamageSource.a(nmsSource);
                    break;
                case DRAGON_BREATH:
                    src = DamageSource.DRAGON_BREATH;
                    break;
                case CUSTOM:
                    src = DamageSource.GENERIC;
                    break;
                case FLY_INTO_WALL:
                    src = DamageSource.FLY_INTO_WALL;
                    break;
                case HOT_FLOOR:
                    src = DamageSource.HOT_FLOOR;
                    break;
                case CRAMMING:
                    src = DamageSource.CRAMMING;
                    break;
                case DRYOUT:
                    src = DamageSource.DRYOUT;
                    break;
                // case SUICIDE:
                default:
                    EntityDamageEvent ede = fireFakeDamageEvent(target, source, cause, amount);
                    if (ede.isCancelled()) {
                        return;
                    }
                    break;
            }
        }
        nmsTarget.damageEntity(src, amount);
    } finally {
        CraftEventFactory.entityDamage = null;
    }
}
Also used : Entity(org.bukkit.entity.Entity) net.minecraft.server.v1_16_R3(net.minecraft.server.v1_16_R3) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 62 with EntityDamageEvent

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

the class EntityHelperImpl method damage.

@Override
public void damage(LivingEntity target, float amount, Entity source, EntityDamageEvent.DamageCause cause) {
    if (target == null) {
        return;
    }
    net.minecraft.world.entity.LivingEntity nmsTarget = ((CraftLivingEntity) target).getHandle();
    net.minecraft.world.entity.Entity nmsSource = source == null ? null : ((CraftEntity) source).getHandle();
    CraftEventFactory.entityDamage = nmsSource;
    try {
        DamageSource src = getSourceFor(nmsSource, cause);
        if (src instanceof FakeDamageSrc) {
            src = ((FakeDamageSrc) src).real;
            EntityDamageEvent ede = fireFakeDamageEvent(target, source, cause, amount);
            if (ede.isCancelled()) {
                return;
            }
        }
        nmsTarget.hurt(src, amount);
    } finally {
        CraftEventFactory.entityDamage = null;
    }
}
Also used : DamageSource(net.minecraft.world.damagesource.DamageSource) org.bukkit.entity(org.bukkit.entity) net.minecraft.world.entity(net.minecraft.world.entity) org.bukkit.craftbukkit.v1_18_R2.entity(org.bukkit.craftbukkit.v1_18_R2.entity) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 63 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project Denizen 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 64 with EntityDamageEvent

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

the class EntityHelper method damage.

public void damage(LivingEntity target, float amount, Entity source, EntityDamageEvent.DamageCause cause) {
    if (cause == null) {
        if (source == null) {
            target.damage(amount);
        } else {
            target.damage(amount, source);
        }
    } else {
        EntityDamageEvent ede = fireFakeDamageEvent(target, source, cause, amount);
        if (!ede.isCancelled()) {
            target.setLastDamageCause(ede);
            if (source == null) {
                target.damage(ede.getFinalDamage());
            } else {
                target.damage(ede.getFinalDamage(), source);
            }
            target.setLastDamageCause(ede);
        }
    }
}
Also used : EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 65 with EntityDamageEvent

use of org.bukkit.event.entity.EntityDamageEvent in project AllMiniGames by MiniGameWorlds.

the class Tiny method onEvent.

@Override
protected void onEvent(Event event) {
    if (event instanceof ProjectileHitEvent) {
        ProjectileHitEvent e = (ProjectileHitEvent) event;
        Entity hitEntity = e.getHitEntity();
        if (hitEntity != null && hitEntity.equals(this.bat)) {
            // event detector can detect shooter from ProjectileHitEvent
            plusScore(1);
            // sound
            SoundTool.play(hitEntity.getLocation(), Sound.BLOCK_NOTE_BLOCK_BELL);
            // particle
            spawnHitParticles(hitEntity);
        }
    } else if (event instanceof ProjectileLaunchEvent) {
        ProjectileLaunchEvent e = (ProjectileLaunchEvent) event;
        Projectile proj = e.getEntity();
        ProjectileSource shooterEntity = proj.getShooter();
        // event detector can detect player from ProjectileLaunchEvent
        Player shooter = (Player) shooterEntity;
        // don't change amount of snowball
        shooter.getInventory().addItem(new ItemStack(Material.SNOWBALL));
        // give cooldown
        shooter.setCooldown(Material.SNOWBALL, 20);
    } else if (event instanceof EntityDamageEvent) {
        EntityDamageEvent e = (EntityDamageEvent) event;
        if (e.getEntity().equals(this.bat)) {
            e.setDamage(0);
        }
    } else if (event instanceof EntityDeathEvent) {
        EntityDeathEvent e = (EntityDeathEvent) event;
        if (e.getEntity().equals(this.bat)) {
            summonBat();
        }
    }
}
Also used : ProjectileHitEvent(org.bukkit.event.entity.ProjectileHitEvent) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) ProjectileSource(org.bukkit.projectiles.ProjectileSource) ItemStack(org.bukkit.inventory.ItemStack) Projectile(org.bukkit.entity.Projectile) 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