aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/EventCounter.kt
blob: 28f1e254b3b726246351d345e30484e2e73aca85 (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.data

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds

object EventCounter {

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

    private var map = mutableMapOf<String, Int>()
    private var lastUpdate = SimpleTimeMark.farPast()

    private var enabled = false

    @SubscribeEvent
    fun onSecondPassed(event: SecondPassedEvent) {
        enabled = LorenzUtils.onHypixel && config.eventCounter
    }

    fun count(eventName: String) {
        if (!enabled) return

        map.addOrPut(eventName, 1)

        if (lastUpdate == SimpleTimeMark.farPast()) {
            lastUpdate = SimpleTimeMark.now()
        }

        if (lastUpdate.passedSince() > 1.seconds) {
            lastUpdate = SimpleTimeMark.now()

            print(map)

            map.clear()
        }
    }

    private fun print(map: MutableMap<String, Int>) {
        println("")
        var total = 0
        for ((name, amount) in map.entries.sortedBy { it.value }) {
            println("$name (${amount.addSeparators()} times)")
            total += amount
        }
        println("")
        println("total: ${total.addSeparators()}")
    }
}