From 8ed23bc8f3d41a3626d6e28f9573e7a48a450ae7 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Sun, 1 Dec 2024 15:23:59 -0800 Subject: [PATCH] Fix several off-by-one errors in view distance calculations 1. For NearbyPlayers, we need to be using the view distance, and not the load distance (which is +1 of the view distance). 2. Correctly clamp tick distance to view distance. Since load distance is +1 of view distance, we need to subtract one from the load distance when clamping. Additionally, add checks inside ViewDistances to ensure that the inputs are in range to catch future errors. --- .../moonrise/common/misc/NearbyPlayers.java | 2 +- .../moonrise/common/util/ChunkSystem.java | 4 ++-- .../player/RegionizedPlayerChunkLoader.java | 24 ++++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java index 13895dd..e319619 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java @@ -122,7 +122,7 @@ public final class NearbyPlayers { players[NearbyMapType.GENERAL_SMALL.ordinal()].update(chunk.x, chunk.z, GENERAL_SMALL_VIEW_DISTANCE); players[NearbyMapType.GENERAL_REALLY_SMALL.ordinal()].update(chunk.x, chunk.z, GENERAL_REALLY_SMALL_VIEW_DISTANCE); players[NearbyMapType.TICK_VIEW_DISTANCE.ordinal()].update(chunk.x, chunk.z, ChunkSystem.getTickViewDistance(player)); - players[NearbyMapType.VIEW_DISTANCE.ordinal()].update(chunk.x, chunk.z, ChunkSystem.getLoadViewDistance(player)); + players[NearbyMapType.VIEW_DISTANCE.ordinal()].update(chunk.x, chunk.z, ChunkSystem.getViewDistance(player)); players[NearbyMapType.SPAWN_RANGE.ordinal()].update(chunk.x, chunk.z, ChunkTickConstants.PLAYER_SPAWN_TRACK_RANGE); // Moonrise - chunk tick iteration } diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java b/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java index 7d5a4c9..e73d5f1 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java @@ -152,8 +152,8 @@ public final class ChunkSystem { return RegionizedPlayerChunkLoader.getAPISendViewDistance(player); } - public static int getLoadViewDistance(final ServerPlayer player) { - return RegionizedPlayerChunkLoader.getLoadViewDistance(player); + public static int getViewDistance(final ServerPlayer player) { + return RegionizedPlayerChunkLoader.getAPIViewDistance(player); } public static int getTickViewDistance(final ServerPlayer player) { diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/player/RegionizedPlayerChunkLoader.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/player/RegionizedPlayerChunkLoader.java index b2fa988..b3610f6 100644 --- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/player/RegionizedPlayerChunkLoader.java +++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/player/RegionizedPlayerChunkLoader.java @@ -6,6 +6,7 @@ import ca.spottedleaf.moonrise.common.PlatformHooks; import ca.spottedleaf.moonrise.common.misc.AllocatingRateLimiter; import ca.spottedleaf.moonrise.common.misc.SingleUserAreaMap; import ca.spottedleaf.moonrise.common.util.CoordinateUtils; +import ca.spottedleaf.moonrise.common.util.MoonriseConstants; import ca.spottedleaf.moonrise.common.util.TickThread; import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel; import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel; @@ -114,14 +115,25 @@ public final class RegionizedPlayerChunkLoader { int sendViewDistance ) { public ViewDistances setTickViewDistance(final int distance) { + if (distance != -1 && (distance < (0) || distance > (MoonriseConstants.MAX_VIEW_DISTANCE))) { + throw new IllegalArgumentException(Integer.toString(distance)); + } return new ViewDistances(distance, this.loadViewDistance, this.sendViewDistance); } public ViewDistances setLoadViewDistance(final int distance) { + // note: load view distance = api view distance + 1 + if (distance != -1 && (distance < (2 + 1) || distance > (MoonriseConstants.MAX_VIEW_DISTANCE + 1))) { + throw new IllegalArgumentException(Integer.toString(distance)); + } return new ViewDistances(this.tickViewDistance, distance, this.sendViewDistance); } public ViewDistances setSendViewDistance(final int distance) { + // note: send view distance <= load view distance - 1 + if (distance != -1 && (distance < (0) || distance > (MoonriseConstants.MAX_VIEW_DISTANCE))) { + throw new IllegalArgumentException(Integer.toString(distance)); + } return new ViewDistances(this.tickViewDistance, this.loadViewDistance, distance); } @@ -155,16 +167,6 @@ public final class RegionizedPlayerChunkLoader { return data.lastLoadDistance - 1; } - public static int getLoadViewDistance(final ServerPlayer player) { - final ServerLevel level = player.serverLevel(); - final PlayerChunkLoaderData data = ((ChunkSystemServerPlayer)player).moonrise$getChunkLoader(); - if (data == null) { - return ((ChunkSystemServerLevel)level).moonrise$getPlayerChunkLoader().getAPIViewDistance(); - } - // view distance = load distance + 1 - return data.lastLoadDistance - 1; - } - public static int getAPISendViewDistance(final ServerPlayer player) { final ServerLevel level = player.serverLevel(); final PlayerChunkLoaderData data = ((ChunkSystemServerPlayer)player).moonrise$getChunkLoader(); @@ -516,7 +518,7 @@ public final class RegionizedPlayerChunkLoader { final int playerLoadViewDistance, final int worldLoadViewDistance) { return Math.min( playerTickViewDistance < 0 ? worldTickViewDistance : playerTickViewDistance, - playerLoadViewDistance < 0 ? worldLoadViewDistance : playerLoadViewDistance + playerLoadViewDistance < 0 ? (worldLoadViewDistance - 1) : (playerLoadViewDistance - 1) ); }