From 77be05085d2a0bc5d826953db26fd17895ee78b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noel=20N=C3=A9meth?= Date: Sat, 26 Nov 2022 11:10:44 +0100 Subject: [PATCH] Clear chunk history on teleport and instance change, initialize history with less common chunk coordinate; fixes #1536 --- src/main/java/net/minestom/server/entity/Player.java | 7 +++++++ .../server/utils/chunk/ChunkUpdateLimitChecker.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 247b5e35f..bec6eff24 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -560,6 +560,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, return AsyncUtils.VOID_FUTURE; } // Must update the player chunks + chunkUpdateLimitChecker.clearHistory(); final boolean dimensionChange = !Objects.equals(dimensionType, instance.getDimensionType()); final Consumer runnable = (i) -> spawnPlayer(i, spawnPosition, currentInstance == null, dimensionChange, true); @@ -2064,6 +2065,12 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } } + @Override + public @NotNull CompletableFuture teleport(@NotNull Pos position, long @Nullable [] chunks) { + chunkUpdateLimitChecker.clearHistory(); + return super.teleport(position, chunks); + } + /** * Represents the main or off hand of the player. */ diff --git a/src/main/java/net/minestom/server/utils/chunk/ChunkUpdateLimitChecker.java b/src/main/java/net/minestom/server/utils/chunk/ChunkUpdateLimitChecker.java index 40dd52546..a690e369e 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkUpdateLimitChecker.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkUpdateLimitChecker.java @@ -3,6 +3,8 @@ package net.minestom.server.utils.chunk; import net.minestom.server.instance.Chunk; import org.jetbrains.annotations.ApiStatus; +import java.util.Arrays; + @ApiStatus.Internal public final class ChunkUpdateLimitChecker { private final int historySize; @@ -11,6 +13,7 @@ public final class ChunkUpdateLimitChecker { public ChunkUpdateLimitChecker(int historySize) { this.historySize = historySize; this.chunkHistory = new long[historySize]; + this.clearHistory(); } /** @@ -32,4 +35,8 @@ public final class ChunkUpdateLimitChecker { chunkHistory[lastIndex] = index; return result; } + + public void clearHistory() { + Arrays.fill(this.chunkHistory, Long.MAX_VALUE); + } }