From 0360fd3f796573d49a713524587e009d3187e6f8 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Mon, 13 Sep 2021 15:05:16 +0800 Subject: [PATCH 1/2] Fix 2 entity loading issues --- .../net/minestom/server/entity/Player.java | 18 +++++++++++++----- .../server/entity/fakeplayer/FakePlayer.java | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 02cf44763..99f7f94b3 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -546,24 +546,32 @@ public class Player extends LivingEntity implements CommandSender, Localizable, if (!firstSpawn) { // Player instance changed, clear current viewable collections previousChunks.forEach(chunk -> chunk.removeViewer(this)); + + //TODO: entity#removeViewer sends a packet for each removed entity + //Sending destroy entity packets is not necessary when the dimension changes + //and, potentially, this could also be rewritten to send only a single DestroyEntitiesPacket + //with the list of all destroyed entities this.viewableEntities.forEach(entity -> entity.removeViewer(this)); } - super.setInstance(instance, spawnPosition); - if (dimensionChange) { sendDimension(instance.getDimensionType()); } + super.setInstance(instance, spawnPosition); + if (updateChunks) { // Warning: loop to remove once `refreshVisibleChunks` manage it - previousChunks.forEach(chunk -> - playerConnection.sendPacket(new UnloadChunkPacket(chunk.getChunkX(), chunk.getChunkZ()))); + if(!dimensionChange) { //The client already forcefully unloads all chunks & entities when the dimension changes + previousChunks.forEach(chunk -> + playerConnection.sendPacket(new UnloadChunkPacket(chunk.getChunkX(), chunk.getChunkZ()))); + } refreshVisibleChunks(); } + synchronizePosition(true); // So the player doesn't get stuck + if (dimensionChange || firstSpawn) { - synchronizePosition(true); // So the player doesn't get stuck this.inventory.update(); } diff --git a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java index 3b4c5a833..ee1ae34f2 100644 --- a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java +++ b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java @@ -134,6 +134,7 @@ public class FakePlayer extends Player implements NavigableEntity { protected boolean addViewer0(@NotNull Player player) { final boolean result = super.addViewer0(player); if (result) { + player.getPlayerConnection().sendPacket(getAddPlayerToList()); handleTabList(player.getPlayerConnection()); } return result; From 5bf4758a3ae8eefb14be1f9359b314bc24d05a18 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Mon, 13 Sep 2021 15:08:57 +0800 Subject: [PATCH 2/2] Remove extra chunk unloads --- src/main/java/net/minestom/server/entity/Player.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 8fdf4be11..2b46d201b 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -561,11 +561,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable, super.setInstance(instance, spawnPosition); if (updateChunks) { - // Warning: loop to remove once `refreshVisibleChunks` manage it - if(!dimensionChange) { //The client already forcefully unloads all chunks & entities when the dimension changes - previousChunks.forEach(chunk -> - playerConnection.sendPacket(new UnloadChunkPacket(chunk.getChunkX(), chunk.getChunkZ()))); - } refreshVisibleChunks(); }