diff --git a/api/build.gradle b/api/build.gradle index d876e1f1f..168f7b6b2 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -27,6 +27,7 @@ dependencies { api "net.kyori:adventure-text-serializer-gson:${adventureVersion}" api "net.kyori:adventure-text-serializer-legacy:${adventureVersion}" api "net.kyori:adventure-text-serializer-plain:${adventureVersion}" + api("net.kyori:coffee:1.0.0-SNAPSHOT") api "org.slf4j:slf4j-api:${slf4jVersion}" api 'com.google.inject:guice:4.2.3' 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 0f71bf505..c1dc3c798 100644 --- a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -6,11 +6,12 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.Map; import java.util.Set; +import net.kyori.coffee.Ordered; /** * Represents each Minecraft protocol version. */ -public enum ProtocolVersion { +public enum ProtocolVersion implements Ordered { UNKNOWN(-1, "Unknown"), LEGACY(-2, "Legacy"), MINECRAFT_1_7_2(4, "1.7.2"), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index e2c6670d7..4053640ba 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -645,7 +645,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience { } public static Gson getPingGsonInstance(ProtocolVersion version) { - return version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0 ? POST_1_16_PING_SERIALIZER + return version.gte(ProtocolVersion.MINECRAFT_1_16) ? POST_1_16_PING_SERIALIZER : 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 3456f0a82..4363b6cc9 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -224,8 +224,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().lt(ProtocolVersion.MINECRAFT_1_8) + && this.getProtocolVersion().gte(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/MinecraftSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftSessionHandler.java index 71b16d997..ce0e5dc5d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftSessionHandler.java @@ -3,8 +3,8 @@ package com.velocitypowered.proxy.connection; import com.velocitypowered.proxy.protocol.Packet; import com.velocitypowered.proxy.protocol.packet.AvailableCommandsPacket; import com.velocitypowered.proxy.protocol.packet.BossBarPacket; -import com.velocitypowered.proxy.protocol.packet.ChatPacket; import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket; +import com.velocitypowered.proxy.protocol.packet.ClientboundChatPacket; import com.velocitypowered.proxy.protocol.packet.DisconnectPacket; import com.velocitypowered.proxy.protocol.packet.EncryptionRequestPacket; import com.velocitypowered.proxy.protocol.packet.EncryptionResponsePacket; @@ -21,6 +21,7 @@ import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket; import com.velocitypowered.proxy.protocol.packet.RespawnPacket; import com.velocitypowered.proxy.protocol.packet.ServerLoginPacket; import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccessPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundChatPacket; import com.velocitypowered.proxy.protocol.packet.SetCompressionPacket; import com.velocitypowered.proxy.protocol.packet.StatusPingPacket; import com.velocitypowered.proxy.protocol.packet.StatusRequestPacket; @@ -82,7 +83,11 @@ public interface MinecraftSessionHandler { return false; } - default boolean handle(ChatPacket packet) { + default boolean handle(ClientboundChatPacket packet) { + return false; + } + + default boolean handle(ServerboundChatPacket packet) { return false; } 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 09a48a984..855943a76 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 @@ -303,7 +303,7 @@ public class BungeeCordMessageResponder { } static String getBungeeCordChannel(ProtocolVersion version) { - return version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0 ? MODERN_CHANNEL.getId() + return version.gte(ProtocolVersion.MINECRAFT_1_13) ? MODERN_CHANNEL.getId() : LEGACY_CHANNEL.getId(); } 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 761a78457..d87060cd1 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 @@ -22,13 +22,14 @@ import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.protocol.Packet; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.BossBarPacket; -import com.velocitypowered.proxy.protocol.packet.ChatPacket; import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket; +import com.velocitypowered.proxy.protocol.packet.ClientboundChatPacket; import com.velocitypowered.proxy.protocol.packet.JoinGamePacket; import com.velocitypowered.proxy.protocol.packet.KeepAlivePacket; import com.velocitypowered.proxy.protocol.packet.PluginMessagePacket; import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket; import com.velocitypowered.proxy.protocol.packet.RespawnPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundChatPacket; import com.velocitypowered.proxy.protocol.packet.TabCompleteRequestPacket; import com.velocitypowered.proxy.protocol.packet.TabCompleteResponsePacket; import com.velocitypowered.proxy.protocol.packet.TabCompleteResponsePacket.Offer; @@ -117,7 +118,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } @Override - public boolean handle(ChatPacket packet) { + public boolean handle(ClientboundChatPacket packet) { VelocityServerConnection serverConnection = player.getConnectedServer(); if (serverConnection == null) { return true; @@ -154,7 +155,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (chatResult.isAllowed()) { Optional eventMsg = pme.getResult().getMessage(); if (eventMsg.isPresent()) { - smc.write(ChatPacket.createServerbound(eventMsg.get())); + smc.write(new ServerboundChatPacket(eventMsg.get())); } else { smc.write(packet); } @@ -365,7 +366,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } // Clear any title from the previous server. - if (player.getProtocolVersion().compareTo(MINECRAFT_1_8) >= 0) { + if (player.getProtocolVersion().gte(MINECRAFT_1_8)) { player.getConnection() .delayedWrite(TitlePacket.resetForProtocolVersion(player.getProtocolVersion())); } @@ -386,7 +387,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // to perform entity ID rewrites, eliminating potential issues from rewriting packets and // improving compatibility with mods. int sentOldDim = joinGame.getDimension(); - if (player.getProtocolVersion().compareTo(MINECRAFT_1_16) < 0) { + if (player.getProtocolVersion().lt(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. @@ -439,7 +440,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { String commandLabel = command.substring(0, commandEndPosition); if (!server.getCommandManager().hasCommand(commandLabel)) { - if (player.getProtocolVersion().compareTo(MINECRAFT_1_13) < 0) { + if (player.getProtocolVersion().lt(MINECRAFT_1_13)) { // Outstanding tab completes are recorded for use with 1.12 clients and below to provide // additional tab completion support. outstandingTabComplete = packet; @@ -476,7 +477,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } private boolean handleRegularTabComplete(TabCompleteRequestPacket packet) { - if (player.getProtocolVersion().compareTo(MINECRAFT_1_13) < 0) { + if (player.getProtocolVersion().lt(MINECRAFT_1_13)) { // Outstanding tab completes are recorded for use with 1.12 clients and below to provide // additional tab completion support. outstandingTabComplete = packet; @@ -508,7 +509,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { String command = request.getCommand().substring(1); server.getCommandManager().offerSuggestions(player, command) .thenAcceptAsync(offers -> { - boolean legacy = player.getProtocolVersion().compareTo(MINECRAFT_1_13) < 0; + boolean legacy = player.getProtocolVersion().lt(MINECRAFT_1_13); try { for (String offer : offers) { offer = legacy && !offer.startsWith("/") ? "/" + offer : offer; @@ -564,13 +565,13 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { MinecraftConnection smc = player.ensureAndGetCurrentServer().ensureConnected(); String commandToRun = result.getCommand().orElse(originalCommand); if (result.isForwardToServer()) { - return CompletableFuture.runAsync(() -> smc.write(ChatPacket.createServerbound("/" + return CompletableFuture.runAsync(() -> smc.write(new ServerboundChatPacket("/" + commandToRun)), smc.eventLoop()); } else { return server.getCommandManager().executeImmediately(player, commandToRun) .thenAcceptAsync(hasRun -> { if (!hasRun) { - smc.write(ChatPacket.createServerbound("/" + commandToRun)); + smc.write(new ServerboundChatPacket("/" + commandToRun)); } }, smc.eventLoop()); } 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 4f3682322..e16e6ea16 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 @@ -38,12 +38,13 @@ import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; -import com.velocitypowered.proxy.protocol.packet.ChatPacket; import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket; +import com.velocitypowered.proxy.protocol.packet.ClientboundChatPacket; import com.velocitypowered.proxy.protocol.packet.DisconnectPacket; import com.velocitypowered.proxy.protocol.packet.KeepAlivePacket; import com.velocitypowered.proxy.protocol.packet.PluginMessagePacket; import com.velocitypowered.proxy.protocol.packet.ResourcePackRequestPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundChatPacket; import com.velocitypowered.proxy.protocol.packet.TitlePacket; import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; import com.velocitypowered.proxy.server.VelocityRegisteredServer; @@ -112,7 +113,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, @Nullable InetSocketAddress virtualHost, boolean onlineMode) { this.server = server; - if (connection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (connection.getProtocolVersion().gte(ProtocolVersion.MINECRAFT_1_8)) { this.tabList = new VelocityTabList(connection); } else { this.tabList = new VelocityTabListLegacy(connection); @@ -226,26 +227,25 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return connection.getProtocolVersion(); } - @Override - public void sendMessage(@NonNull Identity identity, @NonNull Component message) { - connection.write(ChatPacket.createClientbound(identity, message, this.getProtocolVersion())); - } - @Override public void sendMessage(@NonNull Identity identity, @NonNull Component message, @NonNull MessageType type) { Preconditions.checkNotNull(message, "message"); Preconditions.checkNotNull(type, "type"); - ChatPacket packet = ChatPacket.createClientbound(identity, message, this.getProtocolVersion()); - packet.setType(type == MessageType.CHAT ? ChatPacket.CHAT_TYPE : ChatPacket.SYSTEM_TYPE); - connection.write(packet); + connection.write(new ClientboundChatPacket( + ProtocolUtils.getJsonChatSerializer(this.getProtocolVersion()).serialize(message), + type == MessageType.CHAT + ? ClientboundChatPacket.CHAT_TYPE + : ClientboundChatPacket.SYSTEM_TYPE, + identity.uuid() + )); } @Override public void sendActionBar(net.kyori.adventure.text.@NonNull Component message) { ProtocolVersion playerVersion = getProtocolVersion(); - if (playerVersion.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0) { + if (playerVersion.gte(ProtocolVersion.MINECRAFT_1_11)) { // Use the title packet instead. TitlePacket pkt = new TitlePacket(); pkt.setAction(TitlePacket.SET_ACTION_BAR); @@ -257,10 +257,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { // legacy message and then inject the legacy text into a component... yuck! JsonObject object = new JsonObject(); object.addProperty("text", LegacyComponentSerializer.legacySection().serialize(message)); - ChatPacket chat = new ChatPacket(); - chat.setMessage(object.toString()); - chat.setType(ChatPacket.GAME_INFO_TYPE); - connection.write(chat); + connection.write(new ClientboundChatPacket( + object.toString(), + ClientboundChatPacket.GAME_INFO_TYPE, + Identity.nil().uuid() + )); } } @@ -672,10 +673,10 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @Override public void spoofChatInput(String input) { - Preconditions.checkArgument(input.length() <= ChatPacket.MAX_SERVERBOUND_MESSAGE_LENGTH, - "input cannot be greater than " + ChatPacket.MAX_SERVERBOUND_MESSAGE_LENGTH + Preconditions.checkArgument(input.length() <= ServerboundChatPacket.MAX_MESSAGE_LENGTH, + "input cannot be greater than " + ServerboundChatPacket.MAX_MESSAGE_LENGTH + " characters in length"); - ensureBackendConnection().write(ChatPacket.createServerbound(input)); + ensureBackendConnection().write(new ServerboundChatPacket(input)); } @Override @@ -707,9 +708,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { */ public void sendKeepAlive() { if (connection.getState() == StateRegistry.PLAY) { - KeepAlivePacket keepAlive = new KeepAlivePacket(); - keepAlive.setRandomId(ThreadLocalRandom.current().nextLong()); - connection.write(keepAlive); + connection.write(new KeepAlivePacket(ThreadLocalRandom.current().nextLong())); } } @@ -751,7 +750,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { boolean minecraftOrFmlMessage; // By default, all internal Minecraft and Forge channels are forwarded from the server. - if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) <= 0) { + if (version.lte(ProtocolVersion.MINECRAFT_1_12_2)) { String channel = message.getChannel(); minecraftOrFmlMessage = channel.startsWith("MC|") || channel.startsWith(LegacyForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL) 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 cdcc581a4..08a3b1c4a 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 @@ -114,7 +114,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().lt(ProtocolVersion.MINECRAFT_1_13)) { ic.disconnectQuietly(Component.text("This server is only compatible with 1.13 and above.")); return; } @@ -126,9 +126,9 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { private ConnectionType getHandshakeConnectionType(HandshakePacket handshake) { // 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().lt(ProtocolVersion.MINECRAFT_1_13)) { return ConnectionTypes.LEGACY_FORGE; - } else if (handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_7_6) <= 0) { + } else if (handshake.getProtocolVersion().lte(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/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index 4fc9f6ea5..c9e298582 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -242,7 +242,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { private void completeLoginProtocolPhaseAndInitialize(ConnectedPlayer player) { int threshold = server.getConfiguration().getCompressionThreshold(); - if (threshold >= 0 && mcConnection.getProtocolVersion().compareTo(MINECRAFT_1_8) >= 0) { + if (threshold >= 0 && mcConnection.getProtocolVersion().gte(MINECRAFT_1_8)) { mcConnection.write(new SetCompressionPacket(threshold)); mcConnection.setCompressionThreshold(threshold); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java index 234bb28e9..f51459816 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java @@ -225,7 +225,7 @@ public final class DimensionData { String registryIdentifier = dimTag.getString("name"); CompoundBinaryTag details; Integer dimensionId = null; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { dimensionId = dimTag.getInt("id"); details = dimTag.getCompound("element"); } else { @@ -243,7 +243,7 @@ public final class DimensionData { */ public CompoundBinaryTag encodeAsCompoundTag(ProtocolVersion version) { CompoundBinaryTag details = serializeDimensionDetails(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { if (dimensionId == null) { throw new IllegalStateException("Tried to serialize a 1.16.2+ dimension registry entry " + "without an ID"); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/Packet.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/Packet.java index b4dfcee26..e04f1d2c4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/Packet.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/Packet.java @@ -6,9 +6,16 @@ import io.netty.buffer.ByteBuf; public interface Packet { - void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion); + @Deprecated + default void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion) { + throw new IllegalStateException(); + } void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion); boolean handle(MinecraftSessionHandler handler); + + interface Decoder

{ + P decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version); + } } 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 85b479cee..8c4cabc8f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -459,7 +459,7 @@ public enum ProtocolUtils { * @return the appropriate {@link GsonComponentSerializer} */ public static GsonComponentSerializer getJsonChatSerializer(ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { return MODERN_SERIALIZER; } return PRE_1_16_SERIALIZER; 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 d2e694f52..7312aa17d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -18,8 +18,8 @@ import static com.velocitypowered.api.network.ProtocolVersion.SUPPORTED_VERSIONS import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.packet.AvailableCommandsPacket; import com.velocitypowered.proxy.protocol.packet.BossBarPacket; -import com.velocitypowered.proxy.protocol.packet.ChatPacket; import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket; +import com.velocitypowered.proxy.protocol.packet.ClientboundChatPacket; import com.velocitypowered.proxy.protocol.packet.DisconnectPacket; import com.velocitypowered.proxy.protocol.packet.EncryptionRequestPacket; import com.velocitypowered.proxy.protocol.packet.EncryptionResponsePacket; @@ -36,6 +36,7 @@ import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket; import com.velocitypowered.proxy.protocol.packet.RespawnPacket; import com.velocitypowered.proxy.protocol.packet.ServerLoginPacket; import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccessPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundChatPacket; import com.velocitypowered.proxy.protocol.packet.SetCompressionPacket; import com.velocitypowered.proxy.protocol.packet.StatusPingPacket; import com.velocitypowered.proxy.protocol.packet.StatusRequestPacket; @@ -43,6 +44,7 @@ import com.velocitypowered.proxy.protocol.packet.StatusResponsePacket; import com.velocitypowered.proxy.protocol.packet.TabCompleteRequestPacket; import com.velocitypowered.proxy.protocol.packet.TabCompleteResponsePacket; import com.velocitypowered.proxy.protocol.packet.TitlePacket; +import io.netty.buffer.ByteBuf; import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectMap; import it.unimi.dsi.fastutil.objects.Object2IntMap; @@ -65,14 +67,14 @@ public enum StateRegistry { }, STATUS { { - serverbound.register(StatusRequestPacket.class, () -> StatusRequestPacket.INSTANCE, + serverbound.registerNew(StatusRequestPacket.class, StatusRequestPacket.DECODER, map(0x00, MINECRAFT_1_7_2, false)); - serverbound.register(StatusPingPacket.class, StatusPingPacket::new, + serverbound.registerNew(StatusPingPacket.class, StatusPingPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false)); clientbound.register(StatusResponsePacket.class, StatusResponsePacket::new, map(0x00, MINECRAFT_1_7_2, false)); - clientbound.register(StatusPingPacket.class, StatusPingPacket::new, + clientbound.registerNew(StatusPingPacket.class, StatusPingPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false)); } }, @@ -88,7 +90,7 @@ public enum StateRegistry { map(0x01, MINECRAFT_1_12_1, false), map(0x05, MINECRAFT_1_13, false), map(0x06, MINECRAFT_1_14, false)); - serverbound.register(ChatPacket.class, ChatPacket::new, + serverbound.registerNew(ServerboundChatPacket.class, ServerboundChatPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false), map(0x02, MINECRAFT_1_9, false), map(0x03, MINECRAFT_1_12, false), @@ -107,7 +109,7 @@ public enum StateRegistry { map(0x09, MINECRAFT_1_12_1, false), map(0x0A, MINECRAFT_1_13, false), map(0x0B, MINECRAFT_1_14, false)); - serverbound.register(KeepAlivePacket.class, KeepAlivePacket::new, + serverbound.registerNew(KeepAlivePacket.class, KeepAlivePacket.DECODER, map(0x00, MINECRAFT_1_7_2, false), map(0x0B, MINECRAFT_1_9, false), map(0x0C, MINECRAFT_1_12, false), @@ -128,7 +130,7 @@ public enum StateRegistry { map(0x0C, MINECRAFT_1_9, false), map(0x0D, MINECRAFT_1_15, false), map(0x0C, MINECRAFT_1_16, false)); - clientbound.register(ChatPacket.class, ChatPacket::new, + clientbound.register(ClientboundChatPacket.class, ClientboundChatPacket::new, map(0x02, MINECRAFT_1_7_2, true), map(0x0F, MINECRAFT_1_9, true), map(0x0E, MINECRAFT_1_13, true), @@ -162,7 +164,7 @@ public enum StateRegistry { map(0x1B, MINECRAFT_1_15, false), map(0x1A, MINECRAFT_1_16, false), map(0x19, MINECRAFT_1_16_2, false)); - clientbound.register(KeepAlivePacket.class, KeepAlivePacket::new, + clientbound.registerNew(KeepAlivePacket.class, KeepAlivePacket.DECODER, map(0x00, MINECRAFT_1_7_2, false), map(0x1F, MINECRAFT_1_9, false), map(0x21, MINECRAFT_1_13, false), @@ -242,7 +244,7 @@ public enum StateRegistry { map(0x01, MINECRAFT_1_7_2, false)); clientbound.register(ServerLoginSuccessPacket.class, ServerLoginSuccessPacket::new, map(0x02, MINECRAFT_1_7_2, false)); - clientbound.register(SetCompressionPacket.class, SetCompressionPacket::new, + clientbound.registerNew(SetCompressionPacket.class, SetCompressionPacket.DECODER, map(0x03, MINECRAFT_1_8, false)); clientbound.register(LoginPluginMessagePacket.class, LoginPluginMessagePacket::new, map(0x04, MINECRAFT_1_13, false)); @@ -290,7 +292,7 @@ public enum StateRegistry { return registry; } -

void register(Class

clazz, Supplier

packetSupplier, +

void registerNew(Class

clazz, Packet.Decoder

decoder, PacketMapping... mappings) { if (mappings.length == 0) { throw new IllegalArgumentException("At least one mapping must be provided."); @@ -307,6 +309,53 @@ public enum StateRegistry { "Next mapping version (%s) should be lower then current (%s)", to, from)); } + for (ProtocolVersion protocol : EnumSet.range(from, to)) { + if (protocol == to && next != current) { + break; + } + ProtocolRegistry registry = this.versions.get(protocol); + if (registry == null) { + throw new IllegalArgumentException("Unknown protocol version " + + current.protocolVersion); + } + + if (registry.packetIdToSupplier.containsKey(current.id)) { + throw new IllegalArgumentException("Can not register class " + clazz.getSimpleName() + + " with id " + current.id + " for " + registry.version + + " because another packet is already registered"); + } + + if (registry.packetClassToId.containsKey(clazz)) { + throw new IllegalArgumentException(clazz.getSimpleName() + + " is already registered for version " + registry.version); + } + + if (!current.encodeOnly) { + registry.packetIdToDecoder.put(current.id, decoder); + } + registry.packetClassToId.put(clazz, current.id); + } + } + } + + @Deprecated +

void register(Class

clazz, Supplier

packetSupplier, + PacketMapping... mappings) { + if (mappings.length == 0) { + throw new IllegalArgumentException("At least one mapping must be provided."); + } + + for (int i = 0; i < mappings.length; i++) { + PacketMapping current = mappings[i]; + PacketMapping next = (i + 1 < mappings.length) ? mappings[i + 1] : current; + ProtocolVersion from = current.protocolVersion; + ProtocolVersion to = current == next ? getLast(SUPPORTED_VERSIONS) : next.protocolVersion; + + if (from.gte(to) && from != getLast(SUPPORTED_VERSIONS)) { + throw new IllegalArgumentException(String.format( + "Next mapping version (%s) should be lower then current (%s)", to, from)); + } + for (ProtocolVersion protocol : EnumSet.range(from, to)) { if (protocol == to && next != current) { break; @@ -339,8 +388,11 @@ public enum StateRegistry { public class ProtocolRegistry { public final ProtocolVersion version; + @Deprecated final IntObjectMap> packetIdToSupplier = new IntObjectHashMap<>(16, 0.5f); + final IntObjectMap> packetIdToDecoder = + new IntObjectHashMap<>(16, 0.5f); final Object2IntMap> packetClassToId = new Object2IntOpenHashMap<>(16, 0.5f); @@ -355,6 +407,7 @@ public enum StateRegistry { * @param id the packet ID * @return the packet instance, or {@code null} if the ID is not registered */ + @Deprecated public @Nullable Packet createPacket(final int id) { final Supplier supplier = this.packetIdToSupplier.get(id); if (supplier == null) { @@ -363,6 +416,14 @@ public enum StateRegistry { return supplier.get(); } + public @Nullable Packet decodePacket(final int id, ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion) { + final Packet.Decoder decoder = this.packetIdToDecoder.get(id); + if (decoder == null) { + return null; + } + return decoder.decode(buf, direction, protocolVersion); + } + /** * Attempts to look up the packet ID for an {@code packet}. * diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java index e61104923..fd051e4e6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java @@ -53,16 +53,25 @@ public class MinecraftDecoder extends ChannelInboundHandlerAdapter { int originalReaderIndex = buf.readerIndex(); int packetId = ProtocolUtils.readVarInt(buf); - Packet packet = this.registry.createPacket(packetId); + final boolean decoded; + Packet packet = this.registry.decodePacket(packetId, buf, direction, registry.version); + if (packet == null) { + packet = this.registry.createPacket(packetId); + decoded = false; + } else { + decoded = true; + } if (packet == null) { buf.readerIndex(originalReaderIndex); ctx.fireChannelRead(buf); } else { try { - try { - packet.decode(buf, direction, registry.version); - } catch (Exception e) { - throw handleDecodeFailure(e, packet, packetId); + if (!decoded) { + try { + packet.decode(buf, direction, registry.version); + } catch (Exception e) { + throw handleDecodeFailure(e, packet, packetId); + } } if (buf.isReadable()) { 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 efc10203c..7fd9ec8eb 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 @@ -101,13 +101,13 @@ public class ClientSettingsPacket implements Packet { this.chatVisibility = ProtocolUtils.readVarInt(buf); this.chatColors = buf.readBoolean(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) <= 0) { + if (version.lte(ProtocolVersion.MINECRAFT_1_7_6)) { this.difficulty = buf.readByte(); } this.skinParts = buf.readUnsignedByte(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_9)) { this.mainHand = ProtocolUtils.readVarInt(buf); } } @@ -122,13 +122,13 @@ public class ClientSettingsPacket implements Packet { ProtocolUtils.writeVarInt(buf, chatVisibility); buf.writeBoolean(chatColors); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) <= 0) { + if (version.lte(ProtocolVersion.MINECRAFT_1_7_6)) { buf.writeByte(difficulty); } buf.writeByte(skinParts); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_9)) { ProtocolUtils.writeVarInt(buf, mainHand); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java similarity index 53% rename from proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ChatPacket.java rename to proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java index ac8766d2e..5cea44a3e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java @@ -1,35 +1,29 @@ package com.velocitypowered.proxy.protocol.packet; -import com.google.common.base.Preconditions; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.Packet; import com.velocitypowered.proxy.protocol.ProtocolDirection; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; -import net.kyori.adventure.identity.Identity; -import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.UUID; -public class ChatPacket implements Packet { +public class ClientboundChatPacket implements Packet { public static final byte CHAT_TYPE = (byte) 0; public static final byte SYSTEM_TYPE = (byte) 1; public static final byte GAME_INFO_TYPE = (byte) 2; - public static final int MAX_SERVERBOUND_MESSAGE_LENGTH = 256; - public static final UUID EMPTY_SENDER = new UUID(0, 0); - private @Nullable String message; private byte type; private @Nullable UUID sender; - public ChatPacket() { + public ClientboundChatPacket() { } - public ChatPacket(String message, byte type, UUID sender) { + public ClientboundChatPacket(String message, byte type, UUID sender) { this.message = message; this.type = type; this.sender = sender; @@ -42,29 +36,17 @@ public class ChatPacket implements Packet { return message; } - public void setMessage(String message) { - this.message = message; - } - public byte getType() { return type; } - public void setType(byte type) { - this.type = type; - } - public UUID getSenderUuid() { return sender; } - public void setSenderUuid(UUID sender) { - this.sender = sender; - } - @Override public String toString() { - return "Chat{" + return "ClientboundChatPacket{" + "message='" + message + '\'' + ", type=" + type + ", sender=" + sender @@ -74,9 +56,9 @@ public class ChatPacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { message = ProtocolUtils.readString(buf); - if (direction == ProtocolDirection.CLIENTBOUND && version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { type = buf.readByte(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { sender = ProtocolUtils.readUuid(buf); } } @@ -88,10 +70,10 @@ public class ChatPacket implements Packet { throw new IllegalStateException("Message is not specified"); } ProtocolUtils.writeString(buf, message); - if (direction == ProtocolDirection.CLIENTBOUND && version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { buf.writeByte(type); if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - ProtocolUtils.writeUuid(buf, sender == null ? EMPTY_SENDER : sender); + ProtocolUtils.writeUuid(buf, sender); } } } @@ -100,20 +82,4 @@ public class ChatPacket implements Packet { public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } - - public static ChatPacket createClientbound(Identity identity, - Component component, ProtocolVersion version) { - return createClientbound(component, CHAT_TYPE, identity.uuid(), version); - } - - public static ChatPacket createClientbound(Component component, byte type, - UUID sender, ProtocolVersion version) { - Preconditions.checkNotNull(component, "component"); - return new ChatPacket(ProtocolUtils.getJsonChatSerializer(version).serialize(component), type, - sender); - } - - public static ChatPacket createServerbound(String message) { - return new ChatPacket(message, CHAT_TYPE, EMPTY_SENDER); - } } 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 ffcc9a9c0..353ca312a 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 @@ -44,7 +44,7 @@ public class EncryptionRequestPacket implements Packet { public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { this.serverId = ProtocolUtils.readString(buf, 20); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { publicKey = ProtocolUtils.readByteArray(buf, 256); verifyToken = ProtocolUtils.readByteArray(buf, 16); } else { @@ -57,7 +57,7 @@ public class EncryptionRequestPacket implements Packet { public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { ProtocolUtils.writeString(buf, this.serverId); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(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 ec7e89d8e..c1dafc432 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 @@ -33,7 +33,7 @@ public class EncryptionResponsePacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { this.sharedSecret = ProtocolUtils.readByteArray(buf, 256); this.verifyToken = ProtocolUtils.readByteArray(buf, 128); } else { @@ -44,7 +44,7 @@ public class EncryptionResponsePacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeByteArray(buf, sharedSecret); ProtocolUtils.writeByteArray(buf, verifyToken); } else { 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 2c480b334..e030e659c 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 @@ -167,7 +167,7 @@ public class JoinGamePacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { this.entityId = buf.readInt(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { this.isHardcore = buf.readBoolean(); this.gamemode = buf.readByte(); } else { @@ -177,12 +177,12 @@ public class JoinGamePacket implements Packet { } String dimensionIdentifier = null; String levelName = null; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { this.previousGamemode = buf.readByte(); ImmutableSet levelNames = ImmutableSet.copyOf(ProtocolUtils.readStringArray(buf)); CompoundBinaryTag registryContainer = ProtocolUtils.readCompoundTag(buf); ListBinaryTag dimensionRegistryContainer = null; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { dimensionRegistryContainer = registryContainer.getCompound("minecraft:dimension_type") .getList("value", BinaryTagTypes.COMPOUND); this.biomeRegistry = registryContainer.getCompound("minecraft:worldgen/biome"); @@ -193,7 +193,7 @@ public class JoinGamePacket implements Packet { ImmutableSet readData = DimensionRegistry.fromGameData(dimensionRegistryContainer, version); this.dimensionRegistry = new DimensionRegistry(readData, levelNames); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { CompoundBinaryTag currentDimDataTag = ProtocolUtils.readCompoundTag(buf); dimensionIdentifier = ProtocolUtils.readString(buf); this.currentDimensionData = DimensionData.decodeBaseCompoundTag(currentDimDataTag, version) @@ -202,35 +202,35 @@ public class JoinGamePacket implements Packet { dimensionIdentifier = ProtocolUtils.readString(buf); levelName = ProtocolUtils.readString(buf); } - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { + } else if (version.gte(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.lte(ProtocolVersion.MINECRAFT_1_13_2)) { this.difficulty = buf.readUnsignedByte(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_15)) { this.partialHashedSeed = buf.readLong(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { this.maxPlayers = ProtocolUtils.readVarInt(buf); } else { this.maxPlayers = buf.readUnsignedByte(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { + if (version.lt(ProtocolVersion.MINECRAFT_1_16)) { this.levelType = ProtocolUtils.readString(buf, 16); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_14)) { this.viewDistance = ProtocolUtils.readVarInt(buf); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { this.reducedDebugInfo = buf.readBoolean(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_15)) { this.showRespawnScreen = buf.readBoolean(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { boolean isDebug = buf.readBoolean(); boolean isFlat = buf.readBoolean(); this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug); @@ -240,18 +240,18 @@ public class JoinGamePacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { buf.writeInt(entityId); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { buf.writeBoolean(isHardcore); buf.writeByte(gamemode); } else { buf.writeByte(isHardcore ? gamemode | 0x8 : gamemode); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { buf.writeByte(previousGamemode); ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames().toArray(new String[0])); CompoundBinaryTag.Builder registryContainer = CompoundBinaryTag.builder(); ListBinaryTag encodedDimensionRegistry = dimensionRegistry.encodeRegistry(version); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { CompoundBinaryTag.Builder dimensionRegistryEntry = CompoundBinaryTag.builder(); dimensionRegistryEntry.putString("type", "minecraft:dimension_type"); dimensionRegistryEntry.put("value", encodedDimensionRegistry); @@ -261,45 +261,45 @@ public class JoinGamePacket implements Packet { registryContainer.put("dimension", encodedDimensionRegistry); } ProtocolUtils.writeCompoundTag(buf, registryContainer.build()); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { ProtocolUtils.writeCompoundTag(buf, currentDimensionData.serializeDimensionDetails()); ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); } else { ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); } - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { + } else if (version.gte(ProtocolVersion.MINECRAFT_1_9_1)) { buf.writeInt(dimension); } else { buf.writeByte(dimension); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.lte(ProtocolVersion.MINECRAFT_1_13_2)) { buf.writeByte(difficulty); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_15)) { buf.writeLong(partialHashedSeed); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { ProtocolUtils.writeVarInt(buf, maxPlayers); } else { buf.writeByte(maxPlayers); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { + if (version.lt(ProtocolVersion.MINECRAFT_1_16)) { if (levelType == null) { throw new IllegalStateException("No level type specified."); } ProtocolUtils.writeString(buf, levelType); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_14)) { ProtocolUtils.writeVarInt(buf, viewDistance); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { buf.writeBoolean(reducedDebugInfo); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_15)) { buf.writeBoolean(showRespawnScreen); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { buf.writeBoolean(dimensionInfo.isDebugType()); buf.writeBoolean(dimensionInfo.isFlat()); } 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 8e7bb3f85..7ec93930f 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 @@ -9,47 +9,48 @@ import io.netty.buffer.ByteBuf; public class KeepAlivePacket implements Packet { - private long randomId; - - public long getRandomId() { - return randomId; - } - - public void setRandomId(long randomId) { - this.randomId = randomId; - } - - @Override - public String toString() { - return "KeepAlive{" - + "randomId=" + randomId - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { + public static final Decoder DECODER = (buf, direction, version) -> { + final long randomId; if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { randomId = buf.readLong(); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + } else if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { randomId = ProtocolUtils.readVarInt(buf); } else { randomId = buf.readInt(); } + return new KeepAlivePacket(randomId); + }; + + private final long randomId; + + public KeepAlivePacket(final long randomId) { + this.randomId = randomId; } @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_12_2)) { buf.writeLong(randomId); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + } else if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeVarInt(buf, (int) randomId); } else { buf.writeInt((int) randomId); } } + public long getRandomId() { + return randomId; + } + @Override public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + @Override + public String toString() { + return "KeepAlive{" + + "randomId=" + randomId + + '}'; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItemPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItemPacket.java index 64421dd49..7243c8da7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItemPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PlayerListItemPacket.java @@ -44,7 +44,7 @@ public class PlayerListItemPacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { action = ProtocolUtils.readVarInt(buf); int length = ProtocolUtils.readVarInt(buf); @@ -94,7 +94,7 @@ public class PlayerListItemPacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { ProtocolUtils.writeVarInt(buf, action); ProtocolUtils.writeVarInt(buf, items.size()); for (Item item : items) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java index 8ceaecfde..b1ee990c9 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 @@ -48,10 +48,10 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Packet @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { this.channel = ProtocolUtils.readString(buf); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_13)) { this.channel = transformLegacyToModernChannel(this.channel); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { this.replace(buf.readRetainedSlice(buf.readableBytes())); } else { this.replace(ProtocolUtils.readRetainedByteBufSlice17(buf)); @@ -64,12 +64,12 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Packet if (channel == null) { throw new IllegalStateException("Channel is not specified."); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) { + if (version.gte(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.gte(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/ResourcePackResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ResourcePackResponsePacket.java index d5c1b1ef0..7dc865688 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 @@ -23,7 +23,7 @@ public class ResourcePackResponsePacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_4) <= 0) { + if (protocolVersion.lte(ProtocolVersion.MINECRAFT_1_9_4)) { this.hash = ProtocolUtils.readString(buf); } this.status = Status.values()[ProtocolUtils.readVarInt(buf)]; @@ -31,7 +31,7 @@ public class ResourcePackResponsePacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion) { - if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_4) <= 0) { + if (protocolVersion.lte(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 53cf1b5db..8b499a614 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 @@ -115,8 +115,8 @@ public class RespawnPacket implements Packet { public void decode(ByteBuf buf, ProtocolDirection 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) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { CompoundBinaryTag dimDataTag = ProtocolUtils.readCompoundTag(buf); dimensionIdentifier = ProtocolUtils.readString(buf); this.currentDimensionData = DimensionData.decodeBaseCompoundTag(dimDataTag, version) @@ -128,14 +128,14 @@ public class RespawnPacket implements Packet { } else { this.dimension = buf.readInt(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.lte(ProtocolVersion.MINECRAFT_1_13_2)) { this.difficulty = buf.readUnsignedByte(); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_15)) { this.partialHashedSeed = buf.readLong(); } this.gamemode = buf.readByte(); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { this.previousGamemode = buf.readByte(); boolean isDebug = buf.readBoolean(); boolean isFlat = buf.readBoolean(); @@ -148,8 +148,8 @@ public class RespawnPacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16_2)) { ProtocolUtils.writeCompoundTag(buf, currentDimensionData.serializeDimensionDetails()); ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); } else { @@ -159,14 +159,14 @@ public class RespawnPacket implements Packet { } else { buf.writeInt(dimension); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) { + if (version.lte(ProtocolVersion.MINECRAFT_1_13_2)) { buf.writeByte(difficulty); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_15)) { buf.writeLong(partialHashedSeed); } buf.writeByte(gamemode); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { buf.writeByte(previousGamemode); buf.writeBoolean(dimensionInfo.isDebugType()); buf.writeBoolean(dimensionInfo.isFlat()); 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 11b7defba..c8b282762 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 @@ -47,9 +47,9 @@ public class ServerLoginSuccessPacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { uuid = ProtocolUtils.readUuidIntArray(buf); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) >= 0) { + } else if (version.gte(ProtocolVersion.MINECRAFT_1_7_6)) { uuid = UUID.fromString(ProtocolUtils.readString(buf, 36)); } else { uuid = UuidUtils.fromUndashed(ProtocolUtils.readString(buf, 32)); @@ -62,9 +62,9 @@ public class ServerLoginSuccessPacket implements Packet { if (uuid == null) { throw new IllegalStateException("No UUID specified!"); } - if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { ProtocolUtils.writeUuidIntArray(buf, uuid); - } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_7_6) >= 0) { + } else if (version.gte(ProtocolVersion.MINECRAFT_1_7_6)) { ProtocolUtils.writeString(buf, uuid.toString()); } else { ProtocolUtils.writeString(buf, UuidUtils.toUndashed(uuid)); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerboundChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerboundChatPacket.java new file mode 100644 index 000000000..145ab799f --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerboundChatPacket.java @@ -0,0 +1,52 @@ +package com.velocitypowered.proxy.protocol.packet; + +import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import com.velocitypowered.proxy.protocol.Packet; +import com.velocitypowered.proxy.protocol.ProtocolDirection; +import com.velocitypowered.proxy.protocol.ProtocolUtils; +import io.netty.buffer.ByteBuf; +import org.checkerframework.checker.nullness.qual.Nullable; + +public class ServerboundChatPacket implements Packet { + + public static final Decoder DECODER = (buf, direction, version) -> { + final String message = ProtocolUtils.readString(buf); + return new ServerboundChatPacket(message); + }; + + public static final int MAX_MESSAGE_LENGTH = 256; + + private final @Nullable String message; + + public ServerboundChatPacket(String message) { + this.message = message; + } + + @Override + public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { + if (message == null) { + throw new IllegalStateException("Message is not specified"); + } + ProtocolUtils.writeString(buf, message); + } + + public String getMessage() { + if (message == null) { + throw new IllegalStateException("Message is not specified"); + } + return message; + } + + @Override + public boolean handle(MinecraftSessionHandler handler) { + return handler.handle(this); + } + + @Override + public String toString() { + return "Chat{" + + "message='" + message + '\'' + + '}'; + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompressionPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompressionPacket.java index 71a93b56f..4f2a2e943 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompressionPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/SetCompressionPacket.java @@ -9,42 +9,35 @@ import io.netty.buffer.ByteBuf; public class SetCompressionPacket implements Packet { - private int threshold; + public static final Decoder DECODER = (buf, direction, version) -> { + final int threshold = ProtocolUtils.readVarInt(buf); + return new SetCompressionPacket(threshold); + }; - public SetCompressionPacket() { - } + private final int threshold; public SetCompressionPacket(int threshold) { this.threshold = threshold; } - public int getThreshold() { - return threshold; - } - - public void setThreshold(int threshold) { - this.threshold = threshold; - } - - @Override - public String toString() { - return "SetCompression{" - + "threshold=" + threshold - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - this.threshold = ProtocolUtils.readVarInt(buf); - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { ProtocolUtils.writeVarInt(buf, threshold); } + public int getThreshold() { + return threshold; + } + @Override public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + @Override + public String toString() { + return "SetCompression{" + + "threshold=" + threshold + + '}'; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPingPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPingPacket.java index 712559d93..6af926030 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPingPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusPingPacket.java @@ -8,11 +8,15 @@ import io.netty.buffer.ByteBuf; public class StatusPingPacket implements Packet { - private long randomId; + public static Decoder DECODER = (buf, direction, version) -> { + final long randomId = buf.readLong(); + return new StatusPingPacket(randomId); + }; - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - randomId = buf.readLong(); + private final long randomId; + + public StatusPingPacket(final long randomId) { + this.randomId = randomId; } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequestPacket.java index 9e5d8b41a..7db69f407 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusRequestPacket.java @@ -9,14 +9,9 @@ import io.netty.buffer.ByteBuf; public class StatusRequestPacket implements Packet { public static final StatusRequestPacket INSTANCE = new StatusRequestPacket(); + public static Decoder DECODER = (buf, direction, version) -> INSTANCE; private StatusRequestPacket() { - - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - // There is no additional data to decode. } @Override @@ -24,13 +19,13 @@ public class StatusRequestPacket implements Packet { // There is no data to decode. } - @Override - public String toString() { - return "StatusRequest"; - } - @Override public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + @Override + public String toString() { + return "StatusRequestPacket"; + } } 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 21abfd0a8..132015454 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 @@ -79,15 +79,15 @@ public class TabCompleteRequestPacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.gte(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.gte(MINECRAFT_1_9)) { this.assumeCommand = buf.readBoolean(); } - if (version.compareTo(MINECRAFT_1_8) >= 0) { + if (version.gte(MINECRAFT_1_8)) { this.hasPosition = buf.readBoolean(); if (hasPosition) { this.position = buf.readLong(); @@ -102,15 +102,15 @@ public class TabCompleteRequestPacket implements Packet { throw new IllegalStateException("Command is not specified"); } - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.gte(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.gte(MINECRAFT_1_9)) { buf.writeBoolean(assumeCommand); } - if (version.compareTo(MINECRAFT_1_8) >= 0) { + if (version.gte(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 1c3f4dc38..8fffb9742 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 @@ -61,7 +61,7 @@ public class TabCompleteResponsePacket implements Packet { @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.gte(MINECRAFT_1_13)) { this.transactionId = ProtocolUtils.readVarInt(buf); this.start = ProtocolUtils.readVarInt(buf); this.length = ProtocolUtils.readVarInt(buf); @@ -82,7 +82,7 @@ public class TabCompleteResponsePacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - if (version.compareTo(MINECRAFT_1_13) >= 0) { + if (version.gte(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/TitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java index 35f5b1194..5e9e14d13 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/TitlePacket.java @@ -34,7 +34,7 @@ public class TitlePacket implements Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { ProtocolUtils.writeVarInt(buf, action); - if (version.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_11)) { // 1.11+ shifted the action enum by 1 to handle the action bar switch (action) { case SET_TITLE: @@ -121,21 +121,21 @@ public class TitlePacket implements Packet { public static TitlePacket hideForProtocolVersion(ProtocolVersion version) { TitlePacket packet = new TitlePacket(); - packet.setAction(version.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0 ? TitlePacket.HIDE + packet.setAction(version.gte(ProtocolVersion.MINECRAFT_1_11) ? TitlePacket.HIDE : TitlePacket.HIDE_OLD); return packet; } public static TitlePacket resetForProtocolVersion(ProtocolVersion version) { TitlePacket packet = new TitlePacket(); - packet.setAction(version.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0 ? TitlePacket.RESET + packet.setAction(version.gte(ProtocolVersion.MINECRAFT_1_11) ? TitlePacket.RESET : TitlePacket.RESET_OLD); return packet; } public static TitlePacket timesForProtocolVersion(ProtocolVersion version) { TitlePacket packet = new TitlePacket(); - packet.setAction(version.compareTo(ProtocolVersion.MINECRAFT_1_11) >= 0 ? TitlePacket.SET_TIMES + packet.setAction(version.gte(ProtocolVersion.MINECRAFT_1_11) ? TitlePacket.SET_TIMES : TitlePacket.SET_TIMES_OLD); return packet; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java index 0059eac38..6c75538da 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 @@ -111,7 +111,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.gte(ProtocolVersion.MINECRAFT_1_13) ? REGISTER_CHANNEL : REGISTER_CHANNEL_LEGACY; ByteBuf contents = Unpooled.buffer(); contents.writeCharSequence(String.join("\0", channels), StandardCharsets.UTF_8); @@ -135,7 +135,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.gte(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 b07e4815f..57df8e4e0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/VelocityChannelRegistrar.java @@ -94,7 +94,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.gte(ProtocolVersion.MINECRAFT_1_13)) { return getModernChannelIds(); } return getLegacyChannelIds(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/bossbar/AdventureBossBarManager.java b/proxy/src/main/java/com/velocitypowered/proxy/util/bossbar/AdventureBossBarManager.java index 74cb50925..774b79045 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/bossbar/AdventureBossBarManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/bossbar/AdventureBossBarManager.java @@ -112,7 +112,7 @@ public class AdventureBossBarManager implements BossBar.Listener { BossBarPacket rgbPacket = holder.createTitleUpdate( newName, ProtocolVersion.MINECRAFT_1_16); for (ConnectedPlayer player : holder.subscribers) { - if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { + if (player.getProtocolVersion().gte(ProtocolVersion.MINECRAFT_1_16)) { player.getConnection().write(rgbPacket); } else { player.getConnection().write(pre116Packet); diff --git a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java index a386dbbbc..ed4005bd1 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java @@ -93,7 +93,7 @@ class PacketRegistryTest { () -> registry.register(HandshakePacket.class, HandshakePacket::new, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12, false))); assertThrows(IllegalArgumentException.class, - () -> registry.register(StatusPingPacket.class, StatusPingPacket::new, + () -> registry.registerNew(StatusPingPacket.class, StatusPingPacket.DECODER, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_13, false))); }