From eaa83378d74a23bc9f49d0c850c2dae24bc2c980 Mon Sep 17 00:00:00 2001 From: pkt77 Date: Wed, 6 Dec 2023 14:19:08 -0500 Subject: [PATCH] Fix Disconnects --- .../connection/client/ConnectedPlayer.java | 2 +- .../client/InitialInboundConnection.java | 12 ++++++---- .../util/ConnectionRequestResults.java | 10 ++++---- .../proxy/protocol/packet/BossBar.java | 6 ++--- .../proxy/protocol/packet/Disconnect.java | 24 +++++++++---------- .../protocol/packet/HeaderAndFooter.java | 6 ++--- .../protocol/packet/ResourcePackRequest.java | 4 ++-- .../proxy/protocol/packet/ServerData.java | 4 ++-- .../protocol/packet/UpsertPlayerInfo.java | 4 ++-- .../protocol/packet/chat/ComponentHolder.java | 2 +- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 307b4cf52..815ef1f52 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -651,7 +651,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, return; } - Component disconnectReason = GsonComponentSerializer.gson().deserialize(disconnect.getReason()); + Component disconnectReason = disconnect.getReason().getComponent(); String plainTextReason = PASS_THRU_TRANSLATE.serialize(disconnectReason); if (connectedServer != null && connectedServer.getServerInfo().equals(server.getServerInfo())) { logger.info("{}: kicked from server {}: {}", this, server.getServerInfo().getName(), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java index ecb1cc428..e8ac43b0b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java @@ -47,7 +47,7 @@ public final class InitialInboundConnection implements VelocityInboundConnection private final Handshake handshake; InitialInboundConnection(MinecraftConnection connection, String cleanedAddress, - Handshake handshake) { + Handshake handshake) { this.connection = connection; this.cleanedAddress = cleanedAddress; this.handshake = handshake; @@ -95,7 +95,9 @@ public final class InitialInboundConnection implements VelocityInboundConnection logger.info("{} has disconnected: {}", this, LegacyComponentSerializer.legacySection().serialize(translated)); } - connection.closeWith(Disconnect.create(translated, getProtocolVersion())); + connection.closeWith(Disconnect.create(translated, + getProtocolVersion() == ProtocolVersion.MINECRAFT_1_20_3 // Login disconnects are string + ? ProtocolVersion.MINECRAFT_1_20_2 : getProtocolVersion())); } /** @@ -106,6 +108,8 @@ public final class InitialInboundConnection implements VelocityInboundConnection public void disconnectQuietly(Component reason) { Component translated = GlobalTranslator.render(reason, ClosestLocaleMatcher.INSTANCE .lookupClosest(Locale.getDefault())); - connection.closeWith(Disconnect.create(translated, getProtocolVersion())); + connection.closeWith(Disconnect.create(translated, + getProtocolVersion() == ProtocolVersion.MINECRAFT_1_20_3 // Login disconnects are string + ? ProtocolVersion.MINECRAFT_1_20_2 : getProtocolVersion())); } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionRequestResults.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionRequestResults.java index 7ef1601e1..a6c8824db 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionRequestResults.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionRequestResults.java @@ -24,7 +24,6 @@ import com.velocitypowered.proxy.protocol.packet.Disconnect; import java.util.Optional; import javax.annotation.Nullable; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; /** * Common connection request results. @@ -64,13 +63,12 @@ public class ConnectionRequestResults { } public static Impl forDisconnect(Disconnect disconnect, RegisteredServer server) { - Component deserialized = GsonComponentSerializer.gson().deserialize(disconnect.getReason()); - return forDisconnect(deserialized, server); + return forDisconnect(disconnect.getReason().getComponent(), server); } public static Impl forUnsafeDisconnect(Disconnect disconnect, RegisteredServer server) { - Component deserialized = GsonComponentSerializer.gson().deserialize(disconnect.getReason()); - return new Impl(Status.SERVER_DISCONNECTED, deserialized, server, false); + return new Impl(Status.SERVER_DISCONNECTED, disconnect.getReason().getComponent(), server, + false); } /** @@ -115,4 +113,4 @@ public class ConnectionRequestResults { return safe; } } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java index 135804af6..569fcac73 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java @@ -158,7 +158,7 @@ public class BossBar implements MinecraftPacket { if (name == null) { throw new IllegalStateException("No name specified!"); } - name.write(buf, version); + name.write(buf); buf.writeFloat(percent); ProtocolUtils.writeVarInt(buf, color); ProtocolUtils.writeVarInt(buf, overlay); @@ -173,7 +173,7 @@ public class BossBar implements MinecraftPacket { if (name == null) { throw new IllegalStateException("No name specified!"); } - name.write(buf, version); + name.write(buf); break; case UPDATE_STYLE: ProtocolUtils.writeVarInt(buf, color); @@ -198,4 +198,4 @@ public class BossBar implements MinecraftPacket { packet.setAction(REMOVE); return packet; } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java index cdc9d0d75..53b576206 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java @@ -22,28 +22,30 @@ import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; +import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import io.netty.buffer.ByteBuf; +import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; public class Disconnect implements MinecraftPacket { - private @Nullable String reason; + private @Nullable ComponentHolder reason; public Disconnect() { } - public Disconnect(String reason) { + public Disconnect(ComponentHolder reason) { this.reason = Preconditions.checkNotNull(reason, "reason"); } - public String getReason() { + public ComponentHolder getReason() { if (reason == null) { throw new IllegalStateException("No reason specified"); } return reason; } - public void setReason(@Nullable String reason) { + public void setReason(@Nullable ComponentHolder reason) { this.reason = reason; } @@ -56,15 +58,12 @@ public class Disconnect implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - reason = ProtocolUtils.readString(buf); + reason = ComponentHolder.read(buf, version); } @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (reason == null) { - throw new IllegalStateException("No reason specified."); - } - ProtocolUtils.writeString(buf, reason); + getReason().write(buf); } @Override @@ -72,9 +71,8 @@ public class Disconnect implements MinecraftPacket { return handler.handle(this); } - public static Disconnect create(net.kyori.adventure.text.Component component, - ProtocolVersion version) { + public static Disconnect create(Component component, ProtocolVersion version) { Preconditions.checkNotNull(component, "component"); - return new Disconnect(ProtocolUtils.getJsonChatSerializer(version).serialize(component)); + return new Disconnect(new ComponentHolder(version, component)); } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java index 987bfd477..4ca819a13 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java @@ -57,8 +57,8 @@ public class HeaderAndFooter implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - header.write(buf, version); - footer.write(buf, version); + header.write(buf); + footer.write(buf); } @Override @@ -75,4 +75,4 @@ public class HeaderAndFooter implements MinecraftPacket { public static HeaderAndFooter reset() { return RESET; } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequest.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequest.java index dbef1b14d..72832fbba 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequest.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequest.java @@ -118,7 +118,7 @@ public class ResourcePackRequest implements MinecraftPacket { buf.writeBoolean(isRequired); if (prompt != null) { buf.writeBoolean(true); - prompt.write(buf, protocolVersion); + prompt.write(buf); } else { buf.writeBoolean(false); } @@ -149,4 +149,4 @@ public class ResourcePackRequest implements MinecraftPacket { return "ResourcePackRequest{" + "url='" + url + '\'' + ", hash='" + hash + '\'' + ", isRequired=" + isRequired + ", prompt='" + prompt + '\'' + '}'; } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerData.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerData.java index 470a1ffb2..cbb768674 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerData.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerData.java @@ -76,7 +76,7 @@ public class ServerData implements MinecraftPacket { buf.writeBoolean(hasDescription); } if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_4) >= 0 || hasDescription) { - this.description.write(buf, protocolVersion); + this.description.write(buf); } boolean hasFavicon = this.favicon != null; @@ -115,4 +115,4 @@ public class ServerData implements MinecraftPacket { public boolean isSecureChatEnforced() { return secureChatEnforced; } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/UpsertPlayerInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/UpsertPlayerInfo.java index 1eac48007..e323f5e8f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/UpsertPlayerInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/UpsertPlayerInfo.java @@ -186,7 +186,7 @@ public class UpsertPlayerInfo implements MinecraftPacket { }, (version, buf, info) -> { // write buf.writeBoolean(info.displayName != null); if (info.displayName != null) { - info.displayName.write(buf, version); + info.displayName.write(buf); } }); @@ -292,4 +292,4 @@ public class UpsertPlayerInfo implements MinecraftPacket { '}'; } } -} +} \ No newline at end of file diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java index 4cf1f2302..37fd5b514 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java @@ -268,7 +268,7 @@ public class ComponentHolder { } } - public void write(ByteBuf buf, ProtocolVersion version) { + public void write(ByteBuf buf) { if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { ProtocolUtils.writeBinaryTag(buf, version, getBinaryTag()); } else {