Skip to content

Commit 8a93f6a

Browse files
committed
Send the client render distance to the server
Previously we've had discussions on if the render distance math should be tweaked like we do server -> client, but for now this is better than nothing and can be tweaked further in the future.
1 parent 516d8e5 commit 8a93f6a

File tree

6 files changed

+87
-22
lines changed

6 files changed

+87
-22
lines changed

core/src/main/java/org/geysermc/connector/network/session/GeyserSession.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public int getRemotePort() {
6464
}
6565

6666
public int getRenderDistance() {
67-
return this.handle.getRenderDistance();
67+
return this.handle.getServerRenderDistance();
6868
}
6969

7070
public boolean isSentSpawnPacket() {

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

+30-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@
3838
import com.github.steveice10.mc.protocol.data.UnexpectedEncryptionException;
3939
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose;
4040
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
41+
import com.github.steveice10.mc.protocol.data.game.entity.player.HandPreference;
4142
import com.github.steveice10.mc.protocol.data.game.recipe.Recipe;
43+
import com.github.steveice10.mc.protocol.data.game.setting.ChatVisibility;
44+
import com.github.steveice10.mc.protocol.data.game.setting.SkinPart;
4245
import com.github.steveice10.mc.protocol.data.game.statistic.CustomStatistic;
4346
import com.github.steveice10.mc.protocol.data.game.statistic.Statistic;
4447
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
48+
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundClientInformationPacket;
4549
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundMovePlayerPosPacket;
4650
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerAbilitiesPacket;
4751
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundCustomQueryPacket;
@@ -245,7 +249,9 @@ public class GeyserSession implements GeyserConnection, CommandSender {
245249

246250
@Setter
247251
private Vector2i lastChunkPosition = null;
248-
private int renderDistance;
252+
@Setter
253+
private int clientRenderDistance = -1;
254+
private int serverRenderDistance;
249255

250256
// Exposed for GeyserConnect usage
251257
protected boolean sentSpawnPacket;
@@ -1160,9 +1166,9 @@ public String getLocale() {
11601166
return clientData.getLanguageCode();
11611167
}
11621168

1163-
public void setRenderDistance(int renderDistance) {
1169+
public void setServerRenderDistance(int renderDistance) {
11641170
renderDistance = GenericMath.ceil(++renderDistance * MathUtils.SQRT_OF_TWO); //square to circle
1165-
this.renderDistance = renderDistance;
1171+
this.serverRenderDistance = renderDistance;
11661172

11671173
ChunkRadiusUpdatedPacket chunkRadiusUpdatedPacket = new ChunkRadiusUpdatedPacket();
11681174
chunkRadiusUpdatedPacket.setRadius(renderDistance);
@@ -1420,6 +1426,27 @@ public void sendAdventureSettings() {
14201426
sendUpstreamPacket(adventureSettingsPacket);
14211427
}
14221428

1429+
private int getRenderDistance() {
1430+
if (clientRenderDistance != -1) {
1431+
// The client has sent a render distance
1432+
return clientRenderDistance;
1433+
}
1434+
return serverRenderDistance;
1435+
}
1436+
1437+
// We need to send our skin parts to the server otherwise java sees us with no hat, jacket etc
1438+
private static final List<SkinPart> SKIN_PARTS = Arrays.asList(SkinPart.values());
1439+
1440+
/**
1441+
* Send a packet to the server to indicate client render distance, locale, skin parts, and hand preference.
1442+
*/
1443+
public void sendJavaClientSettings() {
1444+
ServerboundClientInformationPacket clientSettingsPacket = new ServerboundClientInformationPacket(getLocale(),
1445+
getRenderDistance(), ChatVisibility.FULL, true, SKIN_PARTS,
1446+
HandPreference.RIGHT_HAND, false, true);
1447+
sendDownstreamPacket(clientSettingsPacket);
1448+
}
1449+
14231450
/**
14241451
* Used for updating statistic values since we only get changes from the server
14251452
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*
22+
* @author GeyserMC
23+
* @link https://github.com/GeyserMC/Geyser
24+
*/
25+
26+
package org.geysermc.geyser.translator.protocol.bedrock;
27+
28+
import com.nukkitx.protocol.bedrock.packet.RequestChunkRadiusPacket;
29+
import org.geysermc.geyser.session.GeyserSession;
30+
import org.geysermc.geyser.translator.protocol.PacketTranslator;
31+
import org.geysermc.geyser.translator.protocol.Translator;
32+
33+
/**
34+
* Sent when the client updates its desired render distance.
35+
*/
36+
@Translator(packet = RequestChunkRadiusPacket.class)
37+
public class BedrockRequestChunkRadiusTranslator extends PacketTranslator<RequestChunkRadiusPacket> {
38+
39+
@Override
40+
public void translate(GeyserSession session, RequestChunkRadiusPacket packet) {
41+
session.setClientRenderDistance(packet.getRadius());
42+
43+
if (session.isLoggedIn()) {
44+
session.sendJavaClientSettings();
45+
}
46+
}
47+
}

core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaLoginTranslator.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,25 @@
2525

2626
package org.geysermc.geyser.translator.protocol.java;
2727

28-
import com.github.steveice10.mc.protocol.data.game.entity.player.HandPreference;
29-
import com.github.steveice10.mc.protocol.data.game.setting.ChatVisibility;
30-
import com.github.steveice10.mc.protocol.data.game.setting.SkinPart;
3128
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
32-
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundClientInformationPacket;
3329
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCustomPayloadPacket;
3430
import com.nukkitx.protocol.bedrock.data.GameRuleData;
3531
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
36-
import com.nukkitx.protocol.bedrock.packet.*;
37-
import org.geysermc.geyser.session.auth.AuthType;
32+
import com.nukkitx.protocol.bedrock.packet.AdventureSettingsPacket;
33+
import com.nukkitx.protocol.bedrock.packet.GameRulesChangedPacket;
34+
import com.nukkitx.protocol.bedrock.packet.SetPlayerGameTypePacket;
3835
import org.geysermc.geyser.entity.type.player.PlayerEntity;
3936
import org.geysermc.geyser.session.GeyserSession;
37+
import org.geysermc.geyser.session.auth.AuthType;
38+
import org.geysermc.geyser.translator.level.BiomeTranslator;
4039
import org.geysermc.geyser.translator.protocol.PacketTranslator;
4140
import org.geysermc.geyser.translator.protocol.Translator;
42-
import org.geysermc.geyser.translator.level.BiomeTranslator;
4341
import org.geysermc.geyser.util.ChunkUtils;
4442
import org.geysermc.geyser.util.DimensionUtils;
4543
import org.geysermc.geyser.util.PluginMessageUtils;
4644

47-
import java.util.Arrays;
48-
import java.util.List;
49-
5045
@Translator(packet = ClientboundLoginPacket.class)
5146
public class JavaLoginTranslator extends PacketTranslator<ClientboundLoginPacket> {
52-
private static final List<SkinPart> SKIN_PART_VALUES = Arrays.asList(SkinPart.values());
5347

5448
@Override
5549
public void translate(GeyserSession session, ClientboundLoginPacket packet) {
@@ -99,13 +93,10 @@ public void translate(GeyserSession session, ClientboundLoginPacket packet) {
9993

10094
session.setReducedDebugInfo(packet.isReducedDebugInfo());
10195

102-
session.setRenderDistance(packet.getViewDistance());
96+
session.setServerRenderDistance(packet.getViewDistance());
10397

104-
// We need to send our skin parts to the server otherwise java sees us with no hat, jacket etc
105-
String locale = session.getLocale();
10698
// TODO customize
107-
ServerboundClientInformationPacket infoPacket = new ServerboundClientInformationPacket(locale, (byte) session.getRenderDistance(), ChatVisibility.FULL, true, SKIN_PART_VALUES, HandPreference.RIGHT_HAND, false, true);
108-
session.sendDownstreamPacket(infoPacket);
99+
session.sendJavaClientSettings();
109100

110101
session.sendDownstreamPacket(new ServerboundCustomPayloadPacket("minecraft:brand", PluginMessageUtils.getGeyserBrandData()));
111102

core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaSetChunkCacheRadiusTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public class JavaSetChunkCacheRadiusTranslator extends PacketTranslator<Clientbo
3535

3636
@Override
3737
public void translate(GeyserSession session, ClientboundSetChunkCacheRadiusPacket packet) {
38-
session.setRenderDistance(packet.getViewDistance());
38+
session.setServerRenderDistance(packet.getViewDistance());
3939
}
4040
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static void updateChunkPosition(GeyserSession session, Vector3i position)
121121
if (chunkPos == null || !chunkPos.equals(newChunkPos)) {
122122
NetworkChunkPublisherUpdatePacket chunkPublisherUpdatePacket = new NetworkChunkPublisherUpdatePacket();
123123
chunkPublisherUpdatePacket.setPosition(position);
124-
chunkPublisherUpdatePacket.setRadius(session.getRenderDistance() << 4);
124+
chunkPublisherUpdatePacket.setRadius(session.getServerRenderDistance() << 4);
125125
session.sendUpstreamPacket(chunkPublisherUpdatePacket);
126126

127127
session.setLastChunkPosition(newChunkPos);

0 commit comments

Comments
 (0)