summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/rift/everywhere/CruxTalismanDisplay.kt
blob: ccdb19fc509458daa0d59347eb6ea30cf1de3df7 (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
package at.hannibal2.skyhanni.features.rift.everywhere

import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.features.rift.RiftAPI
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.ConditionalUtils
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.roundToPrecision
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object CruxTalismanDisplay {

    private val config get() = RiftAPI.config.cruxTalisman

    private val progressPattern by RepoPattern.pattern(
        "rift.everywhere.crux.progress",
        ".*(?<tier>§[0-9a-z][IV1-4-]+)\\s+(?<name>§[0-9a-z]\\w+)§[0-9a-z]:\\s*(?<progress>§[0-9a-z](?:§[0-9a-z])?MAXED|§[0-9a-z]\\d+§[0-9a-z]/§[0-9a-z]\\d+).*"
    )

    private val partialName = "CRUX_TALISMAN"
    private var display = emptyList<List<Any>>()
    private val displayLine = mutableListOf<Crux>()
    private val bonusesLine = mutableListOf<String>()
    private var maxed = false
    private var percentValue = 0.0

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) {
        if (!isEnabled()) return
        config.position.renderStringsAndItems(
            display,
            posLabel = "Crux Talisman Display"
        )
    }

    private fun update() {
        display = drawDisplay()
    }

    private fun drawDisplay() = buildList {
        var maxedKill = 0
        var percent = 0
        for (crux in displayLine)
            if (crux.maxed)
                maxedKill++
        if (maxedKill == 6)
            maxed = true

        if (!config.compactWhenMaxed && maxed) maxed = false

        if (displayLine.isNotEmpty()) {
            addAsSingletonList("§7Crux Talisman Progress: ${if (maxed) "§a§lMAXED!" else "§a$percentValue%"}")
            if (!maxed) {
                for (line in displayLine) {
                    percent += if (config.compactWhenMaxed) {
                        if (!line.maxed) {
                            "(?<progress>\\d+)/\\d+".toRegex().find(line.progress.removeColor())?.groupValues?.get(1)
                                ?.toInt() ?: 0
                        } else 100
                    } else {
                        if (line.progress.contains("MAXED"))
                            100
                        else {
                            "(?<progress>\\d+)/\\d+".toRegex().find(line.progress.removeColor())?.groupValues?.get(1)
                                ?.toInt() ?: 0
                        }
                    }
                    addAsSingletonList("  ${line.tier} ${line.name}: ${line.progress}")
                }
            }
        }
        percentValue = ((percent.toDouble() / 600) * 100).roundToPrecision(1)
        if (bonusesLine.isNotEmpty() && config.showBonuses.get()) {
            addAsSingletonList("§7Bonuses:")
            bonusesLine.forEach { addAsSingletonList("  $it") }
        }
    }

    @SubscribeEvent
    fun onSecondPassed(event: SecondPassedEvent) {
        if (!isEnabled()) return
        if (!event.repeatSeconds(2)) return
        if (!InventoryUtils.getItemsInOwnInventory().any { it.getInternalName().startsWith(partialName) }) return

        displayLine.clear()
        bonusesLine.clear()
        maxed = false
        var bonusFound = false
        val inventoryStack = InventoryUtils.getItemsInOwnInventory()
        for (stack in inventoryStack) {
            line@ for (line in stack.getLore()) {
                progressPattern.matchMatcher(line) {
                    val tier = group("tier").replace("-", "0")
                    val name = group("name")
                    val progress = group("progress")
                    val crux = Crux(name, tier, progress, progress.contains("MAXED"))
                    displayLine.add(crux)
                }
                if (line.startsWith("§7Total Bonuses")) {
                    bonusFound = true
                    continue@line
                }
                if (bonusFound) {
                    if (line.isEmpty()) {
                        bonusFound = false
                        continue@line
                    }
                    bonusesLine.add(line)
                }
            }
        }
        update()
    }

    @SubscribeEvent
    fun onConfigLoad(event: ConfigLoadEvent) {
        ConditionalUtils.onToggle(config.showBonuses) { update() }
    }

    data class Crux(val name: String, val tier: String, val progress: String, val maxed: Boolean)

    fun isEnabled() = RiftAPI.inRift() && config.enabled && LorenzUtils.skyBlockArea != "Mirrorverse"
}