From c3d6af3afe8650e572616fcfdca94e0c256a4ae6 Mon Sep 17 00:00:00 2001 From: ThatCreeper Date: Wed, 30 Dec 2020 19:07:07 -0600 Subject: [PATCH] make removeEffect send a packet Make Potion immutable, and use long instead of Long --- .../net/minestom/server/entity/Entity.java | 12 +++++-- .../server/play/EntityEffectPacket.java | 6 ++-- .../net/minestom/server/potion/Potion.java | 36 +++++++++++-------- .../minestom/server/potion/TimedPotion.java | 6 ++-- .../java/demo/commands/PotionCommand.java | 1 + 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 956436c0b..fd325c94e 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -405,7 +405,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P // remove expired effects { effects.removeIf(timedPotion -> time >= - (timedPotion.getStartingTime() + timedPotion.getPotion().duration * MinecraftServer.TICK_MS)); + (timedPotion.getStartingTime() + timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS)); } // scheduled tasks @@ -1464,7 +1464,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @param effect The effect to remove */ public void removeEffect(@NotNull PotionEffect effect) { - effects.removeIf(timedPotion -> timedPotion.getPotion().effect == effect); + effects.removeIf(timedPotion -> { + if (timedPotion.getPotion().getEffect() == effect) { + timedPotion.getPotion().sendRemovePacket(this); + return true; + } + return false; + }); } /** @@ -1473,7 +1479,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @param potion The potion to add */ public void addEffect(@NotNull Potion potion) { - removeEffect(potion.effect); + removeEffect(potion.getEffect()); effects.add(new TimedPotion(potion, System.currentTimeMillis())); potion.sendAddPacket(this); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java index b9fbaacaf..15c2691e8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java @@ -14,9 +14,9 @@ public class EntityEffectPacket implements ServerPacket { @Override public void write(@NotNull BinaryWriter writer) { writer.writeVarInt(entityId); - writer.writeByte((byte) potion.effect.getId()); - writer.writeByte(potion.amplifier); - writer.writeVarInt(potion.duration); + writer.writeByte((byte) potion.getEffect().getId()); + writer.writeByte(potion.getAmplifier()); + writer.writeVarInt(potion.getDuration()); writer.writeByte(potion.getFlags()); } diff --git a/src/main/java/net/minestom/server/potion/Potion.java b/src/main/java/net/minestom/server/potion/Potion.java index 65e3d0d30..dc3f8a34b 100644 --- a/src/main/java/net/minestom/server/potion/Potion.java +++ b/src/main/java/net/minestom/server/potion/Potion.java @@ -7,12 +7,10 @@ import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket; import org.jetbrains.annotations.NotNull; public class Potion { - public PotionEffect effect; - public byte amplifier; - public int duration; - public boolean ambient; - public boolean particles; - public boolean icon; + private final PotionEffect effect; + private final byte amplifier; + private final int duration; + private final byte flags; public Potion(PotionEffect effect, byte amplifier, int duration) { this(effect, amplifier, duration, true, true, false); @@ -30,17 +28,27 @@ public class Potion { this.effect = effect; this.amplifier = amplifier; this.duration = duration; - this.particles = particles; - this.icon = icon; - this.ambient = ambient; + byte flags = 0; + if (ambient) flags = (byte)(flags | 0x01); + if (particles) flags = (byte)(flags | 0x02); + if (icon) flags = (byte)(flags | 0x04); + this.flags = flags; + } + + public PotionEffect getEffect() { + return effect; + } + + public byte getAmplifier() { + return amplifier; + } + + public int getDuration() { + return duration; } public byte getFlags() { - byte computed = 0x00; - if (ambient) computed = (byte)(computed | 0x01); - if (particles) computed = (byte)(computed | 0x02); - if (icon) computed = (byte)(computed | 0x04); - return computed; + return flags; } public void sendAddPacket(@NotNull Entity entity) { diff --git a/src/main/java/net/minestom/server/potion/TimedPotion.java b/src/main/java/net/minestom/server/potion/TimedPotion.java index 7674c9f7d..c77215be7 100644 --- a/src/main/java/net/minestom/server/potion/TimedPotion.java +++ b/src/main/java/net/minestom/server/potion/TimedPotion.java @@ -2,9 +2,9 @@ package net.minestom.server.potion; public class TimedPotion { private final Potion potion; - private final Long startingTime; + private final long startingTime; - public TimedPotion(Potion potion, Long startingTime) { + public TimedPotion(Potion potion, long startingTime) { this.potion = potion; this.startingTime = startingTime; } @@ -13,7 +13,7 @@ public class TimedPotion { return potion; } - public Long getStartingTime() { + public long getStartingTime() { return startingTime; } } diff --git a/src/test/java/demo/commands/PotionCommand.java b/src/test/java/demo/commands/PotionCommand.java index a5278125d..33c05d062 100644 --- a/src/test/java/demo/commands/PotionCommand.java +++ b/src/test/java/demo/commands/PotionCommand.java @@ -40,6 +40,7 @@ public class PotionCommand extends Command { final PotionEffect potion = args.getPotionEffect("potion"); final int duration = args.getInteger("duration"); + player.sendMessage(player.getActiveEffects().toString()); player.addEffect(new Potion( potion, (byte) 0,