From ae6afc8b18919c8f1051ef8618203a0d09512f60 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 23 Jun 2020 13:17:36 -0400 Subject: [PATCH] Fix Minecraft 1.7 support (bad merge) --- .../proxy/connection/MinecraftConnection.java | 11 ++++++++--- .../proxy/protocol/packet/JoinGame.java | 16 ++++++++++------ .../protocol/packet/ServerLoginSuccess.java | 9 +++++++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index 84649904a..dad6bdb3e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -37,6 +37,7 @@ import io.netty.util.ReferenceCountUtil; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.security.GeneralSecurityException; +import java.util.concurrent.TimeUnit; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.logging.log4j.LogManager; @@ -207,14 +208,18 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { */ public void closeWith(Object msg) { if (channel.isActive()) { - if (channel.eventLoop().inEventLoop()) { + if (channel.eventLoop().inEventLoop() + && this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { knownDisconnect = true; channel.writeAndFlush(msg).addListener(ChannelFutureListener.CLOSE); } else { - channel.eventLoop().execute(() -> { + // 1.7.x versions have a race condition with switching protocol versions, so just explicitly + // close the connection after a short while. + this.setAutoReading(false); + channel.eventLoop().schedule(() -> { knownDisconnect = true; channel.writeAndFlush(msg).addListener(ChannelFutureListener.CLOSE); - }); + }, 250, TimeUnit.MILLISECONDS); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java index 630a60de2..bca62398e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java @@ -130,8 +130,8 @@ public class JoinGame implements MinecraftPacket { + ", levelType='" + levelType + '\'' + ", viewDistance=" + viewDistance + ", reducedDebugInfo=" + reducedDebugInfo - + ", dimensionRegistry='" + dimensionRegistry.toString() + '\'' - + ", dimensionInfo='" + dimensionInfo.toString() + '\'' + + ", dimensionRegistry='" + dimensionRegistry + '\'' + + ", dimensionInfo='" + dimensionInfo + '\'' + ", previousGamemode=" + previousGamemode + '}'; } @@ -139,7 +139,7 @@ public class JoinGame implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { this.entityId = buf.readInt(); - this.gamemode = buf.readByte(); + this.gamemode = buf.readUnsignedByte(); String dimensionIdentifier = null; String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { @@ -167,7 +167,9 @@ public class JoinGame implements MinecraftPacket { if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) { this.viewDistance = ProtocolUtils.readVarInt(buf); } - this.reducedDebugInfo = buf.readBoolean(); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + this.reducedDebugInfo = buf.readBoolean(); + } if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { this.showRespawnScreen = buf.readBoolean(); } @@ -208,9 +210,11 @@ public class JoinGame implements MinecraftPacket { ProtocolUtils.writeString(buf, levelType); } if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) { - ProtocolUtils.writeVarInt(buf,viewDistance); + ProtocolUtils.writeVarInt(buf, viewDistance); + } + if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + buf.writeBoolean(reducedDebugInfo); } - buf.writeBoolean(reducedDebugInfo); if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { buf.writeBoolean(showRespawnScreen); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccess.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccess.java index 4ff3e132b..b7a3e54de 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccess.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccess.java @@ -1,6 +1,7 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.api.util.UuidUtils; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; @@ -47,8 +48,10 @@ public class ServerLoginSuccess implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { uuid = ProtocolUtils.readUuidIntArray(buf); - } else { + } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) >= 0) { uuid = UUID.fromString(ProtocolUtils.readString(buf, 36)); + } else { + uuid = UuidUtils.fromUndashed(ProtocolUtils.readString(buf, 32)); } username = ProtocolUtils.readString(buf, 16); } @@ -60,8 +63,10 @@ public class ServerLoginSuccess implements MinecraftPacket { } if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { ProtocolUtils.writeUuidIntArray(buf, uuid); - } else { + } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) >= 0) { ProtocolUtils.writeString(buf, uuid.toString()); + } else { + ProtocolUtils.writeString(buf, UuidUtils.toUndashed(uuid)); } if (username == null) { throw new IllegalStateException("No username specified!");