aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/dungeon/DungeonDeathCounter.kt
blob: 8510b6b6c6b793c400090d1b8ff572db5731321b (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
package at.hannibal2.skyhanni.features.dungeon

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.DungeonStartEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import at.hannibal2.skyhanni.utils.StringUtils.matches
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class DungeonDeathCounter {

    private var display = ""
    private var deaths = 0

    private val deathPatternsList = listOf(
        // TODO USE SH-REPO
        "§c ☠ §r§7You were killed by (.*)§r§7 and became a ghost§r§7.".toPattern(),
        "§c ☠ §r§7(.*) was killed by (.*) and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You were crushed and became a ghost§r§7.".toPattern(),
        "§c ☠ §r§7§r(.*)§r§7 was crushed and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You died to a trap and became a ghost§r§7.".toPattern(),
        "§c ☠ §r(.*)§r§7 died to a trap and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You burnt to death and became a ghost§r§7.".toPattern(),
        "§c ☠ §r(.*)§r§7 burnt to death and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You died and became a ghost§r§7.".toPattern(),
        "§c ☠ §r(.*)§r§7 died and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You suffocated and became a ghost§r§7.".toPattern(),
        "§c ☠ §r§7§r(.*)§r§7 suffocated and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You died to a mob and became a ghost§r§7.".toPattern(),
        "§c ☠ §r(.*)§7 died to a mob and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7You fell into a deep hole and became a ghost§r§7.".toPattern(),
        "§c ☠ §r(.*)§r§7 fell into a deep hole and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§(.*)§r§7 disconnected from the Dungeon and became a ghost§r§7.".toPattern(),

        "§c ☠ §r§7(.*)§r§7 fell to their death with help from §r(.*)§r§7 and became a ghost§r§7.".toPattern()
    )

    private fun isDeathMessage(message: String): Boolean =
        deathPatternsList.any { it.matches(message) }

    @SubscribeEvent(receiveCanceled = true)
    fun onChatPacket(event: LorenzChatEvent) {
        if (!isEnabled()) return

        if (isDeathMessage(event.message)) {
            deaths++
            ChatUtils.chat("§c§l$deaths. DEATH!", false)
            update()
        }
    }

    private fun update() {
        if (deaths == 0) {
            display = ""
            return
        }

        val color = when (deaths) {
            1, 2 -> "§e"
            3 -> "§c"
            else -> "§4"
        }
        display = color + "Deaths: $deaths"
    }

    @SubscribeEvent
    fun onDungeonStart(event: DungeonStartEvent) {
        deaths = 0
        update()
    }

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        deaths = 0
        update()
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) {
        if (!isEnabled()) return

        SkyHanniMod.feature.dungeon.deathCounterPos.renderString(
            DungeonMilestonesDisplay.color + display,
            posLabel = "Dungeon Death Counter"
        )
    }

    private fun isEnabled(): Boolean {
        return LorenzUtils.inDungeons && SkyHanniMod.feature.dungeon.deathCounterDisplay
    }
}