Skip to content

Commit

Permalink
Fix several off-by-one errors in view distance calculations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Spottedleaf committed Dec 1, 2024
1 parent bd938e6 commit 04e2b97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void tickPlayer(final ServerPlayer player) {
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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public static int getSendViewDistance(final ServerPlayer player) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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.MoonriseCommon;
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;
Expand All @@ -16,13 +16,10 @@
import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkTaskScheduler;
import ca.spottedleaf.moonrise.patches.chunk_system.util.ParallelSearchRadiusIteration;
import com.google.gson.JsonObject;
import it.unimi.dsi.fastutil.HashCommon;
import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongComparator;
import it.unimi.dsi.fastutil.longs.LongHeapPriorityQueue;
import it.unimi.dsi.fastutil.longs.LongIterator;
import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundForgetLevelChunkPacket;
Expand All @@ -42,8 +39,6 @@
import net.minecraft.world.level.levelgen.BelowZeroRetrogen;
import java.lang.invoke.VarHandle;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
Expand Down Expand Up @@ -120,14 +115,25 @@ public static final record ViewDistances(
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);
}

Expand Down Expand Up @@ -161,16 +167,6 @@ public static int getAPIViewDistance(final ServerPlayer player) {
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();
Expand Down Expand Up @@ -522,7 +518,7 @@ private static int getTickDistance(final int playerTickViewDistance, final int w
final int playerLoadViewDistance, final int worldLoadViewDistance) {
return Math.min(
playerTickViewDistance < 0 ? worldTickViewDistance : playerTickViewDistance,
playerLoadViewDistance < 0 ? worldLoadViewDistance : playerLoadViewDistance
playerLoadViewDistance < 0 ? (worldLoadViewDistance - 1) : (playerLoadViewDistance - 1)
);
}

Expand Down

0 comments on commit 04e2b97

Please sign in to comment.