From 6a86e92999abe09d0496afa46173ac127623d4d2 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 28 Jul 2021 16:36:21 +0200 Subject: [PATCH] Unload all chunks when changing instance --- src/main/java/net/minestom/server/entity/Player.java | 12 +++++------- .../packet/server/play/UnloadChunkPacket.java | 12 ++++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index cb4216aae..0abb794e8 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -513,7 +513,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * * @param instance the new player instance * @param spawnPosition the new position of the player - * @return + * @return a future called once the player instance changed */ @Override public CompletableFuture setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { @@ -579,6 +579,9 @@ public class Player extends LivingEntity implements CommandSender, Localizable, super.setInstance(instance, spawnPosition); if (updateChunks) { + // Warning: loop to remove once `refreshVisibleChunks` manage it + this.viewableChunks.forEach(chunk -> + playerConnection.sendPacket(new UnloadChunkPacket(chunk.getChunkX(), chunk.getChunkZ()))); refreshVisibleChunks(); } @@ -1199,12 +1202,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, ArrayUtils.forDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks, chunkIndex -> { final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex); final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex); - - final UnloadChunkPacket unloadChunkPacket = new UnloadChunkPacket(); - unloadChunkPacket.chunkX = chunkX; - unloadChunkPacket.chunkZ = chunkZ; - //playerConnection.sendPacket(unloadChunkPacket); - + //playerConnection.sendPacket(new UnloadChunkPacket(chunkX, chunkZ)); final Chunk chunk = instance.getChunk(chunkX, chunkZ); if (chunk != null) { chunk.removeViewer(this); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java index 638e70781..463bd7e9f 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java @@ -10,10 +10,14 @@ public class UnloadChunkPacket implements ServerPacket { public int chunkX, chunkZ; - /** - * Default constructor, required for reflection operations. - */ - public UnloadChunkPacket() {} + public UnloadChunkPacket(int chunkX, int chunkZ) { + this.chunkX = chunkX; + this.chunkZ = chunkZ; + } + + public UnloadChunkPacket() { + this(0, 0); + } @Override public void write(@NotNull BinaryWriter writer) {