cn.nukkit.event.entity.EntityDamageEvent Java Examples

The following examples show how to use cn.nukkit.event.entity.EntityDamageEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnchantmentThorns.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void doPostAttack(Entity attacker, Entity entity) {
    if (!(entity instanceof EntityHumanType)) {
        return;
    }

    EntityHumanType human = (EntityHumanType) entity;

    int thornsLevel = 0;

    for (Item armor : human.getInventory().getArmorContents()) {
        Enchantment thorns = armor.getEnchantment(Enchantment.ID_THORNS);
        if (thorns != null) {
            thornsLevel = Math.max(thorns.getLevel(), thornsLevel);
        }
    }

    ThreadLocalRandom random = ThreadLocalRandom.current();

    if (shouldHit(random, thornsLevel)) {
        attacker.attack(new EntityDamageByEntityEvent(entity, attacker, EntityDamageEvent.DamageCause.ENTITY_ATTACK, getDamage(random, level), 0f));
    }
}
 
Example #2
Source File: DeathEventListener.java    From Plan with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onMobDeath(EntityDeathEvent event) {
    long time = System.currentTimeMillis();
    Entity dead = event.getEntity();

    try {
        EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
        if (!(entityDamageEvent instanceof EntityDamageByEntityEvent)) {
            return;
        }

        EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
        Entity killerEntity = entityDamageByEntityEvent.getDamager();

        handleKill(time, /* Not a player */ null, killerEntity);
    } catch (Exception e) {
        errorLogger.log(L.ERROR, e, ErrorContext.builder().related(event, dead).build());
    }
}
 
Example #3
Source File: DeathEventListener.java    From Plan with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerDeath(PlayerDeathEvent event) {
    long time = System.currentTimeMillis();
    Player dead = event.getEntity();
    SessionCache.getCachedSession(dead.getUniqueId()).ifPresent(Session::died);

    try {
        EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
        if (!(entityDamageEvent instanceof EntityDamageByEntityEvent)) {
            return;
        }

        EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
        Entity killerEntity = entityDamageByEntityEvent.getDamager();

        UUID uuid = dead.getUniqueId();
        handleKill(time, uuid, killerEntity);
    } catch (Exception e) {
        errorLogger.log(L.ERROR, e, ErrorContext.builder().related(event, dead).build());
    }
}
 
Example #4
Source File: Effect.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void applyEffect(Entity entity) {
    switch (this.id) {
        case Effect.POISON: //POISON
            if (entity.getHealth() > 1) {
                entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, 1));
            }
            break;
        case Effect.WITHER: //WITHER
            entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, 1));
            break;
        case Effect.REGENERATION: //REGENERATION
            if (entity.getHealth() < entity.getMaxHealth()) {
                entity.heal(new EntityRegainHealthEvent(entity, 1, EntityRegainHealthEvent.CAUSE_MAGIC));
            }
            break;
    }
}
 
Example #5
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public boolean attack(EntityDamageEvent source) {
    if (hasEffect(Effect.FIRE_RESISTANCE)
            && (source.getCause() == DamageCause.FIRE
            || source.getCause() == DamageCause.FIRE_TICK
            || source.getCause() == DamageCause.LAVA)) {
        return false;
    }

    getServer().getPluginManager().callEvent(source);
    if (source.isCancelled()) {
        return false;
    }
    if (this.absorption > 0) {  //Damage Absorption
        float absorptionHealth = this.absorption - source.getFinalDamage() > 0 ? source.getFinalDamage() : this.absorption;
        this.setAbsorption(this.absorption - absorptionHealth);
        source.setDamage(-absorptionHealth, EntityDamageEvent.DamageModifier.ABSORPTION);
    }
    setLastDamageCause(source);
    setHealth(getHealth() - source.getFinalDamage());
    return true;
}
 
Example #6
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void fall(float fallDistance) {
    float damage = (float) Math.floor(fallDistance - 3 - (this.hasEffect(Effect.JUMP) ? this.getEffect(Effect.JUMP).getAmplifier() + 1 : 0));
    if (damage > 0) {
        this.attack(new EntityDamageEvent(this, DamageCause.FALL, damage));
    }

    if (fallDistance > 0.75) {
        BlockVector3 v = new BlockVector3(getFloorX(), getFloorY() - 1, getFloorZ());
        int down = this.level.getBlockIdAt(v.x, v.y, v.z);

        if (down == Item.FARMLAND) {
            if (this instanceof Player) {
                Player p = (Player) this;
                PlayerInteractEvent ev = new PlayerInteractEvent(p, p.getInventory().getItemInHand(), this.temporalVector.setComponents(v.x, v.y, v.z), null, Action.PHYSICAL);
                this.server.getPluginManager().callEvent(ev);
                if (ev.isCancelled()) {
                    return;
                }
            }
            this.level.setBlock(this.temporalVector.setComponents(v.x, v.y, v.z), new BlockDirt(), true, true);
        }
    }
}
 
Example #7
Source File: EntityXPOrb.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    return (source.getCause() == DamageCause.VOID ||
            source.getCause() == DamageCause.FIRE_TICK ||
            (source.getCause() == DamageCause.ENTITY_EXPLOSION ||
            source.getCause() == DamageCause.BLOCK_EXPLOSION) &&
            !this.isInsideOfWater()) && super.attack(source);
}
 
Example #8
Source File: EntityHumanType.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected double calculateEnchantmentProtectionFactor(Item item, EntityDamageEvent source) {
    if (!item.hasEnchantments()) {
        return 0;
    }

    double epf = 0;

    for (Enchantment ench : item.getEnchantments()) {
        epf += ench.getProtectionFactor(source);
    }

    return epf;
}
 
Example #9
Source File: EntityHumanType.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected double calculateEnchantmentReduction(Item item, EntityDamageEvent source) {
    if (!item.hasEnchantments()) {
        return 0;
    }

    double reduction = 0;

    for (Enchantment ench : item.getEnchantments()) {
        reduction += ench.getDamageProtection(source);
    }

    return reduction;
}
 
Example #10
Source File: EntityVehicle.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    VehicleDamageEvent event = new VehicleDamageEvent(this, source.getEntity(), source.getFinalDamage());
    getServer().getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        return false;
    }

    boolean instantKill = false;

    if (source instanceof EntityDamageByEntityEvent) {
        Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
        instantKill = damager instanceof Player && ((Player) damager).isCreative();
    }

    if (instantKill || getHealth() - source.getFinalDamage() < 1) {
        VehicleDestroyEvent event2 = new VehicleDestroyEvent(this, source.getEntity());
        getServer().getPluginManager().callEvent(event2);

        if (event2.isCancelled()) {
            return false;
        }
    }

    if (instantKill) {
        source.setDamage(1000);
    }

    return super.attack(source);
}
 
Example #11
Source File: EntityPainting.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (super.attack(source)) {
        if (source instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
            if (damager instanceof Player && ((Player) damager).isSurvival() && this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
                this.level.dropItem(this, new ItemPainting());
            }
        }
        this.close();
        return true;
    } else {
        return false;
    }
}
 
Example #12
Source File: EntityEndCrystal.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (source.getCause() == EntityDamageEvent.DamageCause.FIRE || source.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK || source.getCause() == EntityDamageEvent.DamageCause.LAVA) {
        return false;
    }

    if (!super.attack(source)) {
        return false;
    }

    explode();

    return true;
}
 
Example #13
Source File: EntityFirework.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    return (source.getCause() == DamageCause.VOID ||
            source.getCause() == DamageCause.FIRE_TICK ||
            source.getCause() == DamageCause.ENTITY_EXPLOSION ||
            source.getCause() == DamageCause.BLOCK_EXPLOSION)
            && super.attack(source);
}
 
Example #14
Source File: EnchantmentProtectionProjectile.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getProtectionFactor(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.PROJECTILE)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #15
Source File: EnchantmentProtectionFire.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getDamageProtection(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.LAVA && cause != DamageCause.FIRE && cause != DamageCause.FIRE_TICK)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #16
Source File: EnchantmentProtectionExplosion.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getDamageProtection(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.ENTITY_EXPLOSION && cause != DamageCause.BLOCK_EXPLOSION)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #17
Source File: EnchantmentProtectionAll.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getDamageProtection(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || cause == DamageCause.VOID || cause == DamageCause.CUSTOM || cause == DamageCause.MAGIC) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #18
Source File: EnchantmentProtectionFall.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getDamageProtection(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.FALL)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #19
Source File: EntityEnderPearl.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private void teleport() {
    if (!this.level.equals(this.shootingEntity.getLevel())) {
        return;
    }

    this.shootingEntity.teleport(new Vector3(NukkitMath.floorDouble(this.x) + 0.5, this.y, NukkitMath.floorDouble(this.z) + 0.5), TeleportCause.ENDER_PEARL);
    if ((((Player) this.shootingEntity).getGamemode() & 0x01) == 0) {
        this.shootingEntity.attack(new EntityDamageByEntityEvent(this, shootingEntity, EntityDamageEvent.DamageCause.PROJECTILE, 5f, 0f));
    }
    this.level.addSound(this, Sound.MOB_ENDERMEN_PORTAL);
}
 
Example #20
Source File: EntityPainting.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (super.attack(source)) {
        if (source instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
            if (damager instanceof Player && ((Player) damager).isSurvival() && this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
                this.level.dropItem(this, new ItemPainting());
            }
        }
        this.close();
        return true;
    } else {
        return false;
    }
}
 
Example #21
Source File: EntityItem.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    return (source.getCause() == DamageCause.VOID ||
            source.getCause() == DamageCause.CONTACT ||
            source.getCause() == DamageCause.FIRE_TICK ||
            (source.getCause() == DamageCause.ENTITY_EXPLOSION ||
            source.getCause() == DamageCause.BLOCK_EXPLOSION) &&
            !this.isInsideOfWater() && (this.item == null ||
            this.item.getId() != Item.NETHER_STAR)) && super.attack(source);
}
 
Example #22
Source File: EnchantmentProtectionAll.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getProtectionFactor(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || cause == DamageCause.VOID || cause == DamageCause.CUSTOM || cause == DamageCause.MAGIC) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #23
Source File: EnchantmentProtectionExplosion.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getProtectionFactor(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.ENTITY_EXPLOSION && cause != DamageCause.BLOCK_EXPLOSION)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #24
Source File: EnchantmentProtectionFire.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getProtectionFactor(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.LAVA && cause != DamageCause.FIRE && cause != DamageCause.FIRE_TICK)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #25
Source File: EnchantmentProtectionFall.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public float getProtectionFactor(EntityDamageEvent e) {
    DamageCause cause = e.getCause();

    if (level <= 0 || (cause != DamageCause.FALL)) {
        return 0;
    }

    return (float) (getLevel() * getTypeModifier());
}
 
Example #26
Source File: BlockMagma.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onEntityCollide(Entity entity) {
    if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
        if (entity instanceof Player) {
            Player p = (Player) entity;
            if (!p.isCreative() && !p.isSpectator() && !p.isSneaking()) {
                entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.LAVA, 1));
            }
        } else {
            entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.LAVA, 1));
        }
    }
}
 
Example #27
Source File: Player.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (!this.isAlive()) {
        return false;
    }

    if (this.isSpectator() || (this.isCreative() && source.getCause() != DamageCause.SUICIDE)) {
        //source.setCancelled();
        return false;
    } else if (this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && source.getCause() == DamageCause.FALL) {
        //source.setCancelled();
        return false;
    } else if (source.getCause() == DamageCause.FALL) {
        if (this.getLevel().getBlock(this.getPosition().floor().add(0.5, -1, 0.5)).getId() == Block.SLIME_BLOCK) {
            if (!this.isSneaking()) {
                //source.setCancelled();
                this.resetFallDistance();
                return false;
            }
        }
    }

    if (super.attack(source)) { //!source.isCancelled()
        if (this.getLastDamageCause() == source && this.spawned) {
            if (source instanceof EntityDamageByEntityEvent) {
                Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
                if (damager instanceof Player) {
                    ((Player) damager).getFoodData().updateFoodExpLevel(0.3);
                }
            }
            EntityEventPacket pk = new EntityEventPacket();
            pk.eid = this.id;
            pk.event = EntityEventPacket.HURT_ANIMATION;
            this.dataPacket(pk);
        }
        return true;
    } else {
        return false;
    }
}
 
Example #28
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    if (invulnerable) {
        return false;
    } else {
        Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
        boolean instantKill = damager instanceof Player && ((Player) damager).isCreative();
        if (!instantKill) performHurtAnimation((int) source.getFinalDamage());

        if (instantKill || getDamage() > 40) {
            if (linkedEntity != null) {
                mountEntity(linkedEntity);
            }

            if (instantKill && (!hasCustomName())) {
                kill();
            } else {
                if (level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
                    dropItem();
                }
                close();
            }
        }
    }

    return true;
}
 
Example #29
Source File: EntityItem.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    return (source.getCause() == DamageCause.VOID ||
            source.getCause() == DamageCause.FIRE_TICK ||
            source.getCause() == DamageCause.ENTITY_EXPLOSION ||
            source.getCause() == DamageCause.BLOCK_EXPLOSION)
            && super.attack(source);
}
 
Example #30
Source File: EntityXPOrb.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean attack(EntityDamageEvent source) {
    return (source.getCause() == DamageCause.VOID ||
            source.getCause() == DamageCause.FIRE_TICK ||
            source.getCause() == DamageCause.ENTITY_EXPLOSION ||
            source.getCause() == DamageCause.BLOCK_EXPLOSION)
            && super.attack(source);
}