From 82189a6a21c9c69a925cfc5ee650d0da76c2472b Mon Sep 17 00:00:00 2001 From: Maxim Breitman <89523915+maximbreitman@users.noreply.github.com> Date: Sat, 2 Mar 2024 06:42:02 +0300 Subject: [PATCH] Tiny rework of Protocol Version API (#1260) --- .../api/network/ProtocolVersion.java | 81 ++++++++++++------- .../client/HandshakeSessionHandler.java | 2 +- .../util/ServerListPingHandler.java | 2 +- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java index a1d3d1b54..e124245d5 100644 --- a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -21,8 +21,28 @@ import java.util.Set; * Represents each Minecraft protocol version. */ public enum ProtocolVersion implements Ordered { - UNKNOWN(-1, "Unknown"), - LEGACY(-2, "Legacy"), + UNKNOWN(-1, "Unknown") { + @Override + public boolean isUnknown() { + return true; + } + + @Override + public boolean isSupported() { + return false; + } + }, + LEGACY(-2, "Legacy") { + @Override + public boolean isLegacy() { + return true; + } + + @Override + public boolean isSupported() { + return false; + } + }, MINECRAFT_1_7_2(4, "1.7.2", "1.7.3", "1.7.4", "1.7.5"), MINECRAFT_1_7_6(5, @@ -118,7 +138,7 @@ public enum ProtocolVersion implements Ordered { static { Set versions = EnumSet.noneOf(ProtocolVersion.class); for (ProtocolVersion value : values()) { - if (!value.isUnknown() && !value.isLegacy()) { + if (value.isSupported()) { versions.add(value); } } @@ -191,6 +211,35 @@ public enum ProtocolVersion implements Ordered { return ImmutableList.copyOf(names); } + /** + * Returns whether this {@link ProtocolVersion} is supported. + * + * @return if the protocol supported + */ + public boolean isSupported() { + return true; + } + + /** + * Returns whether the protocol is supported. + * + * @param protocol the protocol as an int + * @return if the protocol supported + */ + public static boolean isSupported(int protocol) { + return getProtocolVersion(protocol).isSupported(); + } + + /** + * 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.isSupported(); + } + /** * Gets the {@link ProtocolVersion} for the given protocol. * @@ -201,35 +250,13 @@ public enum ProtocolVersion implements Ordered { 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; + return false; } /** @@ -238,7 +265,7 @@ public enum ProtocolVersion implements Ordered { * @return if the protocol is legacy */ public boolean isLegacy() { - return this == LEGACY; + return false; } @Override 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 3948d0875..f1fc2d49f 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 @@ -117,7 +117,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { } private void handleLogin(HandshakePacket handshake, InitialInboundConnection ic) { - if (!ProtocolVersion.isSupported(handshake.getProtocolVersion())) { + if (!handshake.getProtocolVersion().isSupported()) { // Bump connection into correct protocol state so that we can send the disconnect packet. connection.setState(StateRegistry.LOGIN); ic.disconnectQuietly(Component.translatable() diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java index d253d3594..951d60986 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java @@ -143,7 +143,7 @@ public class ServerListPingHandler { */ public CompletableFuture getInitialPing(VelocityInboundConnection connection) { VelocityConfiguration configuration = server.getConfiguration(); - ProtocolVersion shownVersion = ProtocolVersion.isSupported(connection.getProtocolVersion()) + ProtocolVersion shownVersion = connection.getProtocolVersion().isSupported() ? connection.getProtocolVersion() : ProtocolVersion.MAXIMUM_VERSION; PingPassthroughMode passthroughMode = configuration.getPingPassthrough();