From 6e62f3f15d73b3efe206649cf4c6812517acff4f Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 12 Dec 2024 20:01:40 +0100 Subject: [PATCH 1/6] feat: use mod API for ping packet --- .../gg/skytils/skytilsmod/core/Config.kt | 2 +- .../skytilsmod/features/impl/misc/Ping.kt | 69 ++++++++++--------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/gg/skytils/skytilsmod/core/Config.kt b/src/main/kotlin/gg/skytils/skytilsmod/core/Config.kt index f824169ba..9e1e01a59 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/core/Config.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/core/Config.kt @@ -2619,7 +2619,7 @@ object Config : Vigilant( @Property( type = PropertyType.SELECTOR, name = "Ping Display", - description = "Shows your ping to the current server, similar to the /skytils ping command.\nYou must be in a GUI or not moving in order to queue a ping.\nThere is a tiny chance that this will cause you to be punished.", + description = "Shows your ping to the current server, similar to the /skytils ping command.", category = "Miscellaneous", subcategory = "Other", options = ["Off", "Server List", "Packet"], i18nName = "skytils.config.miscellaneous.other.ping_display", diff --git a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt index 6767cf553..0f0905c37 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt @@ -23,7 +23,9 @@ import gg.skytils.skytilsmod.Skytils import gg.skytils.skytilsmod.Skytils.Companion.mc import gg.skytils.skytilsmod.Skytils.Companion.prefix import gg.skytils.skytilsmod.core.structure.GuiElement +import gg.skytils.skytilsmod.events.impl.HypixelPacketEvent import gg.skytils.skytilsmod.events.impl.PacketEvent +import gg.skytils.skytilsmod.listeners.ServerPayloadInterceptor.getResponse import gg.skytils.skytilsmod.mixins.transformers.accessors.AccessorServerListEntryNormal import gg.skytils.skytilsmod.utils.NumberUtil import gg.skytils.skytilsmod.utils.NumberUtil.roundToPrecision @@ -31,6 +33,8 @@ import gg.skytils.skytilsmod.utils.Utils import gg.skytils.skytilsmod.utils.graphics.SmartFontRenderer import gg.skytils.skytilsmod.utils.graphics.colors.CommonColors import gg.skytils.skytilsmod.utils.hasMoved +import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket +import net.hypixel.modapi.packet.impl.serverbound.ServerboundPingPacket import net.minecraft.client.multiplayer.ServerData import net.minecraft.client.network.OldServerPinger import net.minecraft.network.play.client.C16PacketClientStatus @@ -39,6 +43,8 @@ import net.minecraft.network.play.server.S37PacketStatistics import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.math.abs import kotlin.math.absoluteValue +import kotlinx.coroutines.async +import kotlinx.coroutines.launch object Ping { @@ -56,42 +62,39 @@ object Ping { if (invokedCommand) UChat.chat("§cAlready pinging!") return } - mc.thePlayer.sendQueue.networkManager.sendPacket( - C16PacketClientStatus(C16PacketClientStatus.EnumState.REQUEST_STATS), - { - lastPingAt = System.nanoTime() - } - ) + Skytils.launch { + lastPingAt = System.nanoTime() + ServerboundPingPacket().getResponse() + } } @SubscribeEvent fun onPacket(event: PacketEvent.ReceiveEvent) { - if (lastPingAt > 0) { - when (event.packet) { - is S01PacketJoinGame -> { - lastPingAt = -1L - invokedCommand = false - } + if (lastPingAt > 0 && event.packet is S01PacketJoinGame) { + lastPingAt = -1L + invokedCommand = false + } + } - is S37PacketStatistics -> { - val diff = (abs(System.nanoTime() - lastPingAt) / 1_000_000.0) - lastPingAt *= -1 - pingCache = diff - if (invokedCommand) { - invokedCommand = false - UChat.chat( - "$prefix §${ - when { - diff < 50 -> "a" - diff < 100 -> "2" - diff < 149 -> "e" - diff < 249 -> "6" - else -> "c" - } - }${diff.roundToPrecision(2)} §7ms" - ) - } - } + @SubscribeEvent + fun onHypixelPacket(event: HypixelPacketEvent.ReceiveEvent) { + if (lastPingAt > 0 && event.packet is ClientboundPingPacket) { + val diff = (abs(System.nanoTime() - lastPingAt) / 1_000_000.0) + lastPingAt *= -1 + pingCache = diff + if (invokedCommand) { + invokedCommand = false + UChat.chat( + "$prefix §${ + when { + diff < 50 -> "a" + diff < 100 -> "2" + diff < 149 -> "e" + diff < 249 -> "6" + else -> "c" + } + }${diff.roundToPrecision(2)} §7ms" + ) } } } @@ -116,9 +119,7 @@ object Ping { } 2 -> { - if (lastPingAt < 0 && (mc.currentScreen != null || !mc.thePlayer.hasMoved) && System.nanoTime() - - lastPingAt.absoluteValue > 1_000_000L * 5_000 - ) { + if (lastPingAt < 0 && System.nanoTime() - lastPingAt.absoluteValue > 1_000_000L * 5_000) { sendPing() } } From 949ac865ac8bf1161bf4bd724f065debb86f0367 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 12 Dec 2024 22:03:38 +0100 Subject: [PATCH 2/6] fixes --- .../gg/skytils/skytilsmod/features/impl/misc/Ping.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt index 0f0905c37..94b57ba90 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt @@ -99,6 +99,14 @@ object Ping { } } + @SubscribeEvent + fun onHypixelPacketFail(event: HypixelPacketEvent.FailedEvent) { + if (lastPingAt > 0 && event.type == "hypixel:ping") { + lastPingAt *= -1 + invokedCommand = false + } + } + class PingDisplayElement : GuiElement(name = "Ping Display", x = 10, y = 10) { override fun render() { if (Utils.isOnHypixel && toggled && mc.thePlayer != null) { @@ -119,7 +127,7 @@ object Ping { } 2 -> { - if (lastPingAt < 0 && System.nanoTime() - lastPingAt.absoluteValue > 1_000_000L * 5_000) { + if (lastPingAt < 0 && System.nanoTime() - lastPingAt.absoluteValue > 1_000_000L * 6_000) { sendPing() } } From ba10af7e71945470a9ea2363e7c543a628044e13 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 12 Dec 2024 22:25:08 +0100 Subject: [PATCH 3/6] fixes pt2 --- .../gg/skytils/skytilsmod/features/impl/misc/Ping.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt index 94b57ba90..47cb53a42 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt @@ -49,6 +49,7 @@ import kotlinx.coroutines.launch object Ping { var lastPingAt = -1L + var lastPongAt = -1L var pingCache = -1.0 @@ -79,8 +80,10 @@ object Ping { @SubscribeEvent fun onHypixelPacket(event: HypixelPacketEvent.ReceiveEvent) { if (lastPingAt > 0 && event.packet is ClientboundPingPacket) { - val diff = (abs(System.nanoTime() - lastPingAt) / 1_000_000.0) + val now = System.nanoTime() + val diff = (abs(now - lastPingAt) / 1_000_000.0) lastPingAt *= -1 + lastPongAt = now pingCache = diff if (invokedCommand) { invokedCommand = false @@ -127,7 +130,7 @@ object Ping { } 2 -> { - if (lastPingAt < 0 && System.nanoTime() - lastPingAt.absoluteValue > 1_000_000L * 6_000) { + if (lastPingAt < 0 && System.nanoTime() - lastPongAt > 1_000_000L * 5_000) { sendPing() } } From d585393c0e272e2de6400cb54bc2a2aa5f406145 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 12 Dec 2024 22:53:18 +0100 Subject: [PATCH 4/6] manual ping fix --- .../skytilsmod/features/impl/misc/Ping.kt | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt index 47cb53a42..0057ae92b 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt @@ -63,12 +63,31 @@ object Ping { if (invokedCommand) UChat.chat("§cAlready pinging!") return } + if (System.nanoTime() - lastPongAt > 1_000_000L * 5_000) { + if (invokedCommand) displayPing() + return + } Skytils.launch { lastPingAt = System.nanoTime() ServerboundPingPacket().getResponse() } } + fun displayPing() { + UChat.chat( + "$prefix §${ + when { + pingCache < 50 -> "a" + pingCache < 100 -> "2" + pingCache < 149 -> "e" + pingCache < 249 -> "6" + else -> "c" + } + }${pingCache.roundToPrecision(2)} §7ms" + ) + } + + @SubscribeEvent fun onPacket(event: PacketEvent.ReceiveEvent) { if (lastPingAt > 0 && event.packet is S01PacketJoinGame) { @@ -81,23 +100,12 @@ object Ping { fun onHypixelPacket(event: HypixelPacketEvent.ReceiveEvent) { if (lastPingAt > 0 && event.packet is ClientboundPingPacket) { val now = System.nanoTime() - val diff = (abs(now - lastPingAt) / 1_000_000.0) + pingCache = (abs(now - lastPingAt) / 1_000_000.0) lastPingAt *= -1 lastPongAt = now - pingCache = diff if (invokedCommand) { invokedCommand = false - UChat.chat( - "$prefix §${ - when { - diff < 50 -> "a" - diff < 100 -> "2" - diff < 149 -> "e" - diff < 249 -> "6" - else -> "c" - } - }${diff.roundToPrecision(2)} §7ms" - ) + displayPing() } } } From b61eadbe343a397da705dca74d35a19dadb8b7e4 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 12 Dec 2024 23:10:11 +0100 Subject: [PATCH 5/6] fix infinite loop --- .../kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt index 0057ae92b..980bb0de5 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt @@ -64,7 +64,10 @@ object Ping { return } if (System.nanoTime() - lastPongAt > 1_000_000L * 5_000) { - if (invokedCommand) displayPing() + if (invokedCommand) { + invokedCommand = false + displayPing() + } return } Skytils.launch { From 627687f256fbc1639e32a1a9795f501c22ffa665 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 12 Dec 2024 23:13:53 +0100 Subject: [PATCH 6/6] wrong way around --- .../kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt index 980bb0de5..fccc328b5 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt @@ -63,7 +63,7 @@ object Ping { if (invokedCommand) UChat.chat("§cAlready pinging!") return } - if (System.nanoTime() - lastPongAt > 1_000_000L * 5_000) { + if (System.nanoTime() - lastPongAt < 1_000_000L * 5_000) { if (invokedCommand) { invokedCommand = false displayPing()