Skip to content

Commit 758700c

Browse files
Fix render distance issues (#5381)
* Potentially fix render distance issues * AGGRESSIVE fix render distance issues --------- Co-authored-by: Camotoy <[email protected]>
1 parent 50a0e61 commit 758700c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

core/src/main/java/org/geysermc/geyser/session/GeyserSession.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
341341

342342
@Setter
343343
private Vector2i lastChunkPosition = null;
344-
@Setter
345344
private int clientRenderDistance = -1;
346345
private int serverRenderDistance = -1;
347346

@@ -1453,11 +1452,31 @@ public void sendCommand(String command) {
14531452
sendDownstreamGamePacket(new ServerboundChatCommandSignedPacket(command, Instant.now().toEpochMilli(), 0L, Collections.emptyList(), 0, new BitSet()));
14541453
}
14551454

1455+
public void setClientRenderDistance(int clientRenderDistance) {
1456+
boolean oldSquareToCircle = this.clientRenderDistance < this.serverRenderDistance;
1457+
this.clientRenderDistance = clientRenderDistance;
1458+
boolean newSquareToCircle = this.clientRenderDistance < this.serverRenderDistance;
1459+
1460+
if (this.serverRenderDistance != -1 && oldSquareToCircle != newSquareToCircle) {
1461+
recalculateBedrockRenderDistance();
1462+
}
1463+
}
1464+
14561465
public void setServerRenderDistance(int renderDistance) {
14571466
// Ensure render distance is not above 96 as sending a larger value at any point crashes mobile clients and 96 is the max of any bedrock platform
14581467
renderDistance = Math.min(renderDistance, 96);
14591468
this.serverRenderDistance = renderDistance;
14601469

1470+
recalculateBedrockRenderDistance();
1471+
}
1472+
1473+
/**
1474+
* Ensures that the ChunkRadiusUpdatedPacket uses the correct render distance for whatever the client distance is set as.
1475+
* If the server render distance is larger than the client's, then account for this and add some extra padding.
1476+
* We don't want to apply this for every render distance, if at all possible, because
1477+
*/
1478+
private void recalculateBedrockRenderDistance() {
1479+
int renderDistance = ChunkUtils.squareToCircle(this.serverRenderDistance);
14611480
ChunkRadiusUpdatedPacket chunkRadiusUpdatedPacket = new ChunkRadiusUpdatedPacket();
14621481
chunkRadiusUpdatedPacket.setRadius(renderDistance);
14631482
upstream.sendPacket(chunkRadiusUpdatedPacket);

core/src/main/java/org/geysermc/geyser/util/ChunkUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ public static void updateChunkPosition(GeyserSession session, Vector3i position)
9999
chunkPublisherUpdatePacket.setPosition(position);
100100
// Mitigates chunks not loading on 1.17.1 Paper and 1.19.3 Fabric. As of Bedrock 1.19.60.
101101
// https://github.com/GeyserMC/Geyser/issues/3490
102-
chunkPublisherUpdatePacket.setRadius(GenericMath.ceil((session.getServerRenderDistance() + 1) * MathUtils.SQRT_OF_TWO) << 4);
102+
chunkPublisherUpdatePacket.setRadius(squareToCircle(session.getServerRenderDistance()) << 4);
103103
session.sendUpstreamPacket(chunkPublisherUpdatePacket);
104104

105105
session.setLastChunkPosition(newChunkPos);
106106
}
107107
}
108108

109+
/**
110+
* Converts a Java render distance number to the equivalent in Bedrock.
111+
*/
112+
public static int squareToCircle(int renderDistance) {
113+
return GenericMath.ceil((renderDistance + 1) * MathUtils.SQRT_OF_TWO);
114+
}
115+
109116
/**
110117
* Sends a block update to the Bedrock client. If the platform is not Spigot, this also
111118
* adds that block to the cache.

0 commit comments

Comments
 (0)