From e672f8d7cd0d04afeecc79ac6caf236c92fa206c Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 29 Jul 2018 17:44:16 -0400 Subject: [PATCH] Preserve client settings packets --- .../client/ClientPlaySessionHandler.java | 14 ++- .../connection/client/ConnectedPlayer.java | 10 ++ .../proxy/protocol/StateRegistry.java | 7 +- .../protocol/packets/ClientSettings.java | 95 +++++++++++++++++++ 4 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/velocitypowered/proxy/protocol/packets/ClientSettings.java diff --git a/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index cf23f7bf4..65d5badaa 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -4,11 +4,8 @@ import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.backend.ServerConnection; import com.velocitypowered.proxy.data.ServerInfo; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.packets.Chat; -import com.velocitypowered.proxy.protocol.packets.JoinGame; -import com.velocitypowered.proxy.protocol.packets.KeepAlive; +import com.velocitypowered.proxy.protocol.packets.*; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.protocol.packets.Respawn; import io.netty.buffer.ByteBuf; import io.netty.channel.EventLoop; import org.apache.logging.log4j.LogManager; @@ -58,6 +55,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { return; } + if (packet instanceof ClientSettings) { + player.setClientSettings((ClientSettings) packet); + // forward it on + } + if (packet instanceof Chat) { Chat chat = (Chat) packet; if (chat.getMessage().equals("/connect")) { @@ -106,6 +108,10 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { player.getConnection().flush(); currentDimension = joinGame.getDimension(); } + + if (player.getClientSettings() != null) { + player.getConnectedServer().getChannel().write(player.getClientSettings()); + } } public void setCurrentDimension(int currentDimension) { diff --git a/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 4a6df0651..3c983cfd8 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -5,6 +5,7 @@ import com.velocitypowered.proxy.data.GameProfile; import com.velocitypowered.proxy.protocol.packets.Chat; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.backend.ServerConnection; +import com.velocitypowered.proxy.protocol.packets.ClientSettings; import com.velocitypowered.proxy.util.ThrowableUtils; import com.velocitypowered.proxy.data.ServerInfo; import com.velocitypowered.proxy.protocol.packets.Disconnect; @@ -28,6 +29,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation { private final GameProfile profile; private final MinecraftConnection connection; private ServerConnection connectedServer; + private ClientSettings clientSettings; public ConnectedPlayer(GameProfile profile, MinecraftConnection connection) { this.profile = profile; @@ -58,6 +60,14 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation { return connectedServer; } + public ClientSettings getClientSettings() { + return clientSettings; + } + + public void setClientSettings(ClientSettings clientSettings) { + this.clientSettings = clientSettings; + } + public void handleConnectionException(ServerInfo info, Throwable throwable) { String error = ThrowableUtils.briefDescription(throwable); String userMessage; diff --git a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 540db697a..6c62a558b 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -39,6 +39,9 @@ public enum StateRegistry { SERVERBOUND.register(KeepAlive.class, KeepAlive::new, map(0x0C, MINECRAFT_1_12), map(0x0B, MINECRAFT_1_12_1)); + SERVERBOUND.register(ClientSettings.class, ClientSettings::new, + map(0x05, MINECRAFT_1_12), + map(0x04, MINECRAFT_1_12_1)); CLIENTBOUND.register(Chat.class, Chat::new, map(0x0F, MINECRAFT_1_12)); @@ -107,10 +110,6 @@ public enum StateRegistry { for (final PacketMapping mapping : mappings) { ProtocolVersion version = this.versions.get(mapping.protocolVersion); - if (version == null) { - version = new ProtocolVersion(mapping.protocolVersion); - this.versions.put(mapping.protocolVersion, version); - } version.packetIdToSupplier.put(mapping.id, packetSupplier); version.packetClassToId.put(clazz, mapping.id); diff --git a/src/main/java/com/velocitypowered/proxy/protocol/packets/ClientSettings.java b/src/main/java/com/velocitypowered/proxy/protocol/packets/ClientSettings.java new file mode 100644 index 000000000..8cbe184d4 --- /dev/null +++ b/src/main/java/com/velocitypowered/proxy/protocol/packets/ClientSettings.java @@ -0,0 +1,95 @@ +package com.velocitypowered.proxy.protocol.packets; + +import com.velocitypowered.proxy.protocol.MinecraftPacket; +import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; +import io.netty.buffer.ByteBuf; + +public class ClientSettings implements MinecraftPacket { + private String locale; + private byte viewDistance; + private int chatMode; + private boolean chatColors; + private short skinParts; + private int mainHand; + + public String getLocale() { + return locale; + } + + public void setLocale(String locale) { + this.locale = locale; + } + + public byte getViewDistance() { + return viewDistance; + } + + public void setViewDistance(byte viewDistance) { + this.viewDistance = viewDistance; + } + + public int getChatMode() { + return chatMode; + } + + public void setChatMode(int chatMode) { + this.chatMode = chatMode; + } + + public boolean isChatColors() { + return chatColors; + } + + public void setChatColors(boolean chatColors) { + this.chatColors = chatColors; + } + + public short getSkinParts() { + return skinParts; + } + + public void setSkinParts(short skinParts) { + this.skinParts = skinParts; + } + + public int getMainHand() { + return mainHand; + } + + public void setMainHand(int mainHand) { + this.mainHand = mainHand; + } + + @Override + public String toString() { + return "ClientSettings{" + + "locale='" + locale + '\'' + + ", viewDistance=" + viewDistance + + ", chatMode=" + chatMode + + ", chatColors=" + chatColors + + ", skinParts=" + skinParts + + ", mainHand=" + mainHand + + '}'; + } + + @Override + public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + this.locale = ProtocolUtils.readString(buf, 16); + this.viewDistance = buf.readByte(); + this.chatMode = ProtocolUtils.readVarInt(buf); + this.chatColors = buf.readBoolean(); + this.skinParts = buf.readUnsignedByte(); + this.mainHand = ProtocolUtils.readVarInt(buf); + } + + @Override + public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + ProtocolUtils.writeString(buf, locale); + buf.writeByte(viewDistance); + ProtocolUtils.writeVarInt(buf, chatMode); + buf.writeBoolean(chatColors); + buf.writeByte(skinParts); + ProtocolUtils.writeVarInt(buf, mainHand); + } +}