From 2f9bda99fb718feb18538f68d3d5ca6d626e3f86 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Sun, 8 Nov 2020 17:12:05 -0800 Subject: [PATCH] and then there was more --- .../backend/BackendConnectionPhase.java | 4 +- .../backend/BackendConnectionPhases.java | 4 +- .../backend/BackendPlaySessionHandler.java | 19 +-- .../backend/BungeeCordMessageResponder.java | 11 +- .../backend/LoginSessionHandler.java | 2 +- .../backend/TransitionSessionHandler.java | 6 +- .../backend/VelocityServerConnection.java | 4 +- .../client/ClientConnectionPhase.java | 4 +- .../client/ClientPlaySessionHandler.java | 27 ++-- .../connection/client/ConnectedPlayer.java | 9 +- .../client/InitialConnectSessionHandler.java | 4 +- .../client/InitialInboundConnection.java | 2 +- .../client/LoginSessionHandler.java | 2 +- .../LegacyForgeHandshakeBackendPhase.java | 6 +- .../LegacyForgeHandshakeClientPhase.java | 14 +- .../forge/legacy/LegacyForgeUtil.java | 11 +- .../proxy/network/PluginMessageUtil.java | 28 ++-- .../proxy/network/StateRegistry.java | 55 +++---- .../network/buffer/DeferredByteBufHolder.java | 136 ------------------ .../buffer/TypedDefaultByteBufHolder.java | 37 +++++ .../packet/AbstractKeepAlivePacket.java | 2 +- ....java => AbstractPluginMessagePacket.java} | 79 ++++------ .../packet/AbstractStatusPingPacket.java | 2 +- .../proxy/network/packet/Packet.java | 22 --- .../proxy/network/packet/PacketHandler.java | 15 +- .../proxy/network/packet/PacketReader.java | 28 ++++ .../ClientboundAvailableCommandsPacket.java | 3 +- .../clientbound/ClientboundBossBarPacket.java | 3 +- .../clientbound/ClientboundChatPacket.java | 3 +- .../ClientboundDisconnectPacket.java | 3 +- .../ClientboundEncryptionRequestPacket.java | 3 +- .../ClientboundHeaderAndFooterPacket.java | 3 +- .../ClientboundJoinGamePacket.java | 3 +- .../ClientboundKeepAlivePacket.java | 3 +- .../ClientboundLoginPluginMessagePacket.java | 3 +- .../ClientboundPlayerListItemPacket.java | 3 +- .../ClientboundPluginMessagePacket.java | 26 ++++ .../ClientboundResourcePackRequestPacket.java | 3 +- .../clientbound/ClientboundRespawnPacket.java | 3 +- .../ClientboundServerLoginSuccessPacket.java | 3 +- .../ClientboundSetCompressionPacket.java | 3 +- .../ClientboundStatusPingPacket.java | 3 +- .../ClientboundStatusResponsePacket.java | 3 +- .../ClientboundTabCompleteResponsePacket.java | 3 +- .../clientbound/ClientboundTitlePacket.java | 3 +- .../serverbound/ServerboundChatPacket.java | 3 +- .../ServerboundClientSettingsPacket.java | 3 +- .../ServerboundEncryptionResponsePacket.java | 5 +- .../ServerboundHandshakePacket.java | 3 +- .../ServerboundKeepAlivePacket.java | 3 +- .../ServerboundLoginPluginResponsePacket.java | 3 +- .../ServerboundPluginMessagePacket.java | 26 ++++ ...ServerboundResourcePackResponsePacket.java | 3 +- .../ServerboundServerLoginPacket.java | 3 +- .../ServerboundStatusPingPacket.java | 3 +- .../ServerboundStatusRequestPacket.java | 3 +- .../ServerboundTabCompleteRequestPacket.java | 3 +- .../network/pipeline/MinecraftDecoder.java | 2 +- .../proxy/network/PacketRegistryTest.java | 22 +-- 59 files changed, 331 insertions(+), 367 deletions(-) delete mode 100644 proxy/src/main/java/com/velocitypowered/proxy/network/buffer/DeferredByteBufHolder.java create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/network/buffer/TypedDefaultByteBufHolder.java rename proxy/src/main/java/com/velocitypowered/proxy/network/packet/{shared/PluginMessagePacket.java => AbstractPluginMessagePacket.java} (50%) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketReader.java create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPluginMessagePacket.java create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundPluginMessagePacket.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhase.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhase.java index a051dcb1b..13560c604 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhase.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhase.java @@ -2,7 +2,7 @@ package com.velocitypowered.proxy.connection.backend; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeHandshakeBackendPhase; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; /** * Provides connection phase specific actions. @@ -21,7 +21,7 @@ public interface BackendConnectionPhase { */ default boolean handle(VelocityServerConnection server, ConnectedPlayer player, - PluginMessagePacket message) { + AbstractPluginMessagePacket message) { return false; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhases.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhases.java index b8a630020..a9a772dcd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhases.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendConnectionPhases.java @@ -2,7 +2,7 @@ package com.velocitypowered.proxy.connection.backend; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeHandshakeBackendPhase; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; /** * Contains Vanilla {@link BackendConnectionPhase}s. @@ -29,7 +29,7 @@ public final class BackendConnectionPhases { @Override public boolean handle(VelocityServerConnection serverConn, ConnectedPlayer player, - PluginMessagePacket message) { + AbstractPluginMessagePacket message) { // The connection may be legacy forge. If so, the Forge handler will deal with this // for us. Otherwise, we have nothing to do. return LegacyForgeHandshakeBackendPhase.NOT_STARTED.handle(serverConn, player, message); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 951e4e6b0..feb389bac 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -17,14 +17,15 @@ import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.client.ClientPlaySessionHandler; import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.network.PluginMessageUtil; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundAvailableCommandsPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundBossBarPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundKeepAlivePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPlayerListItemPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundTabCompleteResponsePacket; -import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; @@ -64,7 +65,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { serverConn.getServer().addPlayer(serverConn.getPlayer()); MinecraftConnection serverMc = serverConn.ensureConnected(); serverMc.write(PluginMessageUtil.constructChannelsPacket(serverMc.getProtocolVersion(), - ImmutableList.of(getBungeeCordChannel(serverMc.getProtocolVersion())) + ImmutableList.of(getBungeeCordChannel(serverMc.getProtocolVersion())), ClientboundPluginMessagePacket.FACTORY )); } @@ -102,7 +103,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { } @Override - public boolean handle(PluginMessagePacket packet) { + public boolean handle(ClientboundPluginMessagePacket packet) { if (bungeecordMessageResponder.process(packet)) { return true; } @@ -123,8 +124,8 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { } if (PluginMessageUtil.isMcBrand(packet)) { - PluginMessagePacket rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, - server.getVersion(), playerConnection.getProtocolVersion()); + AbstractPluginMessagePacket rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, + server.getVersion(), playerConnection.getProtocolVersion(), ClientboundPluginMessagePacket.FACTORY); playerConnection.write(rewritten); return true; } @@ -145,7 +146,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { server.getEventManager().fire(event) .thenAcceptAsync(pme -> { if (pme.getResult().isAllowed() && !playerConnection.isClosed()) { - PluginMessagePacket copied = new PluginMessagePacket(packet.getChannel(), + ClientboundPluginMessagePacket copied = new ClientboundPluginMessagePacket(packet.getChannel(), Unpooled.wrappedBuffer(copy)); playerConnection.write(copied); } @@ -244,8 +245,8 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public void handleGeneric(Packet packet) { - if (packet instanceof PluginMessagePacket) { - ((PluginMessagePacket) packet).retain(); + if (packet instanceof AbstractPluginMessagePacket) { + ((AbstractPluginMessagePacket) packet).retain(); } playerConnection.delayedWrite(packet); } 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 b74083ddd..1edbbf87d 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 @@ -11,7 +11,8 @@ import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.network.buffer.ByteBufDataInput; import com.velocitypowered.proxy.network.buffer.ByteBufDataOutput; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundPluginMessagePacket; import com.velocitypowered.proxy.server.VelocityRegisteredServer; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.netty.buffer.ByteBuf; @@ -45,7 +46,7 @@ public class BungeeCordMessageResponder { this.player = player; } - public static boolean isBungeeCordMessage(PluginMessagePacket message) { + public static boolean isBungeeCordMessage(AbstractPluginMessagePacket message) { return MODERN_CHANNEL.getId().equals(message.getChannel()) || LEGACY_CHANNEL.getId() .equals(message.getChannel()); } @@ -312,7 +313,7 @@ public class BungeeCordMessageResponder { MinecraftConnection serverConnection = player.ensureAndGetCurrentServer().ensureConnected(); String chan = getBungeeCordChannel(serverConnection.getProtocolVersion()); - PluginMessagePacket msg = null; + ServerboundPluginMessagePacket msg = null; boolean released = false; try { @@ -322,7 +323,7 @@ public class BungeeCordMessageResponder { } MinecraftConnection serverConn = vsc.ensureConnected(); - msg = new PluginMessagePacket(chan, buf); + msg = new ServerboundPluginMessagePacket(chan, buf); serverConn.write(msg); released = true; } finally { @@ -332,7 +333,7 @@ public class BungeeCordMessageResponder { } } - boolean process(PluginMessagePacket message) { + boolean process(AbstractPluginMessagePacket message) { if (!proxy.getConfiguration().isBungeePluginChannelEnabled()) { return false; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java index a4ac585f3..305f6630f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java @@ -11,12 +11,12 @@ import com.velocitypowered.proxy.connection.util.ConnectionRequestResults; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.StateRegistry; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundEncryptionRequestPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundLoginPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundServerLoginSuccessPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundSetCompressionPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundLoginPluginResponsePacket; -import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import com.velocitypowered.proxy.util.except.QuietRuntimeException; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java index c358ebdbe..0bbe4a587 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java @@ -15,10 +15,10 @@ import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.network.PluginMessageUtil; -import com.velocitypowered.proxy.network.packet.clientbound.ClientboundJoinGamePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundJoinGamePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundKeepAlivePacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import java.io.IOException; import java.util.concurrent.CompletableFuture; import org.apache.logging.log4j.LogManager; @@ -150,7 +150,7 @@ public class TransitionSessionHandler implements MinecraftSessionHandler { } @Override - public boolean handle(PluginMessagePacket packet) { + public boolean handle(ClientboundPluginMessagePacket packet) { if (!serverConn.getPlayer().canForwardPluginMessage(serverConn.ensureConnected() .getProtocolVersion(), packet)) { return true; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index 2f3e624c7..ab15e0f09 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -23,8 +23,8 @@ import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.network.StateRegistry; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundJoinGamePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundServerLoginPacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; import com.velocitypowered.proxy.server.VelocityRegisteredServer; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -234,7 +234,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, MinecraftConnection mc = ensureConnected(); - PluginMessagePacket message = new PluginMessagePacket(identifier.getId(), data); + ServerboundPluginMessagePacket message = new ServerboundPluginMessagePacket(identifier.getId(), data); mc.write(message); return true; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConnectionPhase.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConnectionPhase.java index 6f1052187..e7ca984c7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConnectionPhase.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConnectionPhase.java @@ -2,7 +2,7 @@ package com.velocitypowered.proxy.connection.client; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeHandshakeClientPhase; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; /** * Provides connection phase specific actions. @@ -22,7 +22,7 @@ public interface ClientConnectionPhase { * @return true if handled, false otherwise. */ default boolean handle(ConnectedPlayer player, - PluginMessagePacket message, + AbstractPluginMessagePacket message, VelocityServerConnection server) { return false; } 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 015be6785..86ee58888 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 @@ -21,9 +21,11 @@ import com.velocitypowered.proxy.connection.backend.BungeeCordMessageResponder; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.network.PluginMessageUtil; import com.velocitypowered.proxy.network.StateRegistry; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundBossBarPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundJoinGamePacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundRespawnPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundTabCompleteResponsePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundTabCompleteResponsePacket.Offer; @@ -31,9 +33,9 @@ import com.velocitypowered.proxy.network.packet.clientbound.ClientboundTitlePack import com.velocitypowered.proxy.network.packet.serverbound.ServerboundChatPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundClientSettingsPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundKeepAlivePacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundResourcePackResponsePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundTabCompleteRequestPacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; @@ -64,7 +66,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { private final ConnectedPlayer player; private boolean spawned = false; private final List serverBossBars = new ArrayList<>(); - private final Queue loginPluginMessages = new ArrayDeque<>(); + private final Queue loginPluginMessages = new ArrayDeque<>(); private final VelocityServer server; private @Nullable ServerboundTabCompleteRequestPacket outstandingTabComplete; @@ -83,7 +85,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { Collection channels = server.getChannelRegistrar().getChannelsForProtocol(player .getProtocolVersion()); if (!channels.isEmpty()) { - PluginMessagePacket register = constructChannelsPacket(player.getProtocolVersion(), channels); + AbstractPluginMessagePacket register = constructChannelsPacket(player.getProtocolVersion(), + channels, ClientboundPluginMessagePacket.FACTORY); player.getConnection().write(register); player.getKnownChannels().addAll(channels); } @@ -91,7 +94,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { @Override public void deactivated() { - for (PluginMessagePacket message : loginPluginMessages) { + for (ServerboundPluginMessagePacket message : loginPluginMessages) { ReferenceCountUtil.release(message); } } @@ -180,7 +183,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } @Override - public boolean handle(PluginMessagePacket packet) { + public boolean handle(ServerboundPluginMessagePacket packet) { VelocityServerConnection serverConn = player.getConnectedServer(); MinecraftConnection backendConn = serverConn != null ? serverConn.getConnection() : null; if (serverConn != null && backendConn != null) { @@ -195,7 +198,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { backendConn.write(packet.retain()); } else if (PluginMessageUtil.isMcBrand(packet)) { backendConn.write(PluginMessageUtil - .rewriteMinecraftBrand(packet, server.getVersion(), player.getProtocolVersion())); + .rewriteMinecraftBrand(packet, server.getVersion(), player.getProtocolVersion(), ServerboundPluginMessagePacket.FACTORY)); } else if (BungeeCordMessageResponder.isBungeeCordMessage(packet)) { return true; } else { @@ -229,7 +232,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { ByteBufUtil.getBytes(packet.content())); server.getEventManager().fire(event).thenAcceptAsync(pme -> { if (pme.getResult().isAllowed()) { - PluginMessagePacket message = new PluginMessagePacket(packet.getChannel(), + ServerboundPluginMessagePacket message = new ServerboundPluginMessagePacket(packet.getChannel(), Unpooled.wrappedBuffer(copy)); backendConn.write(message); } @@ -265,8 +268,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { MinecraftConnection smc = serverConnection.getConnection(); if (smc != null && serverConnection.getPhase().consideredComplete()) { - if (packet instanceof PluginMessagePacket) { - ((PluginMessagePacket) packet).retain(); + if (packet instanceof AbstractPluginMessagePacket) { + ((AbstractPluginMessagePacket) packet).retain(); } smc.write(packet); } @@ -356,11 +359,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // Tell the server about this client's plugin message channels. ProtocolVersion serverVersion = serverMc.getProtocolVersion(); if (!player.getKnownChannels().isEmpty()) { - serverMc.delayedWrite(constructChannelsPacket(serverVersion, player.getKnownChannels())); + serverMc.delayedWrite(constructChannelsPacket(serverVersion, player.getKnownChannels(), ServerboundPluginMessagePacket.FACTORY)); } // If we had plugin messages queued during login/FML handshake, send them now. - PluginMessagePacket pm; + ServerboundPluginMessagePacket pm; while ((pm = loginPluginMessages.poll()) != null) { serverMc.delayedWrite(pm); } @@ -585,7 +588,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (serverConnection != null) { MinecraftConnection connection = serverConnection.getConnection(); if (connection != null) { - PluginMessagePacket pm; + ServerboundPluginMessagePacket pm; while ((pm = loginPluginMessages.poll()) != null) { connection.write(pm); } 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 1edf2c7cf..412e38646 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 @@ -39,14 +39,15 @@ import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.network.PluginMessageUtil; import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.StateRegistry; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundChatPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundKeepAlivePacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundResourcePackRequestPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundTitlePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundChatPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundClientSettingsPacket; -import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; import com.velocitypowered.proxy.server.VelocityRegisteredServer; import com.velocitypowered.proxy.tablist.VelocityTabList; import com.velocitypowered.proxy.tablist.VelocityTabListLegacy; @@ -659,7 +660,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { public boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data) { Preconditions.checkNotNull(identifier, "identifier"); Preconditions.checkNotNull(data, "data"); - PluginMessagePacket message = new PluginMessagePacket(identifier.getId(), + ClientboundPluginMessagePacket message = new ClientboundPluginMessagePacket(identifier.getId(), Unpooled.wrappedBuffer(data)); connection.write(message); return true; @@ -734,7 +735,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { * @param message the plugin message to forward to the client * @return {@code true} if the message can be forwarded, {@code false} otherwise */ - public boolean canForwardPluginMessage(ProtocolVersion version, PluginMessagePacket message) { + public boolean canForwardPluginMessage(ProtocolVersion version, AbstractPluginMessagePacket message) { boolean minecraftOrFmlMessage; // By default, all internal Minecraft and Forge channels are forwarded from the server. diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialConnectSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialConnectSessionHandler.java index 275b31c52..bb329ded8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialConnectSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialConnectSessionHandler.java @@ -4,7 +4,7 @@ import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.backend.BungeeCordMessageResponder; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.network.PluginMessageUtil; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundPluginMessagePacket; public class InitialConnectSessionHandler implements MinecraftSessionHandler { @@ -15,7 +15,7 @@ public class InitialConnectSessionHandler implements MinecraftSessionHandler { } @Override - public boolean handle(PluginMessagePacket packet) { + public boolean handle(ServerboundPluginMessagePacket packet) { VelocityServerConnection serverConn = player.getConnectionInFlight(); if (serverConn != null) { if (player.getPhase().handle(player, packet, serverConn)) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java index 9bbda289f..eb6288386 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java @@ -4,8 +4,8 @@ import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.connection.InboundConnection; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; -import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; import java.net.InetSocketAddress; import java.util.Optional; import net.kyori.adventure.text.Component; 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 545a132d8..3d91382ba 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 @@ -26,12 +26,12 @@ import com.velocitypowered.proxy.config.VelocityConfiguration; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.network.StateRegistry; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundEncryptionRequestPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundServerLoginSuccessPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundSetCompressionPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundEncryptionResponsePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundServerLoginPacket; -import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import io.netty.buffer.ByteBuf; import java.net.InetSocketAddress; import java.security.GeneralSecurityException; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java index 549b7fcaf..987940dca 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java @@ -6,7 +6,7 @@ import com.velocitypowered.proxy.connection.backend.BackendConnectionPhase; import com.velocitypowered.proxy.connection.backend.BackendConnectionPhases; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import javax.annotation.Nullable; /** @@ -107,7 +107,7 @@ public enum LegacyForgeHandshakeBackendPhase implements BackendConnectionPhase { @Override public final boolean handle(VelocityServerConnection serverConnection, ConnectedPlayer player, - PluginMessagePacket message) { + AbstractPluginMessagePacket message) { if (message.getChannel().equals(LegacyForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { // Get the phase and check if we need to start the next phase. LegacyForgeHandshakeBackendPhase newPhase = getNewPhase(serverConnection, message); @@ -162,7 +162,7 @@ public enum LegacyForgeHandshakeBackendPhase implements BackendConnectionPhase { * @return The phase to transition to, which may be the same as before. */ private LegacyForgeHandshakeBackendPhase getNewPhase(VelocityServerConnection serverConnection, - PluginMessagePacket packet) { + AbstractPluginMessagePacket packet) { if (packetToAdvanceOn != null && LegacyForgeUtil.getHandshakePacketDiscriminator(packet) == packetToAdvanceOn) { LegacyForgeHandshakeBackendPhase phaseToTransitionTo = nextPhase(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java index aa831f856..7684cf75c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java @@ -7,7 +7,7 @@ import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.client.ClientConnectionPhase; import com.velocitypowered.proxy.connection.client.ClientPlaySessionHandler; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import java.util.List; import javax.annotation.Nullable; @@ -39,7 +39,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { @Override boolean onHandle(ConnectedPlayer player, - PluginMessagePacket message, + AbstractPluginMessagePacket message, MinecraftConnection backendConn) { // If we stay in this phase, we do nothing because it means the packet wasn't handled. // Returning false indicates this @@ -73,7 +73,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { @Override boolean onHandle(ConnectedPlayer player, - PluginMessagePacket message, + AbstractPluginMessagePacket message, MinecraftConnection backendConn) { // Read the mod list if we haven't already. if (!player.getModInfo().isPresent()) { @@ -147,7 +147,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { @Override boolean onHandle(ConnectedPlayer player, - PluginMessagePacket message, + AbstractPluginMessagePacket message, MinecraftConnection backendConn) { super.onHandle(player, message, backendConn); @@ -180,7 +180,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { @Override public final boolean handle(ConnectedPlayer player, - PluginMessagePacket message, + AbstractPluginMessagePacket message, VelocityServerConnection server) { if (server != null) { MinecraftConnection backendConn = server.getConnection(); @@ -211,7 +211,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { * @return true if handled, false otherwise. */ boolean onHandle(ConnectedPlayer player, - PluginMessagePacket message, + AbstractPluginMessagePacket message, MinecraftConnection backendConn) { // Send the packet on to the server. backendConn.write(message.retain()); @@ -241,7 +241,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { * @param packet The packet * @return The phase to transition to, which may be the same as before. */ - private LegacyForgeHandshakeClientPhase getNewPhase(PluginMessagePacket packet) { + private LegacyForgeHandshakeClientPhase getNewPhase(AbstractPluginMessagePacket packet) { if (packetToAdvanceOn != null && LegacyForgeUtil.getHandshakePacketDiscriminator(packet) == packetToAdvanceOn) { return nextPhase(); 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 8cfb67dc4..faa430e75 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 @@ -8,7 +8,8 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.util.ModInfo; import com.velocitypowered.proxy.network.ProtocolUtils; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.util.List; @@ -25,7 +26,7 @@ class LegacyForgeUtil { * @param message The message to analyse * @return The discriminator */ - static byte getHandshakePacketDiscriminator(PluginMessagePacket message) { + static byte getHandshakePacketDiscriminator(AbstractPluginMessagePacket message) { Preconditions.checkArgument(message.getChannel().equals(FORGE_LEGACY_HANDSHAKE_CHANNEL)); Preconditions.checkArgument(message.content().isReadable()); return message.content().getByte(0); @@ -37,7 +38,7 @@ class LegacyForgeUtil { * @param message The message * @return The list of mods. May be empty. */ - static List readModList(PluginMessagePacket message) { + static List readModList(AbstractPluginMessagePacket message) { Preconditions.checkNotNull(message, "message"); Preconditions .checkArgument(message.getChannel().equals(FORGE_LEGACY_HANDSHAKE_CHANNEL), @@ -71,8 +72,8 @@ class LegacyForgeUtil { * * @return A copy of the reset packet */ - static PluginMessagePacket resetPacket() { - return new PluginMessagePacket( + static AbstractPluginMessagePacket resetPacket() { + return new ClientboundPluginMessagePacket( FORGE_LEGACY_HANDSHAKE_CHANNEL, Unpooled.wrappedBuffer(FORGE_LEGACY_HANDSHAKE_RESET_DATA.clone()) ); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/PluginMessageUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/network/PluginMessageUtil.java index 4f528fdc7..6143d1e22 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/PluginMessageUtil.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/PluginMessageUtil.java @@ -6,7 +6,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.util.ProxyVersion; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.nio.charset.StandardCharsets; @@ -33,7 +33,7 @@ public final class PluginMessageUtil { * @param message the plugin message * @return whether or not this is a brand plugin message */ - public static boolean isMcBrand(PluginMessagePacket message) { + public static boolean isMcBrand(AbstractPluginMessagePacket message) { checkNotNull(message, "message"); return message.getChannel().equals(BRAND_CHANNEL_LEGACY) || message.getChannel() .equals(BRAND_CHANNEL); @@ -44,7 +44,7 @@ public final class PluginMessageUtil { * @param message the plugin message * @return whether we are registering plugin channels or not */ - public static boolean isRegister(PluginMessagePacket message) { + public static boolean isRegister(AbstractPluginMessagePacket message) { checkNotNull(message, "message"); return message.getChannel().equals(REGISTER_CHANNEL_LEGACY) || message.getChannel() .equals(REGISTER_CHANNEL); @@ -55,7 +55,7 @@ public final class PluginMessageUtil { * @param message the plugin message * @return whether we are unregistering plugin channels or not */ - public static boolean isUnregister(PluginMessagePacket message) { + public static boolean isUnregister(AbstractPluginMessagePacket message) { checkNotNull(message, "message"); return message.getChannel().equals(UNREGISTER_CHANNEL_LEGACY) || message.getChannel() .equals(UNREGISTER_CHANNEL); @@ -66,7 +66,7 @@ public final class PluginMessageUtil { * @param message the plugin message * @return whether this is a legacy register message */ - public static boolean isLegacyRegister(PluginMessagePacket message) { + public static boolean isLegacyRegister(AbstractPluginMessagePacket message) { checkNotNull(message, "message"); return message.getChannel().equals(REGISTER_CHANNEL_LEGACY); } @@ -77,7 +77,7 @@ public final class PluginMessageUtil { * @param message the plugin message * @return whether this is a legacy unregister message */ - public static boolean isLegacyUnregister(PluginMessagePacket message) { + public static boolean isLegacyUnregister(AbstractPluginMessagePacket message) { checkNotNull(message, "message"); return message.getChannel().equals(UNREGISTER_CHANNEL_LEGACY); } @@ -87,7 +87,7 @@ public final class PluginMessageUtil { * @param message the message to get the channels from * @return the channels, as an immutable list */ - public static List getChannels(PluginMessagePacket message) { + public static List getChannels(AbstractPluginMessagePacket message) { checkNotNull(message, "message"); checkArgument(isRegister(message) || isUnregister(message), "Unknown channel type %s", message.getChannel()); @@ -106,15 +106,16 @@ public final class PluginMessageUtil { * @param channels the channels to register * @return the plugin message to send */ - public static PluginMessagePacket constructChannelsPacket(ProtocolVersion protocolVersion, - Collection channels) { + public static AbstractPluginMessagePacket constructChannelsPacket(ProtocolVersion protocolVersion, + Collection channels, + AbstractPluginMessagePacket.Factory factory) { checkNotNull(channels, "channels"); checkArgument(!channels.isEmpty(), "no channels specified"); 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); - return new PluginMessagePacket(channelName, contents); + return factory.create(channelName, contents); } /** @@ -123,9 +124,10 @@ public final class PluginMessageUtil { * @param version the proxy version * @return the rewritten plugin message */ - public static PluginMessagePacket rewriteMinecraftBrand(PluginMessagePacket message, + public static AbstractPluginMessagePacket rewriteMinecraftBrand(AbstractPluginMessagePacket message, ProxyVersion version, - ProtocolVersion protocolVersion) { + ProtocolVersion protocolVersion, + AbstractPluginMessagePacket.Factory factory) { checkNotNull(message, "message"); checkNotNull(version, "version"); checkArgument(isMcBrand(message), "message is not a brand plugin message"); @@ -140,7 +142,7 @@ public final class PluginMessageUtil { rewrittenBuf.writeCharSequence(rewrittenBrand, StandardCharsets.UTF_8); } - return new PluginMessagePacket(message.getChannel(), rewrittenBuf); + return factory.create(message.getChannel(), rewrittenBuf); } private static String readBrandMessage(ByteBuf content) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/StateRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/network/StateRegistry.java index 48d74d627..33defdf7e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/StateRegistry.java @@ -18,15 +18,18 @@ import static com.velocitypowered.api.network.ProtocolVersion.SUPPORTED_VERSIONS import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; +import com.velocitypowered.proxy.network.packet.PacketReader; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundAvailableCommandsPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundBossBarPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundChatPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundEncryptionRequestPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundHeaderAndFooterPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundJoinGamePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundKeepAlivePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundLoginPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPlayerListItemPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundResourcePackRequestPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundRespawnPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundServerLoginSuccessPacket; @@ -41,13 +44,12 @@ import com.velocitypowered.proxy.network.packet.serverbound.ServerboundEncryptio import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundKeepAlivePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundLoginPluginResponsePacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundResourcePackResponsePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundServerLoginPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundStatusPingPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundStatusRequestPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundTabCompleteRequestPacket; -import com.velocitypowered.proxy.network.packet.clientbound.ClientboundDisconnectPacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; import io.netty.buffer.ByteBuf; import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectMap; @@ -58,7 +60,6 @@ import java.util.EnumMap; import java.util.EnumSet; import java.util.Map; import java.util.Objects; -import java.util.function.Supplier; import org.checkerframework.checker.nullness.qual.Nullable; public enum StateRegistry { @@ -128,8 +129,8 @@ public enum StateRegistry { map(0x05, MINECRAFT_1_14, false) ); serverbound.register( - PluginMessagePacket.class, - PluginMessagePacket.DECODER, + ServerboundPluginMessagePacket.class, + ServerboundPluginMessagePacket.DECODER, map(0x17, MINECRAFT_1_7_2, false), map(0x09, MINECRAFT_1_9, false), map(0x0A, MINECRAFT_1_12, false), @@ -195,8 +196,8 @@ public enum StateRegistry { map(0x10, MINECRAFT_1_16_2, false) ); clientbound.register( - PluginMessagePacket.class, - PluginMessagePacket.DECODER, + ClientboundPluginMessagePacket.class, + ClientboundPluginMessagePacket.DECODER, map(0x3F, MINECRAFT_1_7_2, false), map(0x18, MINECRAFT_1_9, false), map(0x19, MINECRAFT_1_13, false), @@ -400,7 +401,7 @@ public enum StateRegistry { return registry; } -

void register(Class

clazz, Packet.Decoder

decoder, +

void register(Class

clazz, PacketReader

decoder, PacketMapping... mappings) { if (mappings.length == 0) { throw new IllegalArgumentException("At least one mapping must be provided."); @@ -427,7 +428,7 @@ public enum StateRegistry { + current.protocolVersion); } - if (registry.packetIdToSupplier.containsKey(current.id)) { + if (registry.packetIdToReader.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"); @@ -439,7 +440,7 @@ public enum StateRegistry { } if (!current.encodeOnly) { - registry.packetIdToDecoder.put(current.id, decoder); + registry.packetIdToReader.put(current.id, decoder); } registry.packetClassToId.put(clazz, current.id); } @@ -449,10 +450,7 @@ public enum StateRegistry { public class ProtocolRegistry { public final ProtocolVersion version; - @Deprecated - final IntObjectMap> packetIdToSupplier = - new IntObjectHashMap<>(16, 0.5f); - final IntObjectMap> packetIdToDecoder = + final IntObjectMap> packetIdToReader = new IntObjectHashMap<>(16, 0.5f); final Object2IntMap> packetClassToId = new Object2IntOpenHashMap<>(16, 0.5f); @@ -462,21 +460,6 @@ public enum StateRegistry { this.packetClassToId.defaultReturnValue(Integer.MIN_VALUE); } - /** - * Attempts to create a packet from the specified {@code id}. - * - * @param id the packet ID - * @return the packet instance, or {@code null} if the ID is not registered - */ - @Deprecated - @Nullable Packet createPacket(final int id) { - final Supplier supplier = this.packetIdToSupplier.get(id); - if (supplier == null) { - return null; - } - return supplier.get(); - } - /** * Attempts to create a packet from the specified {@code id}. * @@ -486,13 +469,13 @@ public enum StateRegistry { * @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, PacketDirection direction, - ProtocolVersion version) { - final Packet.Decoder decoder = this.packetIdToDecoder.get(id); + public @Nullable Packet readPacket(final int id, ByteBuf buf, PacketDirection direction, + ProtocolVersion version) { + final PacketReader decoder = this.packetIdToReader.get(id); if (decoder == null) { return null; } - return decoder.decode(buf, direction, version); + return decoder.read(buf, direction, version); } /** @@ -517,9 +500,9 @@ public enum StateRegistry { public static final class PacketMapping { - private final int id; - private final ProtocolVersion protocolVersion; - private final boolean encodeOnly; + final int id; + final ProtocolVersion protocolVersion; + final boolean encodeOnly; PacketMapping(int id, ProtocolVersion protocolVersion, boolean packetDecoding) { this.id = id; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/buffer/DeferredByteBufHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/buffer/DeferredByteBufHolder.java deleted file mode 100644 index 9952e4fd0..000000000 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/buffer/DeferredByteBufHolder.java +++ /dev/null @@ -1,136 +0,0 @@ -package com.velocitypowered.proxy.network.buffer; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufHolder; -import io.netty.util.IllegalReferenceCountException; -import org.checkerframework.checker.nullness.qual.MonotonicNonNull; - -/** - * A special-purpose implementation of {@code ByteBufHolder} that can defer accepting its buffer. - * This is required because Velocity packets are, for better or worse, mutable. - */ -public class DeferredByteBufHolder implements ByteBufHolder { - - @MonotonicNonNull - private ByteBuf backing; - - public DeferredByteBufHolder( - @MonotonicNonNull ByteBuf backing) { - this.backing = backing; - } - - @Override - public ByteBuf content() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - if (backing.refCnt() <= 0) { - throw new IllegalReferenceCountException(backing.refCnt()); - } - return backing; - } - - @Override - public ByteBufHolder copy() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - return new DeferredByteBufHolder(backing.copy()); - } - - @Override - public ByteBufHolder duplicate() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - return new DeferredByteBufHolder(backing.duplicate()); - } - - @Override - public ByteBufHolder retainedDuplicate() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - return new DeferredByteBufHolder(backing.retainedDuplicate()); - } - - @Override - public ByteBufHolder replace(ByteBuf content) { - if (content == null) { - throw new NullPointerException("content"); - } - this.backing = content; - return this; - } - - @Override - public int refCnt() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - return backing.refCnt(); - } - - @Override - public ByteBufHolder retain() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - backing.retain(); - return this; - } - - @Override - public ByteBufHolder retain(int increment) { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - backing.retain(increment); - return this; - } - - @Override - public ByteBufHolder touch() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - backing.touch(); - return this; - } - - @Override - public ByteBufHolder touch(Object hint) { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - backing.touch(hint); - return this; - } - - @Override - public boolean release() { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - return backing.release(); - } - - @Override - public boolean release(int decrement) { - if (backing == null) { - throw new IllegalStateException("Trying to obtain contents of holder with a null buffer"); - } - return backing.release(decrement); - } - - @Override - public String toString() { - String str = "DeferredByteBufHolder["; - if (backing == null) { - str += "null"; - } else { - str += backing.toString(); - } - return str + "]"; - } -} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/buffer/TypedDefaultByteBufHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/buffer/TypedDefaultByteBufHolder.java new file mode 100644 index 000000000..7320e24a6 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/buffer/TypedDefaultByteBufHolder.java @@ -0,0 +1,37 @@ +package com.velocitypowered.proxy.network.buffer; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.DefaultByteBufHolder; + +public abstract class TypedDefaultByteBufHolder> extends DefaultByteBufHolder { + protected TypedDefaultByteBufHolder(final ByteBuf data) { + super(data); + } + + @Override + public abstract S replace(final ByteBuf content); + + @Override + @SuppressWarnings("unchecked") + public S retain() { + return (S) super.retain(); + } + + @Override + @SuppressWarnings("unchecked") + public S retain(final int increment) { + return (S) super.retain(increment); + } + + @Override + @SuppressWarnings("unchecked") + public S touch() { + return (S) super.touch(); + } + + @Override + @SuppressWarnings("unchecked") + public S touch(final Object hint) { + return (S) super.touch(hint); + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractKeepAlivePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractKeepAlivePacket.java index c3db20c6d..22112a079 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractKeepAlivePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractKeepAlivePacket.java @@ -7,7 +7,7 @@ import io.netty.buffer.ByteBuf; import java.util.function.LongFunction; public abstract class AbstractKeepAlivePacket implements Packet { - protected static

Decoder

decoder(final LongFunction

factory) { + protected static

PacketReader

decoder(final LongFunction

factory) { return (buf, direction, version) -> { final long randomId; if (version.gte(ProtocolVersion.MINECRAFT_1_12_2)) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/shared/PluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractPluginMessagePacket.java similarity index 50% rename from proxy/src/main/java/com/velocitypowered/proxy/network/packet/shared/PluginMessagePacket.java rename to proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractPluginMessagePacket.java index 2b5e1eaa0..f3e964655 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/shared/PluginMessagePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractPluginMessagePacket.java @@ -1,38 +1,37 @@ -package com.velocitypowered.proxy.network.packet.shared; +package com.velocitypowered.proxy.network.packet; import static com.velocitypowered.proxy.network.PluginMessageUtil.transformLegacyToModernChannel; import com.google.common.base.MoreObjects; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.network.ProtocolUtils; -import com.velocitypowered.proxy.network.packet.Packet; -import com.velocitypowered.proxy.network.packet.PacketDirection; -import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.buffer.TypedDefaultByteBufHolder; 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 DefaultByteBufHolder implements Packet { - 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 abstract class AbstractPluginMessagePacket> extends TypedDefaultByteBufHolder implements Packet { + protected static

> PacketReader

decoder(final Factory

factory) { + return (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 factory.create(channel, data); + }; + } - private final @Nullable String channel; + protected final @Nullable String channel; - public PluginMessagePacket(String channel, - @MonotonicNonNull ByteBuf backing) { + protected AbstractPluginMessagePacket(String channel, + @MonotonicNonNull ByteBuf backing) { super(backing); this.channel = channel; } @@ -54,11 +53,6 @@ public class PluginMessagePacket extends DefaultByteBufHolder implements Packet } } - @Override - public boolean handle(PacketHandler handler) { - return handler.handle(this); - } - public String getChannel() { if (channel == null) { throw new IllegalStateException("Channel is not specified."); @@ -66,31 +60,6 @@ public class PluginMessagePacket extends DefaultByteBufHolder implements Packet return channel; } - @Override - public PluginMessagePacket replace(ByteBuf content) { - return new PluginMessagePacket(this.channel, content); - } - - @Override - public PluginMessagePacket retain() { - return (PluginMessagePacket) super.retain(); - } - - @Override - public PluginMessagePacket retain(int increment) { - return (PluginMessagePacket) super.retain(increment); - } - - @Override - public PluginMessagePacket touch() { - return (PluginMessagePacket) super.touch(); - } - - @Override - public PluginMessagePacket touch(Object hint) { - return (PluginMessagePacket) super.touch(hint); - } - @Override public boolean equals(final Object other) { if (this == other) { @@ -99,7 +68,7 @@ public class PluginMessagePacket extends DefaultByteBufHolder implements Packet if (other == null || this.getClass() != other.getClass()) { return false; } - final PluginMessagePacket that = (PluginMessagePacket) other; + final AbstractPluginMessagePacket that = (AbstractPluginMessagePacket) other; return Objects.equals(this.channel, that.channel) && super.equals(other); } @@ -116,4 +85,8 @@ public class PluginMessagePacket extends DefaultByteBufHolder implements Packet .add("data", this.contentToString()) .toString(); } + + public interface Factory

> { + P create(final String channel, final ByteBuf data); + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractStatusPingPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractStatusPingPacket.java index 69ffdb4c1..5c9056998 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractStatusPingPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/AbstractStatusPingPacket.java @@ -6,7 +6,7 @@ import io.netty.buffer.ByteBuf; import java.util.function.LongFunction; public abstract class AbstractStatusPingPacket implements Packet { - protected static

Decoder

decoder(final LongFunction

factory) { + protected static

PacketReader

decoder(final LongFunction

factory) { return (buf, direction, version) -> { final long randomId = buf.readLong(); return factory.apply(randomId); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java index 8e7e9b664..fb45c86eb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/Packet.java @@ -15,26 +15,4 @@ public interface Packet { boolean handle(PacketHandler handler); - interface Decoder

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

Decoder

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

Decoder

instance(final P packet) { - return (buf, direction, version) -> packet; - } - - @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/network/packet/PacketHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketHandler.java index 38881d15f..25dc49923 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketHandler.java @@ -10,6 +10,7 @@ import com.velocitypowered.proxy.network.packet.clientbound.ClientboundJoinGameP import com.velocitypowered.proxy.network.packet.clientbound.ClientboundKeepAlivePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundLoginPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPlayerListItemPacket; +import com.velocitypowered.proxy.network.packet.clientbound.ClientboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundResourcePackRequestPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundRespawnPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundServerLoginSuccessPacket; @@ -26,18 +27,14 @@ import com.velocitypowered.proxy.network.packet.serverbound.ServerboundEncryptio import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundKeepAlivePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundLoginPluginResponsePacket; +import com.velocitypowered.proxy.network.packet.serverbound.ServerboundPluginMessagePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundResourcePackResponsePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundServerLoginPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundStatusPingPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundStatusRequestPacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundTabCompleteRequestPacket; -import com.velocitypowered.proxy.network.packet.shared.PluginMessagePacket; public interface PacketHandler { - default boolean handle(PluginMessagePacket packet) { - return false; - } - /* * Clientbound */ @@ -82,6 +79,10 @@ public interface PacketHandler { return false; } + default boolean handle(ClientboundPluginMessagePacket packet) { + return false; + } + default boolean handle(ClientboundResourcePackRequestPacket packet) { return false; } @@ -142,6 +143,10 @@ public interface PacketHandler { return false; } + default boolean handle(ServerboundPluginMessagePacket packet) { + return false; + } + default boolean handle(ServerboundResourcePackResponsePacket packet) { return false; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketReader.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketReader.java new file mode 100644 index 000000000..d5200dc53 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/PacketReader.java @@ -0,0 +1,28 @@ +package com.velocitypowered.proxy.network.packet; + +import com.velocitypowered.api.network.ProtocolVersion; +import io.netty.buffer.ByteBuf; +import java.util.function.Supplier; + +public interface PacketReader

{ + P read(final ByteBuf buf, final PacketDirection direction, final ProtocolVersion version); + + static

PacketReader

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

PacketReader

instance(final P packet) { + return (buf, direction, version) -> packet; + } + + @Deprecated + static

PacketReader

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/network/packet/clientbound/ClientboundAvailableCommandsPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java index 38d98a43e..b8094acaf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundAvailableCommandsPacket.java @@ -22,6 +22,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import com.velocitypowered.proxy.network.serialization.brigadier.ArgumentPropertyRegistry; import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap; @@ -36,7 +37,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundAvailableCommandsPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundAvailableCommandsPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundAvailableCommandsPacket::new); private static final Command PLACEHOLDER_COMMAND = source -> 0; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundBossBarPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundBossBarPacket.java index d60b5e965..1ebb0b976 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundBossBarPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundBossBarPacket.java @@ -6,13 +6,14 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import java.util.UUID; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundBossBarPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundBossBarPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundBossBarPacket::new); public static final int ADD = 0; public static final int REMOVE = 1; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundChatPacket.java index 896afdb80..a8919992e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundChatPacket.java @@ -6,12 +6,13 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import java.util.UUID; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundChatPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundChatPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundChatPacket::new); public static final byte CHAT_TYPE = (byte) 0; public static final byte SYSTEM_TYPE = (byte) 1; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java index f5f268df1..8433cc556 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundDisconnectPacket.java @@ -7,12 +7,13 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundDisconnectPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundDisconnectPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundDisconnectPacket::new); private @Nullable String reason; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundEncryptionRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundEncryptionRequestPacket.java index ee226f4c9..6654e529e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundEncryptionRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundEncryptionRequestPacket.java @@ -8,10 +8,11 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ClientboundEncryptionRequestPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundEncryptionRequestPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundEncryptionRequestPacket::new); private String serverId = ""; private byte[] publicKey = EMPTY_BYTE_ARRAY; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java index e24594299..24a6179cb 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundHeaderAndFooterPacket.java @@ -8,10 +8,11 @@ import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ClientboundHeaderAndFooterPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundHeaderAndFooterPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundHeaderAndFooterPacket::new); private static final String EMPTY_COMPONENT = "{\"translate\":\"\"}"; private static final ClientboundHeaderAndFooterPacket RESET diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java index 1b5b43f17..81fdb4650 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundJoinGamePacket.java @@ -10,6 +10,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import net.kyori.adventure.nbt.BinaryTagTypes; import net.kyori.adventure.nbt.CompoundBinaryTag; @@ -17,7 +18,7 @@ import net.kyori.adventure.nbt.ListBinaryTag; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundJoinGamePacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundJoinGamePacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundJoinGamePacket::new); private int entityId; private short gamemode; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundKeepAlivePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundKeepAlivePacket.java index f8331151f..ac419a285 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundKeepAlivePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundKeepAlivePacket.java @@ -3,9 +3,10 @@ package com.velocitypowered.proxy.network.packet.clientbound; import com.velocitypowered.proxy.network.packet.AbstractKeepAlivePacket; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; public class ClientboundKeepAlivePacket extends AbstractKeepAlivePacket implements Packet { - public static final Decoder DECODER = decoder(ClientboundKeepAlivePacket::new); + public static final PacketReader DECODER = decoder(ClientboundKeepAlivePacket::new); public ClientboundKeepAlivePacket(final long randomId) { super(randomId); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundLoginPluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundLoginPluginMessagePacket.java index 1205eafb5..7d9906f65 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundLoginPluginMessagePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundLoginPluginMessagePacket.java @@ -6,6 +6,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.Unpooled; @@ -13,7 +14,7 @@ import java.util.Objects; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundLoginPluginMessagePacket extends DefaultByteBufHolder implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final int id = ProtocolUtils.readVarInt(buf); final String channel = ProtocolUtils.readString(buf); final ByteBuf data; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPlayerListItemPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPlayerListItemPacket.java index 5b7aaef3b..4fd46e1a0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPlayerListItemPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPlayerListItemPacket.java @@ -9,6 +9,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import java.util.ArrayList; import java.util.List; @@ -18,7 +19,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundPlayerListItemPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundPlayerListItemPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundPlayerListItemPacket::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/network/packet/clientbound/ClientboundPluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPluginMessagePacket.java new file mode 100644 index 000000000..b2cae9775 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundPluginMessagePacket.java @@ -0,0 +1,26 @@ +package com.velocitypowered.proxy.network.packet.clientbound; + +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; +import com.velocitypowered.proxy.network.packet.Packet; +import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; +import io.netty.buffer.ByteBuf; + +public class ClientboundPluginMessagePacket extends AbstractPluginMessagePacket implements Packet { + public static final Factory FACTORY = ClientboundPluginMessagePacket::new; + public static final PacketReader DECODER = decoder(FACTORY); + + public ClientboundPluginMessagePacket(final String channel, final ByteBuf backing) { + super(channel, backing); + } + + @Override + public boolean handle(PacketHandler handler) { + return handler.handle(this); + } + + @Override + public ClientboundPluginMessagePacket replace(ByteBuf content) { + return new ClientboundPluginMessagePacket(this.channel, content); + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java index e9a9ad969..64529299d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundResourcePackRequestPacket.java @@ -6,11 +6,12 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import java.util.Objects; public class ClientboundResourcePackRequestPacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final String url = ProtocolUtils.readString(buf); final String hash = ProtocolUtils.readString(buf); return new ClientboundResourcePackRequestPacket(url, hash); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundRespawnPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundRespawnPacket.java index cf5939974..03b46b847 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundRespawnPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundRespawnPacket.java @@ -8,11 +8,12 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import net.kyori.adventure.nbt.CompoundBinaryTag; public class ClientboundRespawnPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundRespawnPacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundRespawnPacket::new); private int dimension; private long partialHashedSeed; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java index 95497e492..9cb9959c8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundServerLoginSuccessPacket.java @@ -7,12 +7,13 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import java.util.Objects; import java.util.UUID; public class ClientboundServerLoginSuccessPacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final UUID uuid; if (version.gte(ProtocolVersion.MINECRAFT_1_16)) { uuid = ProtocolUtils.readUuidIntArray(buf); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundSetCompressionPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundSetCompressionPacket.java index de5a0323a..d5e843c88 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundSetCompressionPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundSetCompressionPacket.java @@ -6,10 +6,11 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ClientboundSetCompressionPacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final int threshold = ProtocolUtils.readVarInt(buf); return new ClientboundSetCompressionPacket(threshold); }; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusPingPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusPingPacket.java index a833afd76..709f8015b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusPingPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusPingPacket.java @@ -3,9 +3,10 @@ package com.velocitypowered.proxy.network.packet.clientbound; import com.velocitypowered.proxy.network.packet.AbstractStatusPingPacket; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; public class ClientboundStatusPingPacket extends AbstractStatusPingPacket implements Packet { - public static final Decoder DECODER = decoder(ClientboundStatusPingPacket::new); + public static final PacketReader DECODER = decoder(ClientboundStatusPingPacket::new); public ClientboundStatusPingPacket(final long randomId) { super(randomId); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusResponsePacket.java index b7cd19550..686dae233 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundStatusResponsePacket.java @@ -6,11 +6,12 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundStatusResponsePacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final String status = ProtocolUtils.readString(buf, Short.MAX_VALUE); return new ClientboundStatusResponsePacket(status); }; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTabCompleteResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTabCompleteResponsePacket.java index 9d3fb68d6..827b3ff68 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTabCompleteResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTabCompleteResponsePacket.java @@ -6,6 +6,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import java.util.ArrayList; import java.util.List; @@ -13,7 +14,7 @@ import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundTabCompleteResponsePacket implements Packet { - public static final Decoder DECODER = Decoder.method(ClientboundTabCompleteResponsePacket::new); + public static final PacketReader DECODER = PacketReader.method(ClientboundTabCompleteResponsePacket::new); private int transactionId; private int start; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTitlePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTitlePacket.java index b1e690ac2..9429ea715 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTitlePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/clientbound/ClientboundTitlePacket.java @@ -7,6 +7,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import com.velocitypowered.proxy.util.DurationUtils; import io.netty.buffer.ByteBuf; import java.util.Arrays; @@ -14,7 +15,7 @@ import net.kyori.adventure.title.Title; import org.checkerframework.checker.nullness.qual.Nullable; public class ClientboundTitlePacket implements Packet { - public static final Decoder DECODER = Decoder.unsupported(); + public static final PacketReader DECODER = PacketReader.unsupported(); public static ClientboundTitlePacket hide(final ProtocolVersion version) { return version.gte(ProtocolVersion.MINECRAFT_1_11) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundChatPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundChatPacket.java index c9b774ddd..0885d659c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundChatPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundChatPacket.java @@ -6,10 +6,11 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ServerboundChatPacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final String message = ProtocolUtils.readString(buf); return new ServerboundChatPacket(message); }; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundClientSettingsPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundClientSettingsPacket.java index 9591cdae6..3a054a1b3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundClientSettingsPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundClientSettingsPacket.java @@ -6,11 +6,12 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; public class ServerboundClientSettingsPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ServerboundClientSettingsPacket::new); + public static final PacketReader DECODER = PacketReader.method(ServerboundClientSettingsPacket::new); private @Nullable String locale; private byte viewDistance; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundEncryptionResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundEncryptionResponsePacket.java index 78401e2ad..6511409d2 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundEncryptionResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundEncryptionResponsePacket.java @@ -6,10 +6,11 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ServerboundEncryptionResponsePacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final byte[] sharedSecret; final byte[] verifyToken; if (version.gte(ProtocolVersion.MINECRAFT_1_8)) { @@ -25,7 +26,7 @@ public class ServerboundEncryptionResponsePacket implements Packet { private final byte[] sharedSecret; private final byte[] verifyToken; - public ServerboundEncryptionResponsePacket(final byte[] sharedSecret, final byte[] verifyToken) { + private ServerboundEncryptionResponsePacket(final byte[] sharedSecret, final byte[] verifyToken) { this.sharedSecret = sharedSecret; this.verifyToken = verifyToken; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundHandshakePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundHandshakePacket.java index 7f910a0fb..87a57d9a3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundHandshakePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundHandshakePacket.java @@ -6,10 +6,11 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ServerboundHandshakePacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { int realProtocolVersion = ProtocolUtils.readVarInt(buf); final ProtocolVersion protocolVersion = ProtocolVersion.getProtocolVersion(realProtocolVersion); final String hostname = ProtocolUtils.readString(buf); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundKeepAlivePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundKeepAlivePacket.java index 7c680b184..04e1c6aba 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundKeepAlivePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundKeepAlivePacket.java @@ -3,9 +3,10 @@ package com.velocitypowered.proxy.network.packet.serverbound; import com.velocitypowered.proxy.network.packet.AbstractKeepAlivePacket; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; public class ServerboundKeepAlivePacket extends AbstractKeepAlivePacket implements Packet { - public static final Decoder DECODER = decoder(ServerboundKeepAlivePacket::new); + public static final PacketReader DECODER = decoder(ServerboundKeepAlivePacket::new); public ServerboundKeepAlivePacket(final long randomId) { super(randomId); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundLoginPluginResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundLoginPluginResponsePacket.java index 4591156a8..86ad89c50 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundLoginPluginResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundLoginPluginResponsePacket.java @@ -6,6 +6,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.Unpooled; @@ -13,7 +14,7 @@ import java.util.Objects; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; public class ServerboundLoginPluginResponsePacket extends DefaultByteBufHolder implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final int id = ProtocolUtils.readVarInt(buf); final boolean success = buf.readBoolean(); final ByteBuf data; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundPluginMessagePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundPluginMessagePacket.java new file mode 100644 index 000000000..55d04a285 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundPluginMessagePacket.java @@ -0,0 +1,26 @@ +package com.velocitypowered.proxy.network.packet.serverbound; + +import com.velocitypowered.proxy.network.packet.AbstractPluginMessagePacket; +import com.velocitypowered.proxy.network.packet.Packet; +import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; +import io.netty.buffer.ByteBuf; + +public class ServerboundPluginMessagePacket extends AbstractPluginMessagePacket implements Packet { + public static final Factory FACTORY = ServerboundPluginMessagePacket::new; + public static final PacketReader DECODER = decoder(FACTORY); + + public ServerboundPluginMessagePacket(final String channel, final ByteBuf backing) { + super(channel, backing); + } + + @Override + public boolean handle(PacketHandler handler) { + return handler.handle(this); + } + + @Override + public ServerboundPluginMessagePacket replace(ByteBuf content) { + return new ServerboundPluginMessagePacket(this.channel, content); + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundResourcePackResponsePacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundResourcePackResponsePacket.java index 4f93b2f90..6602c4715 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundResourcePackResponsePacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundResourcePackResponsePacket.java @@ -7,11 +7,12 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; public class ServerboundResourcePackResponsePacket implements Packet { - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final String hash; if (version.lte(ProtocolVersion.MINECRAFT_1_9_4)) { hash = ProtocolUtils.readString(buf); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundServerLoginPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundServerLoginPacket.java index 443f2ce71..8d4bd52c0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundServerLoginPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundServerLoginPacket.java @@ -6,6 +6,7 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import com.velocitypowered.proxy.util.except.QuietDecoderException; import io.netty.buffer.ByteBuf; import java.util.Objects; @@ -13,7 +14,7 @@ import java.util.Objects; public class ServerboundServerLoginPacket implements Packet { private static final QuietDecoderException EMPTY_USERNAME = new QuietDecoderException("Empty username!"); - public static final Decoder DECODER = (buf, direction, version) -> { + public static final PacketReader DECODER = (buf, direction, version) -> { final String username = ProtocolUtils.readString(buf, 16); if (username.isEmpty()) { throw EMPTY_USERNAME; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusPingPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusPingPacket.java index 6fff3934c..15de5d58d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusPingPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusPingPacket.java @@ -3,9 +3,10 @@ package com.velocitypowered.proxy.network.packet.serverbound; import com.velocitypowered.proxy.network.packet.AbstractStatusPingPacket; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; public class ServerboundStatusPingPacket extends AbstractStatusPingPacket implements Packet { - public static final Decoder DECODER = decoder(ServerboundStatusPingPacket::new); + public static final PacketReader DECODER = decoder(ServerboundStatusPingPacket::new); public ServerboundStatusPingPacket(final long randomId) { super(randomId); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java index 675fbe064..2ba11209c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundStatusRequestPacket.java @@ -4,11 +4,12 @@ import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; public class ServerboundStatusRequestPacket implements Packet { public static final ServerboundStatusRequestPacket INSTANCE = new ServerboundStatusRequestPacket(); - public static final Decoder DECODER = Decoder.instance(INSTANCE); + public static final PacketReader DECODER = PacketReader.instance(INSTANCE); private ServerboundStatusRequestPacket() { } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundTabCompleteRequestPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundTabCompleteRequestPacket.java index 4b41b959a..be2791627 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundTabCompleteRequestPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/packet/serverbound/ServerboundTabCompleteRequestPacket.java @@ -10,11 +10,12 @@ import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.packet.Packet; import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.PacketHandler; +import com.velocitypowered.proxy.network.packet.PacketReader; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; public class ServerboundTabCompleteRequestPacket implements Packet { - public static final Decoder DECODER = Decoder.method(ServerboundTabCompleteRequestPacket::new); + public static final PacketReader DECODER = PacketReader.method(ServerboundTabCompleteRequestPacket::new); private static final int VANILLA_MAX_TAB_COMPLETE_LEN = 2048; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java index 3271b82b5..e423d04b2 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/pipeline/MinecraftDecoder.java @@ -55,7 +55,7 @@ public class MinecraftDecoder extends ChannelInboundHandlerAdapter { int packetId = ProtocolUtils.readVarInt(buf); Packet packet = null; try { - packet = this.registry.decodePacket(packetId, buf, direction, registry.version); + packet = this.registry.readPacket(packetId, buf, direction, registry.version); } catch (Exception e) { throw handleDecodeFailure(e, packet, packetId); // TODO: packet is always null } diff --git a/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java b/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java index e9eb47721..862304c80 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java @@ -20,6 +20,7 @@ import com.velocitypowered.proxy.network.packet.PacketDirection; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; import com.velocitypowered.proxy.network.packet.serverbound.ServerboundStatusPingPacket; +import io.netty.buffer.Unpooled; import org.junit.jupiter.api.Test; class PacketRegistryTest { @@ -36,7 +37,7 @@ class PacketRegistryTest { @Test void packetRegistryWorks() { StateRegistry.PacketRegistry registry = setupRegistry(); - Packet packet = registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0); + Packet packet = registry.getProtocolRegistry(MINECRAFT_1_12).readPacket(0, Unpooled.EMPTY_BUFFER, PacketDirection.SERVERBOUND, MINECRAFT_1_12); assertNotNull(packet, "Packet was not found in registry"); assertEquals(ServerboundHandshakePacket.class, packet.getClass(), "Registry returned wrong class"); @@ -48,7 +49,7 @@ class PacketRegistryTest { @Test void packetRegistryLinkingWorks() { StateRegistry.PacketRegistry registry = setupRegistry(); - Packet packet = registry.getProtocolRegistry(MINECRAFT_1_12_1).createPacket(0); + Packet packet = registry.getProtocolRegistry(MINECRAFT_1_12_1).readPacket(0, Unpooled.EMPTY_BUFFER, PacketDirection.SERVERBOUND, MINECRAFT_1_12); assertNotNull(packet, "Packet was not found in registry"); assertEquals(ServerboundHandshakePacket.class, packet.getClass(), "Registry returned wrong class"); @@ -58,7 +59,7 @@ class PacketRegistryTest { "Registry did not return the correct packet ID"); assertEquals(1, registry.getProtocolRegistry(MINECRAFT_1_11).getPacketId(packet), "Registry did not return the correct packet ID"); - assertNull(registry.getProtocolRegistry(MINECRAFT_1_14_2).createPacket(0x01), + assertNull(registry.getProtocolRegistry(MINECRAFT_1_14_2).readPacket(0x01, Unpooled.EMPTY_BUFFER, PacketDirection.SERVERBOUND, MINECRAFT_1_12), "Registry should return a null"); } @@ -126,14 +127,19 @@ class PacketRegistryTest { new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, false), new StateRegistry.PacketMapping(0x02, MINECRAFT_1_13, false)); assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0x00).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_12).readPacket(0x00, Unpooled.EMPTY_BUFFER, + PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_12_1).createPacket(0x01).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_12_1).readPacket(0x01, Unpooled.EMPTY_BUFFER, + PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_12_2).createPacket(0x01).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_12_2).readPacket(0x01, Unpooled.EMPTY_BUFFER, + PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_13).createPacket(0x02).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_13).readPacket(0x02, Unpooled.EMPTY_BUFFER, + PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_14_2).createPacket(0x02).getClass()); + registry.getProtocolRegistry(MINECRAFT_1_14_2).readPacket(0x02, Unpooled.EMPTY_BUFFER, + PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); } } \ No newline at end of file