From 51c402e3d3f3acb79c31b8beb2fb8aa1e36b529c Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 15 Oct 2018 17:01:18 -0400 Subject: [PATCH] Create a specific Forge-related package --- .../proxy/connection/VelocityConstants.java | 5 -- .../backend/BackendPlaySessionHandler.java | 7 ++- .../client/ClientPlaySessionHandler.java | 6 +-- .../connection/client/ConnectedPlayer.java | 6 +-- .../connection/forge/ForgeConstants.java | 4 ++ .../proxy/connection/forge/ForgeUtil.java | 51 +++++++++++++++++++ .../protocol/util/PluginMessageUtil.java | 29 +---------- 7 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeConstants.java create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeUtil.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/VelocityConstants.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/VelocityConstants.java index b524b31e1..819a521e5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/VelocityConstants.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/VelocityConstants.java @@ -7,9 +7,4 @@ public class VelocityConstants { public static final String VELOCITY_IP_FORWARDING_CHANNEL = "velocity:player_info"; - public static final String FORGE_LEGACY_HANDSHAKE_CHANNEL = "FML|HS"; - public static final String FORGE_LEGACY_CHANNEL = "FML"; - public static final String FORGE_MULTIPART_LEGACY_CHANNEL = "FML|MP"; - - public static final byte[] FORGE_LEGACY_HANDSHAKE_RESET_DATA = new byte[] { -2, 0 }; } 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 dc2b6a6cd..03f3679e6 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 @@ -5,14 +5,13 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.connection.VelocityConstants; import com.velocitypowered.proxy.connection.client.ClientPlaySessionHandler; +import com.velocitypowered.proxy.connection.forge.ForgeUtil; import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.packet.*; import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; -import com.velocitypowered.proxy.tablist.VelocityTabList; import io.netty.buffer.ByteBuf; public class BackendPlaySessionHandler implements MinecraftSessionHandler { @@ -85,7 +84,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { return true; } - if (!serverConn.hasCompletedJoin() && packet.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { + if (!serverConn.hasCompletedJoin() && packet.getChannel().equals(ForgeUtil.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { if (!serverConn.isLegacyForge()) { serverConn.setLegacyForge(true); @@ -174,7 +173,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { boolean isMCOrFMLMessage; if (serverConn.getConnection().getProtocolVersion() <= ProtocolConstants.MINECRAFT_1_12_2) { String channel = message.getChannel(); - isMCOrFMLMessage = channel.startsWith("MC|") || channel.startsWith(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); + isMCOrFMLMessage = channel.startsWith("MC|") || channel.startsWith(ForgeUtil.FORGE_LEGACY_HANDSHAKE_CHANNEL); } else { isMCOrFMLMessage = message.getChannel().startsWith("minecraft:"); } 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 acfedf1cd..707db0f78 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 @@ -6,8 +6,8 @@ import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.util.ModInfo; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.connection.VelocityConstants; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; +import com.velocitypowered.proxy.connection.forge.ForgeUtil; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.packet.*; @@ -144,9 +144,9 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } else if (PluginMessageUtil.isMCBrand(packet)) { player.getConnectedServer().getConnection().write(PluginMessageUtil.rewriteMCBrand(packet)); } else if (player.getConnectedServer().isLegacyForge() && !player.getConnectedServer().hasCompletedJoin()) { - if (packet.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { + if (packet.getChannel().equals(ForgeUtil.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { if (!player.getModInfo().isPresent()) { - PluginMessageUtil.readModList(packet).ifPresent(mods -> player.setModInfo(new ModInfo("FML", mods))); + ForgeUtil.readModList(packet).ifPresent(mods -> player.setModInfo(new ModInfo("FML", mods))); } // Always forward the FML handshake to the remote server. 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 a388bc099..63f53e3b6 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 @@ -25,8 +25,8 @@ import com.velocitypowered.api.util.title.Titles; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; -import com.velocitypowered.proxy.connection.VelocityConstants; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; +import com.velocitypowered.proxy.connection.forge.ForgeUtil; import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults; import com.velocitypowered.proxy.protocol.ProtocolConstants; @@ -400,8 +400,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { public void sendLegacyForgeHandshakeResetPacket() { if (connection.canSendLegacyFMLResetPacket()) { PluginMessage resetPacket = new PluginMessage(); - resetPacket.setChannel(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); - resetPacket.setData(VelocityConstants.FORGE_LEGACY_HANDSHAKE_RESET_DATA); + resetPacket.setChannel(ForgeUtil.FORGE_LEGACY_HANDSHAKE_CHANNEL); + resetPacket.setData(ForgeUtil.FORGE_LEGACY_HANDSHAKE_RESET_DATA); connection.write(resetPacket); connection.setCanSendLegacyFMLResetPacket(false); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeConstants.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeConstants.java new file mode 100644 index 000000000..fc69eafe4 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeConstants.java @@ -0,0 +1,4 @@ +package com.velocitypowered.proxy.connection.forge; + +public class ForgeConstants { +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeUtil.java new file mode 100644 index 000000000..f7f283de1 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/ForgeUtil.java @@ -0,0 +1,51 @@ +package com.velocitypowered.proxy.connection.forge; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.util.ModInfo; +import com.velocitypowered.proxy.protocol.ProtocolUtils; +import com.velocitypowered.proxy.protocol.packet.PluginMessage; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import java.util.List; +import java.util.Optional; + +public class ForgeUtil { + public static final String FORGE_LEGACY_HANDSHAKE_CHANNEL = "FML|HS"; + public static final String FORGE_LEGACY_CHANNEL = "FML"; + public static final String FORGE_MULTIPART_LEGACY_CHANNEL = "FML|MP"; + public static final byte[] FORGE_LEGACY_HANDSHAKE_RESET_DATA = new byte[] { -2, 0 }; + + private ForgeUtil() { + throw new AssertionError(); + } + + public static Optional> readModList(PluginMessage message) { + Preconditions.checkNotNull(message, "message"); + Preconditions.checkArgument(message.getChannel().equals(FORGE_LEGACY_HANDSHAKE_CHANNEL), + "message is not a FML HS plugin message"); + + ByteBuf byteBuf = Unpooled.wrappedBuffer(message.getData()); + try { + byte discriminator = byteBuf.readByte(); + + if (discriminator == 2) { + ImmutableList.Builder mods = ImmutableList.builder(); + int modCount = ProtocolUtils.readVarInt(byteBuf); + + for (int index = 0; index < modCount; index++) { + String id = ProtocolUtils.readString(byteBuf); + String version = ProtocolUtils.readString(byteBuf); + mods.add(new ModInfo.Mod(id, version)); + } + + return Optional.of(mods.build()); + } + + return Optional.empty(); + } finally { + byteBuf.release(); + } + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java index bf2f48cf9..67fc67ec9 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java @@ -80,32 +80,5 @@ public class PluginMessageUtil { newMsg.setData(rewrittenData); return newMsg; } - - public static Optional> readModList(PluginMessage message) { - Preconditions.checkNotNull(message, "message"); - Preconditions.checkArgument(message.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL), - "message is not a FML HS plugin message"); - - ByteBuf byteBuf = Unpooled.wrappedBuffer(message.getData()); - try { - byte discriminator = byteBuf.readByte(); - - if (discriminator == 2) { - ImmutableList.Builder mods = ImmutableList.builder(); - int modCount = ProtocolUtils.readVarInt(byteBuf); - - for (int index = 0; index < modCount; index++) { - String id = ProtocolUtils.readString(byteBuf); - String version = ProtocolUtils.readString(byteBuf); - mods.add(new ModInfo.Mod(id, version)); - } - - return Optional.of(mods.build()); - } - - return Optional.empty(); - } finally { - byteBuf.release(); - } - } + }