Skip to content

Commit

Permalink
Fix render distance issues (#5381)
Browse files Browse the repository at this point in the history
* Potentially fix render distance issues

* AGGRESSIVE fix render distance issues

---------

Co-authored-by: Camotoy <[email protected]>
  • Loading branch information
onebeastchris and Camotoy authored Feb 27, 2025
1 parent 50a0e61 commit 758700c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {

@Setter
private Vector2i lastChunkPosition = null;
@Setter
private int clientRenderDistance = -1;
private int serverRenderDistance = -1;

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

public void setClientRenderDistance(int clientRenderDistance) {
boolean oldSquareToCircle = this.clientRenderDistance < this.serverRenderDistance;
this.clientRenderDistance = clientRenderDistance;
boolean newSquareToCircle = this.clientRenderDistance < this.serverRenderDistance;

if (this.serverRenderDistance != -1 && oldSquareToCircle != newSquareToCircle) {
recalculateBedrockRenderDistance();
}
}

public void setServerRenderDistance(int renderDistance) {
// 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
renderDistance = Math.min(renderDistance, 96);
this.serverRenderDistance = renderDistance;

recalculateBedrockRenderDistance();
}

/**
* Ensures that the ChunkRadiusUpdatedPacket uses the correct render distance for whatever the client distance is set as.
* If the server render distance is larger than the client's, then account for this and add some extra padding.
* We don't want to apply this for every render distance, if at all possible, because
*/
private void recalculateBedrockRenderDistance() {
int renderDistance = ChunkUtils.squareToCircle(this.serverRenderDistance);
ChunkRadiusUpdatedPacket chunkRadiusUpdatedPacket = new ChunkRadiusUpdatedPacket();
chunkRadiusUpdatedPacket.setRadius(renderDistance);
upstream.sendPacket(chunkRadiusUpdatedPacket);
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/org/geysermc/geyser/util/ChunkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,20 @@ public static void updateChunkPosition(GeyserSession session, Vector3i position)
chunkPublisherUpdatePacket.setPosition(position);
// Mitigates chunks not loading on 1.17.1 Paper and 1.19.3 Fabric. As of Bedrock 1.19.60.
// https://github.com/GeyserMC/Geyser/issues/3490
chunkPublisherUpdatePacket.setRadius(GenericMath.ceil((session.getServerRenderDistance() + 1) * MathUtils.SQRT_OF_TWO) << 4);
chunkPublisherUpdatePacket.setRadius(squareToCircle(session.getServerRenderDistance()) << 4);
session.sendUpstreamPacket(chunkPublisherUpdatePacket);

session.setLastChunkPosition(newChunkPos);
}
}

/**
* Converts a Java render distance number to the equivalent in Bedrock.
*/
public static int squareToCircle(int renderDistance) {
return GenericMath.ceil((renderDistance + 1) * MathUtils.SQRT_OF_TWO);
}

/**
* Sends a block update to the Bedrock client. If the platform is not Spigot, this also
* adds that block to the cache.
Expand Down

0 comments on commit 758700c

Please sign in to comment.