diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index 89d88de73..2862e36ab 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -17,6 +17,11 @@ public enum ProtocolUtils { ; private static final int DEFAULT_MAX_STRING_SIZE = 65536; // 64KiB + /** + * Reads a Minecraft-style VarInt from the specified {@code buf}. + * @param buf the buffer to read from + * @return the decoded VarInt + */ public static int readVarInt(ByteBuf buf) { int i = 0; int j = 0; @@ -33,6 +38,11 @@ public enum ProtocolUtils { return i; } + /** + * Writes a Minecraft-style VarInt to the specified {@code buf}. + * @param buf the buffer to read from + * @param value the integer to write + */ public static void writeVarInt(ByteBuf buf, int value) { while (true) { if ((value & 0xFFFFFF80) == 0) { @@ -49,6 +59,13 @@ public enum ProtocolUtils { return readString(buf, DEFAULT_MAX_STRING_SIZE); } + /** + * Reads a VarInt length-prefixed string from the {@code buf}, making sure to not go over + * {@code cap} size. + * @param buf the buffer to read from + * @param cap the maximum size of the string, in UTF-8 character length + * @return the decoded string + */ public static String readString(ByteBuf buf, int cap) { int length = readVarInt(buf); checkArgument(length >= 0, "Got a negative-length string (%s)", length); @@ -140,7 +157,7 @@ public enum ProtocolUtils { public StateRegistry.PacketRegistry.ProtocolRegistry getProtocolRegistry(StateRegistry state, ProtocolVersion protocolVersion) { - return (this == SERVERBOUND ? state.SERVERBOUND : state.CLIENTBOUND) + return (this == SERVERBOUND ? state.serverbound : state.clientbound) .getProtocolRegistry(protocolVersion); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index fa7d7033d..50201daa2 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -1,6 +1,5 @@ package com.velocitypowered.proxy.protocol; -import static com.velocitypowered.proxy.protocol.ProtocolUtils.Direction; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_10; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_11; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_11_1; @@ -16,10 +15,9 @@ import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_9_1; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_9_2; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_9_4; import static com.velocitypowered.api.network.ProtocolVersion.MINIMUM_VERSION; +import static com.velocitypowered.proxy.protocol.ProtocolUtils.Direction; -import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.packet.BossBar; import com.velocitypowered.proxy.protocol.packet.Chat; @@ -59,111 +57,111 @@ public enum StateRegistry { HANDSHAKE { { - SERVERBOUND.register(Handshake.class, Handshake::new, + serverbound.register(Handshake.class, Handshake::new, genericMappings(0x00)); } }, STATUS { { - SERVERBOUND.register(StatusRequest.class, () -> StatusRequest.INSTANCE, + serverbound.register(StatusRequest.class, () -> StatusRequest.INSTANCE, genericMappings(0x00)); - SERVERBOUND.register(StatusPing.class, StatusPing::new, + serverbound.register(StatusPing.class, StatusPing::new, genericMappings(0x01)); - CLIENTBOUND.register(StatusResponse.class, StatusResponse::new, + clientbound.register(StatusResponse.class, StatusResponse::new, genericMappings(0x00)); - CLIENTBOUND.register(StatusPing.class, StatusPing::new, + clientbound.register(StatusPing.class, StatusPing::new, genericMappings(0x01)); } }, PLAY { { - SERVERBOUND.fallback = false; - CLIENTBOUND.fallback = false; + serverbound.fallback = false; + clientbound.fallback = false; - SERVERBOUND.register(TabCompleteRequest.class, TabCompleteRequest::new, + serverbound.register(TabCompleteRequest.class, TabCompleteRequest::new, map(0x14, MINECRAFT_1_8, false), map(0x01, MINECRAFT_1_9, false), map(0x02, MINECRAFT_1_12, false), map(0x01, MINECRAFT_1_12_1, false)); - SERVERBOUND.register(Chat.class, Chat::new, + serverbound.register(Chat.class, Chat::new, map(0x01, MINECRAFT_1_8, false), map(0x02, MINECRAFT_1_9, false), map(0x03, MINECRAFT_1_12, false), map(0x02, MINECRAFT_1_12_1, false), map(0x02, MINECRAFT_1_13, false)); - SERVERBOUND.register(ClientSettings.class, ClientSettings::new, + serverbound.register(ClientSettings.class, ClientSettings::new, map(0x15, MINECRAFT_1_8, false), map(0x04, MINECRAFT_1_9, false), map(0x05, MINECRAFT_1_12, false), map(0x04, MINECRAFT_1_12_1, false), map(0x04, MINECRAFT_1_13, false)); - SERVERBOUND.register(PluginMessage.class, PluginMessage::new, + serverbound.register(PluginMessage.class, PluginMessage::new, map(0x17, MINECRAFT_1_8, false), map(0x09, MINECRAFT_1_9, false), map(0x0A, MINECRAFT_1_12, false), map(0x09, MINECRAFT_1_12_1, false), map(0x0A, MINECRAFT_1_13, false)); - SERVERBOUND.register(KeepAlive.class, KeepAlive::new, + serverbound.register(KeepAlive.class, KeepAlive::new, map(0x00, MINECRAFT_1_8, false), map(0x0B, MINECRAFT_1_9, false), map(0x0C, MINECRAFT_1_12, false), map(0x0B, MINECRAFT_1_12_1, false), map(0x0E, MINECRAFT_1_13, false)); - CLIENTBOUND.register(BossBar.class, BossBar::new, + clientbound.register(BossBar.class, BossBar::new, map(0x0C, MINECRAFT_1_9, false), map(0x0C, MINECRAFT_1_12, false), map(0x0C, MINECRAFT_1_13, false)); - CLIENTBOUND.register(Chat.class, Chat::new, + clientbound.register(Chat.class, Chat::new, map(0x02, MINECRAFT_1_8, true), map(0x0F, MINECRAFT_1_9, true), map(0x0F, MINECRAFT_1_12, true), map(0x0E, MINECRAFT_1_13, true)); - CLIENTBOUND.register(TabCompleteResponse.class, TabCompleteResponse::new, + clientbound.register(TabCompleteResponse.class, TabCompleteResponse::new, map(0x3A, MINECRAFT_1_8, false), map(0x0E, MINECRAFT_1_9, false), map(0x0E, MINECRAFT_1_12, false)); - CLIENTBOUND.register(PluginMessage.class, PluginMessage::new, + clientbound.register(PluginMessage.class, PluginMessage::new, map(0x3F, MINECRAFT_1_8, false), map(0x18, MINECRAFT_1_9, false), map(0x18, MINECRAFT_1_12, false), map(0x19, MINECRAFT_1_13, false)); - CLIENTBOUND.register(Disconnect.class, Disconnect::new, + clientbound.register(Disconnect.class, Disconnect::new, map(0x40, MINECRAFT_1_8, false), map(0x1A, MINECRAFT_1_9, false), map(0x1A, MINECRAFT_1_12, false), map(0x1B, MINECRAFT_1_13, false)); - CLIENTBOUND.register(KeepAlive.class, KeepAlive::new, + clientbound.register(KeepAlive.class, KeepAlive::new, map(0x00, MINECRAFT_1_8, false), map(0x1F, MINECRAFT_1_9, false), map(0x1F, MINECRAFT_1_12, false), map(0x21, MINECRAFT_1_13, false)); - CLIENTBOUND.register(JoinGame.class, JoinGame::new, + clientbound.register(JoinGame.class, JoinGame::new, map(0x01, MINECRAFT_1_8, false), map(0x23, MINECRAFT_1_9, false), map(0x23, MINECRAFT_1_12, false), map(0x25, MINECRAFT_1_13, false)); - CLIENTBOUND.register(Respawn.class, Respawn::new, + clientbound.register(Respawn.class, Respawn::new, map(0x07, MINECRAFT_1_8, true), map(0x33, MINECRAFT_1_9, true), map(0x34, MINECRAFT_1_12, true), map(0x35, MINECRAFT_1_12_2, true), map(0x38, MINECRAFT_1_13, true)); - CLIENTBOUND.register(HeaderAndFooter.class, HeaderAndFooter::new, + clientbound.register(HeaderAndFooter.class, HeaderAndFooter::new, map(0x47, MINECRAFT_1_8, true), map(0x48, MINECRAFT_1_9, true), map(0x47, MINECRAFT_1_9_4, true), map(0x49, MINECRAFT_1_12, true), map(0x4A, MINECRAFT_1_12_1, true), map(0x4E, MINECRAFT_1_13, true)); - CLIENTBOUND.register(TitlePacket.class, TitlePacket::new, + clientbound.register(TitlePacket.class, TitlePacket::new, map(0x45, MINECRAFT_1_8, true), map(0x45, MINECRAFT_1_9, true), map(0x47, MINECRAFT_1_12, true), map(0x48, MINECRAFT_1_12_1, true), map(0x4B, MINECRAFT_1_13, true)); - CLIENTBOUND.register(PlayerListItem.class, PlayerListItem::new, + clientbound.register(PlayerListItem.class, PlayerListItem::new, map(0x38, MINECRAFT_1_8, false), map(0x2D, MINECRAFT_1_9, false), map(0x2D, MINECRAFT_1_12, false), @@ -173,30 +171,30 @@ public enum StateRegistry { }, LOGIN { { - SERVERBOUND.register(ServerLogin.class, ServerLogin::new, + serverbound.register(ServerLogin.class, ServerLogin::new, genericMappings(0x00)); - SERVERBOUND.register(EncryptionResponse.class, EncryptionResponse::new, + serverbound.register(EncryptionResponse.class, EncryptionResponse::new, genericMappings(0x01)); - SERVERBOUND.register(LoginPluginResponse.class, LoginPluginResponse::new, + serverbound.register(LoginPluginResponse.class, LoginPluginResponse::new, map(0x02, MINECRAFT_1_13, false)); - CLIENTBOUND.register(Disconnect.class, Disconnect::new, + clientbound.register(Disconnect.class, Disconnect::new, genericMappings(0x00)); - CLIENTBOUND.register(EncryptionRequest.class, EncryptionRequest::new, + clientbound.register(EncryptionRequest.class, EncryptionRequest::new, genericMappings(0x01)); - CLIENTBOUND.register(ServerLoginSuccess.class, ServerLoginSuccess::new, + clientbound.register(ServerLoginSuccess.class, ServerLoginSuccess::new, genericMappings(0x02)); - CLIENTBOUND.register(SetCompression.class, SetCompression::new, + clientbound.register(SetCompression.class, SetCompression::new, genericMappings(0x03)); - CLIENTBOUND.register(LoginPluginMessage.class, LoginPluginMessage::new, + clientbound.register(LoginPluginMessage.class, LoginPluginMessage::new, map(0x04, MINECRAFT_1_13, false)); } }; public static final int STATUS_ID = 1; public static final int LOGIN_ID = 2; - public final PacketRegistry CLIENTBOUND = new PacketRegistry(Direction.CLIENTBOUND); - public final PacketRegistry SERVERBOUND = new PacketRegistry(Direction.SERVERBOUND); + public final PacketRegistry clientbound = new PacketRegistry(Direction.CLIENTBOUND); + public final PacketRegistry serverbound = new PacketRegistry(Direction.SERVERBOUND); public static class PacketRegistry { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java index 0f6ad989f..560d0aafd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java @@ -7,10 +7,10 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.collect.ImmutableSet; import com.velocitypowered.api.event.query.ProxyQueryEvent; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.server.QueryResponse; -import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyDisconnect.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyDisconnect.java index 7437199cb..5f7146f83 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyDisconnect.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyDisconnect.java @@ -12,7 +12,7 @@ public class LegacyDisconnect { } /** - * Converts a legacy response into a disconnect packet. + * Converts a legacy response into an legacy disconnect packet. * @param response the response to convert * @return the disconnect packet */ @@ -28,8 +28,16 @@ public class LegacyDisconnect { return new LegacyDisconnect(kickMessage); } + /** + * Converts a {@link TextComponent} into a legacy disconnect packet. + * @param component the component to convert + * @return the disconnect packet + */ public static LegacyDisconnect from(TextComponent component) { - return new LegacyDisconnect(ComponentSerializers.LEGACY.serialize(component)); + // We intentionally use the legacy serializers, because the old clients can't understand JSON. + @SuppressWarnings("deprecated") + String serialized = ComponentSerializers.LEGACY.serialize(component); + return new LegacyDisconnect(serialized); } public String getReason() { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyHandshake.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyHandshake.java index 79797c28b..a7141b9e4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyHandshake.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyHandshake.java @@ -9,12 +9,12 @@ import io.netty.buffer.ByteBuf; public class LegacyHandshake implements MinecraftPacket { @Override - public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { throw new UnsupportedOperationException(); } @Override - public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { throw new UnsupportedOperationException(); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPing.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPing.java index abeef2c98..ad7559a4d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPing.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPing.java @@ -9,12 +9,12 @@ import io.netty.buffer.ByteBuf; public class LegacyPing implements MinecraftPacket { @Override - public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { throw new UnsupportedOperationException(); } @Override - public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { throw new UnsupportedOperationException(); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItem.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItem.java index cd392e4cb..439202483 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItem.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItem.java @@ -1,9 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.util.GameProfile; -import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; 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 efb9d27a3..854f75f9e 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 @@ -5,8 +5,8 @@ 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.api.network.ProtocolVersion; +import com.velocitypowered.api.util.ProxyVersion; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.PluginMessage; import io.netty.buffer.ByteBuf; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index ef9fd0b42..edb449590 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -4,7 +4,6 @@ import com.google.common.base.Preconditions; import com.velocitypowered.api.proxy.player.TabList; import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.util.GameProfile; -import com.velocitypowered.api.util.UuidUtils; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.protocol.packet.HeaderAndFooter; import com.velocitypowered.proxy.protocol.packet.PlayerListItem; @@ -87,11 +86,11 @@ public class VelocityTabList implements TabList { } public void processBackendPacket(PlayerListItem packet) { - //Packets are already forwarded on, so no need to do that here + // Packets are already forwarded on, so no need to do that here for (PlayerListItem.Item item : packet.getItems()) { UUID uuid = item.getUuid(); if (packet.getAction() != PlayerListItem.ADD_PLAYER && !entries.containsKey(uuid)) { - //Sometimes UPDATE_GAMEMODE is sent before ADD_PLAYER so don't want to warn here + // Sometimes UPDATE_GAMEMODE is sent before ADD_PLAYER so don't want to warn here continue; } @@ -136,6 +135,9 @@ public class VelocityTabList implements TabList { } break; } + default: + // Nothing we can do here + break; } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java b/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java index 3184dc323..05a38b669 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java @@ -2,6 +2,7 @@ package com.velocitypowered.proxy.util; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.messages.ChannelRegistrar; import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; @@ -12,7 +13,6 @@ import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import com.velocitypowered.api.network.ProtocolVersion; import org.checkerframework.checker.nullness.qual.Nullable; public class VelocityChannelRegistrar implements ChannelRegistrar { @@ -72,6 +72,11 @@ public class VelocityChannelRegistrar implements ChannelRegistrar { return identifierMap.get(id); } + /** + * Returns all the channel names to register depending on the Minecraft protocol version. + * @param protocolVersion the protocol version in use + * @return the list of channels to register + */ public Collection getChannelsForProtocol(ProtocolVersion protocolVersion) { if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { return getModernChannelIds();