diff options
| author | Linnea Gräf <nea@nea.moe> | 2024-05-03 19:34:32 +0200 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2024-05-03 19:34:32 +0200 |
| commit | 9ba9c9bf092b5873ab28162e105574d6c994300e (patch) | |
| tree | 4d9b3bbdf4da18819e26fa8e9a4d4d5260ccf4dd /src/main/java/at/hannibal2/skyhanni/utils | |
| parent | e210a5849b2c31628489568470580ec0416285e7 (diff) | |
| download | SkyHanni-feat/styledreplace.tar.gz SkyHanni-feat/styledreplace.tar.bz2 SkyHanni-feat/styledreplace.zip | |
Basic styled replacefeat/styledreplace
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt | 13 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 9 |
2 files changed, 13 insertions, 9 deletions
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] |
