aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/NonGodPotEffectDisplay.kt
blob: 86c7b96eb8098c3e81ad5c333f4a18dcb920da4f (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package at.hannibal2.skyhanni.features

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.events.ProfileApiDataLoadedEvent
import at.hannibal2.skyhanni.test.GriffinJavaUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings
import at.hannibal2.skyhanni.utils.StringUtils
import net.minecraft.network.play.server.S30PacketWindowItems
import net.minecraft.network.play.server.S47PacketPlayerListHeaderFooter
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 net.minecraftforge.fml.common.gameevent.TickEvent
import java.util.regex.Pattern

class NonGodPotEffectDisplay {

    private var checkFooter = false
    private val activeEffects = mutableMapOf<String, Long>()
    private val textToRender = mutableListOf<String>()
    private var lastTick = 0L

    private var nonGodPotEffects = mapOf(
        "smoldering_polarization" to "§aSmoldering Polarization I",
        "mushed_glowy_tonic" to "§2Mushed Glowy Tonic I",
        "wisp_ice" to "§bWisp's Ice-Flavored Water I",
    )

    private var patternEffectsCount = Pattern.compile("§7You have §e(\\d+) §7non-god effects\\.")
    private var totalEffectsCount = 0

    @SubscribeEvent
    fun onChatMessage(event: LorenzChatEvent) {
        if (event.message == "§aYou ate a §r§aRe-heated Gummy Polar Bear§r§a!") {
            checkFooter = true
            activeEffects["§aSmoldering Polarization I"] = System.currentTimeMillis() + 1000 * 60 * 60
            format()
        }

        if (event.message == "§a§lBUFF! §fYou have gained §r§2Mushed Glowy Tonic I§r§f! Press TAB or type /effects to view your active effects!") {
            checkFooter = true
            activeEffects["§2Mushed Glowy Tonic I"] = System.currentTimeMillis() + 1000 * 60 * 60
            format()
        }

        if (event.message == "§a§lBUFF! §fYou splashed yourself with §r§bWisp's Ice-Flavored Water I§r§f! Press TAB or type /effects to view your active effects!") {
            checkFooter = true
            activeEffects["§bWisp's Ice-Flavored Water I"] = System.currentTimeMillis() + 1000 * 60 * 5
            format()
        }
    }

    private fun format() {
        val now = System.currentTimeMillis()
        textToRender.clear()
        if (activeEffects.values.removeIf { now > it }) {
            //to fetch the real amount of active pots
            totalEffectsCount = 0
            checkFooter = true
        }
        for (effect in GriffinJavaUtils.sortByValue(activeEffects)) {
            val label = effect.key
            val until = effect.value
            val seconds = (until - now) / 1000
            val format = StringUtils.formatDuration(seconds)

            val color = colorForTime(seconds)

            textToRender.add("$label $color$format")
        }
        val diff = totalEffectsCount - activeEffects.size
        if (diff > 0) {
            textToRender.add("§eOpen the /effects inventory")
            textToRender.add("§eto show the missing $diff effects!")
            checkFooter = true
        }
    }

    private fun colorForTime(seconds: Long): String {
        return if (seconds <= 60) {
            "§c"
        } else if (seconds <= 60 * 3) {
            "§6"
        } else if (seconds <= 60 * 10) {
            "§e"
        } else {
            "§f"
        }
    }

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        if (!isEnabled()) return
        if (lastTick + 1_000 > System.currentTimeMillis()) return
        lastTick = System.currentTimeMillis()

        format()
    }

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        checkFooter = true
    }

    @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
    fun onChatPacket(event: PacketEvent.ReceiveEvent) {
        val packet = event.packet
        if (packet is S30PacketWindowItems) {
            for (stack in packet.itemStacks) {
                val name = stack?.name ?: continue
                if (name in nonGodPotEffects.values) {
                    for (line in stack.getLore()) {
                        if (line.contains("Remaining")) {
                            val duration = readDuration(line.split("§f")[1])
                            activeEffects[name] = System.currentTimeMillis() + duration
                            format()
                        }
                    }
                }
            }
        }

        if (!checkFooter) return
        if (packet is S47PacketPlayerListHeaderFooter) {
            val formattedText = packet.footer.formattedText
            val lines = formattedText.replace("§r", "").split("\n")

            if (!lines.any { it.contains("§a§lActive Effects") }) return
            checkFooter = false

            for (line in lines) {
                if (line.startsWith("§2Mushed Glowy Tonic I")) {
                    val duration = readDuration(line.split("§f")[1])
                    activeEffects["§2Mushed Glowy Tonic I"] = System.currentTimeMillis() + duration
                    format()
                }
                val matcher = patternEffectsCount.matcher(line)
                if (matcher.matches()) {
                    val group = matcher.group(1)
                    totalEffectsCount = group.toInt()
                }
            }
        }
    }

    private fun readDuration(text: String): Int {
        val split = text.split(":")
        return when (split.size) {
            3 -> {
                val hours = split[0].toInt() * 1000 * 60 * 60
                val minutes = split[1].toInt() * 1000 * 60
                val seconds = split[2].toInt() * 1000
                seconds + minutes + hours
            }

            2 -> {
                val minutes = split[0].toInt() * 1000 * 60
                val seconds = split[1].toInt() * 1000
                seconds + minutes
            }

            1 -> {
                split[0].toInt() * 1000
            }

            else -> {
                throw RuntimeException("Invalid format: '$text'")
            }
        }
    }

    @SubscribeEvent
    fun renderOverlay(event: RenderGameOverlayEvent.Post) {
        if (event.type != RenderGameOverlayEvent.ElementType.ALL) return
        if (!isEnabled()) return

        SkyHanniMod.feature.misc.nonGodPotEffectPos.renderStrings(textToRender)
    }

    @SubscribeEvent
    fun onProfileDataLoad(event: ProfileApiDataLoadedEvent) {
        val profileData = event.profileData
        val effects = profileData["active_effects"]?.asJsonArray ?: return
        for (element in effects) {
            val effect = element.asJsonObject
            val name = effect["effect"].asString
            val label = nonGodPotEffects[name] ?: continue

            val time = effect["ticks_remaining"].asLong / 20
            val newValue = System.currentTimeMillis() + time * 1000
            val old = activeEffects.getOrDefault(label, 0)
            val diff = newValue - old
            activeEffects[label] = newValue
        }
    }

    private fun isEnabled(): Boolean {
        return LorenzUtils.inSkyblock && SkyHanniMod.feature.misc.nonGodPotEffectDisplay && !LorenzUtils.inDungeons && !LorenzUtils.inKuudraFight
    }
}