diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-02-11 06:44:49 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-02-11 06:44:49 +0100 |
commit | 5488a0ab94c68e0fd846e39b2892a40f781d7bd7 (patch) | |
tree | cf3e58453c88d9093be4286d7fe6abac3a445110 /src/main/java/at/hannibal2/skyhanni/features/misc | |
parent | c618fb4e335a3d03b04ba8840f7e3e6fb8832eaf (diff) | |
download | skyhanni-5488a0ab94c68e0fd846e39b2892a40f781d7bd7.tar.gz skyhanni-5488a0ab94c68e0fd846e39b2892a40f781d7bd7.tar.bz2 skyhanni-5488a0ab94c68e0fd846e39b2892a40f781d7bd7.zip |
Tps Display - Show the Tps of the current server.
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/misc')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt new file mode 100644 index 000000000..dcb125b2f --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt @@ -0,0 +1,108 @@ +package at.hannibal2.skyhanni.features.misc + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.HyPixelData +import at.hannibal2.skyhanni.events.HypixelTickEvent +import at.hannibal2.skyhanni.events.PacketEvent +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.round +import at.hannibal2.skyhanni.utils.RenderUtils.renderString +import net.minecraftforge.client.event.RenderGameOverlayEvent +import net.minecraftforge.event.world.WorldEvent +import net.minecraftforge.fml.common.eventhandler.EventPriority +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.concurrent.fixedRateTimer + +class TpsCounter { + companion object { + private const val minDataAmount = 5 + private const val waitAfterWorldSwitch = 6 + } + + private var packetsFromLastSecond = 0 + private var tpsList = mutableListOf<Int>() + private var ignoreFirstTicks = waitAfterWorldSwitch + private var hasPacketReceived = false + + private var display = "" + + init { + fixedRateTimer(name = "skyhanni-tps-counter-seconds", period = 1000L) { + if (!HyPixelData.skyBlock) return@fixedRateTimer + if (!SkyHanniMod.feature.misc.tpsDisplayEnabled) return@fixedRateTimer + if (packetsFromLastSecond == 0) return@fixedRateTimer + + if (ignoreFirstTicks > 0) { + ignoreFirstTicks-- + val current = ignoreFirstTicks + minDataAmount + display = "§eSkyHanni Tps: §fCalculating.. (${current}s)" + packetsFromLastSecond = 0 + return@fixedRateTimer + } + + tpsList.add(packetsFromLastSecond) + packetsFromLastSecond = 0 + if (tpsList.size > 10) { + tpsList = tpsList.drop(1).toMutableList() + } + + display = if (tpsList.size < minDataAmount) { + val current = minDataAmount - tpsList.size + "§eSkyHanni Tps: §fCalculating.. (${current}s)" + } else { + val sum = tpsList.sum().toDouble() + var tps = (sum / tpsList.size).round(1) + if (tps > 20) tps = 20.0 + val color = getColor(tps) + "§eSkyHanni Tps: $color$tps" + } + } + fixedRateTimer(name = "skyhanni-tps-counter-ticks", period = 50L) { + if (!HyPixelData.skyBlock) return@fixedRateTimer + if (!SkyHanniMod.feature.misc.tpsDisplayEnabled) return@fixedRateTimer + + if (hasPacketReceived) { + hasPacketReceived = false + HypixelTickEvent().postAndCatch() + packetsFromLastSecond++ + } + } + } + + @SubscribeEvent + fun onWorldChange(event: WorldEvent.Load) { + tpsList.clear() + packetsFromLastSecond = 0 + ignoreFirstTicks = waitAfterWorldSwitch + display = "" + } + + @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true) + fun onChatPacket(event: PacketEvent.ReceiveEvent) { + if (!SkyHanniMod.feature.misc.tpsDisplayEnabled) return + hasPacketReceived = true + } + + @SubscribeEvent + fun onRenderOverlay(event: RenderGameOverlayEvent.Post) { + if (!LorenzUtils.inSkyBlock) return + if (!SkyHanniMod.feature.misc.tpsDisplayEnabled) return + if (event.type != RenderGameOverlayEvent.ElementType.ALL) return + + SkyHanniMod.feature.misc.tpsDisplayPosition.renderString(display) + } + + private fun getColor(tps: Double): String { + return if (tps > 19.8) { + "§2" + } else if (tps > 19) { + "§a" + } else if (tps > 17.5) { + "§6" + } else if (tps > 12) { + "§c" + } else { + "§8NOT PLAYABLE - " + } + } +}
\ No newline at end of file |