From fd920ffdfe76993624861defdfc7665714ab4ae3 Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 31 Dec 2020 12:05:36 +0100 Subject: [PATCH] Potion cleanup --- .../net/minestom/server/entity/Entity.java | 21 +++++--- .../net/minestom/server/potion/Potion.java | 54 ++++++++++--------- .../minestom/server/potion/TimedPotion.java | 6 ++- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index ff7b58733..3d131e050 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -134,7 +134,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P protected boolean noGravity; protected Pose pose = Pose.STANDING; - private CopyOnWriteArrayList effects = new CopyOnWriteArrayList<>(); + private final List effects = new CopyOnWriteArrayList<>(); // list of scheduled tasks to be executed during the next entity tick protected final Queue> nextTick = Queues.newConcurrentLinkedQueue(); @@ -404,9 +404,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P // remove expired effects { - effects.removeIf(timedPotion -> time - >= - (timedPotion.getStartingTime() + timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS)); + this.effects.removeIf(timedPotion -> { + final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS; + return timedPotion.getStartingTime() + potionTime <= time; + }); } // scheduled tasks @@ -1455,8 +1456,14 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P DYING } + /** + * Gets all the potion effect of this entity. + * + * @return an unmodifiable list of all this entity effects + */ + @NotNull public List getActiveEffects() { - return effects; + return Collections.unmodifiableList(effects); } /** @@ -1465,7 +1472,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @param effect The effect to remove */ public void removeEffect(@NotNull PotionEffect effect) { - effects.removeIf(timedPotion -> { + this.effects.removeIf(timedPotion -> { if (timedPotion.getPotion().getEffect() == effect) { timedPotion.getPotion().sendRemovePacket(this); return true; @@ -1481,7 +1488,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P */ public void addEffect(@NotNull Potion potion) { removeEffect(potion.getEffect()); - effects.add(new TimedPotion(potion, System.currentTimeMillis())); + this.effects.add(new TimedPotion(potion, System.currentTimeMillis())); potion.sendAddPacket(this); } diff --git a/src/main/java/net/minestom/server/potion/Potion.java b/src/main/java/net/minestom/server/potion/Potion.java index 4501c9c41..960340c02 100644 --- a/src/main/java/net/minestom/server/potion/Potion.java +++ b/src/main/java/net/minestom/server/potion/Potion.java @@ -6,6 +6,7 @@ import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket; import org.jetbrains.annotations.NotNull; public class Potion { + private final PotionEffect effect; private final byte amplifier; private final int duration; @@ -14,50 +15,50 @@ public class Potion { /** * Creates a new potion. * - * @param effect The type of potion. + * @param effect The type of potion. * @param amplifier The strength of the potion. - * @param duration The length of the potion in ticks. + * @param duration The length of the potion in ticks. */ - public Potion(PotionEffect effect, byte amplifier, int duration) { + public Potion(@NotNull PotionEffect effect, byte amplifier, int duration) { this(effect, amplifier, duration, true, true, false); } /** * Creates a new potion. * - * @param effect The type of potion. + * @param effect The type of potion. * @param amplifier The strength of the potion. - * @param duration The length of the potion in ticks. + * @param duration The length of the potion in ticks. * @param particles If the potion has particles. */ - public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles) { + public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles) { this(effect, amplifier, duration, particles, true, false); } /** * Creates a new potion. * - * @param effect The type of potion. + * @param effect The type of potion. * @param amplifier The strength of the potion. - * @param duration The length of the potion in ticks. + * @param duration The length of the potion in ticks. * @param particles If the potion has particles. - * @param icon If the potion has an icon. + * @param icon If the potion has an icon. */ - public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon) { + public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon) { this(effect, amplifier, duration, particles, icon, false); } /** * Creates a new potion. * - * @param effect The type of potion. + * @param effect The type of potion. * @param amplifier The strength of the potion. - * @param duration The length of the potion in ticks. + * @param duration The length of the potion in ticks. * @param particles If the potion has particles. - * @param icon If the potion has an icon. - * @param ambient If the potion came from a beacon. + * @param icon If the potion has an icon. + * @param ambient If the potion came from a beacon. */ - public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon, boolean ambient) { + public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon, boolean ambient) { this.effect = effect; this.amplifier = amplifier; this.duration = duration; @@ -74,6 +75,7 @@ public class Potion { this.flags = flags; } + @NotNull public PotionEffect getEffect() { return effect; } @@ -94,25 +96,27 @@ public class Potion { * Sends a packet that a potion effect has been applied to the entity. *

* Used internally by {@link net.minestom.server.entity.Player#addEffect(Potion)} - * @param entity + * + * @param entity the entity to add the effect to */ public void sendAddPacket(@NotNull Entity entity) { - EntityEffectPacket eep = new EntityEffectPacket(); - eep.entityId = entity.getEntityId(); - eep.potion = this; - entity.sendPacketToViewersAndSelf(eep); + EntityEffectPacket entityEffectPacket = new EntityEffectPacket(); + entityEffectPacket.entityId = entity.getEntityId(); + entityEffectPacket.potion = this; + entity.sendPacketToViewersAndSelf(entityEffectPacket); } /** * Sends a packet that a potion effect has been removed from the entity. *

* Used internally by {@link net.minestom.server.entity.Player#removeEffect(PotionEffect)} - * @param entity + * + * @param entity the entity to remove the effect from */ public void sendRemovePacket(@NotNull Entity entity) { - RemoveEntityEffectPacket reep = new RemoveEntityEffectPacket(); - reep.entityId = entity.getEntityId(); - reep.effect = effect; - entity.sendPacketToViewersAndSelf(reep); + RemoveEntityEffectPacket removeEntityEffectPacket = new RemoveEntityEffectPacket(); + removeEntityEffectPacket.entityId = entity.getEntityId(); + removeEntityEffectPacket.effect = effect; + entity.sendPacketToViewersAndSelf(removeEntityEffectPacket); } } diff --git a/src/main/java/net/minestom/server/potion/TimedPotion.java b/src/main/java/net/minestom/server/potion/TimedPotion.java index c77215be7..e2adc8dc8 100644 --- a/src/main/java/net/minestom/server/potion/TimedPotion.java +++ b/src/main/java/net/minestom/server/potion/TimedPotion.java @@ -1,14 +1,18 @@ package net.minestom.server.potion; +import org.jetbrains.annotations.NotNull; + public class TimedPotion { + private final Potion potion; private final long startingTime; - public TimedPotion(Potion potion, long startingTime) { + public TimedPotion(@NotNull Potion potion, long startingTime) { this.potion = potion; this.startingTime = startingTime; } + @NotNull public Potion getPotion() { return potion; }