aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/HotSwapDetection.kt
blob: 20a0d99a560abb9a51f31e47ffa010105bd3de3f (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
package at.hannibal2.skyhanni.test

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.MinecraftData
import at.hannibal2.skyhanni.utils.LorenzUtils
import kotlin.concurrent.fixedRateTimer

object HotSwapDetection {
    private val config get() = SkyHanniMod.feature.dev.debug

    private var latestTick = 0
    private var lastTps = 0
    private var hotswap = false

    init {
        // TODO seems broken somehow?
        fixedRateTimer(name = "skyhanni-hot-swap-detection", period = 1000L) {
            val currentTick = MinecraftData.totalTicks
            val diff = currentTick - latestTick
            latestTick = currentTick

            // we count 2 client ticks per tick, we are bad
            handleTps(diff / 2)
        }
    }

    private fun handleTps(tps: Int) {
        if (!config.hotSwapDetection) return

        // ignore below one minute
        if (latestTick < 20 * 60) return

        println("diff: $tps")

        if (tps < 5) {
            LorenzUtils.debug("Lags! Only $tps tps")
        }

        if (!hotswap) {
            if (tps < 2) {
                if (lastTps > 15) {
                    LorenzUtils.debug("Detected hotswap now!")
                    hotswap = true
                }
            }
        } else {
            if (tps > 15) {
                hotswap = false
                LorenzUtils.debug("Detected hotswap end!")
            }
        }
        lastTps = tps
    }
}