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 | 55 ++++++++++++++----- .../client/HandshakeSessionHandler.java | 2 +- .../util/ServerListPingHandler.java | 2 +- 3 files changed, 43 insertions(+), 16 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 a1d3d1b542..e124245d56 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 @@ * 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); } } @@ -192,13 +212,12 @@ public List getVersionsSupportedBy() { } /** - * Gets the {@link ProtocolVersion} for the given protocol. + * Returns whether this {@link ProtocolVersion} is supported. * - * @param protocol the protocol as an int - * @return the protocol version + * @return if the protocol supported */ - public static ProtocolVersion getProtocolVersion(int protocol) { - return ID_TO_PROTOCOL_CONSTANT.getOrDefault(protocol, UNKNOWN); + public boolean isSupported() { + return true; } /** @@ -208,9 +227,7 @@ public static ProtocolVersion getProtocolVersion(int protocol) { * @return if the protocol supported */ public static boolean isSupported(int protocol) { - ProtocolVersion version = ID_TO_PROTOCOL_CONSTANT.get(protocol); - - return version != null && !version.isUnknown(); + return getProtocolVersion(protocol).isSupported(); } /** @@ -220,7 +237,17 @@ public static boolean isSupported(int protocol) { * @return if the protocol supported */ public static boolean isSupported(ProtocolVersion version) { - return version != null && !version.isUnknown(); + return version != null && version.isSupported(); + } + + /** + * 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); } /** @@ -229,7 +256,7 @@ public static boolean isSupported(ProtocolVersion version) { * @return if the protocol is unknown */ public boolean isUnknown() { - return this == UNKNOWN; + return false; } /** @@ -238,7 +265,7 @@ public boolean isUnknown() { * @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 3948d08759..f1fc2d49f1 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 boolean handle(final HandshakePacket handshake) { } 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 d253d35945..951d609861 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 @@ private CompletableFuture attemptPingPassthrough(VelocityInboundConn */ 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();