Search in sources :

Example 6 with EntityDamageEvent

use of cn.nukkit.event.entity.EntityDamageEvent in project Nukkit by Nukkit.

the class EnchantmentThorns method doPostAttack.

@Override
public void doPostAttack(Entity attacker, Entity entity) {
    if (!(entity instanceof EntityHumanType)) {
        return;
    }
    EntityHumanType human = (EntityHumanType) entity;
    int thornsDamage = 0;
    Random rnd = new Random();
    for (Item armor : human.getInventory().getArmorContents()) {
        Enchantment thorns = armor.getEnchantment(Enchantment.ID_THORNS);
        if (thorns != null && thorns.getLevel() > 0) {
            int chance = thorns.getLevel() * 15;
            if (chance > 90) {
                chance = 90;
            }
            if (rnd.nextInt(100) + 1 <= chance) {
                thornsDamage += rnd.nextInt(4) + 1;
            }
        }
    }
    if (thornsDamage > 0) {
        attacker.attack(new EntityDamageEvent(attacker, DamageCause.MAGIC, rnd.nextInt(4) + 1));
    }
}
Also used : Item(cn.nukkit.item.Item) Random(java.util.Random) EntityHumanType(cn.nukkit.entity.EntityHumanType) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent)

Aggregations

EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)6 Player (cn.nukkit.Player)2 EntityDamageByBlockEvent (cn.nukkit.event.entity.EntityDamageByBlockEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityRegainHealthEvent (cn.nukkit.event.entity.EntityRegainHealthEvent)2 Item (cn.nukkit.item.Item)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockTNT (cn.nukkit.block.BlockTNT)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 Entity (cn.nukkit.entity.Entity)1 EntityHumanType (cn.nukkit.entity.EntityHumanType)1 EntityLiving (cn.nukkit.entity.EntityLiving)1 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)1 EntityExplodeEvent (cn.nukkit.event.entity.EntityExplodeEvent)1 PotionApplyEvent (cn.nukkit.event.potion.PotionApplyEvent)1 ItemBlock (cn.nukkit.item.ItemBlock)1 HugeExplodeSeedParticle (cn.nukkit.level.particle.HugeExplodeSeedParticle)1 ExplodePacket (cn.nukkit.network.protocol.ExplodePacket)1