KeepAliveListener cleanup

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-10-10 03:29:51 +02:00
parent 6e16fb7b13
commit 022ec237ba

View File

@ -5,21 +5,18 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.client.play.ClientKeepAlivePacket; import net.minestom.server.network.packet.client.play.ClientKeepAlivePacket;
public class KeepAliveListener { public final class KeepAliveListener {
private static final Component KICK_MESSAGE = Component.text("Bad Keep Alive packet", NamedTextColor.RED);
public static void listener(ClientKeepAlivePacket packet, Player player) { public static void listener(ClientKeepAlivePacket packet, Player player) {
final long packetId = packet.id; final long packetId = packet.id;
final long playerId = player.getLastKeepAlive(); if (packetId != player.getLastKeepAlive()) {
final boolean equals = packetId == playerId; player.kick(KICK_MESSAGE);
if (!equals) {
player.kick(Component.text("Bad Keep Alive packet", NamedTextColor.RED));
return; return;
} }
player.refreshAnswerKeepAlive(true); player.refreshAnswerKeepAlive(true);
// Update latency // Update latency
final int latency = (int) (System.currentTimeMillis() - packet.id); final int latency = (int) (System.currentTimeMillis() - packetId);
player.refreshLatency(latency); player.refreshLatency(latency);
} }
} }