use of cn.nukkit.potion.Effect in project Nukkit by Nukkit.
the class EffectCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
if (args.length < 2) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
Player player = sender.getServer().getPlayer(args[0]);
if (player == null) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
return true;
}
if (args[1].equalsIgnoreCase("clear")) {
for (Effect effect : player.getEffects().values()) {
player.removeEffect(effect.getId());
}
sender.sendMessage(new TranslationContainer("commands.effect.success.removed.all", player.getDisplayName()));
return true;
}
Effect effect;
try {
effect = Effect.getEffect(Integer.parseInt(args[1]));
} catch (NumberFormatException | ServerException a) {
try {
effect = Effect.getEffectByName(args[1]);
} catch (Exception e) {
sender.sendMessage(new TranslationContainer("commands.effect.notFound", args[1]));
return true;
}
}
int duration = 300;
int amplification = 0;
if (args.length >= 3) {
try {
duration = Integer.valueOf(args[2]);
} catch (NumberFormatException a) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
if (!(effect instanceof InstantEffect)) {
duration *= 20;
}
} else if (effect instanceof InstantEffect) {
duration = 1;
}
if (args.length >= 4) {
try {
amplification = Integer.valueOf(args[3]);
} catch (NumberFormatException a) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
}
if (args.length >= 5) {
String v = args[4].toLowerCase();
if (v.matches("(?i)|on|true|t|1")) {
effect.setVisible(false);
}
}
if (duration == 0) {
if (!player.hasEffect(effect.getId())) {
if (player.getEffects().size() == 0) {
sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive.all", player.getDisplayName()));
} else {
sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive", new String[] { effect.getName(), player.getDisplayName() }));
}
return true;
}
player.removeEffect(effect.getId());
sender.sendMessage(new TranslationContainer("commands.effect.success.removed", new String[] { effect.getName(), player.getDisplayName() }));
} else {
effect.setDuration(duration).setAmplifier(amplification);
player.addEffect(effect);
Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.effect.success", new String[] { effect.getName(), String.valueOf(effect.getId()), String.valueOf(effect.getAmplifier()), player.getDisplayName(), String.valueOf(effect.getDuration() / 20) }));
}
return true;
}
use of cn.nukkit.potion.Effect in project Nukkit by Nukkit.
the class EntityPotion method onUpdate.
@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}
this.timing.startTiming();
int tickDiff = currentTick - this.lastUpdate;
boolean hasUpdate = super.onUpdate(currentTick);
if (this.age > 1200) {
this.kill();
hasUpdate = true;
}
if (this.isCollided) {
this.kill();
Potion potion = Potion.getPotion(this.potionId);
PotionCollideEvent event = new PotionCollideEvent(potion, this);
this.server.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
}
potion = event.getPotion();
if (potion == null) {
return false;
}
potion.setSplash(true);
Particle particle;
int r;
int g;
int b;
Effect effect = Potion.getEffect(potion.getId(), true);
if (effect == null) {
r = 40;
g = 40;
b = 255;
} else {
int[] colors = effect.getColor();
r = colors[0];
g = colors[1];
b = colors[2];
}
if (Potion.isInstant(potion.getId())) {
particle = new InstantSpellParticle(this, r, g, b);
} else {
particle = new SpellParticle(this, r, g, b);
}
this.getLevel().addParticle(particle);
hasUpdate = true;
Entity[] entities = this.getLevel().getNearbyEntities(this.getBoundingBox().grow(8.25, 4.24, 8.25));
for (Entity anEntity : entities) {
double distance = anEntity.distanceSquared(this);
if (distance < 16) {
double d = 1 - Math.sqrt(distance) / 4;
potion.applyPotion(anEntity, d);
}
}
}
this.timing.stopTiming();
return hasUpdate;
}
use of cn.nukkit.potion.Effect in project Nukkit by Nukkit.
the class Entity method addEffect.
public void addEffect(Effect effect) {
if (effect == null) {
// here add null means add nothing
return;
}
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));
}
}
use of cn.nukkit.potion.Effect in project Nukkit by Nukkit.
the class Entity method entityBaseTick.
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;
}
Aggregations