From f873debb72f208abb3863d5fe77ecc65c97d2c4a Mon Sep 17 00:00:00 2001 From: Riley Park Date: Sun, 8 Nov 2020 01:28:24 -0800 Subject: [PATCH] packets packets packets --- build.gradle | 2 +- .../connection/MinecraftSessionHandler.java | 27 ++- .../forge/legacy/LegacyForgeUtil.java | 8 +- .../BackendChannelInitializerHolder.java | 28 +--- .../network/ChannelInitializerHolder.java | 37 +++++ .../proxy/network/ConnectionManager.java | 8 +- .../ServerChannelInitializerHolder.java | 27 +-- .../proxy/protocol/Packet.java | 20 ++- .../proxy/protocol/StateRegistry.java | 155 +++++++----------- .../protocol/netty/MinecraftDecoder.java | 20 +-- .../packet/AvailableCommandsPacket.java | 2 + .../proxy/protocol/packet/BossBarPacket.java | 4 +- .../protocol/packet/ClientSettingsPacket.java | 4 +- .../packet/ClientboundChatPacket.java | 52 +++--- .../protocol/packet/DisconnectPacket.java | 3 +- .../packet/EncryptionRequestPacket.java | 3 +- .../packet/EncryptionResponsePacket.java | 3 +- .../protocol/packet/HandshakePacket.java | 4 +- .../packet/HeaderAndFooterPacket.java | 26 +-- .../proxy/protocol/packet/JoinGamePacket.java | 3 +- .../protocol/packet/KeepAlivePacket.java | 12 +- .../packet/LoginPluginMessagePacket.java | 89 +++++----- .../packet/LoginPluginResponsePacket.java | 91 +++++----- .../protocol/packet/PlayerListItemPacket.java | 1 + .../protocol/packet/PluginMessagePacket.java | 99 +++++------ .../packet/ResourcePackRequestPacket.java | 3 +- .../packet/ResourcePackResponsePacket.java | 4 +- .../proxy/protocol/packet/RespawnPacket.java | 3 +- .../protocol/packet/ServerLoginPacket.java | 3 +- .../packet/ServerLoginSuccessPacket.java | 3 +- .../packet/ServerboundChatPacket.java | 19 +-- .../protocol/packet/SetCompressionPacket.java | 2 +- .../protocol/packet/StatusPingPacket.java | 2 +- .../protocol/packet/StatusRequestPacket.java | 2 +- .../protocol/packet/StatusResponsePacket.java | 41 +++-- .../packet/TabCompleteRequestPacket.java | 2 + .../packet/TabCompleteResponsePacket.java | 4 +- .../proxy/protocol/packet/TitlePacket.java | 4 +- .../packet/legacy/LegacyDisconnectPacket.java | 2 +- .../packet/legacy/LegacyHandshakePacket.java | 7 +- .../protocol/packet/legacy/LegacyPacket.java | 4 + .../packet/legacy/LegacyPingPacket.java | 23 +-- .../proxy/tablist/VelocityTabList.java | 9 +- .../proxy/protocol/PacketRegistryTest.java | 18 +- 44 files changed, 426 insertions(+), 457 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/network/ChannelInitializerHolder.java create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPacket.java diff --git a/build.gradle b/build.gradle index 976fd5afa..61f79225f 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.github.jengelman.gradle.plugins:shadow:5.0.0' + classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0' } } 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 ce0e5dc5d..a576f8b15 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftSessionHandler.java @@ -40,39 +40,30 @@ public interface MinecraftSessionHandler { } default void handleGeneric(Packet packet) { - } default void handleUnknown(ByteBuf buf) { - } default void connected() { - } default void disconnected() { - } default void activated() { - } default void deactivated() { - } default void exception(Throwable throwable) { - } default void writabilityChanged() { - } default void readCompleted() { - } default boolean handle(AvailableCommandsPacket commands) { @@ -123,14 +114,6 @@ public interface MinecraftSessionHandler { return false; } - default boolean handle(LegacyHandshakePacket packet) { - return false; - } - - default boolean handle(LegacyPingPacket packet) { - return false; - } - default boolean handle(LoginPluginMessagePacket packet) { return false; } @@ -194,4 +177,14 @@ public interface MinecraftSessionHandler { default boolean handle(ResourcePackResponsePacket packet) { return false; } + + // Legacy + + default boolean handle(LegacyHandshakePacket packet) { + return false; + } + + default boolean handle(LegacyPingPacket packet) { + return false; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeUtil.java index f73ea4783..ff22d6798 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeUtil.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeUtil.java @@ -72,9 +72,9 @@ class LegacyForgeUtil { * @return A copy of the reset packet */ static PluginMessagePacket resetPacket() { - PluginMessagePacket msg = new PluginMessagePacket(); - msg.setChannel(FORGE_LEGACY_HANDSHAKE_CHANNEL); - msg.replace(Unpooled.wrappedBuffer(FORGE_LEGACY_HANDSHAKE_RESET_DATA.clone())); - return msg; + return new PluginMessagePacket( + FORGE_LEGACY_HANDSHAKE_CHANNEL, + Unpooled.wrappedBuffer(FORGE_LEGACY_HANDSHAKE_RESET_DATA.clone()) + ); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/BackendChannelInitializerHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/BackendChannelInitializerHolder.java index 7da1ab32d..8847ae099 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/BackendChannelInitializerHolder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/BackendChannelInitializerHolder.java @@ -2,34 +2,10 @@ package com.velocitypowered.proxy.network; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; -import java.util.function.Supplier; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -public class BackendChannelInitializerHolder implements Supplier> { - - private static final Logger LOGGER = LogManager.getLogger(ConnectionManager.class); - private ChannelInitializer initializer; +final class BackendChannelInitializerHolder extends ChannelInitializerHolder { BackendChannelInitializerHolder(final ChannelInitializer initializer) { - this.initializer = initializer; - } - - @Override - public ChannelInitializer get() { - return this.initializer; - } - - /** - * Sets the channel initializer. - * - * @param initializer the new initializer to use - * @deprecated Internal implementation detail - */ - @Deprecated - public void set(final ChannelInitializer initializer) { - LOGGER.warn("The backend channel initializer has been replaced by {}", - Thread.currentThread().getStackTrace()[2]); - this.initializer = initializer; + super("backend channel", initializer); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ChannelInitializerHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ChannelInitializerHolder.java new file mode 100644 index 000000000..efbb433d7 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ChannelInitializerHolder.java @@ -0,0 +1,37 @@ +package com.velocitypowered.proxy.network; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import java.util.function.Supplier; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public abstract class ChannelInitializerHolder + implements Supplier> { + private static final Logger LOGGER = LogManager.getLogger(ChannelInitializerHolder.class); + private final String name; + private ChannelInitializer initializer; + + ChannelInitializerHolder(final String name, final ChannelInitializer initializer) { + this.name = name; + this.initializer = initializer; + } + + @Override + public ChannelInitializer get() { + return this.initializer; + } + + /** + * Sets the channel initializer. + * + * @param initializer the new initializer to use + * @deprecated Internal implementation detail + */ + @Deprecated + public void set(final ChannelInitializer initializer) { + LOGGER.warn("The {} initializer has been replaced by {}", this.name, + Thread.currentThread().getStackTrace()[2]); + this.initializer = initializer; + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java index 1a2ee2ca4..3061557bf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java @@ -43,9 +43,9 @@ public final class ConnectionManager { // These are intentionally made public for plugins like ViaVersion, which inject their own // protocol logic into the proxy. @SuppressWarnings("WeakerAccess") - public final ServerChannelInitializerHolder serverChannelInitializer; + public final ChannelInitializerHolder serverChannelInitializer; @SuppressWarnings("WeakerAccess") - public final BackendChannelInitializerHolder backendChannelInitializer; + public final ChannelInitializerHolder backendChannelInitializer; private final SeparatePoolInetNameResolver resolver; private final AsyncHttpClient httpClient; @@ -198,7 +198,7 @@ public final class ConnectionManager { return bossGroup; } - public ServerChannelInitializerHolder getServerChannelInitializer() { + public ChannelInitializerHolder getServerChannelInitializer() { return this.serverChannelInitializer; } @@ -206,7 +206,7 @@ public final class ConnectionManager { return httpClient; } - public BackendChannelInitializerHolder getBackendChannelInitializer() { + public ChannelInitializerHolder getBackendChannelInitializer() { return this.backendChannelInitializer; } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java index c9b2a772e..a991ab0f8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java @@ -2,33 +2,10 @@ package com.velocitypowered.proxy.network; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; -import java.util.function.Supplier; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -public class ServerChannelInitializerHolder implements Supplier> { - - private static final Logger LOGGER = LogManager.getLogger(ConnectionManager.class); - private ChannelInitializer initializer; +final class ServerChannelInitializerHolder extends ChannelInitializerHolder { ServerChannelInitializerHolder(final ChannelInitializer initializer) { - this.initializer = initializer; - } - - @Override - public ChannelInitializer get() { - return this.initializer; - } - - /** - * Sets the channel initializer. - * @param initializer the new initializer to use - * @deprecated Internal implementation detail - */ - @Deprecated - public void set(final ChannelInitializer initializer) { - LOGGER.warn("The server channel initializer has been replaced by {}", - Thread.currentThread().getStackTrace()[2]); - this.initializer = initializer; + super("server channel", initializer); } } 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 e04f1d2c4..b43fad23b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/Packet.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/Packet.java @@ -3,12 +3,13 @@ package com.velocitypowered.proxy.protocol; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import io.netty.buffer.ByteBuf; +import java.util.function.Supplier; public interface Packet { @Deprecated default void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion protocolVersion); @@ -16,6 +17,21 @@ public interface Packet { boolean handle(MinecraftSessionHandler handler); interface Decoder

{ - P decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version); + P decode(final ByteBuf buf, final ProtocolDirection direction, final ProtocolVersion version); + + static

Decoder

unsupported() { + return (buf, direction, version) -> { + throw new UnsupportedOperationException(); + }; + } + + @Deprecated + static

Decoder

method(final Supplier

factory) { + return (buf, direction, version) -> { + final P packet = factory.get(); + packet.decode(buf, direction, version); + return packet; + }; + } } } 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 7cb2f947f..eea99e08b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -59,57 +59,54 @@ import org.checkerframework.checker.nullness.qual.Nullable; public enum StateRegistry { - HANDSHAKE { + HANDSHAKE(true) { { - serverbound.register(HandshakePacket.class, HandshakePacket::new, + serverbound.register(HandshakePacket.class, HandshakePacket.DECODER, map(0x00, MINECRAFT_1_7_2, false)); } }, - STATUS { + STATUS(true) { { - serverbound.registerNew(StatusRequestPacket.class, StatusRequestPacket.DECODER, + serverbound.register(StatusRequestPacket.class, StatusRequestPacket.DECODER, map(0x00, MINECRAFT_1_7_2, false)); - serverbound.registerNew(StatusPingPacket.class, StatusPingPacket.DECODER, + serverbound.register(StatusPingPacket.class, StatusPingPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false)); - clientbound.register(StatusResponsePacket.class, StatusResponsePacket::new, + clientbound.register(StatusResponsePacket.class, StatusResponsePacket.DECODER, map(0x00, MINECRAFT_1_7_2, false)); - clientbound.registerNew(StatusPingPacket.class, StatusPingPacket.DECODER, + clientbound.register(StatusPingPacket.class, StatusPingPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false)); } }, - PLAY { + PLAY(false) { { - serverbound.fallback = false; - clientbound.fallback = false; - - serverbound.register(TabCompleteRequestPacket.class, TabCompleteRequestPacket::new, + serverbound.register(TabCompleteRequestPacket.class, TabCompleteRequestPacket.DECODER, map(0x14, MINECRAFT_1_7_2, false), map(0x01, MINECRAFT_1_9, false), map(0x02, MINECRAFT_1_12, false), map(0x01, MINECRAFT_1_12_1, false), map(0x05, MINECRAFT_1_13, false), map(0x06, MINECRAFT_1_14, false)); - serverbound.registerNew(ServerboundChatPacket.class, ServerboundChatPacket.DECODER, + serverbound.register(ServerboundChatPacket.class, ServerboundChatPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false), map(0x02, MINECRAFT_1_9, false), map(0x03, MINECRAFT_1_12, false), map(0x02, MINECRAFT_1_12_1, false), map(0x03, MINECRAFT_1_14, false)); - serverbound.register(ClientSettingsPacket.class, ClientSettingsPacket::new, + serverbound.register(ClientSettingsPacket.class, ClientSettingsPacket.DECODER, map(0x15, MINECRAFT_1_7_2, false), map(0x04, MINECRAFT_1_9, false), map(0x05, MINECRAFT_1_12, false), map(0x04, MINECRAFT_1_12_1, false), map(0x05, MINECRAFT_1_14, false)); - serverbound.register(PluginMessagePacket.class, PluginMessagePacket::new, + serverbound.register(PluginMessagePacket.class, PluginMessagePacket.DECODER, map(0x17, MINECRAFT_1_7_2, false), map(0x09, MINECRAFT_1_9, false), map(0x0A, MINECRAFT_1_12, false), map(0x09, MINECRAFT_1_12_1, false), map(0x0A, MINECRAFT_1_13, false), map(0x0B, MINECRAFT_1_14, false)); - serverbound.registerNew(KeepAlivePacket.class, KeepAlivePacket.DECODER, + serverbound.register(KeepAlivePacket.class, KeepAlivePacket.DECODER, map(0x00, MINECRAFT_1_7_2, false), map(0x0B, MINECRAFT_1_9, false), map(0x0C, MINECRAFT_1_12, false), @@ -117,7 +114,7 @@ public enum StateRegistry { map(0x0E, MINECRAFT_1_13, false), map(0x0F, MINECRAFT_1_14, false), map(0x10, MINECRAFT_1_16, false)); - serverbound.register(ResourcePackResponsePacket.class, ResourcePackResponsePacket::new, + serverbound.register(ResourcePackResponsePacket.class, ResourcePackResponsePacket.DECODER, map(0x19, MINECRAFT_1_8, false), map(0x16, MINECRAFT_1_9, false), map(0x18, MINECRAFT_1_12, false), @@ -126,29 +123,29 @@ public enum StateRegistry { map(0x20, MINECRAFT_1_16, false), map(0x21, MINECRAFT_1_16_2, false)); - clientbound.register(BossBarPacket.class, BossBarPacket::new, + clientbound.register(BossBarPacket.class, BossBarPacket.DECODER, map(0x0C, MINECRAFT_1_9, false), map(0x0D, MINECRAFT_1_15, false), map(0x0C, MINECRAFT_1_16, false)); - clientbound.register(ClientboundChatPacket.class, ClientboundChatPacket::new, + clientbound.register(ClientboundChatPacket.class, ClientboundChatPacket.DECODER, map(0x02, MINECRAFT_1_7_2, true), map(0x0F, MINECRAFT_1_9, true), map(0x0E, MINECRAFT_1_13, true), map(0x0F, MINECRAFT_1_15, true), map(0x0E, MINECRAFT_1_16, true)); - clientbound.register(TabCompleteResponsePacket.class, TabCompleteResponsePacket::new, + clientbound.register(TabCompleteResponsePacket.class, TabCompleteResponsePacket.DECODER, map(0x3A, MINECRAFT_1_7_2, false), map(0x0E, MINECRAFT_1_9, false), map(0x10, MINECRAFT_1_13, false), map(0x11, MINECRAFT_1_15, false), map(0x10, MINECRAFT_1_16, false), map(0x0F, MINECRAFT_1_16_2, false)); - clientbound.register(AvailableCommandsPacket.class, AvailableCommandsPacket::new, + clientbound.register(AvailableCommandsPacket.class, AvailableCommandsPacket.DECODER, map(0x11, MINECRAFT_1_13, false), map(0x12, MINECRAFT_1_15, false), map(0x11, MINECRAFT_1_16, false), map(0x10, MINECRAFT_1_16_2, false)); - clientbound.register(PluginMessagePacket.class, PluginMessagePacket::new, + clientbound.register(PluginMessagePacket.class, PluginMessagePacket.DECODER, map(0x3F, MINECRAFT_1_7_2, false), map(0x18, MINECRAFT_1_9, false), map(0x19, MINECRAFT_1_13, false), @@ -156,7 +153,7 @@ public enum StateRegistry { map(0x19, MINECRAFT_1_15, false), map(0x18, MINECRAFT_1_16, false), map(0x17, MINECRAFT_1_16_2, false)); - clientbound.register(DisconnectPacket.class, DisconnectPacket::new, + clientbound.register(DisconnectPacket.class, DisconnectPacket.DECODER, map(0x40, MINECRAFT_1_7_2, false), map(0x1A, MINECRAFT_1_9, false), map(0x1B, MINECRAFT_1_13, false), @@ -164,7 +161,7 @@ public enum StateRegistry { map(0x1B, MINECRAFT_1_15, false), map(0x1A, MINECRAFT_1_16, false), map(0x19, MINECRAFT_1_16_2, false)); - clientbound.registerNew(KeepAlivePacket.class, KeepAlivePacket.DECODER, + clientbound.register(KeepAlivePacket.class, KeepAlivePacket.DECODER, map(0x00, MINECRAFT_1_7_2, false), map(0x1F, MINECRAFT_1_9, false), map(0x21, MINECRAFT_1_13, false), @@ -172,7 +169,7 @@ public enum StateRegistry { map(0x21, MINECRAFT_1_15, false), map(0x20, MINECRAFT_1_16, false), map(0x1F, MINECRAFT_1_16_2, false)); - clientbound.register(JoinGamePacket.class, JoinGamePacket::new, + clientbound.register(JoinGamePacket.class, JoinGamePacket.DECODER, map(0x01, MINECRAFT_1_7_2, false), map(0x23, MINECRAFT_1_9, false), map(0x25, MINECRAFT_1_13, false), @@ -180,7 +177,7 @@ public enum StateRegistry { map(0x26, MINECRAFT_1_15, false), map(0x25, MINECRAFT_1_16, false), map(0x24, MINECRAFT_1_16_2, false)); - clientbound.register(RespawnPacket.class, RespawnPacket::new, + clientbound.register(RespawnPacket.class, RespawnPacket.DECODER, map(0x07, MINECRAFT_1_7_2, true), map(0x33, MINECRAFT_1_9, true), map(0x34, MINECRAFT_1_12, true), @@ -190,7 +187,7 @@ public enum StateRegistry { map(0x3B, MINECRAFT_1_15, true), map(0x3A, MINECRAFT_1_16, true), map(0x39, MINECRAFT_1_16_2, true)); - clientbound.register(ResourcePackRequestPacket.class, ResourcePackRequestPacket::new, + clientbound.register(ResourcePackRequestPacket.class, ResourcePackRequestPacket.DECODER, map(0x48, MINECRAFT_1_8, true), map(0x32, MINECRAFT_1_9, true), map(0x33, MINECRAFT_1_12, true), @@ -200,7 +197,7 @@ public enum StateRegistry { map(0x3A, MINECRAFT_1_15, true), map(0x39, MINECRAFT_1_16, true), map(0x38, MINECRAFT_1_16_2, true)); - clientbound.register(HeaderAndFooterPacket.class, HeaderAndFooterPacket::new, + clientbound.register(HeaderAndFooterPacket.class, HeaderAndFooterPacket.DECODER, map(0x47, MINECRAFT_1_8, true), map(0x48, MINECRAFT_1_9, true), map(0x47, MINECRAFT_1_9_4, true), @@ -210,7 +207,7 @@ public enum StateRegistry { map(0x53, MINECRAFT_1_14, true), map(0x54, MINECRAFT_1_15, true), map(0x53, MINECRAFT_1_16, true)); - clientbound.registerNew(TitlePacket.class, TitlePacket.DECODER, + clientbound.register(TitlePacket.class, TitlePacket.DECODER, map(0x45, MINECRAFT_1_8, true), map(0x45, MINECRAFT_1_9, true), map(0x47, MINECRAFT_1_12, true), @@ -219,7 +216,7 @@ public enum StateRegistry { map(0x4F, MINECRAFT_1_14, true), map(0x50, MINECRAFT_1_15, true), map(0x4F, MINECRAFT_1_16, true)); - clientbound.register(PlayerListItemPacket.class, PlayerListItemPacket::new, + clientbound.register(PlayerListItemPacket.class, PlayerListItemPacket.DECODER, map(0x38, MINECRAFT_1_7_2, false), map(0x2D, MINECRAFT_1_9, false), map(0x2E, MINECRAFT_1_12_1, false), @@ -230,31 +227,36 @@ public enum StateRegistry { map(0x32, MINECRAFT_1_16_2, false)); } }, - LOGIN { + LOGIN(true) { { - serverbound.register(ServerLoginPacket.class, ServerLoginPacket::new, + serverbound.register(ServerLoginPacket.class, ServerLoginPacket.DECODER, map(0x00, MINECRAFT_1_7_2, false)); - serverbound.register(EncryptionResponsePacket.class, EncryptionResponsePacket::new, + serverbound.register(EncryptionResponsePacket.class, EncryptionResponsePacket.DECODER, map(0x01, MINECRAFT_1_7_2, false)); - serverbound.register(LoginPluginResponsePacket.class, LoginPluginResponsePacket::new, + serverbound.register(LoginPluginResponsePacket.class, LoginPluginResponsePacket.DECODER, map(0x02, MINECRAFT_1_13, false)); - clientbound.register(DisconnectPacket.class, DisconnectPacket::new, + clientbound.register(DisconnectPacket.class, DisconnectPacket.DECODER, map(0x00, MINECRAFT_1_7_2, false)); - clientbound.register(EncryptionRequestPacket.class, EncryptionRequestPacket::new, + clientbound.register(EncryptionRequestPacket.class, EncryptionRequestPacket.DECODER, map(0x01, MINECRAFT_1_7_2, false)); - clientbound.register(ServerLoginSuccessPacket.class, ServerLoginSuccessPacket::new, + clientbound.register(ServerLoginSuccessPacket.class, ServerLoginSuccessPacket.DECODER, map(0x02, MINECRAFT_1_7_2, false)); - clientbound.registerNew(SetCompressionPacket.class, SetCompressionPacket.DECODER, + clientbound.register(SetCompressionPacket.class, SetCompressionPacket.DECODER, map(0x03, MINECRAFT_1_8, false)); - clientbound.register(LoginPluginMessagePacket.class, LoginPluginMessagePacket::new, + clientbound.register(LoginPluginMessagePacket.class, LoginPluginMessagePacket.DECODER, map(0x04, MINECRAFT_1_13, false)); } }; public static final int STATUS_ID = 1; public static final int LOGIN_ID = 2; - public final PacketRegistry clientbound = new PacketRegistry(ProtocolDirection.CLIENTBOUND); - public final PacketRegistry serverbound = new PacketRegistry(ProtocolDirection.SERVERBOUND); + public final PacketRegistry clientbound; + public final PacketRegistry serverbound; + + StateRegistry(boolean fallback) { + this.clientbound = new PacketRegistry(ProtocolDirection.CLIENTBOUND, fallback); + this.serverbound = new PacketRegistry(ProtocolDirection.SERVERBOUND, fallback); + } public PacketRegistry.ProtocolRegistry getProtocolRegistry(ProtocolDirection direction, ProtocolVersion version) { @@ -266,10 +268,15 @@ public enum StateRegistry { private final ProtocolDirection direction; private final Map versions; - private boolean fallback = true; + private final boolean fallback; PacketRegistry(ProtocolDirection direction) { + this(direction, true); + } + + PacketRegistry(ProtocolDirection direction, boolean fallback) { this.direction = direction; + this.fallback = fallback; Map mutableVersions = new EnumMap<>(ProtocolVersion.class); for (ProtocolVersion version : ProtocolVersion.values()) { @@ -292,7 +299,7 @@ public enum StateRegistry { return registry; } -

void registerNew(Class

clazz, Packet.Decoder

decoder, +

void register(Class

clazz, Packet.Decoder

decoder, PacketMapping... mappings) { if (mappings.length == 0) { throw new IllegalArgumentException("At least one mapping must be provided."); @@ -338,53 +345,6 @@ public enum StateRegistry { } } - @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; - } - 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.packetIdToSupplier.put(current.id, packetSupplier); - } - registry.packetClassToId.put(clazz, current.id); - } - } - } - public class ProtocolRegistry { public final ProtocolVersion version; @@ -408,7 +368,7 @@ public enum StateRegistry { * @return the packet instance, or {@code null} if the ID is not registered */ @Deprecated - public @Nullable Packet createPacket(final int id) { + @Nullable Packet createPacket(final int id) { final Supplier supplier = this.packetIdToSupplier.get(id); if (supplier == null) { return null; @@ -416,13 +376,22 @@ public enum StateRegistry { return supplier.get(); } + /** + * Attempts to create a packet from the specified {@code id}. + * + * @param id the packet ID + * @param buf the bytebuf + * @param direction the packet direction + * @param version the protocol version + * @return the packet instance, or {@code null} if the ID is not registered + */ public @Nullable Packet decodePacket(final int id, ByteBuf buf, ProtocolDirection direction, - ProtocolVersion protocolVersion) { + ProtocolVersion version) { final Packet.Decoder decoder = this.packetIdToDecoder.get(id); if (decoder == null) { return null; } - return decoder.decode(buf, direction, protocolVersion); + return decoder.decode(buf, direction, version); } /** 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 fd051e4e6..b24ba7d07 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,27 +53,17 @@ public class MinecraftDecoder extends ChannelInboundHandlerAdapter { int originalReaderIndex = buf.readerIndex(); int packetId = ProtocolUtils.readVarInt(buf); - 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; + Packet packet = null; + try { + packet = this.registry.decodePacket(packetId, buf, direction, registry.version); + } catch (Exception e) { + throw handleDecodeFailure(e, packet, packetId); // TODO: packet is always null } if (packet == null) { buf.readerIndex(originalReaderIndex); ctx.fireChannelRead(buf); } else { try { - if (!decoded) { - try { - packet.decode(buf, direction, registry.version); - } catch (Exception e) { - throw handleDecodeFailure(e, packet, packetId); - } - } - if (buf.isReadable()) { throw handleNotReadEnough(packet, packetId); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/AvailableCommandsPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/AvailableCommandsPacket.java index b563391c6..ffce940e9 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/AvailableCommandsPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/AvailableCommandsPacket.java @@ -36,6 +36,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; public class AvailableCommandsPacket implements Packet { + public static final Decoder DECODER = Decoder.method(AvailableCommandsPacket::new); + private static final Command PLACEHOLDER_COMMAND = source -> 0; private static final byte NODE_TYPE_ROOT = 0x00; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBarPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBarPacket.java index 64440cf8f..714906e90 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBarPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/BossBarPacket.java @@ -11,6 +11,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class BossBarPacket implements Packet { + public static final Decoder DECODER = Decoder.method(BossBarPacket::new); + public static final int ADD = 0; public static final int REMOVE = 1; public static final int UPDATE_PERCENT = 2; @@ -87,7 +89,7 @@ public class BossBarPacket implements Packet { @Override public String toString() { - return "BossBar{" + return "BossBarPacket{" + "uuid=" + uuid + ", action=" + action + ", name='" + name + '\'' 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 7fd9ec8eb..3e2e6ddb5 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 @@ -10,6 +10,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class ClientSettingsPacket implements Packet { + public static final Decoder DECODER = Decoder.method(ClientSettingsPacket::new); + private @Nullable String locale; private byte viewDistance; private int chatVisibility; @@ -84,7 +86,7 @@ public class ClientSettingsPacket implements Packet { @Override public String toString() { - return "ClientSettings{" + return "ClientSettingsPacket{" + "locale='" + locale + '\'' + ", viewDistance=" + viewDistance + ", chatVisibility=" + chatVisibility diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java index 5cea44a3e..a10f23339 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundChatPacket.java @@ -12,6 +12,8 @@ import java.util.UUID; public class ClientboundChatPacket implements Packet { + public static final Decoder DECODER = Decoder.method(ClientboundChatPacket::new); + 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; @@ -20,7 +22,7 @@ public class ClientboundChatPacket implements Packet { private byte type; private @Nullable UUID sender; - public ClientboundChatPacket() { + private ClientboundChatPacket() { } public ClientboundChatPacket(String message, byte type, UUID sender) { @@ -29,30 +31,6 @@ public class ClientboundChatPacket implements Packet { this.sender = sender; } - public String getMessage() { - if (message == null) { - throw new IllegalStateException("Message is not specified"); - } - return message; - } - - public byte getType() { - return type; - } - - public UUID getSenderUuid() { - return sender; - } - - @Override - public String toString() { - return "ClientboundChatPacket{" - + "message='" + message + '\'' - + ", type=" + type - + ", sender=" + sender - + '}'; - } - @Override public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { message = ProtocolUtils.readString(buf); @@ -82,4 +60,28 @@ public class ClientboundChatPacket implements Packet { public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + public String getMessage() { + if (message == null) { + throw new IllegalStateException("Message is not specified"); + } + return message; + } + + public byte getType() { + return type; + } + + public UUID getSenderUuid() { + return sender; + } + + @Override + public String toString() { + return "ClientboundChatPacket{" + + "message='" + message + '\'' + + ", type=" + type + + ", sender=" + sender + + '}'; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/DisconnectPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/DisconnectPacket.java index 5ae0bc162..05a44b9cc 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/DisconnectPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/DisconnectPacket.java @@ -11,6 +11,7 @@ import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; public class DisconnectPacket implements Packet { + public static final Decoder DECODER = Decoder.method(DisconnectPacket::new); private @Nullable String reason; @@ -34,7 +35,7 @@ public class DisconnectPacket implements Packet { @Override public String toString() { - return "Disconnect{" + return "DisconnectPacket{" + "reason='" + reason + '\'' + '}'; } 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 353ca312a..fcc624754 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 @@ -11,6 +11,7 @@ import io.netty.buffer.ByteBuf; import java.util.Arrays; public class EncryptionRequestPacket implements Packet { + public static final Decoder DECODER = Decoder.method(EncryptionRequestPacket::new); private String serverId = ""; private byte[] publicKey = EMPTY_BYTE_ARRAY; @@ -34,7 +35,7 @@ public class EncryptionRequestPacket implements Packet { @Override public String toString() { - return "EncryptionRequest{" + return "EncryptionRequestPacket{" + "publicKey=" + Arrays.toString(publicKey) + ", verifyToken=" + Arrays.toString(verifyToken) + '}'; 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 c1dafc432..06f1c660a 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 @@ -11,6 +11,7 @@ import io.netty.buffer.ByteBuf; import java.util.Arrays; public class EncryptionResponsePacket implements Packet { + public static final Decoder DECODER = Decoder.method(EncryptionResponsePacket::new); private byte[] sharedSecret = EMPTY_BYTE_ARRAY; private byte[] verifyToken = EMPTY_BYTE_ARRAY; @@ -25,7 +26,7 @@ public class EncryptionResponsePacket implements Packet { @Override public String toString() { - return "EncryptionResponse{" + return "EncryptionResponsePacket{" + "sharedSecret=" + Arrays.toString(sharedSecret) + ", verifyToken=" + Arrays.toString(verifyToken) + '}'; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HandshakePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HandshakePacket.java index cab9f3328..9d11ea1f5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HandshakePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HandshakePacket.java @@ -9,6 +9,8 @@ import io.netty.buffer.ByteBuf; public class HandshakePacket implements Packet { + public static final Decoder DECODER = Decoder.method(HandshakePacket::new); + private ProtocolVersion protocolVersion; private String serverAddress = ""; private int port; @@ -48,7 +50,7 @@ public class HandshakePacket implements Packet { @Override public String toString() { - return "Handshake{" + return "HandshakePacket{" + "protocolVersion=" + protocolVersion + ", serverAddress='" + serverAddress + '\'' + ", port=" + port diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooterPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooterPacket.java index fc7900453..8004ae70c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooterPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/HeaderAndFooterPacket.java @@ -7,11 +7,10 @@ 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.text.serializer.gson.GsonComponentSerializer; public class HeaderAndFooterPacket implements Packet { + public static final Decoder DECODER = Decoder.method(HeaderAndFooterPacket::new); private static final String EMPTY_COMPONENT = "{\"translate\":\"\"}"; private static final HeaderAndFooterPacket RESET = new HeaderAndFooterPacket(); @@ -28,19 +27,6 @@ public class HeaderAndFooterPacket implements Packet { this.footer = Preconditions.checkNotNull(footer, "footer"); } - public String getHeader() { - return header; - } - - public String getFooter() { - return footer; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - throw new UnsupportedOperationException("Decode is not implemented"); - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { writeString(buf, header); @@ -52,10 +38,12 @@ public class HeaderAndFooterPacket implements Packet { return handler.handle(this); } - public static HeaderAndFooterPacket create(net.kyori.adventure.text.Component header, - net.kyori.adventure.text.Component footer, ProtocolVersion protocolVersion) { - GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(protocolVersion); - return new HeaderAndFooterPacket(serializer.serialize(header), serializer.serialize(footer)); + public String getHeader() { + return header; + } + + public String getFooter() { + return footer; } public static HeaderAndFooterPacket reset() { 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 e030e659c..1d8fcb3c4 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 @@ -16,6 +16,7 @@ import net.kyori.adventure.nbt.ListBinaryTag; import org.checkerframework.checker.nullness.qual.Nullable; public class JoinGamePacket implements Packet { + public static final Decoder DECODER = Decoder.method(JoinGamePacket::new); private int entityId; private short gamemode; @@ -148,7 +149,7 @@ public class JoinGamePacket implements Packet { @Override public String toString() { - return "JoinGame{" + return "JoinGamePacket{" + "entityId=" + entityId + ", gamemode=" + gamemode + ", dimension=" + dimension 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 7ec93930f..a8e9afb55 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 @@ -11,7 +11,7 @@ public class KeepAlivePacket implements Packet { public static final Decoder DECODER = (buf, direction, version) -> { final long randomId; - if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) { + if (version.gte(ProtocolVersion.MINECRAFT_1_12_2)) { randomId = buf.readLong(); } else if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { randomId = ProtocolUtils.readVarInt(buf); @@ -38,18 +38,18 @@ public class KeepAlivePacket implements Packet { } } - public long getRandomId() { - return randomId; - } - @Override public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + public long getRandomId() { + return randomId; + } + @Override public String toString() { - return "KeepAlive{" + return "KeepAlivePacket{" + "randomId=" + randomId + '}'; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessagePacket.java index c5287cc07..b50ac10e1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessagePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginMessagePacket.java @@ -5,19 +5,28 @@ 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 com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder; import io.netty.buffer.ByteBuf; +import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.Unpooled; +import java.util.Objects; import org.checkerframework.checker.nullness.qual.Nullable; -public class LoginPluginMessagePacket extends DeferredByteBufHolder implements Packet { +public class LoginPluginMessagePacket extends DefaultByteBufHolder implements Packet { - private int id; - private @Nullable String channel; + public static final Decoder DECODER = (buf, direction, version) -> { + final int id = ProtocolUtils.readVarInt(buf); + final String channel = ProtocolUtils.readString(buf); + final ByteBuf data; + if (buf.isReadable()) { + data = buf.readSlice(buf.readableBytes()); + } else { + data = Unpooled.EMPTY_BUFFER; + } + return new LoginPluginMessagePacket(id, channel, data); + }; - public LoginPluginMessagePacket() { - super(null); - } + private final int id; + private final @Nullable String channel; public LoginPluginMessagePacket(int id, @Nullable String channel, ByteBuf data) { super(data); @@ -25,37 +34,6 @@ public class LoginPluginMessagePacket extends DeferredByteBufHolder implements P this.channel = channel; } - public int getId() { - return id; - } - - public String getChannel() { - if (channel == null) { - throw new IllegalStateException("Channel is not specified!"); - } - return channel; - } - - @Override - public String toString() { - return "LoginPluginMessage{" - + "id=" + id - + ", channel='" + channel + '\'' - + ", data=" + super.toString() - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - this.id = ProtocolUtils.readVarInt(buf); - this.channel = ProtocolUtils.readString(buf); - if (buf.isReadable()) { - this.replace(buf.readSlice(buf.readableBytes())); - } else { - this.replace(Unpooled.EMPTY_BUFFER); - } - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { ProtocolUtils.writeVarInt(buf, id); @@ -70,4 +48,39 @@ public class LoginPluginMessagePacket extends DeferredByteBufHolder implements P public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + public int getId() { + return id; + } + + public String getChannel() { + if (channel == null) { + throw new IllegalStateException("Channel is not specified!"); + } + return channel; + } + + @Override + public String toString() { + return "LoginPluginMessagePacket{" + + "id=" + id + + ", channel='" + channel + '\'' + + ", data=" + super.toString() + + '}'; + } + + @Override + public boolean equals(final Object other) { + if(this == other) return true; + if(other == null || this.getClass() != other.getClass()) return false; + final LoginPluginMessagePacket that = (LoginPluginMessagePacket) other; + return this.id == that.id + && Objects.equals(this.channel, that.channel) + && super.equals(other); + } + + @Override + public int hashCode() { + return Objects.hash(this.id, this.channel, super.hashCode()); + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponsePacket.java index bfd403f56..ba1b6e738 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponsePacket.java @@ -5,19 +5,28 @@ 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 com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder; import io.netty.buffer.ByteBuf; +import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.Unpooled; +import java.util.Objects; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; -public class LoginPluginResponsePacket extends DeferredByteBufHolder implements Packet { +public class LoginPluginResponsePacket extends DefaultByteBufHolder implements Packet { - private int id; - private boolean success; + public static final Decoder DECODER = (buf, direction, version) -> { + final int id = ProtocolUtils.readVarInt(buf); + final boolean success = buf.readBoolean(); + final ByteBuf data; + if (buf.isReadable()) { + data = buf.readSlice(buf.readableBytes()); + } else { + data = Unpooled.EMPTY_BUFFER; + } + return new LoginPluginResponsePacket(id, success, data); + }; - public LoginPluginResponsePacket() { - super(Unpooled.EMPTY_BUFFER); - } + private final int id; + private final boolean success; public LoginPluginResponsePacket(int id, boolean success, @MonotonicNonNull ByteBuf buf) { super(buf); @@ -25,42 +34,6 @@ public class LoginPluginResponsePacket extends DeferredByteBufHolder implements this.success = success; } - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public boolean isSuccess() { - return success; - } - - public void setSuccess(boolean success) { - this.success = success; - } - - @Override - public String toString() { - return "LoginPluginResponse{" - + "id=" + id - + ", success=" + success - + ", data=" + super.toString() - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - this.id = ProtocolUtils.readVarInt(buf); - this.success = buf.readBoolean(); - if (buf.isReadable()) { - this.replace(buf.readSlice(buf.readableBytes())); - } else { - this.replace(Unpooled.EMPTY_BUFFER); - } - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { ProtocolUtils.writeVarInt(buf, id); @@ -72,4 +45,36 @@ public class LoginPluginResponsePacket extends DeferredByteBufHolder implements public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + public int getId() { + return id; + } + + public boolean isSuccess() { + return success; + } + + @Override + public String toString() { + return "LoginPluginResponsePacket{" + + "id=" + id + + ", success=" + success + + ", data=" + super.toString() + + '}'; + } + + @Override + public boolean equals(final Object other) { + if(this == other) return true; + if(other == null || this.getClass() != other.getClass()) return false; + final LoginPluginResponsePacket that = (LoginPluginResponsePacket) other; + return this.id == that.id + && Objects.equals(this.success, that.success) + && super.equals(other); + } + + @Override + public int hashCode() { + return Objects.hash(this.id, this.success, super.hashCode()); + } } 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 7243c8da7..992fc5085 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 @@ -17,6 +17,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.checkerframework.checker.nullness.qual.Nullable; public class PlayerListItemPacket implements Packet { + public static final Decoder DECODER = Decoder.method(PlayerListItemPacket::new); public static final int ADD_PLAYER = 0; public static final int UPDATE_GAMEMODE = 1; 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 b1ee990c9..a23450812 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 @@ -7,18 +7,29 @@ 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 com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder; import io.netty.buffer.ByteBuf; +import io.netty.buffer.DefaultByteBufHolder; +import java.util.Objects; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; -public class PluginMessagePacket extends DeferredByteBufHolder implements Packet { +public class PluginMessagePacket extends DefaultByteBufHolder implements Packet { - private @Nullable String channel; + public static final Decoder DECODER = (buf, direction, version) -> { + String channel = ProtocolUtils.readString(buf); + if (version.gte(ProtocolVersion.MINECRAFT_1_13)) { + channel = transformLegacyToModernChannel(channel); + } + final ByteBuf data; + if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { + data = buf.readRetainedSlice(buf.readableBytes()); + } else { + data = ProtocolUtils.readRetainedByteBufSlice17(buf); + } + return new PluginMessagePacket(channel, data); + }; - public PluginMessagePacket() { - super(null); - } + private final @Nullable String channel; public PluginMessagePacket(String channel, @MonotonicNonNull ByteBuf backing) { @@ -26,39 +37,6 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Packet this.channel = channel; } - public String getChannel() { - if (channel == null) { - throw new IllegalStateException("Channel is not specified."); - } - return channel; - } - - public void setChannel(String channel) { - this.channel = channel; - } - - @Override - public String toString() { - return "PluginMessage{" - + "channel='" + channel + '\'' - + ", data=" + super.toString() - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - this.channel = ProtocolUtils.readString(buf); - if (version.gte(ProtocolVersion.MINECRAFT_1_13)) { - this.channel = transformLegacyToModernChannel(this.channel); - } - if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { - this.replace(buf.readRetainedSlice(buf.readableBytes())); - } else { - this.replace(ProtocolUtils.readRetainedByteBufSlice17(buf)); - } - - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { if (channel == null) { @@ -74,7 +52,6 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Packet } else { ProtocolUtils.writeByteBuf17(content(), buf, true); // True for Forge support } - } @Override @@ -82,24 +59,16 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Packet return handler.handle(this); } - @Override - public PluginMessagePacket copy() { - return (PluginMessagePacket) super.copy(); - } - - @Override - public PluginMessagePacket duplicate() { - return (PluginMessagePacket) super.duplicate(); - } - - @Override - public PluginMessagePacket retainedDuplicate() { - return (PluginMessagePacket) super.retainedDuplicate(); + public String getChannel() { + if (channel == null) { + throw new IllegalStateException("Channel is not specified."); + } + return channel; } @Override public PluginMessagePacket replace(ByteBuf content) { - return (PluginMessagePacket) super.replace(content); + return new PluginMessagePacket(this.channel, content); } @Override @@ -121,4 +90,26 @@ public class PluginMessagePacket extends DeferredByteBufHolder implements Packet public PluginMessagePacket touch(Object hint) { return (PluginMessagePacket) super.touch(hint); } + + @Override + public String toString() { + return "PluginMessagePacket{" + + "channel='" + channel + '\'' + + ", data=" + super.toString() + + '}'; + } + + @Override + public boolean equals(final Object other) { + if(this == other) return true; + if(other == null || this.getClass() != other.getClass()) return false; + final PluginMessagePacket that = (PluginMessagePacket) other; + return Objects.equals(this.channel, that.channel) + && super.equals(other); + } + + @Override + public int hashCode() { + return Objects.hash(this.channel, super.hashCode()); + } } 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 1bd277173..6b4b2504f 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 @@ -10,6 +10,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; public class ResourcePackRequestPacket implements Packet { + public static final Decoder DECODER = Decoder.method(ResourcePackRequestPacket::new); private @MonotonicNonNull String url; private @MonotonicNonNull String hash; @@ -52,7 +53,7 @@ public class ResourcePackRequestPacket implements Packet { @Override public String toString() { - return "ResourcePackRequest{" + return "ResourcePackRequestPacket{" + "url='" + url + '\'' + ", hash='" + hash + '\'' + '}'; 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 7dc865688..98433d7e4 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 @@ -11,6 +11,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; public class ResourcePackResponsePacket implements Packet { + public static final Decoder DECODER = Decoder.method(ResourcePackResponsePacket::new); + private String hash = ""; private @MonotonicNonNull Status status; @@ -44,7 +46,7 @@ public class ResourcePackResponsePacket implements Packet { @Override public String toString() { - return "ResourcePackResponse{" + return "ResourcePackResponsePacket{" + "hash=" + hash + ", " + "status=" + status + '}'; 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 8b499a614..81c35f365 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 @@ -11,6 +11,7 @@ import io.netty.buffer.ByteBuf; import net.kyori.adventure.nbt.CompoundBinaryTag; public class RespawnPacket implements Packet { + public static final Decoder DECODER = Decoder.method(RespawnPacket::new); private int dimension; private long partialHashedSeed; @@ -97,7 +98,7 @@ public class RespawnPacket implements Packet { @Override public String toString() { - return "Respawn{" + return "RespawnPacket{" + "dimension=" + dimension + ", partialHashedSeed=" + partialHashedSeed + ", difficulty=" + difficulty 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 500fe4cdd..b778cef00 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 @@ -11,6 +11,7 @@ import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; public class ServerLoginPacket implements Packet { + public static final Decoder DECODER = Decoder.method(ServerLoginPacket::new); private static final QuietDecoderException EMPTY_USERNAME = new QuietDecoderException("Empty username!"); @@ -32,7 +33,7 @@ public class ServerLoginPacket implements Packet { @Override public String toString() { - return "ServerLogin{" + return "ServerLoginPacket{" + "username='" + username + '\'' + '}'; } 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 c8b282762..160a8d213 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 @@ -11,6 +11,7 @@ import java.util.UUID; import org.checkerframework.checker.nullness.qual.Nullable; public class ServerLoginSuccessPacket implements Packet { + public static final Decoder DECODER = Decoder.method(ServerLoginSuccessPacket::new); private @Nullable UUID uuid; private @Nullable String username; @@ -39,7 +40,7 @@ public class ServerLoginSuccessPacket implements Packet { @Override public String toString() { - return "ServerLoginSuccess{" + return "ServerLoginSuccessPacket{" + "uuid=" + uuid + ", username='" + username + '\'' + '}'; 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 index 145ab799f..25f5c6507 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerboundChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerboundChatPacket.java @@ -6,7 +6,6 @@ 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 { @@ -17,7 +16,7 @@ public class ServerboundChatPacket implements Packet { public static final int MAX_MESSAGE_LENGTH = 256; - private final @Nullable String message; + private final String message; public ServerboundChatPacket(String message) { this.message = message; @@ -25,27 +24,21 @@ public class ServerboundChatPacket implements Packet { @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); } + public String getMessage() { + return message; + } + @Override public String toString() { - return "Chat{" + return "ServerboundChatPacket{" + "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 4f2a2e943..3423cbfaa 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 @@ -36,7 +36,7 @@ public class SetCompressionPacket implements Packet { @Override public String toString() { - return "SetCompression{" + return "SetCompressionPacket{" + "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 6af926030..501164737 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,7 +8,7 @@ import io.netty.buffer.ByteBuf; public class StatusPingPacket implements Packet { - public static Decoder DECODER = (buf, direction, version) -> { + public static final Decoder DECODER = (buf, direction, version) -> { final long randomId = buf.readLong(); return new StatusPingPacket(randomId); }; 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 7db69f407..f7872628e 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,7 +9,7 @@ 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; + public static final Decoder DECODER = (buf, direction, version) -> INSTANCE; private StatusRequestPacket() { } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponsePacket.java index 36199352f..9bde7cabc 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/StatusResponsePacket.java @@ -10,34 +10,17 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class StatusResponsePacket implements Packet { - private @Nullable CharSequence status; + public static final Decoder DECODER = (buf, direction, version) -> { + final String status = ProtocolUtils.readString(buf, Short.MAX_VALUE); + return new StatusResponsePacket(status); + }; - public StatusResponsePacket() { - } + private final @Nullable CharSequence status; public StatusResponsePacket(CharSequence status) { this.status = status; } - public String getStatus() { - if (status == null) { - throw new IllegalStateException("Status is not specified"); - } - return status.toString(); - } - - @Override - public String toString() { - return "StatusResponse{" - + "status='" + status + '\'' - + '}'; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - status = ProtocolUtils.readString(buf, Short.MAX_VALUE); - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { if (status == null) { @@ -50,4 +33,18 @@ public class StatusResponsePacket implements Packet { public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + public String getStatus() { + if (status == null) { + throw new IllegalStateException("Status is not specified"); + } + return status.toString(); + } + + @Override + public String toString() { + return "StatusResponsePacket{" + + "status='" + status + '\'' + + '}'; + } } 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 132015454..d06c9bf38 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 @@ -15,6 +15,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class TabCompleteRequestPacket implements Packet { + public static final Decoder DECODER = Decoder.method(TabCompleteRequestPacket::new); + private static final int VANILLA_MAX_TAB_COMPLETE_LEN = 2048; private @Nullable String command; 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 8fffb9742..c9f11b282 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 @@ -16,6 +16,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class TabCompleteResponsePacket implements Packet { + public static final Decoder DECODER = Decoder.method(TabCompleteResponsePacket::new); + private int transactionId; private int start; private int length; @@ -51,7 +53,7 @@ public class TabCompleteResponsePacket implements Packet { @Override public String toString() { - return "TabCompleteResponse{" + return "TabCompleteResponsePacket{" + "transactionId=" + transactionId + ", start=" + start + ", length=" + 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 70b7dd9f0..dd8bc007a 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 @@ -14,9 +14,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class TitlePacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { - throw new UnsupportedOperationException(); - }; + public static final Decoder DECODER = Decoder.unsupported(); public static TitlePacket hide(final ProtocolVersion version) { return version.gte(ProtocolVersion.MINECRAFT_1_11) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyDisconnectPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyDisconnectPacket.java index 0965bb26b..917a0b9f9 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyDisconnectPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyDisconnectPacket.java @@ -8,7 +8,7 @@ import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; -public class LegacyDisconnectPacket { +public class LegacyDisconnectPacket implements LegacyPacket { private static final ServerPing.Players FAKE_PLAYERS = new ServerPing.Players(0, 0, ImmutableList.of()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyHandshakePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyHandshakePacket.java index efc1cb8c9..566550bf3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyHandshakePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyHandshakePacket.java @@ -6,12 +6,7 @@ import com.velocitypowered.proxy.protocol.Packet; import com.velocitypowered.proxy.protocol.ProtocolDirection; import io.netty.buffer.ByteBuf; -public class LegacyHandshakePacket implements Packet { - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - throw new UnsupportedOperationException(); - } +public class LegacyHandshakePacket implements LegacyPacket, Packet { @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPacket.java new file mode 100644 index 000000000..8f284c16b --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPacket.java @@ -0,0 +1,4 @@ +package com.velocitypowered.proxy.protocol.packet.legacy; + +public interface LegacyPacket { +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPingPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPingPacket.java index 87b55b25f..3d8413e45 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPingPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/legacy/LegacyPingPacket.java @@ -9,7 +9,7 @@ import io.netty.buffer.ByteBuf; import java.net.InetSocketAddress; import org.checkerframework.checker.nullness.qual.Nullable; -public class LegacyPingPacket implements Packet { +public class LegacyPingPacket implements LegacyPacket, Packet { private final LegacyMinecraftPingVersion version; private final @Nullable InetSocketAddress vhost; @@ -24,19 +24,6 @@ public class LegacyPingPacket implements Packet { this.vhost = vhost; } - public LegacyMinecraftPingVersion getVersion() { - return version; - } - - public @Nullable InetSocketAddress getVhost() { - return vhost; - } - - @Override - public void decode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { - throw new UnsupportedOperationException(); - } - @Override public void encode(ByteBuf buf, ProtocolDirection direction, ProtocolVersion version) { throw new UnsupportedOperationException(); @@ -46,4 +33,12 @@ public class LegacyPingPacket implements Packet { public boolean handle(MinecraftSessionHandler handler) { return handler.handle(this); } + + public LegacyMinecraftPingVersion getVersion() { + return version; + } + + public @Nullable InetSocketAddress getVhost() { + return vhost; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index 5a5100b39..276d08fb8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -5,6 +5,7 @@ import com.velocitypowered.api.proxy.player.TabList; import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.proxy.connection.MinecraftConnection; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.HeaderAndFooterPacket; import com.velocitypowered.proxy.protocol.packet.PlayerListItemPacket; import java.util.ArrayList; @@ -15,6 +16,7 @@ import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import org.checkerframework.checker.nullness.qual.Nullable; public class VelocityTabList implements TabList { @@ -31,7 +33,12 @@ public class VelocityTabList implements TabList { net.kyori.adventure.text.Component footer) { Preconditions.checkNotNull(header, "header"); Preconditions.checkNotNull(footer, "footer"); - connection.write(HeaderAndFooterPacket.create(header, footer, connection.getProtocolVersion())); + GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer( + connection.getProtocolVersion()); + connection.write(new HeaderAndFooterPacket( + serializer.serialize(header), + serializer.serialize(footer) + )); } @Override 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 ed4005bd1..a2227d22f 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/protocol/PacketRegistryTest.java @@ -25,7 +25,7 @@ class PacketRegistryTest { private StateRegistry.PacketRegistry setupRegistry() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( ProtocolDirection.CLIENTBOUND); - registry.register(HandshakePacket.class, HandshakePacket::new, + registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, false), new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false)); return registry; @@ -63,7 +63,7 @@ class PacketRegistryTest { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( ProtocolDirection.CLIENTBOUND); assertThrows(IllegalArgumentException.class, - () -> registry.register(HandshakePacket.class, HandshakePacket::new)); + () -> registry.register(HandshakePacket.class, HandshakePacket.DECODER)); assertThrows(IllegalArgumentException.class, () -> registry.getProtocolRegistry(ProtocolVersion.UNKNOWN) .getPacketId(new HandshakePacket())); @@ -74,11 +74,11 @@ class PacketRegistryTest { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( ProtocolDirection.CLIENTBOUND); assertThrows(IllegalArgumentException.class, - () -> registry.register(HandshakePacket.class, HandshakePacket::new, + () -> registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, false), new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, false))); assertThrows(IllegalArgumentException.class, - () -> registry.register(HandshakePacket.class, HandshakePacket::new, + () -> registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, false), new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, false))); } @@ -87,13 +87,13 @@ class PacketRegistryTest { void failOnDuplicate() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( ProtocolDirection.CLIENTBOUND); - registry.register(HandshakePacket.class, HandshakePacket::new, + registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, false)); assertThrows(IllegalArgumentException.class, - () -> registry.register(HandshakePacket.class, HandshakePacket::new, + () -> registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12, false))); assertThrows(IllegalArgumentException.class, - () -> registry.registerNew(StatusPingPacket.class, StatusPingPacket.DECODER, + () -> registry.register(StatusPingPacket.class, StatusPingPacket.DECODER, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_13, false))); } @@ -101,7 +101,7 @@ class PacketRegistryTest { void shouldNotFailWhenRegisterLatestProtocolVersion() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( ProtocolDirection.CLIENTBOUND); - assertDoesNotThrow(() -> registry.register(HandshakePacket.class, HandshakePacket::new, + assertDoesNotThrow(() -> registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, false), new StateRegistry.PacketMapping(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS), false))); @@ -111,7 +111,7 @@ class PacketRegistryTest { void registrySuppliesCorrectPacketsByProtocol() { StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( ProtocolDirection.CLIENTBOUND); - registry.register(HandshakePacket.class, HandshakePacket::new, + registry.register(HandshakePacket.class, HandshakePacket.DECODER, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false), new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, false), new StateRegistry.PacketMapping(0x02, MINECRAFT_1_13, false));