From d6470594aeff799ef159980d91689306697bd773 Mon Sep 17 00:00:00 2001 From: mworzala Date: Wed, 7 Feb 2024 07:49:54 -0500 Subject: [PATCH] feat: handle ping request packet to make f3+3 ping graph functional (cherry picked from commit 9fa936af0c298150a0d657415bea632b73f78048) --- .../minestom/server/listener/PlayPingListener.java | 13 +++++++++++++ .../listener/manager/PacketListenerManager.java | 4 ++-- .../server/listener/preplay/StatusListener.java | 10 +++++----- .../network/packet/client/ClientPacketsHandler.java | 5 ++--- .../ClientPingRequestPacket.java} | 6 +++--- .../packet/server/ServerPacketIdentifier.java | 4 ++-- .../PingResponsePacket.java} | 9 +++++---- .../server/network/PacketWriteReadTest.java | 4 ++-- 8 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/main/java/net/minestom/server/listener/PlayPingListener.java rename src/main/java/net/minestom/server/network/packet/client/{status/PingPacket.java => common/ClientPingRequestPacket.java} (64%) rename src/main/java/net/minestom/server/network/packet/server/{status/PongPacket.java => common/PingResponsePacket.java} (69%) diff --git a/src/main/java/net/minestom/server/listener/PlayPingListener.java b/src/main/java/net/minestom/server/listener/PlayPingListener.java new file mode 100644 index 000000000..7ad0497ed --- /dev/null +++ b/src/main/java/net/minestom/server/listener/PlayPingListener.java @@ -0,0 +1,13 @@ +package net.minestom.server.listener; + +import net.minestom.server.entity.Player; +import net.minestom.server.network.packet.client.common.ClientPingRequestPacket; +import net.minestom.server.network.packet.server.common.PingResponsePacket; +import org.jetbrains.annotations.NotNull; + +public final class PlayPingListener { + + public static void requestListener(@NotNull ClientPingRequestPacket packet, @NotNull Player player) { + player.sendPacket(new PingResponsePacket(packet.number())); + } +} diff --git a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java index b9f764813..3a45935f8 100644 --- a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java +++ b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java @@ -22,7 +22,6 @@ import net.minestom.server.network.packet.client.login.ClientLoginAcknowledgedPa import net.minestom.server.network.packet.client.login.ClientLoginPluginResponsePacket; import net.minestom.server.network.packet.client.login.ClientLoginStartPacket; import net.minestom.server.network.packet.client.play.*; -import net.minestom.server.network.packet.client.status.PingPacket; import net.minestom.server.network.packet.client.status.StatusRequestPacket; import net.minestom.server.network.player.PlayerConnection; import org.jetbrains.annotations.NotNull; @@ -46,7 +45,7 @@ public final class PacketListenerManager { setListener(ConnectionState.HANDSHAKE, ClientHandshakePacket.class, HandshakeListener::listener); setListener(ConnectionState.STATUS, StatusRequestPacket.class, StatusListener::requestListener); - setListener(ConnectionState.STATUS, PingPacket.class, StatusListener::pingListener); + setListener(ConnectionState.STATUS, ClientPingRequestPacket.class, StatusListener::pingRequestListener); setListener(ConnectionState.LOGIN, ClientLoginStartPacket.class, LoginListener::loginStartListener); setListener(ConnectionState.LOGIN, ClientEncryptionResponsePacket.class, LoginListener::loginEncryptionResponseListener); @@ -95,6 +94,7 @@ public final class PacketListenerManager { setPlayListener(ClientEditBookPacket.class, BookListener::listener); setPlayListener(ClientChatSessionUpdatePacket.class, (packet, player) -> {/* empty */}); setPlayListener(ClientChunkBatchReceivedPacket.class, ChunkBatchListener::batchReceivedListener); + setPlayListener(ClientPingRequestPacket.class, PlayPingListener::requestListener); } /** diff --git a/src/main/java/net/minestom/server/listener/preplay/StatusListener.java b/src/main/java/net/minestom/server/listener/preplay/StatusListener.java index 7c07de39d..5bc63192b 100644 --- a/src/main/java/net/minestom/server/listener/preplay/StatusListener.java +++ b/src/main/java/net/minestom/server/listener/preplay/StatusListener.java @@ -4,9 +4,9 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.server.ClientPingServerEvent; import net.minestom.server.event.server.ServerListPingEvent; -import net.minestom.server.network.packet.client.status.PingPacket; +import net.minestom.server.network.packet.client.common.ClientPingRequestPacket; import net.minestom.server.network.packet.client.status.StatusRequestPacket; -import net.minestom.server.network.packet.server.status.PongPacket; +import net.minestom.server.network.packet.server.common.PingResponsePacket; import net.minestom.server.network.packet.server.status.ResponsePacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.ping.ServerListPingType; @@ -21,7 +21,7 @@ public final class StatusListener { connection.sendPacket(new ResponsePacket(pingVersion.getPingResponse(statusRequestEvent.getResponseData())))); } - public static void pingListener(@NotNull PingPacket packet, @NotNull PlayerConnection connection) { + public static void pingRequestListener(@NotNull ClientPingRequestPacket packet, @NotNull PlayerConnection connection) { final ClientPingServerEvent clientPingEvent = new ClientPingServerEvent(connection, packet.number()); EventDispatcher.call(clientPingEvent); @@ -29,11 +29,11 @@ public final class StatusListener { connection.disconnect(); } else { if (clientPingEvent.getDelay().isZero()) { - connection.sendPacket(new PongPacket(clientPingEvent.getPayload())); + connection.sendPacket(new PingResponsePacket(clientPingEvent.getPayload())); connection.disconnect(); } else { MinecraftServer.getSchedulerManager().buildTask(() -> { - connection.sendPacket(new PongPacket(clientPingEvent.getPayload())); + connection.sendPacket(new PingResponsePacket(clientPingEvent.getPayload())); connection.disconnect(); }).delay(clientPingEvent.getDelay()).schedule(); } diff --git a/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java b/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java index 26f621496..80ec4e5c1 100644 --- a/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java +++ b/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java @@ -8,7 +8,6 @@ import net.minestom.server.network.packet.client.login.ClientLoginAcknowledgedPa import net.minestom.server.network.packet.client.login.ClientLoginPluginResponsePacket; import net.minestom.server.network.packet.client.login.ClientLoginStartPacket; import net.minestom.server.network.packet.client.play.*; -import net.minestom.server.network.packet.client.status.PingPacket; import net.minestom.server.network.packet.client.status.StatusRequestPacket; import net.minestom.server.utils.collection.ObjectArray; import org.jetbrains.annotations.NotNull; @@ -46,7 +45,7 @@ public sealed class ClientPacketsHandler permits ClientPacketsHandler.Status, Cl public Status() { register(nextId(), StatusRequestPacket::new); - register(nextId(), PingPacket::new); + register(nextId(), ClientPingRequestPacket::new); } } @@ -119,7 +118,7 @@ public sealed class ClientPacketsHandler permits ClientPacketsHandler.Status, Cl register(nextId(), ClientVehicleMovePacket::new); register(nextId(), ClientSteerBoatPacket::new); register(nextId(), ClientPickItemPacket::new); - nextId(); // Ping request + register(nextId(), ClientPingRequestPacket::new); register(nextId(), ClientCraftRecipeRequest::new); register(nextId(), ClientPlayerAbilitiesPacket::new); register(nextId(), ClientPlayerDiggingPacket::new); diff --git a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java b/src/main/java/net/minestom/server/network/packet/client/common/ClientPingRequestPacket.java similarity index 64% rename from src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java rename to src/main/java/net/minestom/server/network/packet/client/common/ClientPingRequestPacket.java index c84b62850..901fdd4b4 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/common/ClientPingRequestPacket.java @@ -1,4 +1,4 @@ -package net.minestom.server.network.packet.client.status; +package net.minestom.server.network.packet.client.common; import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; @@ -6,8 +6,8 @@ import org.jetbrains.annotations.NotNull; import static net.minestom.server.network.NetworkBuffer.LONG; -public record PingPacket(long number) implements ClientPacket { - public PingPacket(@NotNull NetworkBuffer reader) { +public record ClientPingRequestPacket(long number) implements ClientPacket { + public ClientPingRequestPacket(@NotNull NetworkBuffer reader) { this(reader.read(LONG)); } diff --git a/src/main/java/net/minestom/server/network/packet/server/ServerPacketIdentifier.java b/src/main/java/net/minestom/server/network/packet/server/ServerPacketIdentifier.java index 3d006b9ca..c6afcdc87 100644 --- a/src/main/java/net/minestom/server/network/packet/server/ServerPacketIdentifier.java +++ b/src/main/java/net/minestom/server/network/packet/server/ServerPacketIdentifier.java @@ -6,7 +6,7 @@ public final class ServerPacketIdentifier { private static final AtomicInteger PLAY_ID = new AtomicInteger(0); public static final int STATUS_RESPONSE = 0x00; - public static final int STATUS_PONG = 0x01; + public static final int STATUS_PING_RESPONSE = 0x01; public static final int LOGIN_DISCONNECT = 0x00; public static final int LOGIN_ENCRYPTION_REQUEST = 0x01; @@ -77,7 +77,7 @@ public final class ServerPacketIdentifier { public static final int OPEN_WINDOW = nextPlayId(); public static final int OPEN_SIGN_EDITOR = nextPlayId(); public static final int PING = nextPlayId(); - public static final int PONG_RESPONSE = nextPlayId(); + public static final int PING_RESPONSE = nextPlayId(); public static final int CRAFT_RECIPE_RESPONSE = nextPlayId(); public static final int PLAYER_ABILITIES = nextPlayId(); public static final int PLAYER_CHAT = nextPlayId(); diff --git a/src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java b/src/main/java/net/minestom/server/network/packet/server/common/PingResponsePacket.java similarity index 69% rename from src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java rename to src/main/java/net/minestom/server/network/packet/server/common/PingResponsePacket.java index ff123ba6c..5c0305bf5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/common/PingResponsePacket.java @@ -1,4 +1,4 @@ -package net.minestom.server.network.packet.server.status; +package net.minestom.server.network.packet.server.common; import net.minestom.server.network.ConnectionState; import net.minestom.server.network.NetworkBuffer; @@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull; import static net.minestom.server.network.NetworkBuffer.LONG; -public record PongPacket(long number) implements ServerPacket { - public PongPacket(@NotNull NetworkBuffer reader) { +public record PingResponsePacket(long number) implements ServerPacket { + public PingResponsePacket(@NotNull NetworkBuffer reader) { this(reader.read(LONG)); } @@ -22,7 +22,8 @@ public record PongPacket(long number) implements ServerPacket { @Override public int getId(@NotNull ConnectionState state) { return switch (state) { - case STATUS -> ServerPacketIdentifier.STATUS_PONG; + case STATUS -> ServerPacketIdentifier.STATUS_PING_RESPONSE; + case PLAY -> ServerPacketIdentifier.PING_RESPONSE; default -> PacketUtils.invalidPacketState(getClass(), state, ConnectionState.STATUS); }; } diff --git a/src/test/java/net/minestom/server/network/PacketWriteReadTest.java b/src/test/java/net/minestom/server/network/PacketWriteReadTest.java index 4c24ef6ee..0e5fe3594 100644 --- a/src/test/java/net/minestom/server/network/PacketWriteReadTest.java +++ b/src/test/java/net/minestom/server/network/PacketWriteReadTest.java @@ -18,12 +18,12 @@ import net.minestom.server.network.packet.client.ClientPacket; import net.minestom.server.network.packet.client.handshake.ClientHandshakePacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.common.DisconnectPacket; +import net.minestom.server.network.packet.server.common.PingResponsePacket; import net.minestom.server.network.packet.server.login.LoginDisconnectPacket; import net.minestom.server.network.packet.server.login.LoginSuccessPacket; import net.minestom.server.network.packet.server.login.SetCompressionPacket; import net.minestom.server.network.packet.server.play.*; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket.Ingredient; -import net.minestom.server.network.packet.server.status.PongPacket; import net.minestom.server.network.packet.server.status.ResponsePacket; import net.minestom.server.recipe.RecipeCategory; import org.jglrxavpok.hephaistos.nbt.NBT; @@ -55,7 +55,7 @@ public class PacketWriteReadTest { // Handshake SERVER_PACKETS.add(new ResponsePacket(new JsonObject().toString())); // Status - SERVER_PACKETS.add(new PongPacket(5)); + SERVER_PACKETS.add(new PingResponsePacket(5)); // Login //SERVER_PACKETS.add(new EncryptionRequestPacket("server", generateByteArray(16), generateByteArray(16))); SERVER_PACKETS.add(new LoginDisconnectPacket(COMPONENT));