diff --git a/patches/server/0036-Purpur-Alternate-Keepalive-Handling.patch b/patches/server/0036-Purpur-Alternate-Keepalive-Handling.patch new file mode 100644 index 0000000..b66a261 --- /dev/null +++ b/patches/server/0036-Purpur-Alternate-Keepalive-Handling.patch @@ -0,0 +1,67 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Etil <81570777+etil2jz@users.noreply.github.com> +Date: Tue, 21 Sep 2021 16:37:32 +0200 +Subject: [PATCH] (Purpur) Alternate Keepalive Handling + + +diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +index a31e066571b98d18cd6805c4037ebc0cefc1f17f..147e65b9ef349e53244338cec67c9db17c222d2b 100644 +--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java ++++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +@@ -357,6 +357,21 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser + // This should effectively place the keepalive handling back to "as it was" before 1.12.2 + long currentTime = Util.getMillis(); + long elapsedTime = currentTime - this.keepAliveTime; ++ ++ // Purpur start ++ if (xyz.arthurb.mirai.MiraiConfig.useAlternateKeepAlive) { ++ if (elapsedTime >= 1000L) { // 1 second ++ if (!processedDisconnect && keepAlives.size() > KEEPALIVE_LIMIT) { ++ LOGGER.warn("{} was kicked due to keepalive timeout!", player.getName()); ++ disconnect(new TranslatableComponent("disconnect.timeout")); ++ } else { ++ keepAliveTime = currentTime; // hijack this field for 1 second intervals ++ keepAlives.add(currentTime); // currentTime is ID ++ send(new ClientboundKeepAlivePacket(currentTime)); ++ } ++ } ++ } else ++ // Purpur end + + if (this.keepAlivePending) { + if (!this.processedDisconnect && elapsedTime >= KEEPALIVE_LIMIT) { // check keepalive limit, don't fire if already disconnected +@@ -3072,6 +3087,16 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser + + @Override + public void handleKeepAlive(ServerboundKeepAlivePacket packet) { ++ // Purpur start ++ if (xyz.arthurb.mirai.MiraiConfig.useAlternateKeepAlive) { ++ long id = packet.getId(); ++ if (keepAlives.size() > 0 && keepAlives.contains(id)) { ++ int ping = (int) (Util.getMillis() - id); ++ player.latency = (player.latency * 3 + ping) / 4; ++ keepAlives.clear(); // we got a valid response, lets roll with it and forget the rest ++ } ++ } else ++ // Purpur end + //PlayerConnectionUtils.ensureMainThread(packetplayinkeepalive, this, this.player.getWorldServer()); // CraftBukkit // Paper - This shouldn't be on the main thread + if (this.keepAlivePending && packet.getId() == this.keepAliveChallenge) { + int i = (int) (Util.getMillis() - this.keepAliveTime); +diff --git a/src/main/java/xyz/arthurb/mirai/MiraiConfig.java b/src/main/java/xyz/arthurb/mirai/MiraiConfig.java +index b444f912ee87a3b4b612f2ce425960d01b2e013c..f870a060820b1fa0495dd000c934e78d7b1a99ae 100644 +--- a/src/main/java/xyz/arthurb/mirai/MiraiConfig.java ++++ b/src/main/java/xyz/arthurb/mirai/MiraiConfig.java +@@ -209,5 +209,11 @@ public class MiraiConfig { + private static void protocolLib() { + fixProtocolLib = getBoolean("settings.fix-protocollib", fixProtocolLib); + } +- ++ ++ public static boolean useAlternateKeepAlive = true; ++ ++ private static void useAlternateKeepAlive() { ++ useAlternateKeepAlive = getBoolean("settings.use-alternate-keepalive", useAlternateKeepAlive); ++ } ++ + } +\ No newline at end of file