Java Code Examples for cn.nukkit.potion.Effect#getDuration()

The following examples show how to use cn.nukkit.potion.Effect#getDuration() . 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: Entity.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void addEffect(Effect effect) {
    if (effect == null) {
        return; //here add null means add nothing
    }

    Effect oldEffect = this.effects.getOrDefault(effect.getId(), null);
    if (oldEffect != null) {
        if (Math.abs(effect.getAmplifier()) < Math.abs(oldEffect.getAmplifier())) return;
        if (Math.abs(effect.getAmplifier()) == Math.abs(oldEffect.getAmplifier())
                && effect.getDuration() < oldEffect.getDuration()) return;
        effect.add(this, true);
    } else {
        effect.add(this, false);
    }

    this.effects.put(effect.getId(), effect);

    this.recalculateEffectColor();

    if (effect.getId() == Effect.HEALTH_BOOST) {
        this.setHealth(this.getHealth() + 4 * (effect.getAmplifier() + 1));
    }

}
 
Example 2
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void sendPotionEffects(Player player) {
    for (Effect effect : this.effects.values()) {
        MobEffectPacket pk = new MobEffectPacket();
        pk.entityRuntimeId = this.getId();
        pk.effectId = effect.getId();
        pk.amplifier = effect.getAmplifier();
        pk.particles = effect.isVisible();
        pk.duration = effect.getDuration();
        pk.eventId = MobEffectPacket.EVENT_ADD;

        player.dataPacket(pk);
    }
}
 
Example 3
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void sendPotionEffects(Player player) {
    for (Effect effect : this.effects.values()) {
        MobEffectPacket pk = new MobEffectPacket();
        pk.eid = this.getId();
        pk.effectId = effect.getId();
        pk.amplifier = effect.getAmplifier();
        pk.particles = effect.isVisible();
        pk.duration = effect.getDuration();
        pk.eventId = MobEffectPacket.EVENT_ADD;

        player.dataPacket(pk);
    }
}
 
Example 4
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addEffect(Effect effect) {
    if (effect == null) {
        return; //here add null means add nothing
    }

    Effect oldEffect = this.effects.getOrDefault(effect.getId(), null);
    if (oldEffect != null) {
        if (Math.abs(effect.getAmplifier()) < Math.abs(oldEffect.getAmplifier())) {
            return;
        }
        if (Math.abs(effect.getAmplifier()) == Math.abs(oldEffect.getAmplifier())
                && effect.getDuration() < oldEffect.getDuration()) {
            return;
        }
        effect.add(this, true);
    } else {
        effect.add(this, false);
    }

    this.effects.put(effect.getId(), effect);

    this.recalculateEffectColor();

    if (effect.getId() == Effect.HEALTH_BOOST) {
        this.setHealth(this.getHealth() + 4 * (effect.getAmplifier() + 1));
    }

}
 
Example 5
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void sendPotionEffects(Player player) {
    for (Effect effect : this.effects.values()) {
        MobEffectPacket pk = new MobEffectPacket();
        pk.eid = this.getId();
        pk.effectId = effect.getId();
        pk.amplifier = effect.getAmplifier();
        pk.particles = effect.isVisible();
        pk.duration = effect.getDuration();
        pk.eventId = MobEffectPacket.EVENT_ADD;

        player.dataPacket(pk);
    }
}
 
Example 6
Source File: Entity.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public boolean entityBaseTick(int tickDiff) {
    Timings.entityBaseTickTimer.startTiming();

    if (!this.isPlayer) {
        this.blocksAround = null;
        this.collisionBlocks = null;
    }
    this.justCreated = false;

    if (!this.isAlive()) {
        this.removeAllEffects();
        this.despawnFromAll();
        if (!this.isPlayer) {
            this.close();
        }
        Timings.entityBaseTickTimer.stopTiming();
        return false;
    }

    if (riding != null && !riding.isAlive()) {
        ((EntityVehicle) riding).mountEntity(this);
    }

    if (!this.effects.isEmpty()) {
        for (Effect effect : new ArrayList<>(this.effects.values())) {
            if (effect.canTick()) {
                effect.applyEffect(this);
            }
            effect.setDuration(effect.getDuration() - tickDiff);

            if (effect.getDuration() <= 0) {
                this.removeEffect(effect.getId());
            }
        }
    }

    boolean hasUpdate = false;

    this.checkBlockCollision();

    if (this.y <= -16 && this.isAlive()) {
        this.attack(new EntityDamageEvent(this, DamageCause.VOID, 10));
        hasUpdate = true;
    }

    if (this.fireTicks > 0) {
        if (this.fireProof) {
            this.fireTicks -= 4 * tickDiff;
            if (this.fireTicks < 0) {
                this.fireTicks = 0;
            }
        } else {
            if (!this.hasEffect(Effect.FIRE_RESISTANCE) && ((this.fireTicks % 20) == 0 || tickDiff > 20)) {
                this.attack(new EntityDamageEvent(this, DamageCause.FIRE_TICK, 1));
            }
            this.fireTicks -= tickDiff;
        }
        if (this.fireTicks <= 0) {
            this.extinguish();
        } else {
            this.setDataFlag(DATA_FLAGS, DATA_FLAG_ONFIRE, true);
            hasUpdate = true;
        }
    }

    if (this.noDamageTicks > 0) {
        this.noDamageTicks -= tickDiff;
        if (this.noDamageTicks < 0) {
            this.noDamageTicks = 0;
        }
    }

    if (this.inPortalTicks > 80) {
        EntityPortalEnterEvent ev = new EntityPortalEnterEvent(this, EntityPortalEnterEvent.TYPE_NETHER);
        getServer().getPluginManager().callEvent(ev);
    }

    this.age += tickDiff;
    this.ticksLived += tickDiff;
    TimingsHistory.activatedEntityTicks++;

    Timings.entityBaseTickTimer.stopTiming();
    return hasUpdate;
}
 
Example 7
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean entityBaseTick(int tickDiff) {
    Timings.entityBaseTickTimer.startTiming();

    if (!this.isPlayer) {
        this.blocksAround = null;
        this.collisionBlocks = null;
    }
    this.justCreated = false;

    if (!this.isAlive()) {
        this.removeAllEffects();
        this.despawnFromAll();
        if (!this.isPlayer) {
            this.close();
        }
        Timings.entityBaseTickTimer.stopTiming();
        return false;
    }
    if (riding != null && !riding.isAlive() && riding instanceof EntityRideable) {
        ((EntityRideable) riding).mountEntity(this);
    }

    if (!this.effects.isEmpty()) {
        for (Effect effect : this.effects.values()) {
            if (effect.canTick()) {
                effect.applyEffect(this);
            }
            effect.setDuration(effect.getDuration() - tickDiff);

            if (effect.getDuration() <= 0) {
                this.removeEffect(effect.getId());
            }
        }
    }

    boolean hasUpdate = false;

    this.checkBlockCollision();

    if (this.y <= -16 && this.isAlive()) {
        this.attack(new EntityDamageEvent(this, DamageCause.VOID, 10));
        hasUpdate = true;
    }

    if (this.fireTicks > 0) {
        if (this.fireProof) {
            this.fireTicks -= 4 * tickDiff;
            if (this.fireTicks < 0) {
                this.fireTicks = 0;
            }
        } else {
            if (!this.hasEffect(Effect.FIRE_RESISTANCE) && ((this.fireTicks % 20) == 0 || tickDiff > 20)) {
                this.attack(new EntityDamageEvent(this, DamageCause.FIRE_TICK, 1));
            }
            this.fireTicks -= tickDiff;
        }
        if (this.fireTicks <= 0) {
            this.extinguish();
        } else {
            this.setDataFlag(DATA_FLAGS, DATA_FLAG_ONFIRE, true);
            hasUpdate = true;
        }
    }

    if (this.noDamageTicks > 0) {
        this.noDamageTicks -= tickDiff;
        if (this.noDamageTicks < 0) {
            this.noDamageTicks = 0;
        }
    }

    if (this.inPortalTicks == 80) {
        EntityPortalEnterEvent ev = new EntityPortalEnterEvent(this, PortalType.NETHER);
        getServer().getPluginManager().callEvent(ev);

        //TODO: teleport
    }

    this.age += tickDiff;
    this.ticksLived += tickDiff;
    TimingsHistory.activatedEntityTicks++;

    Timings.entityBaseTickTimer.stopTiming();
    return hasUpdate;
}