diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2024-04-19 11:24:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-19 11:24:36 +0200 |
commit | f8efe9effd27bee18bced64385fc1ad9b05b68bd (patch) | |
tree | 1429920226fc79af8bae814bb7596b9e65e0cf7e /src/main/java/at/hannibal2/skyhanni/utils | |
parent | 7feb5c67fbc1c9f63118952f1bea649ada1dccd6 (diff) | |
download | skyhanni-f8efe9effd27bee18bced64385fc1ad9b05b68bd.tar.gz skyhanni-f8efe9effd27bee18bced64385fc1ad9b05b68bd.tar.bz2 skyhanni-f8efe9effd27bee18bced64385fc1ad9b05b68bd.zip |
Feature + Fix: Player Chat Rework (#1483)
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 59b82cd8c..68c92f7a7 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -127,6 +127,7 @@ object StringUtils { fun String.cleanPlayerName(displayName: Boolean = false): String { return if (displayName) { if (SkyHanniMod.feature.chat.playerMessage.playerRankHider) { + // TODO custom color "§b" + internalCleanPlayerName() } else this } else { @@ -317,4 +318,58 @@ object StringUtils { fun isEmpty(message: String): Boolean = message.removeColor().trimWhiteSpaceAndResets().isEmpty() fun generateRandomId() = UUID.randomUUID().toString() + + fun replaceIfNeeded( + original: IChatComponent, + newText: String, + ): ChatComponentText? { + val foundCommands = mutableListOf<IChatComponent>() + + addComponent(foundCommands, original) + for (sibling in original.siblings) { + addComponent(foundCommands, sibling) + } + + val size = foundCommands.size + if (size > 1) { + return null + } + + if (LorenzUtils.stripVanillaMessage(original.formattedText) == newText) return null + // TODO remove debug +// println("replaceIfNeeded!") +// println("original: ${original.formattedText}") +// println("newText: $newText") +// println(" ") + + val text = ChatComponentText(newText) + if (size == 1) { + val chatStyle = foundCommands[0].chatStyle + text.chatStyle.chatClickEvent = chatStyle.chatClickEvent + text.chatStyle.chatHoverEvent = chatStyle.chatHoverEvent + } + + return text + } + + private fun addComponent(foundCommands: MutableList<IChatComponent>, message: IChatComponent) { + val clickEvent = message.chatStyle.chatClickEvent + if (clickEvent != null) { + if (foundCommands.size == 1 && foundCommands[0].chatStyle.chatClickEvent.value == clickEvent.value) { + return + } + foundCommands.add(message) + } + } + + fun String.replaceAll(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { + var text = this + while (true) { + val newText = text.replace(oldValue, newValue, ignoreCase = ignoreCase) + if (newText == text) { + return text + } + text = newText + } + } } |