aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/ComponentMatcherUtils.kt13
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt9
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]