diff --git a/src/main/java/com/loohp/limbo/network/ClientConnection.java b/src/main/java/com/loohp/limbo/network/ClientConnection.java index 445cefd..072be06 100644 --- a/src/main/java/com/loohp/limbo/network/ClientConnection.java +++ b/src/main/java/com/loohp/limbo/network/ClientConnection.java @@ -43,7 +43,6 @@ import com.loohp.limbo.inventory.ItemStack; import com.loohp.limbo.location.GlobalPos; import com.loohp.limbo.location.Location; import com.loohp.limbo.network.protocol.packets.ClientboundFinishConfigurationPacket; -import com.loohp.limbo.network.protocol.packets.ClientboundLevelChunkWithLightPacket; import com.loohp.limbo.network.protocol.packets.ClientboundRegistryDataPacket; import com.loohp.limbo.network.protocol.packets.PacketHandshakingIn; import com.loohp.limbo.network.protocol.packets.PacketIn; @@ -112,7 +111,6 @@ import com.loohp.limbo.utils.GameMode; import com.loohp.limbo.utils.InventoryClickUtils; import com.loohp.limbo.utils.MojangAPIUtils; import com.loohp.limbo.utils.MojangAPIUtils.SkinResponse; -import com.loohp.limbo.world.BlockPosition; import com.loohp.limbo.world.BlockState; import com.loohp.limbo.world.World; import net.kyori.adventure.key.Key; @@ -167,10 +165,12 @@ public class ClientConnection implements Runnable { private boolean running; private volatile ClientState state; + private final AtomicLong lastPacketTimestamp; + private final AtomicLong lastKeepAlivePayLoad; + private final AtomicLong lastKeepAliveResponse; + private Player player; private TimerTask keepAliveTask; - private AtomicLong lastPacketTimestamp; - private AtomicLong lastKeepAlivePayLoad; private InetAddress inetAddress; private boolean ready; @@ -179,6 +179,7 @@ public class ClientConnection implements Runnable { this.inetAddress = clientSocket.getInetAddress(); this.lastPacketTimestamp = new AtomicLong(-1); this.lastKeepAlivePayLoad = new AtomicLong(-1); + this.lastKeepAliveResponse = new AtomicLong(-1); this.channel = null; this.running = false; this.ready = false; @@ -196,6 +197,10 @@ public class ClientConnection implements Runnable { this.lastKeepAlivePayLoad.set(payLoad); } + public long getLastKeepAliveResponse() { + return lastKeepAliveResponse.get(); + } + public long getLastPacketTimestamp() { return lastPacketTimestamp.get(); } @@ -259,6 +264,9 @@ public class ClientConnection implements Runnable { } catch (IOException ignored) { } try { + ServerProperties properties = Limbo.getInstance().getServerProperties(); + String str = (properties.isLogPlayerIPAddresses() ? inetAddress.getHostName() : "") + ":" + clientSocket.getPort(); + Limbo.getInstance().getConsole().sendMessage("[/" + str + "] <-> Player disconnected with the reason " + PlainTextComponentSerializer.plainText().serialize(reason)); clientSocket.close(); } catch (IOException ignored) { } @@ -654,27 +662,26 @@ public class ClientConnection implements Runnable { player.sendPlayerListHeaderAndFooter(properties.getTabHeader(), properties.getTabFooter()); ready = true; - + keepAliveTask = new TimerTask() { @Override public void run() { - if (state.equals(ClientState.DISCONNECTED)) { + if (state != ClientState.PLAY || !ready) { this.cancel(); - } else if (ready && state.equals(ClientState.PLAY)) { - long now = System.currentTimeMillis(); - if (now - getLastPacketTimestamp() > 15000) { - PacketPlayOutKeepAlive keepAlivePacket = new PacketPlayOutKeepAlive(now); - try { - sendPacket(keepAlivePacket); - setLastKeepAlivePayLoad(now); - } catch (Exception e) { - } - } + } + + long now = System.currentTimeMillis(); + PacketPlayOutKeepAlive keepAlive = new PacketPlayOutKeepAlive(now); + try { + sendPacket(keepAlive); + setLastKeepAlivePayLoad(now); + } catch (IOException e) { + cancel(); } } }; - new Timer().schedule(keepAliveTask, 5000, 10000); - + new Timer().schedule(keepAliveTask, 0, 10000); + while (clientSocket.isConnected()) { try { CheckedBiConsumer processMoveEvent = (event, originalTo) -> { @@ -725,12 +732,12 @@ public class ClientConnection implements Runnable { processMoveEvent.consume(event, to); } } else if (packetIn instanceof PacketPlayInKeepAlive) { - long lastPayload = getLastKeepAlivePayLoad(); PacketPlayInKeepAlive alive = (PacketPlayInKeepAlive) packetIn; - if (lastPayload == -1) { - Limbo.getInstance().getConsole().sendMessage("Unsolicited KeepAlive packet for player " + player.getName()); - } else if (alive.getPayload() != lastPayload) { - Limbo.getInstance().getConsole().sendMessage("Incorrect Payload received in KeepAlive packet for player " + player.getName()); + + if (alive.getPayload() == getLastKeepAlivePayLoad()) { + lastKeepAliveResponse.set(System.currentTimeMillis()); + } else { + disconnect(Component.text("Bad Keepalive Payload")); break; } } else if (packetIn instanceof PacketPlayInTabComplete) {