From 89c8bf8103b9429fbc7a1ea63612dac565872d43 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Thu, 20 Jun 2024 23:25:12 +0200 Subject: Backend: PlayerDeathEvent (#1544) Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../java/at/hannibal2/skyhanni/data/MiningAPI.kt | 16 ++++--- .../hannibal2/skyhanni/data/PlayerDeathManager.kt | 31 ++++++++++++ .../skyhanni/events/player/PlayerDeathEvent.kt | 9 ++++ .../skyhanni/features/chat/PlayerDeathMessages.kt | 56 +++++++++------------- 4 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/data/PlayerDeathManager.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/events/player/PlayerDeathEvent.kt (limited to 'src/main/java/at') diff --git a/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt index 47d38ef74..940b4ea35 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt @@ -10,6 +10,7 @@ import at.hannibal2.skyhanni.events.PlaySoundEvent import at.hannibal2.skyhanni.events.ScoreboardUpdateEvent import at.hannibal2.skyhanni.events.ServerBlockChangeEvent import at.hannibal2.skyhanni.events.mining.OreMinedEvent +import at.hannibal2.skyhanni.events.player.PlayerDeathEvent import at.hannibal2.skyhanni.features.gui.customscoreboard.ScoreboardPattern import at.hannibal2.skyhanni.features.mining.OreBlock import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -20,7 +21,6 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.inAnyIsland import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst -import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.TimeUtils.format @@ -37,7 +37,7 @@ object MiningAPI { private val group = RepoPattern.group("data.miningapi") private val glaciteAreaPattern by group.pattern("area.glacite", "Glacite Tunnels|Glacite Lake") private val dwarvenBaseCampPattern by group.pattern("area.basecamp", "Dwarven Base Camp") - private val coldReset by group.pattern( + val coldReset by group.pattern( "cold.reset", "§6The warmth of the campfire reduced your §r§b❄ Cold §r§6to §r§a0§r§6!|§c ☠ §r§7You froze to death§r§7.", ) @@ -129,11 +129,13 @@ object MiningAPI { updateCold(0) lastColdReset = SimpleTimeMark.now() } - coldResetDeath.matchMatcher(event.message) { - if (group("name") == LorenzUtils.getPlayerName()) { - updateCold(0) - lastColdReset = SimpleTimeMark.now() - } + } + + @SubscribeEvent + fun onPlayerDeath(event: PlayerDeathEvent) { + if (event.name == LorenzUtils.getPlayerName()) { + updateCold(0) + lastColdReset = SimpleTimeMark.now() } } diff --git a/src/main/java/at/hannibal2/skyhanni/data/PlayerDeathManager.kt b/src/main/java/at/hannibal2/skyhanni/data/PlayerDeathManager.kt new file mode 100644 index 000000000..75616520f --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/PlayerDeathManager.kt @@ -0,0 +1,31 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.player.PlayerDeathEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +@SkyHanniModule +object PlayerDeathManager { + + /** + * REGEX-TEST: §c ☠ §r§7§r§bZeroHazel§r§7 was killed by §r§8§lAshfang§r§7§r§7. + */ + private val deathMessagePattern by RepoPattern.pattern( + "chat.player.death", + "§c ☠ §r§7§r§.(?.+)§r§7 (?.+)", + ) + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + val message = event.message + deathMessagePattern.matchMatcher(message) { + val name = group("name") + val reason = group("reason").removeColor() + PlayerDeathEvent(name, reason, event).postAndCatch() + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/events/player/PlayerDeathEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/player/PlayerDeathEvent.kt new file mode 100644 index 000000000..fe9cac685 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/player/PlayerDeathEvent.kt @@ -0,0 +1,9 @@ +package at.hannibal2.skyhanni.events.player + +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.LorenzEvent + +/** + * When the player "you" dies in the game. does not fire when other players die. + */ +class PlayerDeathEvent(val name: String, val reason: String, val chatEvent: LorenzChatEvent) : LorenzEvent() diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt index 55c690199..d256bfe63 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/PlayerDeathMessages.kt @@ -1,8 +1,8 @@ package at.hannibal2.skyhanni.features.chat import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.events.SecondPassedEvent +import at.hannibal2.skyhanni.events.player.PlayerDeathEvent import at.hannibal2.skyhanni.features.dungeon.DungeonAPI import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -10,10 +10,7 @@ import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher -import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.getLorenzVec -import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern import net.minecraft.client.entity.EntityOtherPlayerMP import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -22,12 +19,6 @@ object PlayerDeathMessages { private val lastTimePlayerSeen = mutableMapOf() - //§c ☠ §r§7§r§bZeroHazel§r§7 was killed by §r§8§lAshfang§r§7§r§7. - private val deathMessagePattern by RepoPattern.pattern( - "chat.player.death", - "§c ☠ §r§7§r§.(?.+)§r§7 (?.+)" - ) - @SubscribeEvent fun onSecondPassed(event: SecondPassedEvent) { if (!isHideFarDeathsEnabled()) return @@ -36,39 +27,36 @@ object PlayerDeathMessages { } @SubscribeEvent - fun onChat(event: LorenzChatEvent) { + fun onPlayerDeath(event: PlayerDeathEvent) { if (!LorenzUtils.inSkyBlock) return - val message = event.message - deathMessagePattern.matchMatcher(message) { - val name = group("name") - if (MarkedPlayerManager.config.highlightInChat && - !DungeonAPI.inDungeon() && !LorenzUtils.inKuudraFight && MarkedPlayerManager.isMarkedPlayer(name) - ) { - val reason = group("reason").removeColor() - - val color = MarkedPlayerManager.config.chatColor.getChatColor() - ChatUtils.chat(" §c☠ $color$name §7$reason", false) - event.blockedReason = "marked_player_death" - return - } + val name = event.name + + if (MarkedPlayerManager.config.highlightInChat && + !DungeonAPI.inDungeon() && + !LorenzUtils.inKuudraFight && + MarkedPlayerManager.isMarkedPlayer(name)) { + val reason = event.reason + val color = MarkedPlayerManager.config.chatColor.getChatColor() + ChatUtils.chat(" §c☠ $color$name §7$reason", false) + event.chatEvent.blockedReason = "marked_player_death" + return + } - val time = System.currentTimeMillis() > lastTimePlayerSeen.getOrDefault(name, 0) + 30_000 - if (isHideFarDeathsEnabled() && time) { - event.blockedReason = "far_away_player_death" - } + val time = System.currentTimeMillis() > lastTimePlayerSeen.getOrDefault(name, 0) + 30_000 + if (isHideFarDeathsEnabled() && time) { + event.chatEvent.blockedReason = "far_away_player_death" } } private fun checkOtherPlayers() { - val location = LocationUtils.playerLocation() - for (otherPlayer in EntityUtils.getEntities() - .filter { it.getLorenzVec().distance(location) < 25 }) { + val entities = EntityUtils.getEntities() + .filter { it.getLorenzVec().distance(LocationUtils.playerLocation()) < 25 } + for (otherPlayer in entities) { lastTimePlayerSeen[otherPlayer.name] = System.currentTimeMillis() } } - private fun isHideFarDeathsEnabled(): Boolean { - return LorenzUtils.inSkyBlock && SkyHanniMod.feature.chat.hideFarDeathMessages && !DungeonAPI.inDungeon() && !LorenzUtils.inKuudraFight - } + private fun isHideFarDeathsEnabled(): Boolean = + LorenzUtils.inSkyBlock && SkyHanniMod.feature.chat.hideFarDeathMessages && !DungeonAPI.inDungeon() && !LorenzUtils.inKuudraFight } -- cgit