hollow-cube / minestom-ce

1.20.4 Lightweight Minecraft server
https://minestom.net
Apache License 2.0
107 stars 35 forks source link

Ping Packet not implemented #133

Closed nellieldev closed 9 months ago

nellieldev commented 9 months ago

When pressing F3 + 3 the network monitor of minecraft will be opened and ping request packets (play) are being sent contantly to the server. They are not implemented so this error will occurr: java.lang.IllegalStateException: Packet id 0x1e isn't registered!

Dont forget to implement Ping Response (play) too.

nellieldev commented 9 months ago

For everyone who needs a hotfix:

   /* Put into your initializer */
   {
            final Field field = PacketProcessor.class.getDeclaredField("playHandler");
            field.setAccessible(true);
            final ClientPacketsHandler handler = (ClientPacketsHandler.Play) field.get(MinecraftServer.getPacketProcessor());
            handler.register(0x1e, PingRequestPacket::new);

            MinecraftServer.getPacketListenerManager().setPlayListener(PingRequestPacket.class, (pingPacket, playerConnection) -> playerConnection.sendPacket(new PingResponsePacket(pingPacket.payload())));
        }

The two classes:

PingRequestPacket

  import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.packet.client.ClientPacket;
import org.jetbrains.annotations.NotNull;

public record PingRequestPacket(long payload) implements ClientPacket {

    public PingRequestPacket(final NetworkBuffer buffer) {
        this(buffer.read(NetworkBuffer.LONG));
    }

    @Override
    public void write(@NotNull final NetworkBuffer networkBuffer) {
        networkBuffer.write(NetworkBuffer.LONG, this.payload);
    }

}

PingResponsePacket

import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;

public record PingResponsePacket(long payload) implements ServerPacket {

    @Override
    public void write(@NotNull final NetworkBuffer networkBuffer) {
        networkBuffer.write(NetworkBuffer.LONG, this.payload);
    }

    @Override
    public int getId(@NotNull final ConnectionState state) {
        return state == ConnectionState.PLAY ? 0x34 : PacketUtils.invalidPacketState(this.getClass(), state, ConnectionState.PLAY);
    }
}
mworzala commented 9 months ago

Fixed in https://github.com/hollow-cube/minestom-ce/commit/9fa936af0c298150a0d657415bea632b73f78048