From 6b4c809c3958b21e70da6343a19ed6896723ee68 Mon Sep 17 00:00:00 2001 From: Crypnotic Date: Thu, 15 Nov 2018 02:29:58 -0600 Subject: [PATCH] Rework and expose ProtocolVersion to the API --- .../api/network/ProtocolVersion.java | 138 ++++++++++++++++++ .../api/proxy/InboundConnection.java | 4 +- .../api/proxy/server/ServerPing.java | 2 + .../proxy/connection/MinecraftConnection.java | 16 +- .../backend/BackendPlaySessionHandler.java | 4 +- .../backend/VelocityServerConnection.java | 9 +- .../client/ClientPlaySessionHandler.java | 6 +- .../connection/client/ConnectedPlayer.java | 6 +- .../client/HandshakeSessionHandler.java | 16 +- .../client/InitialInboundConnection.java | 3 +- .../client/LoginSessionHandler.java | 6 +- .../client/StatusSessionHandler.java | 11 +- .../network/ServerChannelInitializer.java | 6 +- .../proxy/protocol/MinecraftPacket.java | 5 +- .../proxy/protocol/ProtocolConstants.java | 60 -------- .../proxy/protocol/ProtocolUtils.java | 13 ++ .../proxy/protocol/StateRegistry.java | 103 +++++++------ .../proxy/protocol/netty/GS4QueryHandler.java | 4 +- .../protocol/netty/MinecraftDecoder.java | 26 ++-- .../protocol/netty/MinecraftEncoder.java | 22 +-- .../proxy/protocol/packet/BossBar.java | 6 +- .../proxy/protocol/packet/Chat.java | 10 +- .../proxy/protocol/packet/ClientSettings.java | 10 +- .../proxy/protocol/packet/Disconnect.java | 6 +- .../protocol/packet/EncryptionRequest.java | 6 +- .../protocol/packet/EncryptionResponse.java | 6 +- .../proxy/protocol/packet/Handshake.java | 16 +- .../protocol/packet/HeaderAndFooter.java | 7 +- .../proxy/protocol/packet/JoinGame.java | 10 +- .../proxy/protocol/packet/KeepAlive.java | 14 +- .../protocol/packet/LegacyHandshake.java | 7 +- .../proxy/protocol/packet/LegacyPing.java | 7 +- .../protocol/packet/LegacyPingResponse.java | 3 +- .../protocol/packet/LoginPluginMessage.java | 6 +- .../protocol/packet/LoginPluginResponse.java | 6 +- .../proxy/protocol/packet/PlayerListItem.java | 6 +- .../proxy/protocol/packet/PluginMessage.java | 6 +- .../proxy/protocol/packet/Respawn.java | 6 +- .../proxy/protocol/packet/ServerLogin.java | 6 +- .../protocol/packet/ServerLoginSuccess.java | 6 +- .../proxy/protocol/packet/SetCompression.java | 6 +- .../proxy/protocol/packet/StatusPing.java | 7 +- .../proxy/protocol/packet/StatusRequest.java | 7 +- .../proxy/protocol/packet/StatusResponse.java | 6 +- .../protocol/packet/TabCompleteRequest.java | 12 +- .../protocol/packet/TabCompleteResponse.java | 6 +- .../proxy/protocol/packet/TitlePacket.java | 20 +-- .../protocol/util/PluginMessageUtil.java | 11 +- .../proxy/server/PingSessionHandler.java | 4 +- .../server/VelocityRegisteredServer.java | 6 +- .../proxy/util/VelocityChannelRegistrar.java | 8 +- .../proxy/protocol/PacketRegistryTest.java | 29 ++-- 52 files changed, 423 insertions(+), 309 deletions(-) create mode 100644 api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java delete mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java diff --git a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java new file mode 100644 index 000000000..d413bc8bf --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -0,0 +1,138 @@ +package com.velocitypowered.api.network; + +import com.google.common.collect.ImmutableMap; +import com.velocitypowered.api.proxy.server.ServerInfo; + +import java.util.HashMap; +import java.util.Map; + +/** + * Represents each Minecraft protocol version + */ +public enum ProtocolVersion { + UNKNOWN(-1, "Unknown"), + LEGACY(-1, "Legacy"), + MINECRAFT_1_8(47, "1.8"), + MINECRAFT_1_9(107, "1.9"), + MINECRAFT_1_9_1(108, "1.9.1"), + MINECRAFT_1_9_2(109, "1.9.2"), + MINECRAFT_1_9_4(110, "1.9.4"), + MINECRAFT_1_10(210, "1.10"), + MINECRAFT_1_11(315, "1.11"), + MINECRAFT_1_11_1(316, "1.11.1"), + MINECRAFT_1_12(335, "1.12"), + MINECRAFT_1_12_1(338, "1.12.1"), + MINECRAFT_1_12_2(340, "1.12.2"), + MINECRAFT_1_13(393, "1.13"), + MINECRAFT_1_13_1(401, "1.13.1"), + MINECRAFT_1_13_2(404, "1.13.2"); + + private final int protocol; + private final String name; + + /** + * Represents the lowest supported version + */ + public static final ProtocolVersion MINIMUM_VERSION = MINECRAFT_1_8; + /** + * Represents the highest supported version + */ + public static final ProtocolVersion MAXIMUM_VERSION = MINECRAFT_1_13_2; + + /** + * The user-friendly representation of the lowest and highest supported versions + */ + public static final String SUPPORTED_VERSION_STRING = String.format("%s-%s", MINIMUM_VERSION, MAXIMUM_VERSION); + + /** + * A map linking the protocol version number to its {@link ProtocolVersion} representation + */ + public static final ImmutableMap ID_TO_PROTOCOL_CONSTANT; + + static { + Map versions = new HashMap<>(); + for (ProtocolVersion version : values()) { + versions.put(version.protocol, version); + } + + ID_TO_PROTOCOL_CONSTANT = ImmutableMap.copyOf(versions); + } + + ProtocolVersion(int protocol, String name) { + this.protocol = protocol; + this.name = name; + } + + /** + * Returns the protocol as an int + * + * @return the protocol version + */ + public int getProtocol() { + return protocol; + } + + /** + * Returns the user-friendly name for this protocol + * + * @return the protocol name + */ + public String getName() { + return name; + } + + /** + * Gets the {@link ProtocolVersion} for the given protocol + * + * @param protocol the protocol as an int + * @return the protocol version + */ + public static ProtocolVersion getProtocolVersion(int protocol) { + return ID_TO_PROTOCOL_CONSTANT.getOrDefault(protocol, UNKNOWN); + } + + /** + * Returns whether the protocol is supported + * + * @param protocol the protocol as an int + * @return if the protocol supported + */ + public static boolean isSupported(int protocol) { + ProtocolVersion version = ID_TO_PROTOCOL_CONSTANT.get(protocol); + + return version != null && !version.isUnknown(); + } + + /** + * Returns whether the {@link ProtocolVersion} is supported + * + * @param version the protocol version + * @return if the protocol supported + */ + public static boolean isSupported(ProtocolVersion version) { + return version != null && !version.isUnknown(); + } + + /** + * Returns whether this {@link ProtocolVersion} is unknown to the proxy + * + * @return if the protocol is unknown + */ + public boolean isUnknown() { + return this == UNKNOWN; + } + + /** + * Returns whether this {@link ProtocolVersion} is a legacy protocol + * + * @return if the protocol is legacy + */ + public boolean isLegacy() { + return this == LEGACY; + } + + @Override + public String toString() { + return name; + } +} diff --git a/api/src/main/java/com/velocitypowered/api/proxy/InboundConnection.java b/api/src/main/java/com/velocitypowered/api/proxy/InboundConnection.java index 9e442ff07..74e840310 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/InboundConnection.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/InboundConnection.java @@ -1,5 +1,7 @@ package com.velocitypowered.api.proxy; +import com.velocitypowered.api.network.ProtocolVersion; + import java.net.InetSocketAddress; import java.util.Optional; @@ -34,5 +36,5 @@ public interface InboundConnection { * * @return the protocol version the connection uses */ - int getProtocolVersion(); + ProtocolVersion getProtocolVersion(); } diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java index 6a094b662..e2be628d9 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java @@ -10,6 +10,8 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.UUID; + +import com.velocitypowered.api.network.ProtocolVersion; import net.kyori.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; 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 d2c375f66..ec0e99597 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -10,13 +10,13 @@ import static com.velocitypowered.proxy.network.Connections.MINECRAFT_DECODER; import static com.velocitypowered.proxy.network.Connections.MINECRAFT_ENCODER; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.natives.compression.VelocityCompressor; import com.velocitypowered.natives.encryption.VelocityCipher; import com.velocitypowered.natives.encryption.VelocityCipherFactory; import com.velocitypowered.natives.util.Natives; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftCipherDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftCipherEncoder; @@ -53,8 +53,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { private SocketAddress remoteAddress; private StateRegistry state; private @Nullable MinecraftSessionHandler sessionHandler; - private int protocolVersion; - private int nextProtocolVersion; + private ProtocolVersion protocolVersion; + private ProtocolVersion nextProtocolVersion; private @Nullable MinecraftConnectionAssociation association; private boolean isLegacyForge; private final VelocityServer server; @@ -204,14 +204,14 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { this.channel.pipeline().get(MinecraftDecoder.class).setState(state); } - public int getProtocolVersion() { + public ProtocolVersion getProtocolVersion() { return protocolVersion; } - public void setProtocolVersion(int protocolVersion) { + public void setProtocolVersion(ProtocolVersion protocolVersion) { this.protocolVersion = protocolVersion; this.nextProtocolVersion = protocolVersion; - if (protocolVersion != ProtocolConstants.LEGACY) { + if (protocolVersion != ProtocolVersion.LEGACY) { this.channel.pipeline().get(MinecraftEncoder.class).setProtocolVersion(protocolVersion); this.channel.pipeline().get(MinecraftDecoder.class).setProtocolVersion(protocolVersion); } else { @@ -295,11 +295,11 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { this.canSendLegacyFmlResetPacket = isLegacyForge && canSendLegacyFMLResetPacket; } - public int getNextProtocolVersion() { + public ProtocolVersion getNextProtocolVersion() { return this.nextProtocolVersion; } - public void setNextProtocolVersion(int nextProtocolVersion) { + public void setNextProtocolVersion(ProtocolVersion nextProtocolVersion) { this.nextProtocolVersion = nextProtocolVersion; } } 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 3be1bfeba..3a10dfa5e 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 @@ -2,6 +2,7 @@ package com.velocitypowered.proxy.connection.backend; import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; @@ -9,7 +10,6 @@ import com.velocitypowered.proxy.connection.client.ClientPlaySessionHandler; import com.velocitypowered.proxy.connection.forge.ForgeConstants; 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.BossBar; import com.velocitypowered.proxy.protocol.packet.Disconnect; import com.velocitypowered.proxy.protocol.packet.JoinGame; @@ -171,7 +171,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { return false; } boolean minecraftOrFmlMessage; - if (mc.getProtocolVersion() <= ProtocolConstants.MINECRAFT_1_12_2) { + if (mc.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_12_2) <= 0) { String channel = message.getChannel(); minecraftOrFmlMessage = channel.startsWith("MC|") || channel .startsWith(ForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index aff20854c..2b2f73239 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -14,12 +14,13 @@ import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.server.ServerInfo; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.PlayerInfoForwarding; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder; @@ -69,9 +70,9 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE) .addLast(MINECRAFT_DECODER, - new MinecraftDecoder(ProtocolConstants.Direction.CLIENTBOUND)) + new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND)) .addLast(MINECRAFT_ENCODER, - new MinecraftEncoder(ProtocolConstants.Direction.SERVERBOUND)); + new MinecraftEncoder(ProtocolUtils.Direction.SERVERBOUND)); MinecraftConnection mc = new MinecraftConnection(ch, server); mc.setState(StateRegistry.HANDSHAKE); @@ -137,7 +138,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, handshake.setPort(registeredServer.getServerInfo().getAddress().getPort()); mc.write(handshake); - int protocolVersion = proxyPlayer.getConnection().getNextProtocolVersion(); + ProtocolVersion protocolVersion = proxyPlayer.getConnection().getNextProtocolVersion(); mc.setProtocolVersion(protocolVersion); mc.setState(StateRegistry.LOGIN); mc.write(new ServerLogin(proxyPlayer.getUsername())); 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 678a9967f..582458417 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 @@ -4,6 +4,7 @@ import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.util.ModInfo; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; @@ -11,7 +12,6 @@ import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.forge.ForgeConstants; 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.BossBar; import com.velocitypowered.proxy.protocol.packet.Chat; import com.velocitypowered.proxy.protocol.packet.ClientSettings; @@ -335,9 +335,9 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { serverBossBars.clear(); // Tell the server about this client's plugin message channels. - int serverVersion = serverMc.getProtocolVersion(); + ProtocolVersion serverVersion = serverMc.getProtocolVersion(); Collection toRegister = new HashSet<>(knownChannels); - if (serverVersion >= ProtocolConstants.MINECRAFT_1_13) { + if (serverVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { toRegister.addAll(server.getChannelRegistrar().getModernChannelIds()); } else { toRegister.addAll(server.getChannelRegistrar().getIdsForLegacyConnections()); 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 e06cdacfa..d6c92719f 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 @@ -22,6 +22,7 @@ import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.util.MessagePosition; import com.velocitypowered.api.util.ModInfo; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.util.title.TextTitle; import com.velocitypowered.api.util.title.Title; import com.velocitypowered.api.util.title.Titles; @@ -32,7 +33,6 @@ import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.forge.ForgeConstants; import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.packet.Chat; import com.velocitypowered.proxy.protocol.packet.ClientSettings; import com.velocitypowered.proxy.protocol.packet.Disconnect; @@ -164,7 +164,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } @Override - public int getProtocolVersion() { + public ProtocolVersion getProtocolVersion() { return connection.getProtocolVersion(); } @@ -176,7 +176,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { byte pos = (byte) position.ordinal(); String json; if (position == MessagePosition.ACTION_BAR) { - if (getProtocolVersion() >= ProtocolConstants.MINECRAFT_1_11) { + if (getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0) { // We can use the title packet instead. TitlePacket pkt = new TitlePacket(); pkt.setAction(TitlePacket.SET_ACTION_BAR); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java index fc92cc90e..2f52e49b0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java @@ -6,13 +6,13 @@ import com.velocitypowered.api.event.connection.ConnectionHandshakeEvent; import com.velocitypowered.api.event.proxy.ProxyPingEvent; import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.server.ServerPing; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.PlayerInfoForwarding; import com.velocitypowered.proxy.config.VelocityConfiguration; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.Disconnect; import com.velocitypowered.proxy.protocol.packet.Handshake; @@ -40,11 +40,11 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { @Override public boolean handle(LegacyPing packet) { - connection.setProtocolVersion(ProtocolConstants.LEGACY); + connection.setProtocolVersion(ProtocolVersion.LEGACY); VelocityConfiguration configuration = server.getConfiguration(); ServerPing ping = new ServerPing( - new ServerPing.Version(ProtocolConstants.MAXIMUM_GENERIC_VERSION, - "Velocity " + ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING), + new ServerPing.Version(ProtocolVersion.MAXIMUM_VERSION.getProtocol(), + "Velocity " + ProtocolVersion.SUPPORTED_VERSION_STRING), new ServerPing.Players(server.getPlayerCount(), configuration.getShowMaxPlayers(), ImmutableList.of()), configuration.getMotdComponent(), @@ -82,7 +82,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { connection.setState(StateRegistry.LOGIN); connection.setProtocolVersion(handshake.getProtocolVersion()); - if (!ProtocolConstants.isSupported(handshake.getProtocolVersion())) { + if (!ProtocolVersion.isSupported(handshake.getProtocolVersion())) { connection.closeWith(Disconnect .create(TranslatableComponent.of("multiplayer.disconnect.outdated_client"))); return true; @@ -110,7 +110,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { // If the proxy is configured for modern forwarding, we must deny connections from 1.12.2 // and lower, otherwise IP information will never get forwarded. if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN - && handshake.getProtocolVersion() < ProtocolConstants.MINECRAFT_1_13) { + && handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { connection.closeWith(Disconnect .create(TextComponent.of("This server is only compatible with 1.13 and above."))); return true; @@ -165,8 +165,8 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { } @Override - public int getProtocolVersion() { - return 0; + public ProtocolVersion getProtocolVersion() { + return ProtocolVersion.UNKNOWN; } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java index e36db5b21..c24f46591 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java @@ -1,6 +1,7 @@ package com.velocitypowered.proxy.connection.client; import com.velocitypowered.api.proxy.InboundConnection; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.protocol.packet.Handshake; import java.net.InetSocketAddress; @@ -35,7 +36,7 @@ class InitialInboundConnection implements InboundConnection { } @Override - public int getProtocolVersion() { + public ProtocolVersion getProtocolVersion() { return connection.getProtocolVersion(); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index 725415859..510bbad6a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -3,7 +3,7 @@ package com.velocitypowered.proxy.connection.client; import static com.velocitypowered.proxy.VelocityServer.GSON; import static com.velocitypowered.proxy.connection.VelocityConstants.EMPTY_BYTE_ARRAY; import static com.velocitypowered.proxy.connection.VelocityConstants.VELOCITY_IP_FORWARDING_CHANNEL; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.*; +import static com.velocitypowered.api.network.ProtocolVersion.*; import com.google.common.base.Preconditions; import com.velocitypowered.api.event.connection.LoginEvent; @@ -15,10 +15,12 @@ import com.velocitypowered.api.event.player.GameProfileRequestEvent; import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.util.GameProfile; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.PlayerInfoForwarding; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; + import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.Disconnect; import com.velocitypowered.proxy.protocol.packet.EncryptionRequest; @@ -73,7 +75,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { @Override public boolean handle(ServerLogin packet) { this.login = packet; - if (inbound.getProtocolVersion() >= MINECRAFT_1_13) { + if (inbound.getProtocolVersion().compareTo(MINECRAFT_1_13) >= 0) { playerInfoId = ThreadLocalRandom.current().nextInt(); inbound.write(new LoginPluginMessage(playerInfoId, VELOCITY_IP_FORWARDING_CHANNEL, Unpooled.EMPTY_BUFFER)); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java index d60615289..3787ed7f0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java @@ -5,11 +5,11 @@ import com.velocitypowered.api.event.proxy.ProxyPingEvent; import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.server.ServerPing; import com.velocitypowered.api.util.ModInfo; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.VelocityConfiguration; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.packet.StatusPing; import com.velocitypowered.proxy.protocol.packet.StatusRequest; import com.velocitypowered.proxy.protocol.packet.StatusResponse; @@ -38,11 +38,12 @@ public class StatusSessionHandler implements MinecraftSessionHandler { public boolean handle(StatusRequest packet) { VelocityConfiguration configuration = server.getConfiguration(); - int shownVersion = ProtocolConstants.isSupported(connection.getProtocolVersion()) - ? connection.getProtocolVersion() : ProtocolConstants.MAXIMUM_GENERIC_VERSION; + ProtocolVersion shownVersion = ProtocolVersion.isSupported(connection.getProtocolVersion()) ? connection + .getProtocolVersion() : + ProtocolVersion.MAXIMUM_VERSION; ServerPing initialPing = new ServerPing( - new ServerPing.Version(shownVersion, - "Velocity " + ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING), + new ServerPing.Version(shownVersion.getProtocol(), + "Velocity " + ProtocolVersion.SUPPORTED_VERSION_STRING), new ServerPing.Players(server.getPlayerCount(), configuration.getShowMaxPlayers(), ImmutableList.of()), configuration.getMotdComponent(), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializer.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializer.java index 6187b9426..eb8edc39e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializer.java @@ -11,7 +11,7 @@ import static com.velocitypowered.proxy.network.Connections.READ_TIMEOUT; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.client.HandshakeSessionHandler; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.LegacyPingDecoder; import com.velocitypowered.proxy.protocol.netty.LegacyPingEncoder; @@ -44,8 +44,8 @@ public class ServerChannelInitializer extends ChannelInitializer { .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) .addLast(LEGACY_PING_ENCODER, LegacyPingEncoder.INSTANCE) .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE) - .addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolConstants.Direction.SERVERBOUND)) - .addLast(MINECRAFT_ENCODER, new MinecraftEncoder(ProtocolConstants.Direction.CLIENTBOUND)); + .addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolUtils.Direction.SERVERBOUND)) + .addLast(MINECRAFT_ENCODER, new MinecraftEncoder(ProtocolUtils.Direction.CLIENTBOUND)); final MinecraftConnection connection = new MinecraftConnection(ch, this.server); connection.setState(StateRegistry.HANDSHAKE); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java index 66b37cdd1..11bed9297 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/MinecraftPacket.java @@ -1,13 +1,14 @@ package com.velocitypowered.proxy.protocol; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import io.netty.buffer.ByteBuf; public interface MinecraftPacket { - void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion); + void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion); - void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion); + void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion); boolean handle(MinecraftSessionHandler handler); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java deleted file mode 100644 index c2ad94b46..000000000 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.velocitypowered.proxy.protocol; - -import com.google.common.primitives.ImmutableIntArray; - -public enum ProtocolConstants { - ; - public static final int LEGACY = -1; - - public static final int MINECRAFT_1_8 = 47; - public static final int MINECRAFT_1_9 = 107; - public static final int MINECRAFT_1_9_1 = 108; - public static final int MINECRAFT_1_9_2 = 109; - public static final int MINECRAFT_1_9_4 = 110; - public static final int MINECRAFT_1_10 = 210; - public static final int MINECRAFT_1_11 = 315; - public static final int MINECRAFT_1_11_1 = 316; - public static final int MINECRAFT_1_12 = 335; - public static final int MINECRAFT_1_12_1 = 338; - public static final int MINECRAFT_1_12_2 = 340; - public static final int MINECRAFT_1_13 = 393; - public static final int MINECRAFT_1_13_1 = 401; - public static final int MINECRAFT_1_13_2 = 404; - - public static final int MINIMUM_GENERIC_VERSION = MINECRAFT_1_8; - public static final int MAXIMUM_GENERIC_VERSION = MINECRAFT_1_13_2; - - public static final String SUPPORTED_GENERIC_VERSION_STRING = "1.8-1.13.2"; - - public static final ImmutableIntArray SUPPORTED_VERSIONS = ImmutableIntArray.of( - MINECRAFT_1_8, - MINECRAFT_1_9, - MINECRAFT_1_9_1, - MINECRAFT_1_9_2, - MINECRAFT_1_9_4, - MINECRAFT_1_10, - MINECRAFT_1_11, - MINECRAFT_1_11_1, - MINECRAFT_1_12, - MINECRAFT_1_12_1, - MINECRAFT_1_12_2, - MINECRAFT_1_13, - MINECRAFT_1_13_1, - MINECRAFT_1_13_2 - ); - - public static boolean isSupported(int version) { - return SUPPORTED_VERSIONS.contains(version); - } - - public enum Direction { - SERVERBOUND, - CLIENTBOUND; - - public StateRegistry.PacketRegistry.ProtocolVersion getProtocol(StateRegistry state, - int protocolVersion) { - return (this == SERVERBOUND ? state.SERVERBOUND : state.CLIENTBOUND) - .getVersion(protocolVersion); - } - } -} 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 ded089bd5..8ce5a2cd7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -2,8 +2,10 @@ package com.velocitypowered.proxy.protocol; import com.google.common.base.Preconditions; import com.velocitypowered.api.util.GameProfile; +import com.velocitypowered.api.network.ProtocolVersion; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; + import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -119,4 +121,15 @@ public enum ProtocolUtils { } return properties; } + + public enum Direction { + SERVERBOUND, + CLIENTBOUND; + + public StateRegistry.PacketRegistry.ProtocolRegistry getProtocolRegistry(StateRegistry state, + ProtocolVersion protocolVersion) { + 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 498bc39ef..1c225f192 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -1,23 +1,26 @@ package com.velocitypowered.proxy.protocol; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.Direction; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_10; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_11; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_11_1; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_1; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_2; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_13; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_13_1; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_13_2; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_8; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_9; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_9_1; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_9_2; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_9_4; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINIMUM_GENERIC_VERSION; +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; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12_1; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12_2; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13_1; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13_2; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_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 com.google.common.primitives.ImmutableIntArray; +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; import com.velocitypowered.proxy.protocol.packet.ClientSettings; @@ -46,8 +49,10 @@ import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectMap; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; -import java.util.Objects; + +import java.util.*; import java.util.function.Supplier; + import org.checkerframework.checker.nullness.qual.Nullable; public enum StateRegistry { @@ -195,38 +200,40 @@ public enum StateRegistry { public static class PacketRegistry { - private static final IntObjectMap LINKED_PROTOCOL_VERSIONS = new IntObjectHashMap<>(); + private static final EnumMap> LINKED_PROTOCOL_VERSIONS = new EnumMap(ProtocolVersion.class); static { - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9, - ImmutableIntArray.of(MINECRAFT_1_9_1, MINECRAFT_1_9_2, MINECRAFT_1_9_4)); - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9_4, - ImmutableIntArray.of(MINECRAFT_1_10, MINECRAFT_1_11, MINECRAFT_1_11_1)); - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12, ImmutableIntArray.of(MINECRAFT_1_12_1)); - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12_1, ImmutableIntArray.of(MINECRAFT_1_12_2)); - LINKED_PROTOCOL_VERSIONS - .put(MINECRAFT_1_13, ImmutableIntArray.of(MINECRAFT_1_13_1, MINECRAFT_1_13_2)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9, ImmutableList.of(MINECRAFT_1_9_1, MINECRAFT_1_9_2, MINECRAFT_1_9_4)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9_4, ImmutableList.of(MINECRAFT_1_10, MINECRAFT_1_11, MINECRAFT_1_11_1)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12, ImmutableList.of(MINECRAFT_1_12_1)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12_1, ImmutableList.of(MINECRAFT_1_12_2)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_13, ImmutableList.of(MINECRAFT_1_13_1, MINECRAFT_1_13_2)); } private final Direction direction; - private final IntObjectMap versions = new IntObjectHashMap<>(16); + private final Map versions; private boolean fallback = true; PacketRegistry(Direction direction) { this.direction = direction; - ProtocolConstants.SUPPORTED_VERSIONS - .forEach(version -> versions.put(version, new ProtocolVersion(version))); + + EnumMap mutableVersions = new EnumMap(ProtocolVersion.class); + for (ProtocolVersion version : ProtocolVersion.values()) { + mutableVersions.put(version, new ProtocolRegistry(version)); + } + + this.versions = Collections.unmodifiableMap(mutableVersions); } - public ProtocolVersion getVersion(final int version) { - ProtocolVersion result = versions.get(version); - if (result == null) { + public ProtocolRegistry getProtocolRegistry(final ProtocolVersion version) { + ProtocolRegistry registry = versions.get(version); + if (registry == null) { if (fallback) { - return getVersion(MINIMUM_GENERIC_VERSION); + return getProtocolRegistry(MINIMUM_VERSION); } throw new IllegalArgumentException("Could not find data for protocol version " + version); } - return result; + return registry; } public

void register(Class

clazz, Supplier

packetSupplier, @@ -236,20 +243,20 @@ public enum StateRegistry { } for (final PacketMapping mapping : mappings) { - ProtocolVersion version = this.versions.get(mapping.protocolVersion); - if (version == null) { + ProtocolRegistry registry = this.versions.get(mapping.protocolVersion); + if (registry == null) { throw new IllegalArgumentException("Unknown protocol version " + mapping.protocolVersion); } if (!mapping.encodeOnly) { - version.packetIdToSupplier.put(mapping.id, packetSupplier); + registry.packetIdToSupplier.put(mapping.id, packetSupplier); } - version.packetClassToId.put(clazz, mapping.id); + registry.packetClassToId.put(clazz, mapping.id); - ImmutableIntArray linked = LINKED_PROTOCOL_VERSIONS.get(mapping.protocolVersion); + ImmutableList linked = LINKED_PROTOCOL_VERSIONS.get(mapping.protocolVersion); if (linked != null) { links: - for (int i = 0; i < linked.length(); i++) { - int linkedVersion = linked.get(i); + for (int i = 0; i < linked.size(); i++) { + ProtocolVersion linkedVersion = linked.get(i); // Make sure that later mappings override this one. for (PacketMapping m : mappings) { if (linkedVersion == m.protocolVersion) { @@ -262,15 +269,15 @@ public enum StateRegistry { } } - public class ProtocolVersion { + public class ProtocolRegistry { - public final int version; + public final ProtocolVersion version; final IntObjectMap> packetIdToSupplier = new IntObjectHashMap<>(16, 0.5f); final Object2IntMap> packetClassToId = new Object2IntOpenHashMap<>(16, 0.5f); - ProtocolVersion(final int version) { + ProtocolRegistry(final ProtocolVersion version) { this.version = version; this.packetClassToId.defaultReturnValue(Integer.MIN_VALUE); } @@ -299,10 +306,10 @@ public enum StateRegistry { public static class PacketMapping { private final int id; - private final int protocolVersion; + private final ProtocolVersion protocolVersion; private final boolean encodeOnly; - public PacketMapping(int id, int protocolVersion, boolean packetDecoding) { + public PacketMapping(int id, ProtocolVersion protocolVersion, boolean packetDecoding) { this.id = id; this.protocolVersion = protocolVersion; this.encodeOnly = packetDecoding; @@ -345,7 +352,7 @@ public enum StateRegistry { * @param encodeOnly When true packet decoding will be disabled * @return PacketMapping with the provided arguments */ - private static PacketMapping map(int id, int version, boolean encodeOnly) { + private static PacketMapping map(int id, ProtocolVersion version, boolean encodeOnly) { return new PacketMapping(id, version, encodeOnly); } 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 f329b7f31..c4cd9e686 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 @@ -10,8 +10,8 @@ import com.velocitypowered.api.event.query.ProxyQueryEvent; 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 com.velocitypowered.proxy.protocol.ProtocolConstants; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -73,7 +73,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler return QueryResponse.builder() .hostname(ComponentSerializers.PLAIN .serialize(server.getConfiguration().getMotdComponent())) - .gameVersion(ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING) + .gameVersion(ProtocolVersion.SUPPORTED_VERSION_STRING) .map(server.getConfiguration().getQueryMap()) .currentPlayers(server.getPlayerCount()) .maxPlayers(server.getConfiguration().getShowMaxPlayers()) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java index 22a266a9b..90532d0ec 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.netty; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import io.netty.buffer.ByteBuf; @@ -13,14 +13,14 @@ import java.util.List; public class MinecraftDecoder extends MessageToMessageDecoder { - private final ProtocolConstants.Direction direction; + private final ProtocolUtils.Direction direction; private StateRegistry state; - private StateRegistry.PacketRegistry.ProtocolVersion protocolVersion; + private StateRegistry.PacketRegistry.ProtocolRegistry registry; - public MinecraftDecoder(ProtocolConstants.Direction direction) { + public MinecraftDecoder(ProtocolUtils.Direction direction) { this.direction = Preconditions.checkNotNull(direction, "direction"); - this.protocolVersion = direction - .getProtocol(StateRegistry.HANDSHAKE, ProtocolConstants.MINIMUM_GENERIC_VERSION); + this.registry = direction + .getProtocolRegistry(StateRegistry.HANDSHAKE, ProtocolVersion.MINIMUM_VERSION); this.state = StateRegistry.HANDSHAKE; } @@ -33,35 +33,35 @@ public class MinecraftDecoder extends MessageToMessageDecoder { ByteBuf slice = msg.slice(); int packetId = ProtocolUtils.readVarInt(msg); - MinecraftPacket packet = this.protocolVersion.createPacket(packetId); + MinecraftPacket packet = this.registry.createPacket(packetId); if (packet == null) { msg.skipBytes(msg.readableBytes()); out.add(slice.retain()); } else { try { - packet.decode(msg, direction, protocolVersion.version); + packet.decode(msg, direction, registry.version); } catch (Exception e) { throw new CorruptedFrameException( "Error decoding " + packet.getClass() + " Direction " + direction - + " Protocol " + protocolVersion.version + " State " + state + " ID " + Integer + + " Protocol " + registry.version + " State " + state + " ID " + Integer .toHexString(packetId), e); } if (msg.isReadable()) { throw new CorruptedFrameException( "Did not read full packet for " + packet.getClass() + " Direction " + direction - + " Protocol " + protocolVersion.version + " State " + state + " ID " + Integer + + " Protocol " + registry.version + " State " + state + " ID " + Integer .toHexString(packetId)); } out.add(packet); } } - public void setProtocolVersion(int protocolVersion) { - this.protocolVersion = direction.getProtocol(state, protocolVersion); + public void setProtocolVersion(ProtocolVersion protocolVersion) { + this.registry = direction.getProtocolRegistry(state, protocolVersion); } public void setState(StateRegistry state) { this.state = state; - this.setProtocolVersion(protocolVersion.version); + this.setProtocolVersion(registry.version); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java index e941f2fab..d0caaac40 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.netty; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import io.netty.buffer.ByteBuf; @@ -11,30 +11,30 @@ import io.netty.handler.codec.MessageToByteEncoder; public class MinecraftEncoder extends MessageToByteEncoder { - private final ProtocolConstants.Direction direction; + private final ProtocolUtils.Direction direction; private StateRegistry state; - private StateRegistry.PacketRegistry.ProtocolVersion protocolVersion; + private StateRegistry.PacketRegistry.ProtocolRegistry registry; - public MinecraftEncoder(ProtocolConstants.Direction direction) { + public MinecraftEncoder(ProtocolUtils.Direction direction) { this.direction = Preconditions.checkNotNull(direction, "direction"); - this.protocolVersion = direction - .getProtocol(StateRegistry.HANDSHAKE, ProtocolConstants.MINIMUM_GENERIC_VERSION); + this.registry = direction + .getProtocolRegistry(StateRegistry.HANDSHAKE, ProtocolVersion.MINIMUM_VERSION); this.state = StateRegistry.HANDSHAKE; } @Override protected void encode(ChannelHandlerContext ctx, MinecraftPacket msg, ByteBuf out) { - int packetId = this.protocolVersion.getPacketId(msg); + int packetId = this.registry.getPacketId(msg); ProtocolUtils.writeVarInt(out, packetId); - msg.encode(out, direction, protocolVersion.version); + msg.encode(out, direction, registry.version); } - public void setProtocolVersion(final int protocolVersion) { - this.protocolVersion = direction.getProtocol(state, protocolVersion); + public void setProtocolVersion(final ProtocolVersion protocolVersion) { + this.registry = direction.getProtocolRegistry(state, protocolVersion); } public void setState(StateRegistry state) { this.state = state; - this.setProtocolVersion(protocolVersion.version); + this.setProtocolVersion(registry.version); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java index 7b3b60a18..aa2942a54 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBar.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import java.util.UUID; @@ -97,7 +97,7 @@ public class BossBar implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.uuid = ProtocolUtils.readUuid(buf); this.action = ProtocolUtils.readVarInt(buf); switch (action) { @@ -129,7 +129,7 @@ public class BossBar implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (uuid == null) { throw new IllegalStateException("No boss bar UUID specified"); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java index bffbd1408..330eee6cc 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java @@ -1,9 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import net.kyori.text.Component; @@ -54,20 +54,20 @@ public class Chat implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { message = ProtocolUtils.readString(buf); - if (direction == ProtocolConstants.Direction.CLIENTBOUND) { + if (direction == ProtocolUtils.Direction.CLIENTBOUND) { type = buf.readByte(); } } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (message == null) { throw new IllegalStateException("Message is not specified"); } ProtocolUtils.writeString(buf, message); - if (direction == ProtocolConstants.Direction.CLIENTBOUND) { + if (direction == ProtocolUtils.Direction.CLIENTBOUND) { buf.writeByte(type); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettings.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettings.java index 966dd1169..523fa29a6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettings.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettings.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; @@ -93,20 +93,20 @@ public class ClientSettings implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.locale = ProtocolUtils.readString(buf, 16); this.viewDistance = buf.readByte(); this.chatVisibility = ProtocolUtils.readVarInt(buf); this.chatColors = buf.readBoolean(); this.skinParts = buf.readUnsignedByte(); - if (protocolVersion >= ProtocolConstants.MINECRAFT_1_9) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { this.mainHand = ProtocolUtils.readVarInt(buf); } } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (locale == null) { throw new IllegalStateException("No locale specified"); } @@ -116,7 +116,7 @@ public class ClientSettings implements MinecraftPacket { buf.writeBoolean(chatColors); buf.writeByte(skinParts); - if (protocolVersion >= ProtocolConstants.MINECRAFT_1_9) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { ProtocolUtils.writeVarInt(buf, mainHand); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java index 40ad1644c..4c6e30ced 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Disconnect.java @@ -1,9 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import net.kyori.text.Component; @@ -40,12 +40,12 @@ public class Disconnect implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { reason = ProtocolUtils.readString(buf); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (reason == null) { throw new IllegalStateException("No reason specified."); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequest.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequest.java index 1773817ff..dce6da81e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequest.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequest.java @@ -2,9 +2,9 @@ package com.velocitypowered.proxy.protocol.packet; import static com.velocitypowered.proxy.connection.VelocityConstants.EMPTY_BYTE_ARRAY; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import java.util.Arrays; @@ -40,14 +40,14 @@ public class EncryptionRequest implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.serverId = ProtocolUtils.readString(buf, 20); publicKey = ProtocolUtils.readByteArray(buf, 256); verifyToken = ProtocolUtils.readByteArray(buf, 16); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeString(buf, this.serverId); ProtocolUtils.writeByteArray(buf, publicKey); ProtocolUtils.writeByteArray(buf, verifyToken); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponse.java index 8ad106793..5fca09560 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponse.java @@ -2,9 +2,9 @@ package com.velocitypowered.proxy.protocol.packet; import static com.velocitypowered.proxy.connection.VelocityConstants.EMPTY_BYTE_ARRAY; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import java.util.Arrays; @@ -39,13 +39,13 @@ public class EncryptionResponse implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.sharedSecret = ProtocolUtils.readByteArray(buf, 256); this.verifyToken = ProtocolUtils.readByteArray(buf, 128); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeByteArray(buf, sharedSecret); ProtocolUtils.writeByteArray(buf, verifyToken); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java index 1ab85b002..515bf27b2 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java @@ -1,23 +1,23 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; public class Handshake implements MinecraftPacket { - private int protocolVersion; + private ProtocolVersion protocolVersion; private String serverAddress = ""; private int port; private int nextStatus; - public int getProtocolVersion() { + public ProtocolVersion getProtocolVersion() { return protocolVersion; } - public void setProtocolVersion(int protocolVersion) { + public void setProtocolVersion(ProtocolVersion protocolVersion) { this.protocolVersion = protocolVersion; } @@ -56,16 +56,16 @@ public class Handshake implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - this.protocolVersion = ProtocolUtils.readVarInt(buf); + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + this.protocolVersion = ProtocolVersion.getProtocolVersion(ProtocolUtils.readVarInt(buf)); this.serverAddress = ProtocolUtils.readString(buf); this.port = buf.readUnsignedShort(); this.nextStatus = ProtocolUtils.readVarInt(buf); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - ProtocolUtils.writeVarInt(buf, this.protocolVersion); + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + ProtocolUtils.writeVarInt(buf, this.protocolVersion.getProtocol()); ProtocolUtils.writeString(buf, this.serverAddress); buf.writeShort(this.port); ProtocolUtils.writeVarInt(buf, this.nextStatus); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java index bde4bff8a..c9a7b3006 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooter.java @@ -3,9 +3,10 @@ package com.velocitypowered.proxy.protocol.packet; import static com.velocitypowered.proxy.protocol.ProtocolUtils.writeString; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants.Direction; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializer; @@ -37,12 +38,12 @@ public class HeaderAndFooter implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { throw new UnsupportedOperationException("Decode is not implemented"); } @Override - public void encode(ByteBuf buf, Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { writeString(buf, header); writeString(buf, footer); } 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 3440536bb..f37a9110a 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 @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; @@ -90,10 +90,10 @@ public class JoinGame implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.entityId = buf.readInt(); this.gamemode = buf.readUnsignedByte(); - if (protocolVersion >= ProtocolConstants.MINECRAFT_1_9_1) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { this.dimension = buf.readInt(); } else { this.dimension = buf.readByte(); @@ -105,10 +105,10 @@ public class JoinGame implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { buf.writeInt(entityId); buf.writeByte(gamemode); - if (protocolVersion >= ProtocolConstants.MINECRAFT_1_9_1) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { buf.writeInt(dimension); } else { buf.writeByte(dimension); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java index bb9465122..69e161562 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java @@ -1,10 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_2; - +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; @@ -28,17 +26,17 @@ public class KeepAlive implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - if (protocolVersion >= MINECRAFT_1_12_2) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { randomId = buf.readLong(); } else { randomId = ProtocolUtils.readVarInt(buf); } - } +} @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - if (protocolVersion >= MINECRAFT_1_12_2) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { buf.writeLong(randomId); } else { ProtocolUtils.writeVarInt(buf, (int) randomId); 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 91a11744f..79797c28b 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 @@ -1,19 +1,20 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; public class LegacyHandshake implements MinecraftPacket { @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { throw new UnsupportedOperationException(); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { 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 3d07399fd..abeef2c98 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 @@ -1,19 +1,20 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; public class LegacyPing implements MinecraftPacket { @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { throw new UnsupportedOperationException(); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { throw new UnsupportedOperationException(); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPingResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPingResponse.java index d1bd3235c..2017c053c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPingResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPingResponse.java @@ -2,6 +2,7 @@ package com.velocitypowered.proxy.protocol.packet; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.proxy.server.ServerPing; +import com.velocitypowered.api.network.ProtocolVersion; import net.kyori.text.serializer.ComponentSerializers; public class LegacyPingResponse { @@ -15,7 +16,7 @@ public class LegacyPingResponse { private final int playersMax; public LegacyPingResponse(int protocolVersion, String serverVersion, String motd, - int playersOnline, int playersMax) { + int playersOnline, int playersMax) { this.protocolVersion = protocolVersion; this.serverVersion = serverVersion; this.motd = motd; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessage.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessage.java index 0ae7db44f..25e67e9d8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessage.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessage.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -49,7 +49,7 @@ public class LoginPluginMessage implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.id = ProtocolUtils.readVarInt(buf); this.channel = ProtocolUtils.readString(buf); if (buf.isReadable()) { @@ -60,7 +60,7 @@ public class LoginPluginMessage implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeVarInt(buf, id); if (channel == null) { throw new IllegalStateException("Channel is not specified!"); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java index f2ef0fb64..c37b6cddf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -47,7 +47,7 @@ public class LoginPluginResponse implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.id = ProtocolUtils.readVarInt(buf); this.success = buf.readBoolean(); if (buf.isReadable()) { @@ -58,7 +58,7 @@ public class LoginPluginResponse implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeVarInt(buf, id); buf.writeBoolean(success); buf.writeBytes(data); 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 945d7ac11..1b8ee3769 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 @@ -3,9 +3,9 @@ package com.velocitypowered.proxy.protocol.packet; import com.google.common.collect.ImmutableList; 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.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import java.util.ArrayList; @@ -42,7 +42,7 @@ public class PlayerListItem implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { action = ProtocolUtils.readVarInt(buf); int length = ProtocolUtils.readVarInt(buf); @@ -84,7 +84,7 @@ public class PlayerListItem implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeVarInt(buf, action); ProtocolUtils.writeVarInt(buf, items.size()); for (Item item : items) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java index ba0940bdc..26fea4d39 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessage.java @@ -2,9 +2,9 @@ package com.velocitypowered.proxy.protocol.packet; import static com.velocitypowered.proxy.connection.VelocityConstants.EMPTY_BYTE_ARRAY; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; @@ -43,14 +43,14 @@ public class PluginMessage implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.channel = ProtocolUtils.readString(buf); this.data = new byte[buf.readableBytes()]; buf.readBytes(data); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (channel == null) { throw new IllegalStateException("Channel is not specified."); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java index e8b9535b5..3cab31bcd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; @@ -66,7 +66,7 @@ public class Respawn implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.dimension = buf.readInt(); this.difficulty = buf.readUnsignedByte(); this.gamemode = buf.readUnsignedByte(); @@ -74,7 +74,7 @@ public class Respawn implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { buf.writeInt(dimension); buf.writeByte(difficulty); buf.writeByte(gamemode); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLogin.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLogin.java index e773db7f6..18576c4a1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLogin.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLogin.java @@ -1,9 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; import com.google.common.base.Preconditions; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; @@ -34,12 +34,12 @@ public class ServerLogin implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { username = ProtocolUtils.readString(buf, 16); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (username == null) { throw new IllegalStateException("No username found!"); } 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 333ac3d58..cb297e5f7 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,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import java.util.UUID; @@ -44,13 +44,13 @@ public class ServerLoginSuccess implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { uuid = UUID.fromString(ProtocolUtils.readString(buf, 36)); username = ProtocolUtils.readString(buf, 16); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (uuid == null) { throw new IllegalStateException("No UUID specified!"); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompression.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompression.java index 9f2e2c693..003951433 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompression.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompression.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; @@ -33,12 +33,12 @@ public class SetCompression implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.threshold = ProtocolUtils.readVarInt(buf); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeVarInt(buf, threshold); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPing.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPing.java index 48c9475ce..c2ac1815c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPing.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPing.java @@ -1,8 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; public class StatusPing implements MinecraftPacket { @@ -10,12 +11,12 @@ public class StatusPing implements MinecraftPacket { private long randomId; @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { randomId = buf.readLong(); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { buf.writeLong(randomId); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequest.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequest.java index aa642d9b3..ddd1eece8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequest.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequest.java @@ -1,8 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; public class StatusRequest implements MinecraftPacket { @@ -14,12 +15,12 @@ public class StatusRequest implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { // There is no additional data to decode. } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { // There is no data to decode. } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java index 1e471f884..f49b4b32c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponse.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; @@ -33,12 +33,12 @@ public class StatusResponse implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { status = ProtocolUtils.readString(buf, Short.MAX_VALUE); } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (status == null) { throw new IllegalStateException("Status is not specified"); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequest.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequest.java index 2241d32fd..833b559af 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequest.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequest.java @@ -1,10 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_9; - +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; @@ -62,9 +60,9 @@ public class TabCompleteRequest implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { this.command = ProtocolUtils.readString(buf); - if (protocolVersion >= MINECRAFT_1_9) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { this.assumeCommand = buf.readBoolean(); } this.hasPosition = buf.readBoolean(); @@ -74,12 +72,12 @@ public class TabCompleteRequest implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { if (command == null) { throw new IllegalStateException("Command is not specified"); } ProtocolUtils.writeString(buf, command); - if (protocolVersion >= MINECRAFT_1_9) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { buf.writeBoolean(assumeCommand); } buf.writeBoolean(hasPosition); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponse.java index 6e24fcfc8..eae1c8d11 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponse.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import java.util.ArrayList; @@ -24,7 +24,7 @@ public class TabCompleteResponse implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { int offersAvailable = ProtocolUtils.readVarInt(buf); for (int i = 0; i < offersAvailable; i++) { offers.add(ProtocolUtils.readString(buf)); @@ -32,7 +32,7 @@ public class TabCompleteResponse implements MinecraftPacket { } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeVarInt(buf, offers.size()); for (String offer : offers) { ProtocolUtils.writeString(buf, offer); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java index b353a27fd..2aa7baa38 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java @@ -1,8 +1,8 @@ package com.velocitypowered.proxy.protocol.packet; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; @@ -26,14 +26,14 @@ public class TitlePacket implements MinecraftPacket { private int fadeOut; @Override - public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { throw new UnsupportedOperationException(); // encode only } @Override - public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { ProtocolUtils.writeVarInt(buf, action); - if (protocolVersion >= ProtocolConstants.MINECRAFT_1_11) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0) { // 1.11+ shifted the action enum by 1 to handle the action bar switch (action) { case SET_TITLE: @@ -118,23 +118,23 @@ public class TitlePacket implements MinecraftPacket { this.fadeOut = fadeOut; } - public static TitlePacket hideForProtocolVersion(int protocolVersion) { + public static TitlePacket hideForProtocolVersion(ProtocolVersion protocolVersion) { TitlePacket packet = new TitlePacket(); - packet.setAction(protocolVersion >= ProtocolConstants.MINECRAFT_1_11 ? TitlePacket.HIDE + packet.setAction(protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0 ? TitlePacket.HIDE : TitlePacket.HIDE_OLD); return packet; } - public static TitlePacket resetForProtocolVersion(int protocolVersion) { + public static TitlePacket resetForProtocolVersion(ProtocolVersion protocolVersion) { TitlePacket packet = new TitlePacket(); - packet.setAction(protocolVersion >= ProtocolConstants.MINECRAFT_1_11 ? TitlePacket.RESET + packet.setAction(protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0 ? TitlePacket.RESET : TitlePacket.RESET_OLD); return packet; } - public static TitlePacket timesForProtocolVersion(int protocolVersion) { + public static TitlePacket timesForProtocolVersion(ProtocolVersion protocolVersion) { TitlePacket packet = new TitlePacket(); - packet.setAction(protocolVersion >= ProtocolConstants.MINECRAFT_1_11 ? TitlePacket.SET_TIMES + packet.setAction(protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0 ? TitlePacket.SET_TIMES : TitlePacket.SET_TIMES_OLD); return packet; } 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 12b776a1f..efb9d27a3 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 @@ -6,7 +6,7 @@ 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.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.PluginMessage; import io.netty.buffer.ByteBuf; @@ -80,10 +80,11 @@ public class PluginMessageUtil { * @param channels the channels to register * @return the plugin message to send */ - public static PluginMessage constructChannelsPacket(int protocolVersion, - Collection channels) { - checkNotNull(channels, "channels"); - String channelName = protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ? REGISTER_CHANNEL + + public static PluginMessage constructChannelsPacket(ProtocolVersion protocolVersion, + Collection channels) { + Preconditions.checkNotNull(channels, "channels"); + String channelName = protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0 ? REGISTER_CHANNEL : REGISTER_CHANNEL_LEGACY; PluginMessage message = new PluginMessage(); message.setChannel(channelName); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java index 2d93dc973..766a01b7d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java @@ -2,10 +2,10 @@ package com.velocitypowered.proxy.server; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.ServerPing; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.Handshake; import com.velocitypowered.proxy.protocol.packet.StatusRequest; @@ -33,7 +33,7 @@ public class PingSessionHandler implements MinecraftSessionHandler { handshake.setNextStatus(StateRegistry.STATUS_ID); handshake.setServerAddress(server.getServerInfo().getAddress().getHostString()); handshake.setPort(server.getServerInfo().getAddress().getPort()); - handshake.setProtocolVersion(ProtocolConstants.MINIMUM_GENERIC_VERSION); + handshake.setProtocolVersion(ProtocolVersion.MINIMUM_VERSION); connection.write(handshake); connection.setState(StateRegistry.STATUS); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java index d24cab559..4ea18802e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java @@ -18,7 +18,7 @@ import com.velocitypowered.api.proxy.server.ServerPing; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; -import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder; @@ -74,9 +74,9 @@ public class VelocityRegisteredServer implements RegisteredServer { .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE) .addLast(MINECRAFT_DECODER, - new MinecraftDecoder(ProtocolConstants.Direction.CLIENTBOUND)) + new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND)) .addLast(MINECRAFT_ENCODER, - new MinecraftEncoder(ProtocolConstants.Direction.SERVERBOUND)); + new MinecraftEncoder(ProtocolUtils.Direction.SERVERBOUND)); MinecraftConnection connection = new MinecraftConnection(ch, server); connection.setState(StateRegistry.HANDSHAKE); 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 abf47bf30..3184dc323 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java @@ -6,11 +6,13 @@ import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.messages.ChannelRegistrar; import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; -import com.velocitypowered.proxy.protocol.ProtocolConstants; + import java.util.ArrayList; 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 { @@ -70,8 +72,8 @@ public class VelocityChannelRegistrar implements ChannelRegistrar { return identifierMap.get(id); } - public Collection getChannelsForProtocol(int protocolVersion) { - if (protocolVersion >= ProtocolConstants.MINECRAFT_1_13) { + public Collection getChannelsForProtocol(ProtocolVersion protocolVersion) { + if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { return getModernChannelIds(); } return getIdsForLegacyConnections(); diff --git a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java index 52c02e73a..b943bd31a 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java @@ -1,12 +1,13 @@ package com.velocitypowered.proxy.protocol; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_1; -import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_2; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12_1; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12_2; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.packet.Handshake; import org.junit.jupiter.api.Test; @@ -14,7 +15,7 @@ class PacketRegistryTest { private StateRegistry.PacketRegistry setupRegistry() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolConstants.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false)); return registry; @@ -23,46 +24,46 @@ class PacketRegistryTest { @Test void packetRegistryWorks() { StateRegistry.PacketRegistry registry = setupRegistry(); - MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12).createPacket(0); + MinecraftPacket packet = registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0); assertNotNull(packet, "Packet was not found in registry"); assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class"); - assertEquals(0, registry.getVersion(MINECRAFT_1_12).getPacketId(packet), + assertEquals(0, registry.getProtocolRegistry(MINECRAFT_1_12).getPacketId(packet), "Registry did not return the correct packet ID"); } @Test void packetRegistryLinkingWorks() { StateRegistry.PacketRegistry registry = setupRegistry(); - MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12_1).createPacket(0); + MinecraftPacket packet = registry.getProtocolRegistry(MINECRAFT_1_12_1).createPacket(0); assertNotNull(packet, "Packet was not found in registry"); assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class"); - assertEquals(0, registry.getVersion(MINECRAFT_1_12_1).getPacketId(packet), + assertEquals(0, registry.getProtocolRegistry(MINECRAFT_1_12_1).getPacketId(packet), "Registry did not return the correct packet ID"); } @Test void failOnNoMappings() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolConstants.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND); assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new)); assertThrows(IllegalArgumentException.class, - () -> registry.getVersion(0).getPacketId(new Handshake())); + () -> registry.getProtocolRegistry(ProtocolVersion.UNKNOWN).getPacketId(new Handshake())); } @Test void registrySuppliesCorrectPacketsByProtocol() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - ProtocolConstants.Direction.CLIENTBOUND); + ProtocolUtils.Direction.CLIENTBOUND); registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false), new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, false)); assertEquals(Handshake.class, - registry.getVersion(MINECRAFT_1_12).createPacket(0x00).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0x00).getClass()); assertEquals(Handshake.class, - registry.getVersion(MINECRAFT_1_12_1).createPacket(0x01).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_12_1).createPacket(0x01).getClass()); assertEquals(Handshake.class, - registry.getVersion(MINECRAFT_1_12_2).createPacket(0x01).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_12_2).createPacket(0x01).getClass()); } } \ No newline at end of file