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

The following examples show how to use cn.nukkit.potion.Effect#setDuration() . 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 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 2
Source File: BlockEntityBeacon.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate() {
    //Only apply effects every 4 secs
    if (currentTick++ % 80 != 0) {
        return true;
    }

    int oldPowerLevel = this.getPowerLevel();
    //Get the power level based on the pyramid
    setPowerLevel(calculatePowerLevel());
    int newPowerLevel = this.getPowerLevel();

    //Skip beacons that do not have a pyramid or sky access
    if (newPowerLevel < 1 || !hasSkyAccess()) {
        if (oldPowerLevel > 0) {
            this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BEACON_DEACTIVATE);
        }
        return true;
    } else if (oldPowerLevel < 1) {
        this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BEACON_ACTIVATE);
    } else {
        this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BEACON_AMBIENT);
    }

    //Get all players in game
    Map<Long, Player> players = this.level.getPlayers();

    //Calculate vars for beacon power
    int range = 10 + getPowerLevel() * 10;
    int duration = 9 + getPowerLevel() * 2;

    for(Map.Entry<Long, Player> entry : players.entrySet()) {
        Player p = entry.getValue();

        //If the player is in range
        if (p.distance(this) < range) {
            Effect e;

            if (getPrimaryPower() != 0) {
                //Apply the primary power
                e = Effect.getEffect(getPrimaryPower());

                //Set duration
                e.setDuration(duration * 20);

                //If secondary is selected as the primary too, apply 2 amplification
                if (getSecondaryPower() == getPrimaryPower()) {
                    e.setAmplifier(2);
                } else {
                    e.setAmplifier(1);
                }

                //Hide particles
                e.setVisible(false);

                //Add the effect
                p.addEffect(e);
            }

            //If we have a secondary power as regen, apply it
            if (getSecondaryPower() == Effect.REGENERATION) {
                //Get the regen effect
                e = Effect.getEffect(Effect.REGENERATION);

                //Set duration
                e.setDuration(duration * 20);

                //Regen I
                e.setAmplifier(1);

                //Hide particles
                e.setVisible(false);

                //Add effect
                p.addEffect(e);
            }
        }
    }

    return true;
}
 
Example 3
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;
}