diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java index 603620000..d5c9f7a85 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java @@ -28,6 +28,10 @@ public class VelocityCommand implements Command { private final Map subcommands; + /** + * Initializes the command object for /velocity. + * @param server the Velocity server + */ public VelocityCommand(ProxyServer server) { this.subcommands = ImmutableMap.builder() .put("version", new Info(server)) 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 5bcc76653..3be1bfeba 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 @@ -88,8 +88,9 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { return true; } - if (PluginMessageUtil.isMCBrand(packet)) { - serverConn.getPlayer().getConnection().write(PluginMessageUtil.rewriteMinecraftBrand(packet)); + if (PluginMessageUtil.isMcBrand(packet)) { + PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()); + serverConn.getPlayer().getConnection().write(rewritten); return true; } 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 bdf494fc1..55bf24485 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 @@ -160,7 +160,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { VelocityServerConnection serverConn = player.getConnectedServer(); MinecraftConnection backendConn = serverConn != null ? serverConn.getConnection() : null; if (serverConn != null && backendConn != null) { - if (PluginMessageUtil.isMCRegister(packet)) { + if (PluginMessageUtil.isRegister(packet)) { List actuallyRegistered = new ArrayList<>(); List channels = PluginMessageUtil.getChannels(packet); for (String channel : channels) { @@ -177,12 +177,13 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { .getProtocolVersion(), actuallyRegistered); backendConn.write(newRegisterPacket); } - } else if (PluginMessageUtil.isMCUnregister(packet)) { + } else if (PluginMessageUtil.isUnregister(packet)) { List channels = PluginMessageUtil.getChannels(packet); knownChannels.removeAll(channels); backendConn.write(packet); - } else if (PluginMessageUtil.isMCBrand(packet)) { - backendConn.write(PluginMessageUtil.rewriteMinecraftBrand(packet)); + } else if (PluginMessageUtil.isMcBrand(packet)) { + PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()); + backendConn.write(rewritten); } else if (backendConn.isLegacyForge() && !serverConn.hasCompletedJoin()) { if (packet.getChannel().equals(ForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { if (!player.getModInfo().isPresent()) { 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 9b38ac8d9..12b776a1f 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 @@ -1,7 +1,11 @@ package com.velocitypowered.proxy.protocol.util; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.util.ProxyVersion; import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.PluginMessage; @@ -24,36 +28,61 @@ public class PluginMessageUtil { throw new AssertionError(); } - public static boolean isMCBrand(PluginMessage message) { - Preconditions.checkNotNull(message, "message"); + /** + * Determines whether or not this is a brand plugin message. This is shown on the client. + * @param message the plugin message + * @return whether or not this is a brand plugin message + */ + public static boolean isMcBrand(PluginMessage message) { + checkNotNull(message, "message"); return message.getChannel().equals(BRAND_CHANNEL_LEGACY) || message.getChannel() .equals(BRAND_CHANNEL); } - public static boolean isMCRegister(PluginMessage message) { - Preconditions.checkNotNull(message, "message"); + /** + * Determines whether or not this plugin message is being used to register plugin channels. + * @param message the plugin message + * @return whether we are registering plugin channels or not + */ + public static boolean isRegister(PluginMessage message) { + checkNotNull(message, "message"); return message.getChannel().equals(REGISTER_CHANNEL_LEGACY) || message.getChannel() .equals(REGISTER_CHANNEL); } - public static boolean isMCUnregister(PluginMessage message) { - Preconditions.checkNotNull(message, "message"); + /** + * Determines whether or not this plugin message is being used to unregister plugin channels. + * @param message the plugin message + * @return whether we are unregistering plugin channels or not + */ + public static boolean isUnregister(PluginMessage message) { + checkNotNull(message, "message"); return message.getChannel().equals(UNREGISTER_CHANNEL_LEGACY) || message.getChannel() .equals(UNREGISTER_CHANNEL); } + /** + * Fetches all the channels in a register or unregister plugin message. + * @param message the message to get the channels from + * @return the channels, as an immutable list + */ public static List getChannels(PluginMessage message) { - Preconditions.checkNotNull(message, "message"); - Preconditions - .checkArgument(isMCRegister(message) || isMCUnregister(message), "Unknown channel type %s", + checkNotNull(message, "message"); + checkArgument(isRegister(message) || isUnregister(message), "Unknown channel type %s", message.getChannel()); String channels = new String(message.getData(), StandardCharsets.UTF_8); return ImmutableList.copyOf(channels.split("\0")); } + /** + * Constructs a channel (un)register packet. + * @param protocolVersion the client/server's protocol version + * @param channels the channels to register + * @return the plugin message to send + */ public static PluginMessage constructChannelsPacket(int protocolVersion, Collection channels) { - Preconditions.checkNotNull(channels, "channels"); + checkNotNull(channels, "channels"); String channelName = protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ? REGISTER_CHANNEL : REGISTER_CHANNEL_LEGACY; PluginMessage message = new PluginMessage(); @@ -62,15 +91,24 @@ public class PluginMessageUtil { return message; } - public static PluginMessage rewriteMinecraftBrand(PluginMessage message) { - Preconditions.checkNotNull(message, "message"); - Preconditions.checkArgument(isMCBrand(message), "message is not a MC Brand plugin message"); + /** + * Rewrites the brand message to indicate the presence of Velocity. + * @param message the plugin message + * @param version the proxy version + * @return the rewritten plugin message + */ + public static PluginMessage rewriteMinecraftBrand(PluginMessage message, ProxyVersion version) { + checkNotNull(message, "message"); + checkNotNull(version, "version"); + checkArgument(isMcBrand(message), "message is not a brand plugin message"); + + String toAppend = " (" + version.getName() + ")"; byte[] rewrittenData; ByteBuf rewrittenBuf = Unpooled.buffer(); try { String currentBrand = ProtocolUtils.readString(Unpooled.wrappedBuffer(message.getData())); - ProtocolUtils.writeString(rewrittenBuf, currentBrand + " (Velocity)"); + ProtocolUtils.writeString(rewrittenBuf, currentBrand + toAppend); rewrittenData = new byte[rewrittenBuf.readableBytes()]; rewrittenBuf.readBytes(rewrittenData); } finally {