diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 259ebda9f..b158c564a 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -33,7 +33,6 @@ import net.minestom.server.potion.TimedPotion; import net.minestom.server.tag.Tag; import net.minestom.server.tag.TagHandler; import net.minestom.server.thread.ThreadProvider; -import net.minestom.server.utils.async.AsyncUtils; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.entity.EntityUtils; import net.minestom.server.utils.player.PlayerUtils; @@ -713,9 +712,9 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler public CompletableFuture setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { Check.stateCondition(!instance.isRegistered(), "Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance"); - if (isRemoved()) return AsyncUtils.VOID_FUTURE; - if (this.instance != null) { - this.instance.UNSAFE_removeEntity(this); + final Instance previousInstance = this.instance; + if (previousInstance != null) { + previousInstance.UNSAFE_removeEntity(this); } this.position = spawnPosition; this.isActive = true; diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index b29720506..45f621b97 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -282,6 +282,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable, this.playerConnection.sendPacket(getPropertiesPacket()); // Send default properties refreshHealth(); // Heal and send health packet refreshAbilities(); // Send abilities packet + + setInstance(spawnInstance); } /** @@ -423,7 +425,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, // Runnable called when teleportation is successful (after loading and sending necessary chunk) teleport(respawnEvent.getRespawnPosition()).thenRun(this::refreshAfterTeleport); } - + /** * Refreshes the command list for this player. This checks the * {@link net.minestom.server.command.builder.condition.CommandCondition}s @@ -497,12 +499,13 @@ public class Player extends LivingEntity implements CommandSender, Localizable, */ @Override public CompletableFuture setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { - Check.argCondition(this.instance == instance, "Instance should be different than the current one"); + final Instance currentInstance = this.instance; + Check.argCondition(currentInstance == instance, "Instance should be different than the current one"); // true if the chunks need to be sent to the client, can be false if the instances share the same chunks (e.g. SharedInstance) - if (!InstanceUtils.areLinked(this.instance, instance) || !spawnPosition.sameChunk(this.position)) { + if (!InstanceUtils.areLinked(currentInstance, instance) || !spawnPosition.sameChunk(this.position)) { + final boolean firstSpawn = currentInstance == null; return instance.loadOptionalChunk(spawnPosition) - .thenRun(() -> spawnPlayer(instance, spawnPosition, - this.instance == null, + .thenRun(() -> spawnPlayer(instance, spawnPosition, firstSpawn, !Objects.equals(dimensionType, instance.getDimensionType()), true)); } else { // The player already has the good version of all the chunks. diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index 09a317a9f..d6fc71398 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -372,9 +372,8 @@ public final class ConnectionManager { EventDispatcher.call(loginEvent); final Instance spawningInstance = loginEvent.getSpawningInstance(); Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent"); - // Spawn the player at Player#getRespawnPoint during the next instance tick + // Spawn the player at Player#getRespawnPoint waitingPlayer.UNSAFE_init(spawningInstance); - spawningInstance.scheduleNextTick(waitingPlayer::setInstance); } }