From 7a5857a0b2ddc1116ba2fa91eef8786b5be72a0c Mon Sep 17 00:00:00 2001 From: Daniel Naylor Date: Thu, 6 Sep 2018 19:15:18 +0100 Subject: [PATCH] Modded clients can connect to vanilla servers again. Modded to modded seems to need some work still... --- .../proxy/connection/MinecraftConnection.java | 9 +++++++++ .../backend/BackendPlaySessionHandler.java | 13 +++++++------ .../connection/client/ClientPlaySessionHandler.java | 2 ++ .../proxy/connection/client/ConnectedPlayer.java | 11 +++++++---- 4 files changed, 25 insertions(+), 10 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 3cef702ac..94293ebc1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -47,6 +47,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { private MinecraftConnectionAssociation association; private boolean isLegacyForge; private final VelocityServer server; + private boolean canSendLegacyFMLResetPacket = false; public MinecraftConnection(Channel channel, VelocityServer server) { this.channel = channel; @@ -231,4 +232,12 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { public void setLegacyForge(boolean isForge) { this.isLegacyForge = isForge; } + + public boolean canSendLegacyFMLResetPacket() { + return canSendLegacyFMLResetPacket; + } + + public void setCanSendLegacyFMLResetPacket(boolean canSendLegacyFMLResetPacket) { + this.canSendLegacyFMLResetPacket = isLegacyForge && canSendLegacyFMLResetPacket; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index e013c0b05..77ea3faff 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -47,7 +47,6 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { connection.getPlayer().handleConnectionException(connection.getServerInfo(), original); } else if (packet instanceof JoinGame) { playerHandler.handleBackendJoinGame((JoinGame) packet); - connection.setHasCompletedJoin(true); } else if (packet instanceof BossBar) { BossBar bossBar = (BossBar) packet; switch (bossBar.getAction()) { @@ -74,7 +73,8 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { if (!connection.isModded()) { connection.setModded(true); - // We must always reset the handshake before a modded connection is established. + // We must always reset the handshake before a modded connection is established if + // we haven't done so already. connection.getPlayer().sendLegacyForgeHandshakeResetPacket(); } @@ -113,13 +113,14 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { private boolean canForwardPluginMessage(PluginMessage message) { ClientPlaySessionHandler playerHandler = (ClientPlaySessionHandler) connection.getPlayer().getConnection().getSessionHandler(); - boolean isMCMessage; + boolean isMCOrFMLMessage; if (connection.getMinecraftConnection().getProtocolVersion() <= ProtocolConstants.MINECRAFT_1_12_2) { - isMCMessage = message.getChannel().startsWith("MC|"); + String channel = message.getChannel(); + isMCOrFMLMessage = channel.startsWith("MC|") || channel.startsWith(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); } else { - isMCMessage = message.getChannel().startsWith("minecraft:"); + isMCOrFMLMessage = message.getChannel().startsWith("minecraft:"); } - return isMCMessage || playerHandler.getClientPluginMsgChannels().contains(message.getChannel()) || + return isMCOrFMLMessage || playerHandler.getClientPluginMsgChannels().contains(message.getChannel()) || server.getChannelRegistrar().registered(message.getChannel()); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 3a65e591d..3a93f0330 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -197,6 +197,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // Flush everything player.getConnection().flush(); player.getConnectedServer().getMinecraftConnection().flush(); + player.getConnectedServer().setHasCompletedJoin(true); + player.getConnection().setCanSendLegacyFMLResetPacket(true); } public List getServerBossBars() { 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 b3973aaec..605a376d3 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 @@ -296,10 +296,13 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } public void sendLegacyForgeHandshakeResetPacket() { - PluginMessage resetPacket = new PluginMessage(); - resetPacket.setChannel(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); - resetPacket.setData(VelocityConstants.FORGE_LEGACY_HANDSHAKE_RESET_DATA); - connection.write(resetPacket); + if (connection.canSendLegacyFMLResetPacket()) { + PluginMessage resetPacket = new PluginMessage(); + resetPacket.setChannel(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); + resetPacket.setData(VelocityConstants.FORGE_LEGACY_HANDSHAKE_RESET_DATA); + connection.write(resetPacket); + connection.setCanSendLegacyFMLResetPacket(false); + } } public void close(TextComponent reason) {