Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use mod API for ping packet #523

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
87 changes: 55 additions & 32 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,10 +43,13 @@ 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 {

var lastPingAt = -1L
var lastPongAt = -1L

var pingCache = -1.0

Expand All @@ -56,46 +63,64 @@ object Ping {
if (invokedCommand) UChat.chat("§cAlready pinging!")
return
}
mc.thePlayer.sendQueue.networkManager.sendPacket(
C16PacketClientStatus(C16PacketClientStatus.EnumState.REQUEST_STATS),
{
lastPingAt = System.nanoTime()
if (System.nanoTime() - lastPongAt < 1_000_000L * 5_000) {
if (invokedCommand) {
invokedCommand = false
displayPing()
}
return
}
Skytils.launch {
lastPingAt = System.nanoTime()
ServerboundPingPacket().getResponse<ClientboundPingPacket>()
}
}

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) {
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 now = System.nanoTime()
pingCache = (abs(now - lastPingAt) / 1_000_000.0)
lastPingAt *= -1
lastPongAt = now
if (invokedCommand) {
invokedCommand = false
displayPing()
}
}
}

@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) {
Expand All @@ -116,9 +141,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() - lastPongAt > 1_000_000L * 5_000) {
sendPing()
}
}
Expand Down
Loading