aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmpa <42304516+ItsEmpa@users.noreply.github.com>2024-10-22 21:06:02 +0200
committerGitHub <noreply@github.com>2024-10-22 21:06:02 +0200
commit9ecae1dd3811b4595be9c88819c78675de7f7cb1 (patch)
tree3e42bba6526a641aa618dd769f7a3ed86a012cb8
parentfa3ffae5674699eb13013cbf6102c8f53b017c89 (diff)
downloadSkyHanni-9ecae1dd3811b4595be9c88819c78675de7f7cb1.tar.gz
SkyHanni-9ecae1dd3811b4595be9c88819c78675de7f7cb1.tar.bz2
SkyHanni-9ecae1dd3811b4595be9c88819c78675de7f7cb1.zip
Fix: Tps Display (#2791)
Co-authored-by: Empa <itsempa@users.noreply.github.com>
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt6
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt130
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt5
3 files changed, 75 insertions, 66 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
index fe245eca7..6e87a7e6b 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
+++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
@@ -68,7 +68,6 @@ import at.hannibal2.skyhanni.features.misc.CarryTracker
import at.hannibal2.skyhanni.features.misc.CollectionTracker
import at.hannibal2.skyhanni.features.misc.LockMouseLook
import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager
-import at.hannibal2.skyhanni.features.misc.TpsCounter
import at.hannibal2.skyhanni.features.misc.discordrpc.DiscordRPCManager
import at.hannibal2.skyhanni.features.misc.limbo.LimboTimeTracker
import at.hannibal2.skyhanni.features.misc.massconfiguration.DefaultConfigFeatures
@@ -303,11 +302,6 @@ object Commands {
aliases = listOf("shcolor", "shcolours", "shcolour")
callback { ColorFormattingHelper.printColorCodeList() }
}
- event.register("shtps") {
- description = "Informs in chat about the server ticks per second (TPS)."
- category = CommandCategory.USERS_ACTIVE
- callback { TpsCounter.tpsCommand() }
- }
}
private fun usersNormalReset(event: CommandRegistrationEvent) {
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt
index 1515ee3dc..1905e43af 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/misc/TpsCounter.kt
@@ -3,9 +3,13 @@ package at.hannibal2.skyhanni.features.misc
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
+import at.hannibal2.skyhanni.config.commands.CommandCategory
+import at.hannibal2.skyhanni.config.commands.CommandRegistrationEvent
import at.hannibal2.skyhanni.config.enums.OutsideSbFeature
import at.hannibal2.skyhanni.events.GuiRenderEvent
+import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
+import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketReceivedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
@@ -13,85 +17,86 @@ import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.roundTo
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
-import kotlin.concurrent.fixedRateTimer
+import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object TpsCounter {
private val config get() = SkyHanniMod.feature.gui
- private const val MIN_DATA_AMOUNT = 5
- private const val WAIT_AFTER_WORLD_SWITCH = 6
+ private val ignorePacketDelay = 5.seconds
+ private val minimumSecondsDisplayDelay = 10.seconds
private var packetsFromLastSecond = 0
- private var tpsList = mutableListOf<Int>()
- private var ignoreFirstTicks = WAIT_AFTER_WORLD_SWITCH
- private var hasPacketReceived = false
-
- private var display = ""
-
- init {
- // TODO use SecondPassedEvent + passedSince
- fixedRateTimer(name = "skyhanni-tps-counter-seconds", period = 1000L) {
- if (!LorenzUtils.inSkyBlock) return@fixedRateTimer
- if (packetsFromLastSecond == 0) return@fixedRateTimer
-
- if (ignoreFirstTicks > 0) {
- ignoreFirstTicks--
- val current = ignoreFirstTicks + MIN_DATA_AMOUNT
- display = "§eTPS: §f(${current}s)"
- packetsFromLastSecond = 0
- return@fixedRateTimer
- }
-
- tpsList.add(packetsFromLastSecond)
- packetsFromLastSecond = 0
- if (tpsList.size > 10) {
- tpsList = tpsList.drop(1).toMutableList()
- }
-
- display = if (tpsList.size < MIN_DATA_AMOUNT) {
- val current = MIN_DATA_AMOUNT - tpsList.size
- "§eTPS: §f(${current}s)"
- } else {
- val sum = tpsList.sum().toDouble()
- var tps = (sum / tpsList.size).roundTo(1)
- if (tps > 20) tps = 20.0
- val color = getColor(tps)
- "§eTPS: $color$tps"
- }
+ private val tpsList = mutableListOf<Int>()
+ private var hasRemovedFirstSecond = false
+
+ private var hasReceivedPacket = false
+
+ var tps: Double? = null
+ private set
+
+ private var display: String? = null
+
+ private val timeSinceWorldSwitch get() = LorenzUtils.lastWorldSwitch.passedSince()
+
+ @SubscribeEvent
+ fun onSecondPassed(event: SecondPassedEvent) {
+ if (shouldIgnore()) {
+ updateDisplay()
+ return
+ }
+
+ if (packetsFromLastSecond != 0) {
+ if (hasRemovedFirstSecond) tpsList.add(packetsFromLastSecond)
+ hasRemovedFirstSecond = true
}
- // TODO use DelayedRun
- fixedRateTimer(name = "skyhanni-tps-counter-ticks", period = 50L) {
- if (!LorenzUtils.inSkyBlock) return@fixedRateTimer
-
- if (hasPacketReceived) {
- hasPacketReceived = false
- packetsFromLastSecond++
- }
+ packetsFromLastSecond = 0
+
+ if (tpsList.size > 10) tpsList.removeAt(0)
+
+ updateDisplay()
+ }
+
+ private fun updateDisplay() {
+ val timeUntil = minimumSecondsDisplayDelay - timeSinceWorldSwitch
+ val text = if (timeUntil.isPositive()) {
+ "§f(${timeUntil.inWholeSeconds}s)"
+ } else {
+ val sum = tpsList.sum().toDouble()
+ val newTps = (sum / tpsList.size).roundTo(1).coerceIn(0.0..20.0)
+ tps = newTps
+ val legacyColor = getColor(newTps)
+ "$legacyColor$newTps"
}
+ display = "§eTPS: $text"
}
- fun tpsCommand() {
- if (display.isEmpty()) {
- ChatUtils.userError("No tps data available, make sure you have the setting on.")
- return
+ private fun tpsCommand() {
+ val tps = tps ?: return ChatUtils.chat("§eTPS: §fCalculating...")
+ ChatUtils.chat("§eTPS: ${getColor(tps)}$tps")
+ }
+
+ @SubscribeEvent
+ fun onTick(event: LorenzTickEvent) {
+ if (hasReceivedPacket) {
+ packetsFromLastSecond++
+ hasReceivedPacket = false
}
- ChatUtils.chat(display)
}
@SubscribeEvent
fun onWorldChange(event: LorenzWorldChangeEvent) {
tpsList.clear()
+ tps = null
packetsFromLastSecond = 0
- ignoreFirstTicks = WAIT_AFTER_WORLD_SWITCH
- display = ""
+ display = null
+ hasRemovedFirstSecond = false
}
- @HandleEvent(priority = HandleEvent.LOW, receiveCancelled = true)
+ @HandleEvent(priority = HandleEvent.HIGHEST, receiveCancelled = true)
fun onPacketReceive(event: PacketReceivedEvent) {
- if (!LorenzUtils.inSkyBlock) return
- hasPacketReceived = true
+ hasReceivedPacket = true
}
@SubscribeEvent
@@ -101,6 +106,17 @@ object TpsCounter {
config.tpsDisplayPosition.renderString(display, posLabel = "Tps Display")
}
+ @HandleEvent
+ fun onCommandRegistration(event: CommandRegistrationEvent) {
+ event.register("shtps") {
+ description = "Informs in chat about the server ticks per second (TPS)."
+ category = CommandCategory.USERS_ACTIVE
+ callback { tpsCommand() }
+ }
+ }
+
+ private fun shouldIgnore() = timeSinceWorldSwitch < ignorePacketDelay
+
private fun isEnabled() = LorenzUtils.onHypixel &&
config.tpsDisplay &&
(LorenzUtils.inSkyBlock || OutsideSbFeature.TPS_DISPLAY.isSelected())
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt
index 22d1e4aac..7cf104269 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt
@@ -520,13 +520,12 @@ object RenderUtils {
}
fun Position.renderString(string: String?, offsetX: Int = 0, offsetY: Int = 0, posLabel: String) {
- if (string == null) return
- if (string == "") return
+ if (string.isNullOrBlank()) return
val x = renderString0(string, offsetX, offsetY, isCenter)
GuiEditManager.add(this, posLabel, x, 10)
}
- private fun Position.renderString0(string: String?, offsetX: Int = 0, offsetY: Int = 0, centered: Boolean): Int {
+ private fun Position.renderString0(string: String, offsetX: Int = 0, offsetY: Int = 0, centered: Boolean): Int {
val display = "§f$string"
GlStateManager.pushMatrix()
transform()