Search in sources :

Example 1 with PotionApplyEvent

use of cn.nukkit.event.potion.PotionApplyEvent in project Nukkit by Nukkit.

the class Potion method applyPotion.

public void applyPotion(Entity entity, double health) {
    if (!(entity instanceof EntityLiving)) {
        return;
    }
    Effect applyEffect = getEffect(this.getId(), this.isSplash());
    if (applyEffect == null) {
        return;
    }
    if (entity instanceof Player) {
        if (!((Player) entity).isSurvival() && applyEffect.isBad()) {
            return;
        }
    }
    PotionApplyEvent event = new PotionApplyEvent(this, applyEffect, entity);
    entity.getServer().getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        return;
    }
    applyEffect = event.getApplyEffect();
    switch(this.getId()) {
        case INSTANT_HEALTH:
        case INSTANT_HEALTH_II:
            entity.heal(new EntityRegainHealthEvent(entity, (float) (health * (double) (4 << (applyEffect.getAmplifier() + 1))), EntityRegainHealthEvent.CAUSE_EATING));
            break;
        case HARMING:
        case HARMING_II:
            entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, (float) (health * (double) (6 << (applyEffect.getAmplifier() + 1)))));
            break;
        default:
            int duration = (int) ((isSplash() ? health : 1) * (double) applyEffect.getDuration() + 0.5);
            applyEffect.setDuration(duration);
            entity.addEffect(applyEffect);
    }
}
Also used : Player(cn.nukkit.Player) EntityLiving(cn.nukkit.entity.EntityLiving) EntityRegainHealthEvent(cn.nukkit.event.entity.EntityRegainHealthEvent) PotionApplyEvent(cn.nukkit.event.potion.PotionApplyEvent) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent)

Aggregations

Player (cn.nukkit.Player)1 EntityLiving (cn.nukkit.entity.EntityLiving)1 EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)1 EntityRegainHealthEvent (cn.nukkit.event.entity.EntityRegainHealthEvent)1 PotionApplyEvent (cn.nukkit.event.potion.PotionApplyEvent)1