aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/FrozenTreasureTracker.kt
blob: 664c0a42d044a0a87cce16c9038a8cad24b27f2a (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.data.ScoreboardData
import at.hannibal2.skyhanni.events.*
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.concurrent.fixedRateTimer

class FrozenTreasureTracker {
    private val config get() = SkyHanniMod.feature.misc.frozenTreasureTracker
    private var display = emptyList<List<Any>>()
    private var treasuresMined = 0
    private var compactProcs = 0
    private var estimatedIce = 0L
    private var lastEstimatedIce = 0L
    private val icePerMin = mutableListOf<Long>()
    private var icePerHour = 0
    private var stoppedChecks = 0
    private var compactPattern = "COMPACT! You found an Enchanted Ice!".toPattern()

    private var treasureCount = mapOf<FrozenTreasure, Int>()

    init {
        fixedRateTimer(name = "skyhanni-dungeon-milestone-display", period = 1000) {
            if (!onJerryWorkshop()) return@fixedRateTimer
            calculateIcePerHour()
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        icePerHour = 0
        stoppedChecks = 0
        icePerMin.clear()
        saveAndUpdate()
    }

    private fun calculateIcePerHour() {
        val difference = estimatedIce - lastEstimatedIce
        lastEstimatedIce = estimatedIce

        if (difference == estimatedIce) {
            return
        }

        icePerHour = icePerMin.average().toInt() * 3600
        icePerMin.add(difference)

        if (difference == 0L) {
            stoppedChecks += 1
            if (stoppedChecks == 60) {
                stoppedChecks = 0
                icePerMin.clear()
                icePerHour = 0
            }
            return
        }
        stoppedChecks = 0
    }

    private fun formatDisplay(map: List<List<Any>>): List<List<Any>> {
        val newList = mutableListOf<List<Any>>()
        for (index in config.textFormat) {
            newList.add(map[index])
        }
        return newList
    }

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!ProfileStorageData.loaded) return
        if (!onJerryWorkshop()) return

        val message = event.message.removeColor().trim()

        compactPattern.matchMatcher(message) {
            compactProcs += 1
            saveAndUpdate()
            if (config.hideMessages) event.blockedReason = "frozen treasure tracker"
        }

        for (treasure in FrozenTreasure.values()) {
            if ("FROZEN TREASURE! You found ${treasure.displayName.removeColor()}!".toRegex().matches(message)) {
                treasuresMined += 1
                val old = treasureCount[treasure] ?: 0
                treasureCount = treasureCount.editCopy { this[treasure] = old + 1 }
                saveAndUpdate()
                if (config.hideMessages) event.blockedReason = "frozen treasure tracker"
            }
        }
    }

    @SubscribeEvent
    fun onPreProfileSwitch(event: PreProfileSwitchEvent) {
        display = emptyList()
    }

    @SubscribeEvent
    fun onConfigLoad(event: ConfigLoadEvent) {
        val hidden = ProfileStorageData.profileSpecific?.frozenTreasureTracker ?: return
        treasuresMined = hidden.treasuresMined
        compactProcs = hidden.compactProcs
        treasureCount = hidden.treasureCount
        saveAndUpdate()
    }

    private fun drawTreasureDisplay() = buildList<List<Any>> {
        addAsSingletonList("§1§lFrozen Treasure Tracker")
        addAsSingletonList("§6${formatNumber(treasuresMined)} Treasures Mined")
        addAsSingletonList("§3${formatNumber(estimatedIce)} Total Ice")
        addAsSingletonList("§3${formatNumber(icePerHour)} Ice/hr")
        addAsSingletonList("§8${formatNumber(compactProcs)} Compact Procs")
        addAsSingletonList("")

        for (treasure in FrozenTreasure.values()) {
            val count = (treasureCount[treasure] ?: 0) * if (config.showAsDrops) treasure.defaultAmount else 1
            addAsSingletonList("§b${formatNumber(count)} ${treasure.displayName}")
        }
        addAsSingletonList("")
    }

    fun formatNumber(amount: Number): String {
        if (amount is Int) return amount.addSeparators()
        if (amount is Long) return NumberUtil.format(amount)
        return "$amount"
    }

    private fun saveAndUpdate() {
        val hidden = ProfileStorageData.profileSpecific?.frozenTreasureTracker ?: return
        hidden.treasuresMined = treasuresMined
        hidden.compactProcs = compactProcs
        hidden.treasureCount = treasureCount
        calculateIce()
        display = formatDisplay(drawTreasureDisplay())
    }

    private fun calculateIce() {
        estimatedIce = 0
        estimatedIce += compactProcs * 160
        for (treasure in FrozenTreasure.values()) {
            val amount = treasureCount[treasure] ?: 0
            estimatedIce += amount * treasure.defaultAmount * treasure.iceMultiplier
        }
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GameOverlayRenderEvent) {
        if (!config.enabled) return
        if (!onJerryWorkshop()) return
        if (config.onlyInCave && !inGlacialCave()) return
        config.position.renderStringsAndItems(display, posLabel = "Frozen Treasure Tracker")
    }

    private fun onJerryWorkshop() = LorenzUtils.inIsland(IslandType.WINTER)

    private fun inGlacialCave() = onJerryWorkshop() && ScoreboardData.sidebarLinesFormatted.contains(" §7⏣ §3Glacial Cave")
}