diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index f7a491a98..3fe3eb798 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -10,12 +10,7 @@ import net.minestom.server.data.Data; import net.minestom.server.data.DataContainer; import net.minestom.server.event.Event; import net.minestom.server.event.EventCallback; -import net.minestom.server.event.entity.EntityDeathEvent; -import net.minestom.server.event.entity.EntitySpawnEvent; -import net.minestom.server.event.entity.EntityPotionAddEvent; -import net.minestom.server.event.entity.EntityPotionRemoveEvent; -import net.minestom.server.event.entity.EntityTickEvent; -import net.minestom.server.event.entity.EntityVelocityEvent; +import net.minestom.server.event.entity.*; import net.minestom.server.event.handler.EventHandler; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; @@ -404,24 +399,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P return; } - // remove expired effects - { - this.effects.removeIf(timedPotion -> { - final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS; - // Remove if the potion should be expired - if (time >= timedPotion.getStartingTime() + potionTime) { - // Send the packet that the potion should no longer be applied - timedPotion.getPotion().sendRemovePacket(this); - callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent( - this, - timedPotion.getPotion() - )); - return true; - } - return false; - }); - } - // scheduled tasks if (!nextTick.isEmpty()) { Consumer callback; @@ -572,7 +549,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P sendSynchronization(); // Verify if velocity packet has to be sent if (hasVelocity() || gravityTickCount > 0) { - sendVelocityPacket(); + sendPacketsToViewers(getVelocityPacket()); } } @@ -614,6 +591,24 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P ticks++; callEvent(EntityTickEvent.class, tickEvent); // reuse tickEvent to avoid recreating it each tick + + // remove expired effects + { + this.effects.removeIf(timedPotion -> { + final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS; + // Remove if the potion should be expired + if (time >= timedPotion.getStartingTime() + potionTime) { + // Send the packet that the potion should no longer be applied + timedPotion.getPotion().sendRemovePacket(this); + callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent( + this, + timedPotion.getPotion() + )); + return true; + } + return false; + }); + } } // Scheduled synchronization @@ -627,13 +622,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P } } - /** - * Equivalent to sendPacketsToViewers(getVelocityPacket());. - */ - public void sendVelocityPacket() { - sendPacketsToViewers(getVelocityPacket()); - } - /** * Gets the number of ticks this entity has been active for. * @@ -694,6 +682,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @param uuid the new entity uuid */ protected void setUuid(@NotNull UUID uuid) { + // Refresh internal map + Entity.entityByUuid.remove(this.uuid); + Entity.entityByUuid.put(uuid, this); + this.uuid = uuid; } @@ -1242,6 +1234,47 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P return boundingBox.getHeight() * 0.85f; } + /** + * Gets all the potion effect of this entity. + * + * @return an unmodifiable list of all this entity effects + */ + @NotNull + public List getActiveEffects() { + return Collections.unmodifiableList(effects); + } + + /** + * Removes effect from entity, if it has it. + * + * @param effect The effect to remove + */ + public void removeEffect(@NotNull PotionEffect effect) { + this.effects.removeIf(timedPotion -> { + if (timedPotion.getPotion().getEffect() == effect) { + timedPotion.getPotion().sendRemovePacket(this); + callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent( + this, + timedPotion.getPotion() + )); + return true; + } + return false; + }); + } + + /** + * Adds an effect to an entity. + * + * @param potion The potion to add + */ + public void addEffect(@NotNull Potion potion) { + removeEffect(potion.getEffect()); + this.effects.add(new TimedPotion(potion, System.currentTimeMillis())); + potion.sendAddPacket(this); + callEvent(EntityPotionAddEvent.class, new EntityPotionAddEvent(this, potion)); + } + /** * Removes the entity from the server immediately. *

@@ -1468,50 +1501,6 @@ 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 Collections.unmodifiableList(effects); - } - - /** - * Removes effect from entity, if it has it. - * - * @param effect The effect to remove - */ - public void removeEffect(@NotNull PotionEffect effect) { - this.effects.removeIf(timedPotion -> { - if (timedPotion.getPotion().getEffect() == effect) { - timedPotion.getPotion().sendRemovePacket(this); - callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent( - this, - timedPotion.getPotion() - )); - return true; - } - return false; - }); - } - - /** - * Adds an effect to an entity. - * - * @param potion The potion to add - */ - public void addEffect(@NotNull Potion potion) { - removeEffect(potion.getEffect()); - this.effects.add(new TimedPotion(potion, System.currentTimeMillis())); - potion.sendAddPacket(this); - callEvent(EntityPotionAddEvent.class, new EntityPotionAddEvent( - this, - potion - )); - } - protected boolean shouldRemove() { return shouldRemove; } diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index fe051837b..064e2bc0d 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -107,8 +107,14 @@ public final class EntityManager { final String username = asyncPlayerPreLoginEvent.getUsername(); final UUID uuid = asyncPlayerPreLoginEvent.getPlayerUuid(); - player.setUsername(username); - player.setUuid(uuid); + if (!player.getUsername().equals(username)) { + player.setUsername(username); + } + + if (!player.getUuid().equals(uuid)) { + player.setUuid(uuid); + } + } // Add the player to the waiting list diff --git a/src/main/java/net/minestom/server/event/entity/EntityPotionAddEvent.java b/src/main/java/net/minestom/server/event/entity/EntityPotionAddEvent.java index 569405060..cde952db5 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityPotionAddEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityPotionAddEvent.java @@ -6,6 +6,7 @@ import net.minestom.server.potion.Potion; import org.jetbrains.annotations.NotNull; public class EntityPotionAddEvent extends EntityEvent { + private final Potion potion; public EntityPotionAddEvent(@NotNull Entity entity, @NotNull Potion potion) { @@ -18,6 +19,7 @@ public class EntityPotionAddEvent extends EntityEvent { * * @return the added potion. */ + @NotNull public Potion getPotion() { return potion; } diff --git a/src/main/java/net/minestom/server/event/entity/EntityPotionRemoveEvent.java b/src/main/java/net/minestom/server/event/entity/EntityPotionRemoveEvent.java index 4c142d43e..1ca787a18 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityPotionRemoveEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityPotionRemoveEvent.java @@ -6,6 +6,7 @@ import net.minestom.server.potion.Potion; import org.jetbrains.annotations.NotNull; public class EntityPotionRemoveEvent extends EntityEvent { + private final Potion potion; public EntityPotionRemoveEvent(@NotNull Entity entity, @NotNull Potion potion) { @@ -18,6 +19,7 @@ public class EntityPotionRemoveEvent extends EntityEvent { * * @return the removed potion. */ + @NotNull public Potion getPotion() { return potion; }