diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index fb6099174..baedad11c 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -658,8 +658,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { /** * Gets the entity current instance. * - * @return the entity instance + * @return the entity instance, can be null if the entity doesn't have an instance yet */ + @Nullable public Instance getInstance() { return instance; } @@ -677,12 +678,12 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { "Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance"); if (this.instance != null) { - this.instance.removeEntity(this); + this.instance.UNSAFE_removeEntity(this); } this.isActive = true; this.instance = instance; - instance.addEntity(this); + instance.UNSAFE_addEntity(this); spawn(); EntitySpawnEvent entitySpawnEvent = new EntitySpawnEvent(this, instance); callEvent(EntitySpawnEvent.class, entitySpawnEvent); @@ -1191,7 +1192,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { this.shouldRemove = true; entityById.remove(id); if (instance != null) - instance.removeEntity(this); + instance.UNSAFE_removeEntity(this); } /** diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 48c08a282..95fef2d53 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -606,7 +606,7 @@ public class Player extends LivingEntity implements CommandSender { final boolean firstSpawn = this.instance == null; // TODO: Handle player reconnections, must be false in that case too - // true if the chunks need to be send to the client, can be false if the instances share the same chunks (eg SharedInstance) + // true if the chunks need to be sent to the client, can be false if the instances share the same chunks (eg SharedInstance) final boolean needWorldRefresh = !InstanceUtils.areLinked(this.instance, instance); if (needWorldRefresh) { diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 9634da02e..270829aa6 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -831,10 +831,10 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta * * @param entity the entity to add */ - public void addEntity(@NotNull Entity entity) { + public void UNSAFE_addEntity(@NotNull Entity entity) { final Instance lastInstance = entity.getInstance(); if (lastInstance != null && lastInstance != this) { - lastInstance.removeEntity(entity); // If entity is in another instance, remove it from there and add it to this + lastInstance.UNSAFE_removeEntity(entity); // If entity is in another instance, remove it from there and add it to this } AddEntityToInstanceEvent event = new AddEntityToInstanceEvent(this, entity); callCancellableEvent(AddEntityToInstanceEvent.class, event, () -> { @@ -874,7 +874,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta * * @param entity the entity to remove */ - public void removeEntity(@NotNull Entity entity) { + public void UNSAFE_removeEntity(@NotNull Entity entity) { final Instance entityInstance = entity.getInstance(); if (entityInstance != this) return;