Skip to content

Commit c2294d7

Browse files
committed
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. Also, clamp simulation distance, as values < 0 or above MAX_VIEW_DISTANCE do not make sense to configure.
1 parent ae80a25 commit c2294d7

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

patches/server/0009-MC-Utils.patch

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,7 @@ index 0000000000000000000000000000000000000000..c2d917c2eac55b8a4411a6e159f177f9
25992599
+}
26002600
diff --git a/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java
26012601
new file mode 100644
2602-
index 0000000000000000000000000000000000000000..bb44de17a37082e57f2292a4f470740be1d09b11
2602+
index 0000000000000000000000000000000000000000..7e440b4a46b040365df7317035e577d93e7d855d
26032603
--- /dev/null
26042604
+++ b/src/main/java/ca/spottedleaf/moonrise/common/misc/NearbyPlayers.java
26052605
@@ -0,0 +1,273 @@
@@ -2727,7 +2727,7 @@ index 0000000000000000000000000000000000000000..bb44de17a37082e57f2292a4f470740b
27272727
+ players[NearbyMapType.GENERAL_SMALL.ordinal()].update(chunk.x, chunk.z, GENERAL_SMALL_VIEW_DISTANCE);
27282728
+ players[NearbyMapType.GENERAL_REALLY_SMALL.ordinal()].update(chunk.x, chunk.z, GENERAL_REALLY_SMALL_VIEW_DISTANCE);
27292729
+ players[NearbyMapType.TICK_VIEW_DISTANCE.ordinal()].update(chunk.x, chunk.z, ChunkSystem.getTickViewDistance(player));
2730-
+ players[NearbyMapType.VIEW_DISTANCE.ordinal()].update(chunk.x, chunk.z, ChunkSystem.getLoadViewDistance(player));
2730+
+ players[NearbyMapType.VIEW_DISTANCE.ordinal()].update(chunk.x, chunk.z, ChunkSystem.getViewDistance(player));
27312731
+ players[NearbyMapType.SPAWN_RANGE.ordinal()].update(chunk.x, chunk.z, ChunkTickConstants.PLAYER_SPAWN_TRACK_RANGE); // Moonrise - chunk tick iteration
27322732
+ }
27332733
+
@@ -3311,7 +3311,7 @@ index 0000000000000000000000000000000000000000..4123edddc556c47f3f8d83523c125fd2
33113311
+}
33123312
diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java b/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java
33133313
new file mode 100644
3314-
index 0000000000000000000000000000000000000000..f7cd0aa43d0b9249d0a317fab41fefa0d951bca0
3314+
index 0000000000000000000000000000000000000000..58a99bc38e137431f10af36fa9e2d04fe61694aa
33153315
--- /dev/null
33163316
+++ b/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java
33173317
@@ -0,0 +1,288 @@
@@ -3582,15 +3582,15 @@ index 0000000000000000000000000000000000000000..f7cd0aa43d0b9249d0a317fab41fefa0
35823582
+ }
35833583
+
35843584
+ public static int getSendViewDistance(final ServerPlayer player) {
3585-
+ return getLoadViewDistance(player) - 1;
3585+
+ return getViewDistance(player);
35863586
+ }
35873587
+
3588-
+ public static int getLoadViewDistance(final ServerPlayer player) {
3588+
+ public static int getViewDistance(final ServerPlayer player) {
35893589
+ final ServerLevel level = player.serverLevel();
35903590
+ if (level == null) {
3591-
+ return org.bukkit.Bukkit.getViewDistance() + 1;
3591+
+ return org.bukkit.Bukkit.getViewDistance();
35923592
+ }
3593-
+ return level.chunkSource.chunkMap.serverViewDistance + 1;
3593+
+ return level.chunkSource.chunkMap.serverViewDistance;
35943594
+ }
35953595
+
35963596
+ public static int getTickViewDistance(final ServerPlayer player) {
@@ -6415,7 +6415,7 @@ index 799444e4101283c972a160742a9e2548e604173f..fb8c641604473a3853b2f8b9cd5c8a65
64156415
+ // Paper end
64166416
}
64176417
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
6418-
index 031eed24638659f2633bef0ad2e178832ca058e9..a3550d84c273c9720491484382a4bc50dc3246a6 100644
6418+
index 031eed24638659f2633bef0ad2e178832ca058e9..2e4e6fee48d0f6be3b23a109a7c661b27179ccaa 100644
64196419
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
64206420
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
64216421
@@ -2447,4 +2447,34 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@@ -6425,7 +6425,7 @@ index 031eed24638659f2633bef0ad2e178832ca058e9..a3550d84c273c9720491484382a4bc50
64256425
+
64266426
+ @Override
64276427
+ public int getViewDistance() {
6428-
+ return ca.spottedleaf.moonrise.common.util.ChunkSystem.getLoadViewDistance(this.getHandle()) - 1;
6428+
+ return ca.spottedleaf.moonrise.common.util.ChunkSystem.getViewDistance(this.getHandle());
64296429
+ }
64306430
+
64316431
+ @Override

patches/server/1037-Moonrise-optimisation-patches.patch

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Currently includes:
1818
See https://github.com/Tuinity/Moonrise
1919

2020
diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java b/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java
21-
index f7cd0aa43d0b9249d0a317fab41fefa0d951bca0..fc029c8fb22a7c8eeb23bfc171812f6da91c60fa 100644
21+
index 58a99bc38e137431f10af36fa9e2d04fe61694aa..1d288e73fd8605676c0da676e068afb5b4b8abea 100644
2222
--- a/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java
2323
+++ b/src/main/java/ca/spottedleaf/moonrise/common/util/ChunkSystem.java
2424
@@ -2,11 +2,17 @@ package ca.spottedleaf.moonrise.common.util;
@@ -330,17 +330,17 @@ index f7cd0aa43d0b9249d0a317fab41fefa0d951bca0..fc029c8fb22a7c8eeb23bfc171812f6d
330330
}
331331

332332
public static int getSendViewDistance(final ServerPlayer player) {
333-
- return getLoadViewDistance(player) - 1;
333+
- return getViewDistance(player);
334334
+ return RegionizedPlayerChunkLoader.getAPISendViewDistance(player);
335335
}
336336

337-
public static int getLoadViewDistance(final ServerPlayer player) {
337+
public static int getViewDistance(final ServerPlayer player) {
338338
- final ServerLevel level = player.serverLevel();
339339
- if (level == null) {
340-
- return org.bukkit.Bukkit.getViewDistance() + 1;
340+
- return org.bukkit.Bukkit.getViewDistance();
341341
- }
342-
- return level.chunkSource.chunkMap.serverViewDistance + 1;
343-
+ return RegionizedPlayerChunkLoader.getLoadViewDistance(player);
342+
- return level.chunkSource.chunkMap.serverViewDistance;
343+
+ return RegionizedPlayerChunkLoader.getAPIViewDistance(player);
344344
}
345345

346346
public static int getTickViewDistance(final ServerPlayer player) {
@@ -5545,10 +5545,10 @@ index 0000000000000000000000000000000000000000..003a857e70ead858e8437e3c1bfaf22f
55455545
+}
55465546
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
55475547
new file mode 100644
5548-
index 0000000000000000000000000000000000000000..5a6defc4c4d30c06d4bba856847feb176950ca1e
5548+
index 0000000000000000000000000000000000000000..dd2509996bfd08e8c3f9f2be042229eac6d7692d
55495549
--- /dev/null
55505550
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/player/RegionizedPlayerChunkLoader.java
5551-
@@ -0,0 +1,1090 @@
5551+
@@ -0,0 +1,1092 @@
55525552
+package ca.spottedleaf.moonrise.patches.chunk_system.player;
55535553
+
55545554
+import ca.spottedleaf.concurrentutil.util.ConcurrentUtil;
@@ -5557,6 +5557,7 @@ index 0000000000000000000000000000000000000000..5a6defc4c4d30c06d4bba856847feb17
55575557
+import ca.spottedleaf.moonrise.common.misc.AllocatingRateLimiter;
55585558
+import ca.spottedleaf.moonrise.common.misc.SingleUserAreaMap;
55595559
+import ca.spottedleaf.moonrise.common.util.CoordinateUtils;
5560+
+import ca.spottedleaf.moonrise.common.util.MoonriseConstants;
55605561
+import ca.spottedleaf.moonrise.common.util.TickThread;
55615562
+import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel;
55625563
+import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel;
@@ -5665,14 +5666,25 @@ index 0000000000000000000000000000000000000000..5a6defc4c4d30c06d4bba856847feb17
56655666
+ int sendViewDistance
56665667
+ ) {
56675668
+ public ViewDistances setTickViewDistance(final int distance) {
5669+
+ if (distance != -1 && (distance < (0) || distance > (MoonriseConstants.MAX_VIEW_DISTANCE))) {
5670+
+ throw new IllegalArgumentException(Integer.toString(distance));
5671+
+ }
56685672
+ return new ViewDistances(distance, this.loadViewDistance, this.sendViewDistance);
56695673
+ }
56705674
+
56715675
+ public ViewDistances setLoadViewDistance(final int distance) {
5676+
+ // note: load view distance = api view distance + 1
5677+
+ if (distance != -1 && (distance < (2 + 1) || distance > (MoonriseConstants.MAX_VIEW_DISTANCE + 1))) {
5678+
+ throw new IllegalArgumentException(Integer.toString(distance));
5679+
+ }
56725680
+ return new ViewDistances(this.tickViewDistance, distance, this.sendViewDistance);
56735681
+ }
56745682
+
56755683
+ public ViewDistances setSendViewDistance(final int distance) {
5684+
+ // note: send view distance <= load view distance - 1
5685+
+ if (distance != -1 && (distance < (0) || distance > (MoonriseConstants.MAX_VIEW_DISTANCE))) {
5686+
+ throw new IllegalArgumentException(Integer.toString(distance));
5687+
+ }
56765688
+ return new ViewDistances(this.tickViewDistance, this.loadViewDistance, distance);
56775689
+ }
56785690
+
@@ -5706,16 +5718,6 @@ index 0000000000000000000000000000000000000000..5a6defc4c4d30c06d4bba856847feb17
57065718
+ return data.lastLoadDistance - 1;
57075719
+ }
57085720
+
5709-
+ public static int getLoadViewDistance(final ServerPlayer player) {
5710-
+ final ServerLevel level = player.serverLevel();
5711-
+ final PlayerChunkLoaderData data = ((ChunkSystemServerPlayer)player).moonrise$getChunkLoader();
5712-
+ if (data == null) {
5713-
+ return ((ChunkSystemServerLevel)level).moonrise$getPlayerChunkLoader().getAPIViewDistance();
5714-
+ }
5715-
+ // view distance = load distance + 1
5716-
+ return data.lastLoadDistance - 1;
5717-
+ }
5718-
+
57195721
+ public static int getAPISendViewDistance(final ServerPlayer player) {
57205722
+ final ServerLevel level = player.serverLevel();
57215723
+ final PlayerChunkLoaderData data = ((ChunkSystemServerPlayer)player).moonrise$getChunkLoader();
@@ -6072,7 +6074,7 @@ index 0000000000000000000000000000000000000000..5a6defc4c4d30c06d4bba856847feb17
60726074
+ final int playerLoadViewDistance, final int worldLoadViewDistance) {
60736075
+ return Math.min(
60746076
+ playerTickViewDistance < 0 ? worldTickViewDistance : playerTickViewDistance,
6075-
+ playerLoadViewDistance < 0 ? worldLoadViewDistance : playerLoadViewDistance
6077+
+ playerLoadViewDistance < 0 ? (worldLoadViewDistance - 1) : (playerLoadViewDistance - 1)
60766078
+ );
60776079
+ }
60786080
+
@@ -24171,7 +24173,7 @@ index d9ad32acdf46a43a649334a3b736aeb7b3af21d1..fae17a075d7efaf24d916877dd5968eb
2417124173
public static final int RADIUS_AROUND_FULL_CHUNK = FULL_CHUNK_STEP.accumulatedDependencies().getRadius();
2417224174
public static final int MAX_LEVEL = 33 + RADIUS_AROUND_FULL_CHUNK;
2417324175
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
24174-
index 16e55cc94c8f6e204e4b7ab6ad8d32a6c443357f..80bbf77454ff34505196998bcfeaa3e40a4f639c 100644
24176+
index e9b585387f6cbc454e7b16feb36a256e733c5488..67cfc3236a39008cfcf3acffefafda1a604b8573 100644
2417524177
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
2417624178
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2417724179
@@ -108,7 +108,7 @@ import org.slf4j.Logger;
@@ -25482,7 +25484,7 @@ index 16e55cc94c8f6e204e4b7ab6ad8d32a6c443357f..80bbf77454ff34505196998bcfeaa3e4
2548225484

2548325485
public void updatePlayers(List<ServerPlayer> players) {
2548425486
diff --git a/src/main/java/net/minecraft/server/level/DistanceManager.java b/src/main/java/net/minecraft/server/level/DistanceManager.java
25485-
index f7c2c03749d6be25bf33afd61e1da120770b3432..7a9e7fc688e48d18a6a884f02f768ae652326aae 100644
25487+
index f7c2c03749d6be25bf33afd61e1da120770b3432..746f61661e22d22f2acbbe54a5933e57fbca45b2 100644
2548625488
--- a/src/main/java/net/minecraft/server/level/DistanceManager.java
2548725489
+++ b/src/main/java/net/minecraft/server/level/DistanceManager.java
2548825490
@@ -34,58 +34,57 @@ import net.minecraft.world.level.ChunkPos;
@@ -25754,7 +25756,7 @@ index f7c2c03749d6be25bf33afd61e1da120770b3432..7a9e7fc688e48d18a6a884f02f768ae6
2575425756
}
2575525757

2575625758
public void removePlayer(SectionPos pos, ServerPlayer player) {
25757-
@@ -284,160 +175,89 @@ public abstract class DistanceManager {
25759+
@@ -284,160 +175,93 @@ public abstract class DistanceManager {
2575825760
if (objectset != null) objectset.remove(player); // Paper - some state corruption happens here, don't crash, clean up gracefully
2575925761
if (objectset == null || objectset.isEmpty()) { // Paper
2576025762
this.playersPerChunk.remove(i);
@@ -25805,8 +25807,12 @@ index f7c2c03749d6be25bf33afd61e1da120770b3432..7a9e7fc688e48d18a6a884f02f768ae6
2580525807
- this.simulationDistance = simulationDistance;
2580625808
- this.tickingTicketsTracker.replacePlayerTicketsLevel(this.getPlayerTicketLevel());
2580725809
- }
25808-
+ ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel)this.moonrise$getChunkMap().level).moonrise$getPlayerChunkLoader().setTickDistance(simulationDistance); // Paper - rewrite chunk system
25810+
+ // Paper start - rewrite chunk system
25811+
+ // note: vanilla does not clamp to 0, but we do simply because we need a min of 0
25812+
+ final int clamped = net.minecraft.util.Mth.clamp(simulationDistance, 0, ca.spottedleaf.moonrise.common.util.MoonriseConstants.MAX_VIEW_DISTANCE);
2580925813

25814+
+ ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel)this.moonrise$getChunkMap().level).moonrise$getPlayerChunkLoader().setTickDistance(clamped);
25815+
+ // Paper end - rewrite chunk system
2581025816
}
2581125817

2581225818
public int getNaturalSpawnChunkCount() {
@@ -25940,7 +25946,7 @@ index f7c2c03749d6be25bf33afd61e1da120770b3432..7a9e7fc688e48d18a6a884f02f768ae6
2594025946
private class ChunkTicketTracker extends ChunkTracker {
2594125947

2594225948
private static final int MAX_LEVEL = ChunkLevel.MAX_LEVEL + 1;
25943-
@@ -483,7 +303,7 @@ public abstract class DistanceManager {
25949+
@@ -483,7 +307,7 @@ public abstract class DistanceManager {
2594425950
public int runDistanceUpdates(int distance) {
2594525951
return this.runUpdates(distance);
2594625952
}
@@ -25949,15 +25955,15 @@ index f7c2c03749d6be25bf33afd61e1da120770b3432..7a9e7fc688e48d18a6a884f02f768ae6
2594925955

2595025956
private class FixedPlayerDistanceChunkTracker extends ChunkTracker {
2595125957

25952-
@@ -563,6 +383,7 @@ public abstract class DistanceManager {
25958+
@@ -563,6 +387,7 @@ public abstract class DistanceManager {
2595325959
}
2595425960
}
2595525961

2595625962
+ /* // Paper - rewrite chunk system
2595725963
private class PlayerTicketTracker extends DistanceManager.FixedPlayerDistanceChunkTracker {
2595825964

2595925965
private int viewDistance = 0;
25960-
@@ -657,5 +478,5 @@ public abstract class DistanceManager {
25966+
@@ -657,5 +482,5 @@ public abstract class DistanceManager {
2596125967
private boolean haveTicketFor(int distance) {
2596225968
return distance <= this.viewDistance;
2596325969
}
@@ -28060,7 +28066,7 @@ index b7d29389a357f142237cecd75f8ca91cf1eb6b5b..e4b0dc3121101d54394a0c3a413dabf8
2806028066
this.generatingStep = generationStep;
2806128067
this.cache = chunks;
2806228068
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
28063-
index 88299abf563a041ade1683b66b43103b0eeeea0d..61f3ee42aaad1641c92df3eb60d699b9dd5679e3 100644
28069+
index 88299abf563a041ade1683b66b43103b0eeeea0d..7f5686571ace6155247e085560fcc8919e67734c 100644
2806428070
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
2806528071
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2806628072
@@ -1420,7 +1420,7 @@ public abstract class PlayerList {
@@ -28071,6 +28077,15 @@ index 88299abf563a041ade1683b66b43103b0eeeea0d..61f3ee42aaad1641c92df3eb60d699b9
2807128077
+ //this.broadcastAll(new ClientboundSetChunkCacheRadiusPacket(viewDistance)); // Paper - rewrite chunk system
2807228078
Iterator iterator = this.server.getAllLevels().iterator();
2807328079

28080+
while (iterator.hasNext()) {
28081+
@@ -1435,7 +1435,7 @@ public abstract class PlayerList {
28082+
28083+
public void setSimulationDistance(int simulationDistance) {
28084+
this.simulationDistance = simulationDistance;
28085+
- this.broadcastAll(new ClientboundSetSimulationDistancePacket(simulationDistance));
28086+
+ //this.broadcastAll(new ClientboundSetSimulationDistancePacket(simulationDistance)); // Paper - rewrite chunk system
28087+
Iterator iterator = this.server.getAllLevels().iterator();
28088+
2807428089
while (iterator.hasNext()) {
2807528090
diff --git a/src/main/java/net/minecraft/util/BitStorage.java b/src/main/java/net/minecraft/util/BitStorage.java
2807628091
index 68648c5a5e3ff079f832092af0f2f801c42d1ede..e4e153cb8899e70273aa150b8ea26907cf68b15c 100644
@@ -36264,7 +36279,7 @@ index 4a5a0e33af16369f665bf39e70238e4e5a5486da..696152286a4d16fa51a23ff6e15fb297
3626436279

3626536280
// Paper start - implement pointers
3626636281
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
36267-
index 2bce60712c0fc53d019e1f9a76b05c95c0682141..775989ebd426b6b31fba68a649c6658d082e1e58 100644
36282+
index 7e3552390c7dd11a79fd95d3543707cc5d652c66..71ed0230baf3115a53a8ce8f0a5c72f01954fffc 100644
3626836283
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
3626936284
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
3627036285
@@ -3513,7 +3513,9 @@ public class CraftPlayer extends CraftHumanEntity implements Player {

0 commit comments

Comments
 (0)