From f36ef92c5df4a8257b17758a6170bfd6b2b6ffab Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Mon, 16 Oct 2023 20:13:09 +1100 Subject: Fix: Chat symbols (#567) Hopefully fixed all chat symbol issues and removed the chat symbols on private messages. #567 --- .../skyhanni/features/misc/PlayerChatSymbols.kt | 70 ++++++++++++---------- .../features/misc/compacttablist/TabStringType.kt | 2 +- 2 files changed, 38 insertions(+), 34 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/features') diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt index b826c5077..80ec276db 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt @@ -6,10 +6,10 @@ import at.hannibal2.skyhanni.features.misc.compacttablist.TabStringType import at.hannibal2.skyhanni.mixins.transformers.AccessorChatComponentText import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.StringUtils +import at.hannibal2.skyhanni.utils.StringUtils.getPlayerNameAndRankFromChatMessage import at.hannibal2.skyhanni.utils.StringUtils.getPlayerNameFromChatMessage import at.hannibal2.skyhanni.utils.StringUtils.removeResets import at.hannibal2.skyhanni.utils.TabListData -import net.minecraft.client.Minecraft import net.minecraft.util.ChatComponentText import net.minecraft.util.IChatComponent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -18,6 +18,8 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class PlayerChatSymbols { private val config get() = SkyHanniMod.feature.misc.chatSymbols private val nameSymbols = mutableMapOf() + private val symbolsPattern = "^(?(?:(?:§\\w)+\\S)+) ".toPattern() + private val symbolPattern = "((?:§\\w)+\\S)".toPattern() @SubscribeEvent fun onChatReceived(event: LorenzChatEvent) { @@ -26,55 +28,57 @@ class PlayerChatSymbols { val username = event.message.getPlayerNameFromChatMessage() ?: return - updateSymbolFromTabList(username) + var usernameWithSymbols = TabListData.getTabList() + .find { playerName -> TabStringType.usernameFromLine(playerName) == username } - val usernameWithSymbols = nameSymbols[username] ?: return + if (usernameWithSymbols != null) { + nameSymbols[username] = usernameWithSymbols + } + + usernameWithSymbols = nameSymbols[username] ?: return val split = usernameWithSymbols.split("$username ") - var emblemText = if (split.size > 1) split[1] else "" - emblemText = emblemText.removeResets() + var emblemText = if (split.size > 1) split[1].removeResets() else "" - if (emblemText == "") return - event.chatComponent = StringUtils.replaceFirstChatText(event.chatComponent, "$emblemText ", "") + var matcher = symbolsPattern.matcher("$emblemText ") + emblemText = if (matcher.find()) { + matcher.group("symbols") + } else "" - StringUtils.modifyFirstChatComponent(event.chatComponent) { component -> - modify(component, username, emblemText) + if (emblemText == "") { + return } - } - private fun updateSymbolFromTabList(username: String) { - val talkingPlayer = Minecraft.getMinecraft().theWorld.getPlayerEntityByName(username) - nameSymbols[username] = if (talkingPlayer != null) { - talkingPlayer.displayName.siblings[0].unformattedText - } else { - TabListData.getTabList() - .find { playerName -> TabStringType.usernameFromLine(playerName) == username } ?: return + val emblems = mutableListOf() + matcher = symbolPattern.matcher(emblemText) + while (matcher.find()) { + emblems.add(matcher.group(1)) + } + + for (emblem in emblems) { + event.chatComponent = StringUtils.replaceFirstChatText(event.chatComponent, "$emblem ", "") + } + + val rankAndName = event.message.getPlayerNameAndRankFromChatMessage() ?: return + + StringUtils.modifyFirstChatComponent(event.chatComponent) { component -> + modify(component, emblemText, rankAndName) } } - private fun modify(component: IChatComponent, username: String, emblemText: String): Boolean { + private fun modify(component: IChatComponent, emblemText: String, rankAndName: String): Boolean { if (component !is ChatComponentText) return false component as AccessorChatComponentText - if (!component.text_skyhanni().contains(username)) return false + if (!component.text_skyhanni().contains(rankAndName)) return false val oldText = component.text_skyhanni() - component.setText_skyhanni(component.text_skyhanni().replace(oldText, getNewText(emblemText, oldText))) + component.setText_skyhanni(component.text_skyhanni().replace(oldText, getNewText(emblemText, oldText, rankAndName))) return true } - private fun getNewText(emblemText: String, oldText: String): String = when (config.symbolLocation) { - 0 -> "$emblemText $oldText" - 1 -> iconAfterName(oldText, emblemText) - + private fun getNewText(emblemText: String, oldText: String, rankAndName: String): String = when (config.symbolLocation) { + 0 -> oldText.replace(rankAndName, "$emblemText $rankAndName") + 1 -> oldText.replace(rankAndName, "$rankAndName $emblemText ") else -> oldText } - - private fun iconAfterName(oldText: String, emblemText: String): String { - if (!oldText.contains("§f:")) return "$oldText $emblemText " - - // fixing it for when you type a message as the chat isn't split the same - val ownChatSplit = oldText.split("§f:") - if (ownChatSplit.size <= 1) return oldText - return "${ownChatSplit[0]} $emblemText §f:${ownChatSplit[1]}" - } } \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/TabStringType.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/TabStringType.kt index 996379041..344a21ae1 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/TabStringType.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/TabStringType.kt @@ -9,7 +9,7 @@ enum class TabStringType { PLAYER; companion object { - val usernamePattern = "^\\[(?\\d+)] (?:\\[\\w+] )?(?\\w+)".toPattern() + private val usernamePattern = "^\\[(?\\d+)] (?:\\[\\w+] )?(?\\w+)".toPattern() fun fromLine(line: String): TabStringType { val strippedLine: String = line.removeColor() -- cgit