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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.DungeonEnterEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.utils.GuiRender.renderString
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.matchRegex
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class DungeonDeathCounter {

    private var textToRender = ""
    private var deaths = 0

    private fun isDeathMessage(message: String): Boolean = when {
        message.matchRegex("§c ☠ §r§7You were killed by (.*)§r§7 and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r§7(.*) was killed by (.*) and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You were crushed and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r§7§r(.*)§r§7 was crushed and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You died to a trap and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r(.*)§r§7 died to a trap and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You burnt to death and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r(.*)§r§7 burnt to death and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You died and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r(.*)§r§7 died and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You suffocated and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r§7§r(.*)§r§7 suffocated and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You died to a mob and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r(.*)§7 died to a mob and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7You fell into a deep hole and became a ghost§r§7.") -> true
        message.matchRegex("§c ☠ §r(.*)§r§7 fell into a deep hole and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§(.*)§r§7 disconnected from the Dungeon and became a ghost§r§7.") -> true

        message.matchRegex("§c ☠ §r§7(.*)§r§7 fell to their death with help from §r(.*)§r§7 and became a ghost§r§7.") -> true

        else -> false
    }

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

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

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

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

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

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        deaths = 0
        update()
    }

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

        SkyHanniMod.feature.dungeon.deathCounterPos.renderString(DungeonMilestoneDisplay.color + textToRender)
    }

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