From 0993ce2f8681b92155f47488b61fb6567279274f Mon Sep 17 00:00:00 2001 From: Riley Park Date: Thu, 18 Jan 2024 17:32:42 -0800 Subject: [PATCH] Improve protocol version checking (#1203) * Improve protocol version checking * chore: since 3.3.0 --- api/build.gradle.kts | 2 + .../player/PlayerResourcePackStatusEvent.java | 2 +- .../api/network/ProtocolVersion.java | 3 +- .../api/proxy/crypto/IdentifiedKey.java | 3 +- .../com/velocitypowered/api/util/Ordered.java | 93 +++++++++++++++++++ .../main/kotlin/velocity-spotless.gradle.kts | 1 + gradle/libs.versions.toml | 1 + .../velocitypowered/proxy/VelocityServer.java | 4 +- .../proxy/connection/MinecraftConnection.java | 4 +- .../backend/BungeeCordMessageResponder.java | 2 +- .../backend/LoginSessionHandler.java | 4 +- .../backend/TransitionSessionHandler.java | 2 +- .../backend/VelocityServerConnection.java | 2 +- .../connection/client/AuthSessionHandler.java | 4 +- .../client/ClientPlaySessionHandler.java | 14 +-- .../connection/client/ConnectedPlayer.java | 30 +++--- .../client/HandshakeSessionHandler.java | 8 +- .../client/InitialLoginSessionHandler.java | 4 +- .../client/LoginInboundConnection.java | 2 +- .../proxy/protocol/ProtocolUtils.java | 10 +- .../proxy/protocol/StateRegistry.java | 4 +- .../protocol/packet/ClientSettingsPacket.java | 16 ++-- .../packet/EncryptionRequestPacket.java | 4 +- .../packet/EncryptionResponsePacket.java | 20 ++-- .../proxy/protocol/packet/JoinGamePacket.java | 62 ++++++------- .../protocol/packet/KeepAlivePacket.java | 8 +- .../packet/LegacyPlayerListItemPacket.java | 8 +- .../protocol/packet/PluginMessagePacket.java | 8 +- .../packet/ResourcePackRequestPacket.java | 8 +- .../packet/ResourcePackResponsePacket.java | 8 +- .../proxy/protocol/packet/RespawnPacket.java | 44 ++++----- .../protocol/packet/ServerDataPacket.java | 18 ++-- .../protocol/packet/ServerLoginPacket.java | 22 ++--- .../packet/ServerLoginSuccessPacket.java | 16 ++-- .../packet/TabCompleteRequestPacket.java | 12 +-- .../packet/TabCompleteResponsePacket.java | 4 +- .../packet/brigadier/ArgumentIdentifier.java | 6 +- .../brigadier/ArgumentPropertyRegistry.java | 4 +- .../ModArgumentPropertySerializer.java | 2 +- .../brigadier/TimeArgumentSerializer.java | 4 +- .../protocol/packet/chat/ComponentHolder.java | 4 +- .../packet/chat/SystemChatPacket.java | 2 +- .../chat/builder/ChatBuilderFactory.java | 4 +- .../packet/chat/keyed/KeyedChatHandler.java | 4 +- .../chat/keyed/KeyedCommandHandler.java | 6 +- .../chat/keyed/KeyedPlayerChatPacket.java | 6 +- .../chat/keyed/KeyedPlayerCommandPacket.java | 4 +- .../packet/chat/legacy/LegacyChatPacket.java | 8 +- .../chat/session/SessionCommandHandler.java | 4 +- .../packet/title/GenericTitlePacket.java | 4 +- .../packet/title/LegacyTitlePacket.java | 2 +- .../protocol/util/PluginMessageUtil.java | 4 +- .../proxy/util/VelocityChannelRegistrar.java | 2 +- 53 files changed, 313 insertions(+), 214 deletions(-) create mode 100644 api/src/main/java/com/velocitypowered/api/util/Ordered.java diff --git a/api/build.gradle.kts b/api/build.gradle.kts index c6e35abca..565ab2e9d 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -18,6 +18,8 @@ java { } dependencies { + compileOnlyApi(libs.jspecify) + api(libs.gson) api(libs.guava) diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerResourcePackStatusEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerResourcePackStatusEvent.java index 8605d9bc2..95640596e 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerResourcePackStatusEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerResourcePackStatusEvent.java @@ -100,7 +100,7 @@ public class PlayerResourcePackStatusEvent { */ public void setOverwriteKick(boolean overwriteKick) { Preconditions.checkArgument(player.getProtocolVersion() - .compareTo(ProtocolVersion.MINECRAFT_1_17) < 0, + .lessThan(ProtocolVersion.MINECRAFT_1_17), "overwriteKick is not supported on 1.17 or newer"); this.overwriteKick = overwriteKick; } 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 9fe56010b..a1d3d1b54 100644 --- a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -10,6 +10,7 @@ package com.velocitypowered.api.network; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; +import com.velocitypowered.api.util.Ordered; import java.util.EnumSet; import java.util.HashMap; import java.util.List; @@ -19,7 +20,7 @@ import java.util.Set; /** * Represents each Minecraft protocol version. */ -public enum ProtocolVersion { +public enum ProtocolVersion implements Ordered { UNKNOWN(-1, "Unknown"), LEGACY(-2, "Legacy"), MINECRAFT_1_7_2(4, diff --git a/api/src/main/java/com/velocitypowered/api/proxy/crypto/IdentifiedKey.java b/api/src/main/java/com/velocitypowered/api/proxy/crypto/IdentifiedKey.java index adf5b07ee..d2a6cc94e 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/crypto/IdentifiedKey.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/crypto/IdentifiedKey.java @@ -9,6 +9,7 @@ package com.velocitypowered.api.proxy.crypto; import com.google.common.collect.ImmutableSet; import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.api.util.Ordered; import java.security.PublicKey; import java.util.Set; import java.util.UUID; @@ -57,7 +58,7 @@ public interface IdentifiedKey extends KeySigned { /** * The different versions of player keys, per Minecraft version. */ - enum Revision { + enum Revision implements Ordered { GENERIC_V1(ImmutableSet.of(), ImmutableSet.of(ProtocolVersion.MINECRAFT_1_19)), LINKED_V2(ImmutableSet.of(), ImmutableSet.of(ProtocolVersion.MINECRAFT_1_19_1)); diff --git a/api/src/main/java/com/velocitypowered/api/util/Ordered.java b/api/src/main/java/com/velocitypowered/api/util/Ordered.java new file mode 100644 index 000000000..f162c8d44 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/util/Ordered.java @@ -0,0 +1,93 @@ +/* + * This file is part of commons, licensed under the MIT License. + * + * Copyright (c) 2021-2024 Seiama + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.velocitypowered.api.util; + +import org.jspecify.annotations.NullMarked; + +/** + * Something that is ordered. + * + * @param the type + * @since 3.3.0 + */ +@NullMarked +@SuppressWarnings("ComparableType") // allows us to be more flexible +public interface Ordered extends Comparable { + /** + * Checks if {@code this} is greater than {@code that}. + * + * @param that the other object + * @return {@code true} if {@code this} is greater than {@code that}, {@code false} otherwise + * @since 3.3.0 + */ + default boolean greaterThan(final T that) { + return this.compareTo(that) > 0; + } + + /** + * Checks if {@code this} is greater than or equal to {@code that}. + * + * @param that the other object + * @return {@code true} if {@code this} is greater than or + * equal to {@code that}, {@code false} otherwise + * @since 3.3.0 + */ + default boolean noLessThan(final T that) { + return this.compareTo(that) >= 0; + } + + /** + * Checks if {@code this} is less than {@code that}. + * + * @param that the other object + * @return {@code true} if {@code this} is less than {@code that}, {@code false} otherwise + * @since 3.3.0 + */ + default boolean lessThan(final T that) { + return this.compareTo(that) < 0; + } + + /** + * Checks if {@code this} is less than or equal to {@code that}. + * + * @param that the other object + * @return {@code true} if {@code this} is less than or + * equal to {@code that}, {@code false} otherwise + * @since 3.3.0 + */ + default boolean noGreaterThan(final T that) { + return this.compareTo(that) <= 0; + } + + /** + * Checks if {@code this} is equal to {@code that}. + * + * @param that the other object + * @return {@code true} if {@code this} is equal to {@code that}, {@code false} otherwise + * @since 3.3.0 + */ + default boolean noGreaterOrLessThan(final T that) { + return this.compareTo(that) == 0; + } +} diff --git a/build-logic/src/main/kotlin/velocity-spotless.gradle.kts b/build-logic/src/main/kotlin/velocity-spotless.gradle.kts index cbd3f3d46..59422591c 100644 --- a/build-logic/src/main/kotlin/velocity-spotless.gradle.kts +++ b/build-logic/src/main/kotlin/velocity-spotless.gradle.kts @@ -7,6 +7,7 @@ extensions.configure { java { if (project.name == "velocity-api") { licenseHeaderFile(file("HEADER.txt")) + targetExclude("**/java/com/velocitypowered/api/util/Ordered.java") } else { licenseHeaderFile(rootProject.file("HEADER.txt")) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1e7328dfd..b3d0071b6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -34,6 +34,7 @@ flare-fastutil = { module = "space.vectrix.flare:flare-fastutil", version.ref = jline = "org.jline:jline-terminal-jansi:3.23.0" jopt = "net.sf.jopt-simple:jopt-simple:5.0.4" junit = "org.junit.jupiter:junit-jupiter:5.9.0" +jspecify = "org.jspecify:jspecify:0.3.0" kyori-ansi = "net.kyori:ansi:1.0.3" guava = "com.google.guava:guava:25.1-jre" gson = "com.google.code.gson:gson:2.10.1" diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 6efe04e25..8ccdbc508 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -776,10 +776,10 @@ public class VelocityServer implements ProxyServer, ForwardingAudience { */ public static Gson getPingGsonInstance(ProtocolVersion version) { if (version == ProtocolVersion.UNKNOWN - || version.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + || version.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { return MODERN_PING_SERIALIZER; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { return PRE_1_20_3_PING_SERIALIZER; } return PRE_1_16_PING_SERIALIZER; 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 3a27f7d0d..53d0a2364 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -268,8 +268,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { */ public void closeWith(Object msg) { if (channel.isActive()) { - boolean is17 = this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) < 0 - && this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_7_2) >= 0; + boolean is17 = this.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_8) + && this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_7_2); if (is17 && this.getState() != StateRegistry.STATUS) { channel.eventLoop().execute(() -> { // 1.7.x versions have a race condition with switching protocol states, so just explicitly diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BungeeCordMessageResponder.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BungeeCordMessageResponder.java index 88742467b..3c2691781 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BungeeCordMessageResponder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BungeeCordMessageResponder.java @@ -297,7 +297,7 @@ public class BungeeCordMessageResponder { } static String getBungeeCordChannel(ProtocolVersion version) { - return version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0 ? MODERN_CHANNEL.getId() + return version.noLessThan(ProtocolVersion.MINECRAFT_1_13) ? MODERN_CHANNEL.getId() : LEGACY_CHANNEL.getId(); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java index fde39f5ef..88186bc7b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java @@ -153,7 +153,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { // Move into the PLAY phase. MinecraftConnection smc = serverConn.ensureConnected(); - if (smc.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) { + if (smc.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_20_2)) { smc.setActiveSessionHandler(StateRegistry.PLAY, new TransitionSessionHandler(server, serverConn, resultFuture)); } else { @@ -202,7 +202,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { // Ensure we are in range requested = Math.min(requested, VelocityConstants.MODERN_FORWARDING_MAX_VERSION); if (requested > VelocityConstants.MODERN_FORWARDING_DEFAULT) { - if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + if (player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { return requested >= VelocityConstants.MODERN_LAZY_SESSION ? VelocityConstants.MODERN_LAZY_SESSION : VelocityConstants.MODERN_FORWARDING_DEFAULT; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java index 79d786eee..6655e3fbf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java @@ -144,7 +144,7 @@ public class TransitionSessionHandler implements MinecraftSessionHandler { serverConn.getPlayer().setConnectedServer(serverConn); // Send client settings. In 1.20.2+ this is done in the config state. - if (smc.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0 + if (smc.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_20_2) && player.getClientSettingsPacket() != null) { serverConn.ensureConnected().write(player.getClientSettingsPacket()); } 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 3959dae77..8174fe7c5 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 @@ -203,7 +203,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, mc.setProtocolVersion(protocolVersion); mc.setActiveSessionHandler(StateRegistry.LOGIN); if (proxyPlayer.getIdentifiedKey() == null - && proxyPlayer.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + && proxyPlayer.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { mc.delayedWrite(new ServerLoginPacket(proxyPlayer.getUsername(), proxyPlayer.getUniqueId())); } else { mc.delayedWrite(new ServerLoginPacket(proxyPlayer.getUsername(), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java index ed2504956..d8339f62f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java @@ -131,7 +131,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler { private void startLoginCompletion(ConnectedPlayer player) { int threshold = server.getConfiguration().getCompressionThreshold(); - if (threshold >= 0 && mcConnection.getProtocolVersion().compareTo(MINECRAFT_1_8) >= 0) { + if (threshold >= 0 && mcConnection.getProtocolVersion().noLessThan(MINECRAFT_1_8)) { mcConnection.write(new SetCompressionPacket(threshold)); mcConnection.setCompressionThreshold(threshold); } @@ -216,7 +216,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler { mcConnection.write(success); loginState = State.SUCCESS_SENT; - if (inbound.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) { + if (inbound.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_20_2)) { loginState = State.ACKNOWLEDGED; mcConnection.setActiveSessionHandler(StateRegistry.PLAY, new InitialConnectSessionHandler(player, server)); 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 0aad8c4b2..121fea721 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 @@ -118,10 +118,10 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { this.player = player; this.server = server; - if (this.player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + if (this.player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { this.chatHandler = new SessionChatHandler(this.player, this.server); this.commandHandler = new SessionCommandHandler(this.player, this.server); - } else if (this.player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + } else if (this.player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19)) { this.chatHandler = new KeyedChatHandler(this.server, this.player); this.commandHandler = new KeyedCommandHandler(this.player, this.server); } else { @@ -551,7 +551,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } // Clear any title from the previous server. - if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { player.getConnection().delayedWrite( GenericTitlePacket.constructTitlePacket(GenericTitlePacket.ActionType.RESET, player.getProtocolVersion())); @@ -574,7 +574,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // improving compatibility with mods. final RespawnPacket respawn = RespawnPacket.fromJoinGame(joinGame); - if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { + if (player.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_16)) { // Before Minecraft 1.16, we could not switch to the same dimension without sending an // additional respawn. On older versions of Minecraft this forces the client to perform // garbage collection which adds additional latency. @@ -616,7 +616,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { String commandLabel = command.substring(0, commandEndPosition); if (!server.getCommandManager().hasCommand(commandLabel)) { - if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { + if (player.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { // Outstanding tab completes are recorded for use with 1.12 clients and below to provide // additional tab completion support. outstandingTabComplete = packet; @@ -659,7 +659,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } private boolean handleRegularTabComplete(TabCompleteRequestPacket packet) { - if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { + if (player.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { // Outstanding tab completes are recorded for use with 1.12 clients and below to provide // additional tab completion support. outstandingTabComplete = packet; @@ -692,7 +692,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { server.getCommandManager().offerBrigadierSuggestions(player, command) .thenAcceptAsync(offers -> { boolean legacy = - player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0; + player.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13); try { for (Suggestion suggestion : offers.getList()) { String offer = suggestion.getText(); 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 0e9e18edb..df12320b5 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 @@ -178,9 +178,9 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, this.connectionPhase = connection.getType().getInitialClientPhase(); this.onlineMode = onlineMode; - if (connection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + if (connection.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { this.tabList = new VelocityTabList(this); - } else if (connection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + } else if (connection.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { this.tabList = new KeyedVelocityTabList(this, server); } else { this.tabList = new VelocityTabListLegacy(this, server); @@ -371,7 +371,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, Component translated = translateMessage(message); ProtocolVersion playerVersion = getProtocolVersion(); - if (playerVersion.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0) { + if (playerVersion.noLessThan(ProtocolVersion.MINECRAFT_1_11)) { // Use the title packet instead. GenericTitlePacket pkt = GenericTitlePacket.constructTitlePacket( GenericTitlePacket.ActionType.SET_ACTION_BAR, playerVersion); @@ -416,7 +416,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, Component translatedFooter = translateMessage(footer); this.playerListHeader = translatedHeader; this.playerListFooter = translatedFooter; - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { this.connection.write(HeaderAndFooterPacket.create( translatedHeader, translatedFooter, this.getProtocolVersion())); } @@ -424,7 +424,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, @Override public void showTitle(net.kyori.adventure.title.@NonNull Title title) { - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { GenericTitlePacket timesPkt = GenericTitlePacket.constructTitlePacket( GenericTitlePacket.ActionType.SET_TIMES, this.getProtocolVersion()); net.kyori.adventure.title.Title.Times times = title.times(); @@ -460,7 +460,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, throw new NullPointerException("value"); } - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) < 0) { + if (this.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_8)) { return; } @@ -491,7 +491,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, @Override public void clearTitle() { - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { connection.write(GenericTitlePacket.constructTitlePacket( GenericTitlePacket.ActionType.HIDE, this.getProtocolVersion())); } @@ -499,7 +499,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, @Override public void resetTitle() { - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { connection.write(GenericTitlePacket.constructTitlePacket( GenericTitlePacket.ActionType.RESET, this.getProtocolVersion())); } @@ -507,14 +507,14 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, @Override public void hideBossBar(@NonNull BossBar bar) { - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_9)) { this.server.getBossBarManager().removeBossBar(this, bar); } } @Override public void showBossBar(@NonNull BossBar bar) { - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_9)) { this.server.getBossBarManager().addBossBar(this, bar); } } @@ -542,7 +542,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, @Override public void clearPlayerListHeaderAndFooter() { clearPlayerListHeaderAndFooterSilent(); - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { this.connection.write(HeaderAndFooterPacket.reset(this.getProtocolVersion())); } } @@ -942,7 +942,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, Preconditions.checkArgument(input.length() <= LegacyChatPacket.MAX_SERVERBOUND_MESSAGE_LENGTH, "input cannot be greater than " + LegacyChatPacket.MAX_SERVERBOUND_MESSAGE_LENGTH + " characters in length"); - if (getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19)) { this.chatQueue.hijack(getChatBuilderFactory().builder().asPlayer(this).message(input), (instant, item) -> { item.setTimestamp(instant); @@ -968,7 +968,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, @Override public void sendResourcePackOffer(ResourcePackInfo packInfo) { - if (this.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_8)) { Preconditions.checkNotNull(packInfo, "packInfo"); queueResourcePack(packInfo); } @@ -996,7 +996,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, while (!outstandingResourcePacks.isEmpty()) { queued = outstandingResourcePacks.peek(); if (queued.getShouldForce() && getProtocolVersion() - .compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) { + .noLessThan(ProtocolVersion.MINECRAFT_1_17)) { break; } onResourcePackResponse(PlayerResourcePackStatusEvent.Status.DECLINED); @@ -1068,7 +1068,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, if (event.getStatus() == PlayerResourcePackStatusEvent.Status.DECLINED && event.getPackInfo() != null && event.getPackInfo().getShouldForce() && (!event.isOverwriteKick() || event.getPlayer() - .getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) + .getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_17)) ) { event.getPlayer().disconnect(Component .translatable("multiplayer.requiredTexturePrompt.disconnect")); 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 6eca332e0..303a3bf24 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 @@ -141,7 +141,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().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { + && handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { ic.disconnectQuietly( Component.translatable("velocity.error.modern-forwarding-needs-new-client")); return; @@ -155,14 +155,14 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { private ConnectionType getHandshakeConnectionType(HandshakePacket handshake) { if (handshake.getServerAddress().contains(ModernForgeConstants.MODERN_FORGE_TOKEN) - && handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + && handshake.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { return new ModernForgeConnectionType(handshake.getServerAddress()); } // Determine if we're using Forge (1.8 to 1.12, may not be the case in 1.13). if (handshake.getServerAddress().endsWith(LegacyForgeConstants.HANDSHAKE_HOSTNAME_TOKEN) - && handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { + && handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { return ConnectionTypes.LEGACY_FORGE; - } else if (handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_7_6) <= 0) { + } else if (handshake.getProtocolVersion().noGreaterThan(ProtocolVersion.MINECRAFT_1_7_6)) { // 1.7 Forge will not notify us during handshake. UNDETERMINED will listen for incoming // forge handshake attempts. Also sends a reset handshake packet on every transition. return ConnectionTypes.UNDETERMINED_17; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java index 1946ba7c5..3a49172ec 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java @@ -111,9 +111,9 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler { inbound.disconnect(Component.translatable("multiplayer.disconnect.invalid_public_key")); return true; } - } else if (mcConnection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0 + } else if (mcConnection.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19) && forceKeyAuthentication - && mcConnection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + && mcConnection.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { inbound.disconnect(Component.translatable("multiplayer.disconnect.missing_public_key")); return true; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginInboundConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginInboundConnection.java index 075e96769..997cf4620 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginInboundConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginInboundConnection.java @@ -92,7 +92,7 @@ public class LoginInboundConnection implements LoginPhaseConnection, KeyIdentifi if (consumer == null) { throw new NullPointerException("consumer"); } - if (delegate.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { + if (delegate.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { throw new IllegalStateException("Login plugin messages can only be sent to clients running " + "Minecraft 1.13 and above"); } 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 04a609e93..622186a99 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -435,7 +435,7 @@ public enum ProtocolUtils { public static BinaryTag readBinaryTag(ByteBuf buf, ProtocolVersion version, BinaryTagIO.Reader reader) { BinaryTagType type = BINARY_TAG_TYPES[buf.readByte()]; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) { + if (version.lessThan(ProtocolVersion.MINECRAFT_1_20_2)) { buf.skipBytes(buf.readUnsignedShort()); } try { @@ -456,7 +456,7 @@ public enum ProtocolUtils { BinaryTagType type = (BinaryTagType) tag.type(); buf.writeByte(type.id()); try { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) { + if (version.lessThan(ProtocolVersion.MINECRAFT_1_20_2)) { // Empty name buf.writeShort(0); } @@ -710,10 +710,10 @@ public enum ProtocolUtils { * @return the appropriate {@link GsonComponentSerializer} */ public static GsonComponentSerializer getJsonChatSerializer(ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { return MODERN_SERIALIZER; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { return PRE_1_20_3_SERIALIZER; } return PRE_1_16_SERIALIZER; @@ -741,7 +741,7 @@ public enum ProtocolUtils { long expiry = buf.readLong(); byte[] key = ProtocolUtils.readByteArray(buf); byte[] signature = ProtocolUtils.readByteArray(buf, 4096); - IdentifiedKey.Revision revision = version.compareTo(ProtocolVersion.MINECRAFT_1_19) == 0 + IdentifiedKey.Revision revision = version.noGreaterOrLessThan(ProtocolVersion.MINECRAFT_1_19) ? IdentifiedKey.Revision.GENERIC_V1 : IdentifiedKey.Revision.LINKED_V2; return new IdentifiedKeyImpl(revision, key, expiry, signature); } 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 817d653ac..93344a4e5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -669,7 +669,7 @@ public enum StateRegistry { if (next != current) { throw new IllegalArgumentException("Cannot add a mapping after last valid mapping"); } - if (from.compareTo(lastValid) > 0) { + if (from.greaterThan(lastValid)) { throw new IllegalArgumentException( "Last mapping version cannot be higher than highest mapping version"); } @@ -679,7 +679,7 @@ public enum StateRegistry { ProtocolVersion lastInList = lastValid != null ? lastValid : getLast(SUPPORTED_VERSIONS); - if (from.compareTo(to) >= 0 && from != lastInList) { + if (from.noLessThan(to) && from != lastInList) { throw new IllegalArgumentException(String.format( "Next mapping version (%s) should be lower then current (%s)", to, from)); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettingsPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettingsPacket.java index b372e2239..0e3668030 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettingsPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientSettingsPacket.java @@ -133,19 +133,19 @@ public class ClientSettingsPacket implements MinecraftPacket { this.chatVisibility = ProtocolUtils.readVarInt(buf); this.chatColors = buf.readBoolean(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) <= 0) { + if (version.noGreaterThan(ProtocolVersion.MINECRAFT_1_7_6)) { this.difficulty = buf.readByte(); } this.skinParts = buf.readUnsignedByte(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_9)) { this.mainHand = ProtocolUtils.readVarInt(buf); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_17)) { this.chatFilteringEnabled = buf.readBoolean(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_18) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_18)) { this.clientListingAllowed = buf.readBoolean(); } } @@ -162,19 +162,19 @@ public class ClientSettingsPacket implements MinecraftPacket { ProtocolUtils.writeVarInt(buf, chatVisibility); buf.writeBoolean(chatColors); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) <= 0) { + if (version.noGreaterThan(ProtocolVersion.MINECRAFT_1_7_6)) { buf.writeByte(difficulty); } buf.writeByte(skinParts); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_9)) { ProtocolUtils.writeVarInt(buf, mainHand); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_17)) { buf.writeBoolean(chatFilteringEnabled); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_18) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_18)) { buf.writeBoolean(clientListingAllowed); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequestPacket.java index fcc88aade..98b644d0a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionRequestPacket.java @@ -60,7 +60,7 @@ public class EncryptionRequestPacket implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { this.serverId = ProtocolUtils.readString(buf, 20); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { publicKey = ProtocolUtils.readByteArray(buf, 256); verifyToken = ProtocolUtils.readByteArray(buf, 16); } else { @@ -73,7 +73,7 @@ public class EncryptionRequestPacket implements MinecraftPacket { public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { ProtocolUtils.writeString(buf, this.serverId); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeByteArray(buf, publicKey); ProtocolUtils.writeByteArray(buf, verifyToken); } else { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponsePacket.java index c7971b41c..d6591e7b4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/EncryptionResponsePacket.java @@ -64,17 +64,17 @@ public class EncryptionResponsePacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { this.sharedSecret = ProtocolUtils.readByteArray(buf, 128); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0 - && version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0 + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19) + && version.lessThan(ProtocolVersion.MINECRAFT_1_19_3) && !buf.readBoolean()) { salt = buf.readLong(); } this.verifyToken = ProtocolUtils.readByteArray(buf, - version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0 ? 256 : 128); + version.noLessThan(ProtocolVersion.MINECRAFT_1_19) ? 256 : 128); } else { this.sharedSecret = ProtocolUtils.readByteArray17(buf); this.verifyToken = ProtocolUtils.readByteArray17(buf); @@ -83,10 +83,10 @@ public class EncryptionResponsePacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeByteArray(buf, sharedSecret); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0 - && version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19) + && version.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { if (salt != null) { buf.writeBoolean(false); buf.writeLong(salt); @@ -111,10 +111,10 @@ public class EncryptionResponsePacket implements MinecraftPacket { // It turns out these come out to the same length, whether we're talking >=1.8 or not. // The length prefix always winds up being 2 bytes. int base = 256 + 2 + 2; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { return base + 128; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { // Verify token is twice as long on 1.19+ // Additional 1 byte for left <> right and 8 bytes for salt base += 128 + 8 + 1; @@ -125,7 +125,7 @@ public class EncryptionResponsePacket implements MinecraftPacket { @Override public int expectedMinLength(ByteBuf buf, Direction direction, ProtocolVersion version) { int base = expectedMaxLength(buf, direction, version); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { // These are "optional" base -= 128 + 8; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGamePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGamePacket.java index 5f92c2625..aa5ccaf7a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGamePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGamePacket.java @@ -200,10 +200,10 @@ public class JoinGamePacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { // haha funny, they made 1.20.2 more complicated this.decode1202Up(buf, version); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { // Minecraft 1.16 and above have significantly more complicated logic for reading this packet, // so separate it out. this.decode116Up(buf, version); @@ -218,33 +218,33 @@ public class JoinGamePacket implements MinecraftPacket { this.isHardcore = (this.gamemode & 0x08) != 0; this.gamemode &= ~0x08; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_9_1)) { this.dimension = buf.readInt(); } else { this.dimension = buf.readByte(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.noGreaterThan(ProtocolVersion.MINECRAFT_1_13_2)) { this.difficulty = buf.readUnsignedByte(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_15)) { this.partialHashedSeed = buf.readLong(); } this.maxPlayers = buf.readUnsignedByte(); this.levelType = ProtocolUtils.readString(buf, 16); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_14)) { this.viewDistance = ProtocolUtils.readVarInt(buf); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { this.reducedDebugInfo = buf.readBoolean(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_15)) { this.showRespawnScreen = buf.readBoolean(); } } private void decode116Up(ByteBuf buf, ProtocolVersion version) { this.entityId = buf.readInt(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2)) { this.isHardcore = buf.readBoolean(); this.gamemode = buf.readByte(); } else { @@ -258,8 +258,8 @@ public class JoinGamePacket implements MinecraftPacket { this.registry = ProtocolUtils.readCompoundTag(buf, version, JOINGAME_READER); String dimensionIdentifier; String levelName = null; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0 - && version.compareTo(ProtocolVersion.MINECRAFT_1_19) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2) + && version.lessThan(ProtocolVersion.MINECRAFT_1_19)) { this.currentDimensionData = ProtocolUtils.readCompoundTag(buf, version, JOINGAME_READER); dimensionIdentifier = ProtocolUtils.readString(buf); } else { @@ -268,14 +268,14 @@ public class JoinGamePacket implements MinecraftPacket { } this.partialHashedSeed = buf.readLong(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2)) { this.maxPlayers = ProtocolUtils.readVarInt(buf); } else { this.maxPlayers = buf.readUnsignedByte(); } this.viewDistance = ProtocolUtils.readVarInt(buf); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_18) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_18)) { this.simulationDistance = ProtocolUtils.readVarInt(buf); } @@ -287,11 +287,11 @@ public class JoinGamePacket implements MinecraftPacket { this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug); // optional death location - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0 && buf.readBoolean()) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19) && buf.readBoolean()) { this.lastDeathPosition = Pair.of(ProtocolUtils.readString(buf), buf.readLong()); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20)) { this.portalCooldown = ProtocolUtils.readVarInt(buf); } } @@ -332,10 +332,10 @@ public class JoinGamePacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { // haha funny, they made 1.20.2 more complicated this.encode1202Up(buf, version); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { // Minecraft 1.16 and above have significantly more complicated logic for reading this packet, // so separate it out. this.encode116Up(buf, version); @@ -346,21 +346,21 @@ public class JoinGamePacket implements MinecraftPacket { private void encodeLegacy(ByteBuf buf, ProtocolVersion version) { buf.writeInt(entityId); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2)) { buf.writeBoolean(isHardcore); buf.writeByte(gamemode); } else { buf.writeByte(isHardcore ? gamemode | 0x8 : gamemode); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_9_1)) { buf.writeInt(dimension); } else { buf.writeByte(dimension); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.noGreaterThan(ProtocolVersion.MINECRAFT_1_13_2)) { buf.writeByte(difficulty); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_15)) { buf.writeLong(partialHashedSeed); } buf.writeByte(maxPlayers); @@ -368,20 +368,20 @@ public class JoinGamePacket implements MinecraftPacket { throw new IllegalStateException("No level type specified."); } ProtocolUtils.writeString(buf, levelType); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_14)) { ProtocolUtils.writeVarInt(buf, viewDistance); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { buf.writeBoolean(reducedDebugInfo); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_15)) { buf.writeBoolean(showRespawnScreen); } } private void encode116Up(ByteBuf buf, ProtocolVersion version) { buf.writeInt(entityId); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2)) { buf.writeBoolean(isHardcore); buf.writeByte(gamemode); } else { @@ -391,8 +391,8 @@ public class JoinGamePacket implements MinecraftPacket { ProtocolUtils.writeStringArray(buf, levelNames.toArray(String[]::new)); ProtocolUtils.writeBinaryTag(buf, version, this.registry); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0 - && version.compareTo(ProtocolVersion.MINECRAFT_1_19) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2) + && version.lessThan(ProtocolVersion.MINECRAFT_1_19)) { ProtocolUtils.writeBinaryTag(buf, version, currentDimensionData); ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); } else { @@ -401,14 +401,14 @@ public class JoinGamePacket implements MinecraftPacket { } buf.writeLong(partialHashedSeed); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2)) { ProtocolUtils.writeVarInt(buf, maxPlayers); } else { buf.writeByte(maxPlayers); } ProtocolUtils.writeVarInt(buf, viewDistance); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_18) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_18)) { ProtocolUtils.writeVarInt(buf, simulationDistance); } @@ -419,7 +419,7 @@ public class JoinGamePacket implements MinecraftPacket { buf.writeBoolean(dimensionInfo.isFlat()); // optional death location - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { if (lastDeathPosition != null) { buf.writeBoolean(true); ProtocolUtils.writeString(buf, lastDeathPosition.key()); @@ -429,7 +429,7 @@ public class JoinGamePacket implements MinecraftPacket { } } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20)) { ProtocolUtils.writeVarInt(buf, portalCooldown); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlivePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlivePacket.java index 23315d9b8..a44e50eea 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlivePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlivePacket.java @@ -44,9 +44,9 @@ public class KeepAlivePacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_12_2)) { randomId = buf.readLong(); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { randomId = ProtocolUtils.readVarInt(buf); } else { randomId = buf.readInt(); @@ -55,9 +55,9 @@ public class KeepAlivePacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_12_2)) { buf.writeLong(randomId); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeVarInt(buf, (int) randomId); } else { buf.writeInt((int) randomId); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPlayerListItemPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPlayerListItemPacket.java index 2ba2156ee..268c707d6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPlayerListItemPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LegacyPlayerListItemPacket.java @@ -61,7 +61,7 @@ public class LegacyPlayerListItemPacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { action = ProtocolUtils.readVarInt(buf); int length = ProtocolUtils.readVarInt(buf); @@ -76,7 +76,7 @@ public class LegacyPlayerListItemPacket implements MinecraftPacket { item.setLatency(ProtocolUtils.readVarInt(buf)); item.setDisplayName(readOptionalComponent(buf, version)); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { if (buf.readBoolean()) { item.setPlayerKey(ProtocolUtils.readPlayerKey(version, buf)); } @@ -117,7 +117,7 @@ public class LegacyPlayerListItemPacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeVarInt(buf, action); ProtocolUtils.writeVarInt(buf, items.size()); for (Item item : items) { @@ -132,7 +132,7 @@ public class LegacyPlayerListItemPacket implements MinecraftPacket { ProtocolUtils.writeVarInt(buf, item.getGameMode()); ProtocolUtils.writeVarInt(buf, item.getLatency()); writeDisplayName(buf, item.getDisplayName(), version); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { if (item.getPlayerKey() != null) { buf.writeBoolean(true); ProtocolUtils.writePlayerKey(buf, item.getPlayerKey()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java index 4d56d9412..d78ed8332 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java @@ -64,10 +64,10 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Minecr @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { this.channel = ProtocolUtils.readString(buf); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_13)) { this.channel = transformLegacyToModernChannel(this.channel); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { this.replace(buf.readRetainedSlice(buf.readableBytes())); } else { this.replace(ProtocolUtils.readRetainedByteBufSlice17(buf)); @@ -86,12 +86,12 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Minecr + " freed too many times."); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_13)) { ProtocolUtils.writeString(buf, transformLegacyToModernChannel(this.channel)); } else { ProtocolUtils.writeString(buf, this.channel); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { buf.writeBytes(content()); } else { ProtocolUtils.writeByteBuf17(content(), buf, true); // True for Forge support diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequestPacket.java index 8c5a5bf64..8b8a56f6e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackRequestPacket.java @@ -86,12 +86,12 @@ public class ResourcePackRequestPacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, Direction direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { this.id = ProtocolUtils.readUuid(buf); } this.url = ProtocolUtils.readString(buf); this.hash = ProtocolUtils.readString(buf); - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_17)) { this.isRequired = buf.readBoolean(); if (buf.readBoolean()) { this.prompt = ComponentHolder.read(buf, protocolVersion); @@ -103,7 +103,7 @@ public class ResourcePackRequestPacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, Direction direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { if (id == null) { throw new IllegalStateException("Resource pack id not set yet!"); } @@ -114,7 +114,7 @@ public class ResourcePackRequestPacket implements MinecraftPacket { } ProtocolUtils.writeString(buf, url); ProtocolUtils.writeString(buf, hash); - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_17)) { buf.writeBoolean(isRequired); if (prompt != null) { buf.writeBoolean(true); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackResponsePacket.java index 154a0d106..e5b0183ea 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackResponsePacket.java @@ -56,10 +56,10 @@ public class ResourcePackResponsePacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, Direction direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { this.id = ProtocolUtils.readUuid(buf); } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_4) <= 0) { + if (protocolVersion.noGreaterThan(ProtocolVersion.MINECRAFT_1_9_4)) { this.hash = ProtocolUtils.readString(buf); } this.status = Status.values()[ProtocolUtils.readVarInt(buf)]; @@ -67,10 +67,10 @@ public class ResourcePackResponsePacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, Direction direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { ProtocolUtils.writeUuid(buf, id); } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_4) <= 0) { + if (protocolVersion.noGreaterThan(ProtocolVersion.MINECRAFT_1_9_4)) { ProtocolUtils.writeString(buf, hash); } ProtocolUtils.writeVarInt(buf, status.ordinal()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RespawnPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RespawnPacket.java index 3ab37c8f9..7e9562c44 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RespawnPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RespawnPacket.java @@ -162,9 +162,9 @@ public class RespawnPacket implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { String dimensionIdentifier = null; String levelName = null; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0 - && version.compareTo(ProtocolVersion.MINECRAFT_1_19) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2) + && version.lessThan(ProtocolVersion.MINECRAFT_1_19)) { this.currentDimensionData = ProtocolUtils.readCompoundTag(buf, version, BinaryTagIO.reader()); dimensionIdentifier = ProtocolUtils.readString(buf); } else { @@ -174,42 +174,42 @@ public class RespawnPacket implements MinecraftPacket { } else { this.dimension = buf.readInt(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.noGreaterThan(ProtocolVersion.MINECRAFT_1_13_2)) { this.difficulty = buf.readUnsignedByte(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_15)) { this.partialHashedSeed = buf.readLong(); } this.gamemode = buf.readByte(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { this.previousGamemode = buf.readByte(); boolean isDebug = buf.readBoolean(); boolean isFlat = buf.readBoolean(); this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (version.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { this.dataToKeep = (byte) (buf.readBoolean() ? 1 : 0); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) { + } else if (version.lessThan(ProtocolVersion.MINECRAFT_1_20_2)) { this.dataToKeep = buf.readByte(); } } else { this.levelType = ProtocolUtils.readString(buf, 16); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0 && buf.readBoolean()) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19) && buf.readBoolean()) { this.lastDeathPosition = Pair.of(ProtocolUtils.readString(buf), buf.readLong()); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20)) { this.portalCooldown = ProtocolUtils.readVarInt(buf); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { this.dataToKeep = buf.readByte(); } } @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0 - && version.compareTo(ProtocolVersion.MINECRAFT_1_19) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16_2) + && version.lessThan(ProtocolVersion.MINECRAFT_1_19)) { ProtocolUtils.writeBinaryTag(buf, version, currentDimensionData); ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); } else { @@ -219,20 +219,20 @@ public class RespawnPacket implements MinecraftPacket { } else { buf.writeInt(dimension); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.noGreaterThan(ProtocolVersion.MINECRAFT_1_13_2)) { buf.writeByte(difficulty); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_15)) { buf.writeLong(partialHashedSeed); } buf.writeByte(gamemode); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { buf.writeByte(previousGamemode); buf.writeBoolean(dimensionInfo.isDebugType()); buf.writeBoolean(dimensionInfo.isFlat()); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (version.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { buf.writeBoolean(dataToKeep != 0); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) { + } else if (version.lessThan(ProtocolVersion.MINECRAFT_1_20_2)) { buf.writeByte(dataToKeep); } } else { @@ -240,7 +240,7 @@ public class RespawnPacket implements MinecraftPacket { } // optional death location - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { if (lastDeathPosition != null) { buf.writeBoolean(true); ProtocolUtils.writeString(buf, lastDeathPosition.key()); @@ -250,11 +250,11 @@ public class RespawnPacket implements MinecraftPacket { } } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20)) { ProtocolUtils.writeVarInt(buf, portalCooldown); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { buf.writeByte(dataToKeep); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerDataPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerDataPacket.java index a54f224f5..d92bf0c46 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerDataPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerDataPacket.java @@ -47,12 +47,12 @@ public class ServerDataPacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_4) >= 0 || buf.readBoolean()) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_4) || buf.readBoolean()) { this.description = ComponentHolder.read(buf, protocolVersion); } if (buf.readBoolean()) { String iconBase64; - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_4) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_4)) { byte[] iconBytes = ProtocolUtils.readByteArray(buf); iconBase64 = "data:image/png;base64," + new String(Base64.getEncoder().encode(iconBytes), StandardCharsets.UTF_8); } else { @@ -60,10 +60,10 @@ public class ServerDataPacket implements MinecraftPacket { } this.favicon = new Favicon(iconBase64); } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (protocolVersion.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { buf.readBoolean(); } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { this.secureChatEnforced = buf.readBoolean(); } } @@ -72,17 +72,17 @@ public class ServerDataPacket implements MinecraftPacket { public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { boolean hasDescription = this.description != null; - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_4) < 0) { + if (protocolVersion.lessThan(ProtocolVersion.MINECRAFT_1_19_4)) { buf.writeBoolean(hasDescription); } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_4) >= 0 || hasDescription) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_4) || hasDescription) { this.description.write(buf); } boolean hasFavicon = this.favicon != null; buf.writeBoolean(hasFavicon); if (hasFavicon) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_4) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_4)) { String cutIconBase64 = favicon.getBase64Url().substring("data:image/png;base64,".length()); byte[] iconBytes = Base64.getDecoder().decode(cutIconBase64.getBytes(StandardCharsets.UTF_8)); ProtocolUtils.writeByteArray(buf, iconBytes); @@ -91,10 +91,10 @@ public class ServerDataPacket implements MinecraftPacket { } } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (protocolVersion.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { buf.writeBoolean(false); } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { buf.writeBoolean(this.secureChatEnforced); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java index 6082c42ea..f3f2cfb8c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java @@ -84,8 +84,8 @@ public class ServerLoginPacket implements MinecraftPacket { throw EMPTY_USERNAME; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { playerKey = null; } else { if (buf.readBoolean()) { @@ -95,12 +95,12 @@ public class ServerLoginPacket implements MinecraftPacket { } } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { this.holderUuid = ProtocolUtils.readUuid(buf); return; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { if (buf.readBoolean()) { holderUuid = ProtocolUtils.readUuid(buf); } @@ -117,8 +117,8 @@ public class ServerLoginPacket implements MinecraftPacket { } ProtocolUtils.writeString(buf, username); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { + if (version.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { if (playerKey != null) { buf.writeBoolean(true); ProtocolUtils.writePlayerKey(buf, playerKey); @@ -127,12 +127,12 @@ public class ServerLoginPacket implements MinecraftPacket { } } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_2) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_2)) { ProtocolUtils.writeUuid(buf, this.holderUuid); return; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { if (playerKey != null && playerKey.getSignatureHolder() != null) { buf.writeBoolean(true); ProtocolUtils.writeUuid(buf, playerKey.getSignatureHolder()); @@ -152,8 +152,8 @@ public class ServerLoginPacket implements MinecraftPacket { // legal on the protocol level. int base = 1 + (16 * 3); // Adjustments for Key-authentication - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) < 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { + if (version.lessThan(ProtocolVersion.MINECRAFT_1_19_3)) { // + 1 for the boolean present/ not present // + 8 for the long expiry // + 2 len for varint key size @@ -162,7 +162,7 @@ public class ServerLoginPacket implements MinecraftPacket { // + 512 for signature base += 1 + 8 + 2 + 294 + 2 + 512; } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { // +1 boolean uuid optional // + 2 * 8 for the long msb/lsb base += 1 + 8 + 8; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccessPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccessPacket.java index 93caa62b9..fb7c087b4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccessPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginSuccessPacket.java @@ -76,18 +76,18 @@ public class ServerLoginSuccessPacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { uuid = ProtocolUtils.readUuid(buf); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { uuid = ProtocolUtils.readUuidIntArray(buf); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_7_6)) { uuid = UUID.fromString(ProtocolUtils.readString(buf, 36)); } else { uuid = UuidUtils.fromUndashed(ProtocolUtils.readString(buf, 32)); } username = ProtocolUtils.readString(buf, 16); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { properties = ProtocolUtils.readProperties(buf); } } @@ -97,11 +97,11 @@ public class ServerLoginSuccessPacket implements MinecraftPacket { if (uuid == null) { throw new IllegalStateException("No UUID specified!"); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { ProtocolUtils.writeUuid(buf, uuid); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { ProtocolUtils.writeUuidIntArray(buf, uuid); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_7_6)) { ProtocolUtils.writeString(buf, uuid.toString()); } else { ProtocolUtils.writeString(buf, UuidUtils.toUndashed(uuid)); @@ -111,7 +111,7 @@ public class ServerLoginSuccessPacket implements MinecraftPacket { } ProtocolUtils.writeString(buf, username); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { if (properties == null) { ProtocolUtils.writeVarInt(buf, 0); } else { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequestPacket.java index 0690dc4cf..dda4695bb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteRequestPacket.java @@ -95,15 +95,15 @@ public class TabCompleteRequestPacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.noLessThan(MINECRAFT_1_13)) { this.transactionId = ProtocolUtils.readVarInt(buf); this.command = ProtocolUtils.readString(buf, VANILLA_MAX_TAB_COMPLETE_LEN); } else { this.command = ProtocolUtils.readString(buf, VANILLA_MAX_TAB_COMPLETE_LEN); - if (version.compareTo(MINECRAFT_1_9) >= 0) { + if (version.noLessThan(MINECRAFT_1_9)) { this.assumeCommand = buf.readBoolean(); } - if (version.compareTo(MINECRAFT_1_8) >= 0) { + if (version.noLessThan(MINECRAFT_1_8)) { this.hasPosition = buf.readBoolean(); if (hasPosition) { this.position = buf.readLong(); @@ -118,15 +118,15 @@ public class TabCompleteRequestPacket implements MinecraftPacket { throw new IllegalStateException("Command is not specified"); } - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.noLessThan(MINECRAFT_1_13)) { ProtocolUtils.writeVarInt(buf, transactionId); ProtocolUtils.writeString(buf, command); } else { ProtocolUtils.writeString(buf, command); - if (version.compareTo(MINECRAFT_1_9) >= 0) { + if (version.noLessThan(MINECRAFT_1_9)) { buf.writeBoolean(assumeCommand); } - if (version.compareTo(MINECRAFT_1_8) >= 0) { + if (version.noLessThan(MINECRAFT_1_8)) { buf.writeBoolean(hasPosition); if (hasPosition) { buf.writeLong(position); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponsePacket.java index e71f78d6f..a22fff0bd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TabCompleteResponsePacket.java @@ -77,7 +77,7 @@ public class TabCompleteResponsePacket implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.noLessThan(MINECRAFT_1_13)) { this.transactionId = ProtocolUtils.readVarInt(buf); this.start = ProtocolUtils.readVarInt(buf); this.length = ProtocolUtils.readVarInt(buf); @@ -97,7 +97,7 @@ public class TabCompleteResponsePacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.noLessThan(MINECRAFT_1_13)) { ProtocolUtils.writeVarInt(buf, this.transactionId); ProtocolUtils.writeVarInt(buf, this.start); ProtocolUtils.writeVarInt(buf, this.length); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentIdentifier.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentIdentifier.java index b26401b7f..271d0c507 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentIdentifier.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentIdentifier.java @@ -41,13 +41,13 @@ public class ArgumentIdentifier { VersionSet current = Preconditions.checkNotNull(versions[i]); Preconditions.checkArgument( - current.getVersion().compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0, + current.getVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19), "Version too old for ID index"); - Preconditions.checkArgument(previous == null || previous.compareTo(current.getVersion()) > 0, + Preconditions.checkArgument(previous == null || previous.greaterThan(current.getVersion()), "Invalid protocol version order"); for (ProtocolVersion v : ProtocolVersion.values()) { - if (v.compareTo(current.getVersion()) >= 0) { + if (v.noLessThan(current.getVersion())) { temp.putIfAbsent(v, current.getId()); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java index cd8e5b57e..9eca02fbf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java @@ -135,7 +135,7 @@ public class ArgumentPropertyRegistry { */ public static void writeIdentifier(ByteBuf buf, ArgumentIdentifier identifier, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(MINECRAFT_1_19) >= 0) { + if (protocolVersion.noLessThan(MINECRAFT_1_19)) { Integer id = identifier.getIdByProtocolVersion(protocolVersion); Preconditions.checkNotNull(id, "Don't know how to serialize type " + identifier); @@ -154,7 +154,7 @@ public class ArgumentPropertyRegistry { * @return the identifier read from the buffer */ public static ArgumentIdentifier readIdentifier(ByteBuf buf, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(MINECRAFT_1_19) >= 0) { + if (protocolVersion.noLessThan(MINECRAFT_1_19)) { int id = ProtocolUtils.readVarInt(buf); for (ArgumentIdentifier i : byIdentifier.keySet()) { Integer v = i.getIdByProtocolVersion(protocolVersion); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ModArgumentPropertySerializer.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ModArgumentPropertySerializer.java index 385172022..3859b1d78 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ModArgumentPropertySerializer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ModArgumentPropertySerializer.java @@ -34,7 +34,7 @@ class ModArgumentPropertySerializer implements ArgumentPropertySerializer= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { int idx = ProtocolUtils.readVarInt(buf); identifier = ArgumentIdentifier.id("crossstitch:identified_" + (idx < 0 ? "n" + (-idx) : idx), ArgumentIdentifier.mapSet(version, idx)); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/TimeArgumentSerializer.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/TimeArgumentSerializer.java index 1ddbcd68e..b026e8cc1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/TimeArgumentSerializer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/TimeArgumentSerializer.java @@ -26,7 +26,7 @@ public class TimeArgumentSerializer implements ArgumentPropertySerializer= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_4)) { return buf.readInt(); } return 0; @@ -34,7 +34,7 @@ public class TimeArgumentSerializer implements ArgumentPropertySerializer= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_4)) { buf.writeInt(object); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java index 246676154..d833f1300 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/ComponentHolder.java @@ -279,7 +279,7 @@ public class ComponentHolder { } public static ComponentHolder read(ByteBuf buf, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { return new ComponentHolder(version, ProtocolUtils.readBinaryTag(buf, version, BinaryTagIO.reader())); } else { @@ -288,7 +288,7 @@ public class ComponentHolder { } public void write(ByteBuf buf) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_20_3) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_20_3)) { ProtocolUtils.writeBinaryTag(buf, version, getBinaryTag()); } else { ProtocolUtils.writeString(buf, getJson()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/SystemChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/SystemChatPacket.java index 51079b8df..4224dc482 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/SystemChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/SystemChatPacket.java @@ -54,7 +54,7 @@ public class SystemChatPacket implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { component.write(buf); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { switch (type) { case SYSTEM: buf.writeBoolean(false); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/builder/ChatBuilderFactory.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/builder/ChatBuilderFactory.java index 2d1d78981..b0d2f12af 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/builder/ChatBuilderFactory.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/builder/ChatBuilderFactory.java @@ -30,9 +30,9 @@ public class ChatBuilderFactory { public ChatBuilderFactory(ProtocolVersion version) { this.version = version; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_19_3) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { this.builderFunction = SessionChatBuilder::new; - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_19) >= 0) { + } else if (version.noLessThan(ProtocolVersion.MINECRAFT_1_19)) { this.builderFunction = KeyedChatBuilder::new; } else { this.builderFunction = LegacyChatBuilder::new; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedChatHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedChatHandler.java index 6e55cd13d..f4964e6c2 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedChatHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedChatHandler.java @@ -105,7 +105,7 @@ public class KeyedChatHandler implements return pme -> { PlayerChatEvent.ChatResult chatResult = pme.getResult(); if (!chatResult.isAllowed()) { - if (playerKey.getKeyRevision().compareTo(IdentifiedKey.Revision.LINKED_V2) >= 0) { + if (playerKey.getKeyRevision().noLessThan(IdentifiedKey.Revision.LINKED_V2)) { // Bad, very bad. invalidCancel(logger, player); } @@ -113,7 +113,7 @@ public class KeyedChatHandler implements } if (chatResult.getMessage().map(str -> !str.equals(packet.getMessage())).orElse(false)) { - if (playerKey.getKeyRevision().compareTo(IdentifiedKey.Revision.LINKED_V2) >= 0) { + if (playerKey.getKeyRevision().noLessThan(IdentifiedKey.Revision.LINKED_V2)) { // Bad, very bad. invalidChange(logger, player); } else { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedCommandHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedCommandHandler.java index 7591a41bd..bef75247c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedCommandHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedCommandHandler.java @@ -49,7 +49,7 @@ public class KeyedCommandHandler implements CommandHandler= 0) { + && playerKey.getKeyRevision().noLessThan(IdentifiedKey.Revision.LINKED_V2)) { logger.fatal("A plugin tried to deny a command with signable component(s). " + "This is not supported. " + "Disconnecting player " + player.getUsername() + ". Command packet: " + packet); @@ -72,7 +72,7 @@ public class KeyedCommandHandler implements CommandHandler= 0) { + && playerKey.getKeyRevision().noLessThan(IdentifiedKey.Revision.LINKED_V2)) { logger.fatal("A plugin tried to change a command with signed component(s). " + "This is not supported. " + "Disconnecting player " + player.getUsername() + ". Command packet: " + packet); @@ -92,7 +92,7 @@ public class KeyedCommandHandler implements CommandHandler= 0) { + && playerKey.getKeyRevision().noLessThan(IdentifiedKey.Revision.LINKED_V2)) { logger.fatal("A plugin tried to change a command with signed component(s). " + "This is not supported. " + "Disconnecting player " + player.getUsername() + ". Command packet: " + packet); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerChatPacket.java index 8b58fc2fa..74fa88f5a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerChatPacket.java @@ -86,7 +86,7 @@ public class KeyedPlayerChatPacket implements MinecraftPacket { salt = Longs.toByteArray(saltLong); signature = signatureBytes; expiry = Instant.ofEpochMilli(expiresAt); - } else if ((protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0 + } else if ((protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1) || saltLong == 0L) && signatureBytes.length == 0) { unsigned = true; } else { @@ -98,7 +98,7 @@ public class KeyedPlayerChatPacket implements MinecraftPacket { throw EncryptionUtils.PREVIEW_SIGNATURE_MISSING; } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { int size = ProtocolUtils.readVarInt(buf); if (size < 0 || size > MAXIMUM_PREVIOUS_MESSAGE_COUNT) { throw INVALID_PREVIOUS_MESSAGES; @@ -130,7 +130,7 @@ public class KeyedPlayerChatPacket implements MinecraftPacket { buf.writeBoolean(signedPreview); - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { ProtocolUtils.writeVarInt(buf, previousMessages.length); for (SignaturePair previousMessage : previousMessages) { ProtocolUtils.writeUuid(buf, previousMessage.getSigner()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerCommandPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerCommandPacket.java index c12450b15..0bb43ec8e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerCommandPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/keyed/KeyedPlayerCommandPacket.java @@ -109,7 +109,7 @@ public class KeyedPlayerCommandPacket implements MinecraftPacket { throw EncryptionUtils.PREVIEW_SIGNATURE_MISSING; } - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { int size = ProtocolUtils.readVarInt(buf); if (size < 0 || size > MAXIMUM_PREVIOUS_MESSAGE_COUNT) { throw INVALID_PREVIOUS_MESSAGES; @@ -155,7 +155,7 @@ public class KeyedPlayerCommandPacket implements MinecraftPacket { buf.writeBoolean(signedPreview); - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) { ProtocolUtils.writeVarInt(buf, previousMessages.length); for (SignaturePair previousMessage : previousMessages) { ProtocolUtils.writeUuid(buf, previousMessage.getSigner()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/legacy/LegacyChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/legacy/LegacyChatPacket.java index 88ba247ff..4239489f3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/legacy/LegacyChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/legacy/LegacyChatPacket.java @@ -93,9 +93,9 @@ public class LegacyChatPacket implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { message = ProtocolUtils.readString(buf); if (direction == ProtocolUtils.Direction.CLIENTBOUND - && version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + && version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { type = buf.readByte(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { sender = ProtocolUtils.readUuid(buf); } } @@ -108,9 +108,9 @@ public class LegacyChatPacket implements MinecraftPacket { } ProtocolUtils.writeString(buf, message); if (direction == ProtocolUtils.Direction.CLIENTBOUND - && version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + && version.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { buf.writeByte(type); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_16)) { ProtocolUtils.writeUuid(buf, sender == null ? EMPTY_SENDER : sender); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/session/SessionCommandHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/session/SessionCommandHandler.java index 5bfc4318b..b9f2b238b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/session/SessionCommandHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/chat/session/SessionCommandHandler.java @@ -55,7 +55,7 @@ public class SessionCommandHandler implements CommandHandler= 0) { + if (player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { return CompletableFuture.completedFuture(new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset())); } return CompletableFuture.completedFuture(null); @@ -108,7 +108,7 @@ public class SessionCommandHandler implements CommandHandler= 0) { + if (player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3)) { return new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset()); } return null; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java index 95a55a4e0..d6f5e0e54 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/GenericTitlePacket.java @@ -40,7 +40,7 @@ public abstract class GenericTitlePacket implements MinecraftPacket { } public int getAction(ProtocolVersion version) { - return version.compareTo(ProtocolVersion.MINECRAFT_1_11) < 0 + return version.lessThan(ProtocolVersion.MINECRAFT_1_11) ? action > 2 ? action - 1 : action : action; } } @@ -104,7 +104,7 @@ public abstract class GenericTitlePacket implements MinecraftPacket { */ public static GenericTitlePacket constructTitlePacket(ActionType type, ProtocolVersion version) { GenericTitlePacket packet = null; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_17) >= 0) { + if (version.noLessThan(ProtocolVersion.MINECRAFT_1_17)) { switch (type) { case SET_ACTION_BAR: packet = new TitleActionbarPacket(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java index 904671903..0425f2d3c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/title/LegacyTitlePacket.java @@ -33,7 +33,7 @@ public class LegacyTitlePacket extends GenericTitlePacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_11) < 0 + if (version.lessThan(ProtocolVersion.MINECRAFT_1_11) && getAction() == ActionType.SET_ACTION_BAR) { throw new IllegalStateException("Action bars are only supported on 1.11 and newer"); } 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 603c61e25..1cfd4a6f6 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 @@ -115,7 +115,7 @@ public final class PluginMessageUtil { Collection channels) { checkNotNull(channels, "channels"); checkArgument(!channels.isEmpty(), "no channels specified"); - String channelName = protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0 + String channelName = protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_13) ? REGISTER_CHANNEL : REGISTER_CHANNEL_LEGACY; ByteBuf contents = Unpooled.buffer(); contents.writeCharSequence(String.join("\0", channels), StandardCharsets.UTF_8); @@ -140,7 +140,7 @@ public final class PluginMessageUtil { String rewrittenBrand = String.format("%s (%s)", currentBrand, version.getName()); ByteBuf rewrittenBuf = Unpooled.buffer(); - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeString(rewrittenBuf, rewrittenBrand); } else { rewrittenBuf.writeCharSequence(rewrittenBrand, StandardCharsets.UTF_8); 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 2aaf4ec51..d28ab9bfb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java @@ -115,7 +115,7 @@ public class VelocityChannelRegistrar implements ChannelRegistrar { * @return the list of channels to register */ public Collection getChannelsForProtocol(ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { + if (protocolVersion.noLessThan(ProtocolVersion.MINECRAFT_1_13)) { return getModernChannelIds(); } return getLegacyChannelIds();