From 9ba9c9bf092b5873ab28162e105574d6c994300e Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Fri, 3 May 2024 19:34:32 +0200 Subject: Basic styled replace --- .../features/chat/playerchat/PlayerChatModifier.kt | 38 ++++++++++++++-------- .../skyhanni/features/misc/MarkedPlayerManager.kt | 19 +++++++++++ .../skyhanni/utils/ComponentMatcherUtils.kt | 13 ++++++++ .../at/hannibal2/skyhanni/utils/StringUtils.kt | 9 ----- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt index 2fbb9630e..b9ef6588e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt @@ -4,36 +4,48 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager +import at.hannibal2.skyhanni.utils.ComponentMatcher +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.intoSpan +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.replace +import at.hannibal2.skyhanni.utils.ComponentSpan import at.hannibal2.skyhanni.utils.StringUtils import net.minecraft.util.ChatComponentText +import net.minecraft.util.IChatComponent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.regex.Pattern class PlayerChatModifier { private val config get() = SkyHanniMod.feature.chat.playerMessage - private val patterns = mutableListOf() - - init { - patterns.add("§[ab6]\\[(?:VIP|MVP)(?:§.|\\+)*] {1,2}(?:§[7ab6])?(\\w{2,16})".toRegex()) // ranked player with prefix everywhere - patterns.add("§[7ab6](\\w{2,16})§r(?!§7x)(?!\$)".toRegex()) // all players without rank prefix in notification messages - } + private val patterns = listOf IChatComponent>>( + "\\[(?:VIP|MVP)(?:§.|\\+)*] {1,2}(?:§[7ab6])?(?\\w{2,16})".toPattern() to { + ChatComponentText("§b").appendSibling(componentOrThrow("name")) + }, + "§[7ab6](?\\w{2,16})§r(?!§7x)(?!\$)".toPattern() to { + ChatComponentText("§b").appendSibling(componentOrThrow("name")) + }, + "§[7ab6](?\\w{2,16}'s)".toPattern() to { + ChatComponentText("§b").appendSibling(componentOrThrow("name")) + }, + "§[7ab6](?\\w{2,16} §.)".toPattern() to { + ChatComponentText("§b").appendSibling(componentOrThrow("name")) + } + ) @SubscribeEvent fun onChat(event: SystemMessageEvent) { - val newMessage = cutMessage(event.chatComponent.formattedText) + val newMessage = cutMessage(event.chatComponent.intoSpan()) - event.chatComponent = StringUtils.replaceIfNeeded(event.chatComponent, ChatComponentText(newMessage)) ?: return + event.chatComponent = StringUtils.replaceIfNeeded(event.chatComponent, newMessage.intoComponent()) ?: return } - private fun cutMessage(input: String): String { + private fun cutMessage(input: ComponentSpan): ComponentSpan { var string = input if (config.playerRankHider) { - for (pattern in patterns) { - string = string.replace(pattern, "§b$1") + for ((pattern, func) in patterns) { + string = string.replace(pattern, func).intoSpan() } - string = string.replace("§[7ab6]((?:\\w+){2,16})'s", "§b$1's") - string = string.replace("§[7ab6]((?:\\w+){2,16}) (§.)", "§b$1 $2") } string = MarkedPlayerManager.replaceInChat(string) diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/MarkedPlayerManager.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/MarkedPlayerManager.kt index 5edabcbb1..10e25c54c 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/MarkedPlayerManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/MarkedPlayerManager.kt @@ -9,12 +9,17 @@ import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ColorUtils.withAlpha +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.intoSpan +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.replace +import at.hannibal2.skyhanni.utils.ComponentSpan import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.LorenzUtils import net.minecraft.client.Minecraft import net.minecraft.client.entity.EntityOtherPlayerMP +import net.minecraft.util.ChatComponentText import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.regex.Pattern class MarkedPlayerManager { companion object { @@ -91,6 +96,20 @@ class MarkedPlayerManager { } return text } + + fun replaceInChat(string: ComponentSpan): ComponentSpan { + if (!config.highlightInChat) return string + + val color = config.chatColor.getChatColor() + var text = string + for (markedPlayer in playerNamesToMark) { + text = text.replace(markedPlayer.toPattern(Pattern.LITERAL)) { + ChatComponentText(color) + .appendSibling(group().intoComponent()) + }.intoSpan() + } + return text + } } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt index f5c25ab2b..aed388ef3 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt @@ -31,6 +31,19 @@ object ComponentMatcherUtils { ) } + fun ComponentSpan.replace(pattern: Pattern, replacement: ComponentMatcher.() -> IChatComponent): ChatComponentText { + val matcher = pattern.matcher(getText()) + val elements = ChatComponentText("") + var lastIndex = 0 + while (matcher.find()) { + elements.appendSibling(slice(lastIndex, matcher.start()).intoComponent()) + elements.appendSibling(replacement(ComponentMatcher(matcher, this))) + lastIndex = matcher.end() + } + elements.appendSibling(slice(lastIndex, length).intoComponent()) + return elements + } + /** * Create a styled matcher, analogous to [Pattern.matcher], but while preserving [ChatStyle]. */ diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 4feb0ba13..fb297fe41 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -4,7 +4,6 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.mixins.transformers.AccessorChatComponentText import at.hannibal2.skyhanni.utils.GuiRenderUtils.darkenColor import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators -import at.hannibal2.skyhanni.utils.StringUtils.width import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiUtilRenderComponents import net.minecraft.util.ChatComponentText @@ -322,14 +321,6 @@ object StringUtils { fun generateRandomId() = UUID.randomUUID().toString() - fun replaceIfNeeded( - original: ChatComponentText, - newText: String, - ): ChatComponentText? { - return replaceIfNeeded(original, ChatComponentText(newText)) - } - - private val colorMap = EnumChatFormatting.entries.associateBy { it.toString()[1] } fun enumChatFormattingByCode(char: Char): EnumChatFormatting? { return colorMap[char] -- cgit