feat: handle ping request packet to make f3+3 ping graph functional

(cherry picked from commit 9fa936af0c298150a0d657415bea632b73f78048)
This commit is contained in:
mworzala 2024-02-07 07:49:54 -05:00 committed by Matt Worzala
parent f0f81803a5
commit d6470594ae
8 changed files with 34 additions and 21 deletions

View File

@ -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()));
}
}

View File

@ -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);
}
/**

View File

@ -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();
}

View File

@ -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);

View File

@ -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));
}

View File

@ -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();

View File

@ -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);
};
}

View File

@ -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));