blob: 8fed472a0864f0325790fe8ba89eae130f6233ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 = "§eTps: §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
"§eTps: §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)
"§eTps: $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 - "
}
}
}
|