Skip to content

Commit

Permalink
feat: use mod API for ping packet
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaynx committed Dec 12, 2024
1 parent 34e6394 commit 6e62f3f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/gg/skytils/skytilsmod/core/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
69 changes: 35 additions & 34 deletions src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/Ping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ 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
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
Expand All @@ -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 {

Expand All @@ -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<ClientboundPingPacket>()
}
}

@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"
)
}
}
}
Expand All @@ -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()
}
}
Expand Down

0 comments on commit 6e62f3f

Please sign in to comment.