From afdba2c6a3b27a53817cef0b0a306c05162fbcac Mon Sep 17 00:00:00 2001 From: Lorenz Date: Tue, 12 Jul 2022 12:35:19 +0200 Subject: add dungeon death counter display --- .../at/lorenz/mod/dungeon/DungeonDeathCounter.kt | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/main/java/at/lorenz/mod/dungeon/DungeonDeathCounter.kt (limited to 'src/main/java/at/lorenz/mod/dungeon') diff --git a/src/main/java/at/lorenz/mod/dungeon/DungeonDeathCounter.kt b/src/main/java/at/lorenz/mod/dungeon/DungeonDeathCounter.kt new file mode 100644 index 000000000..156b641c4 --- /dev/null +++ b/src/main/java/at/lorenz/mod/dungeon/DungeonDeathCounter.kt @@ -0,0 +1,97 @@ +package at.lorenz.mod.dungeon + +import at.lorenz.mod.LorenzMod +import at.lorenz.mod.events.DungeonEnterEvent +import at.lorenz.mod.events.LorenzChatEvent +import at.lorenz.mod.utils.GuiRender.renderString +import at.lorenz.mod.utils.LorenzUtils +import at.lorenz.mod.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 + + LorenzMod.feature.dungeon.deathCounterDisplay.renderString(DungeonMilestoneDisplay.color + textToRender) + } + + private fun isEnabled(): Boolean { + return LorenzUtils.inDungeons && LorenzMod.feature.dungeon.deathCounter + } +} \ No newline at end of file -- cgit