From f617d59f1c52ad77f23ce28fb14bf44986e38ed1 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sat, 23 Sep 2023 21:36:29 +1000 Subject: chat message changes #482 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add chat component functions * reword + remove empty text * remove type * isPlayerMessage * include the other stuff in the translator pr plus format * Merge branch 'hannibal002:beta' into chat_component_stuff * Merge remote-tracking branch 'origin/chat_component_stuff' into chat_… * allow to work with priv messages + get ready for future --- .../at/hannibal2/skyhanni/utils/StringUtils.kt | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index f0eb8e059..591519a82 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -1,15 +1,21 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.mixins.transformers.AccessorChatComponentText import at.hannibal2.skyhanni.utils.GuiRenderUtils.darkenColor import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiUtilRenderComponents import net.minecraft.util.ChatComponentText +import net.minecraft.util.IChatComponent import org.intellij.lang.annotations.Language import java.util.* +import java.util.function.Predicate import java.util.regex.Matcher import java.util.regex.Pattern object StringUtils { + private val playerChatPattern = ".*§[f7]: .*".toPattern() + private val chatUsernamePattern = "^(?:\\[\\d+] )?(?:\\S )?(?:\\[\\w.+] )?(?\\w+)(?: \\[.+?])?\$".toPattern() + fun String.firstLetterUppercase(): String { if (isEmpty()) return this @@ -159,4 +165,55 @@ object StringUtils { i < limit } } + + // recursively goes through the chat component until an action is completed + fun modifyFirstChatComponent(chatComponent: IChatComponent, action: Predicate): Boolean { + if (action.test(chatComponent)) { + return true + } + for (sibling in chatComponent.siblings) { + if (modifyFirstChatComponent(sibling, action)) { + return true + } + } + return false + } + + // replaces a word without breaking any chat components + fun replaceFirstChatText(chatComponent: IChatComponent, toReplace: String, replacement: String): IChatComponent { + modifyFirstChatComponent(chatComponent) { component -> + if (component is ChatComponentText) { + component as AccessorChatComponentText + if (component.text_skyhanni().contains(toReplace)) { + component.setText_skyhanni(component.text_skyhanni().replace(toReplace, replacement)) + return@modifyFirstChatComponent true + } + return@modifyFirstChatComponent false + } + return@modifyFirstChatComponent false + } + return chatComponent + } + + fun String.getPlayerName(): String { + if (!playerChatPattern.matcher(this).matches()) return "-" + + var username = this.removeColor().split(":")[0] + + if (username.contains(">")) { + username = username.substring(username.indexOf('>') + 1).trim() + } + if (username.startsWith("From ")) { + username = username.removePrefix("From ") + } + if (username.startsWith("To ")) { + username = username.removePrefix("To ") + } + + val matcher = chatUsernamePattern.matcher(username) + + if (!matcher.matches()) return "-" + username = matcher.group("username") + return username + } } \ No newline at end of file -- cgit