diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/RegexUtils.kt | 12 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 8 |
2 files changed, 17 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RegexUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RegexUtils.kt index db19b63a2..068dfce1d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RegexUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RegexUtils.kt @@ -83,4 +83,16 @@ object RegexUtils { return this.any { it.matches(string) } } + /** + * Returns a list of all occurrences of a pattern within the [input] string. + */ + fun Pattern.findAll(input: String): List<String> { + val matcher = matcher(input) + + return buildList { + while (matcher.find()) { + add(matcher.group()) + } + } + } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 180959658..14e4a989f 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent 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.RegexUtils.findAll import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiUtilRenderComponents import net.minecraft.event.ClickEvent @@ -27,6 +28,7 @@ object StringUtils { private val sFormattingPattern = "(?i)§S".toPattern() private val stringColorPattern = "§[0123456789abcdef].*".toPattern() private val asciiPattern = "[^\\x00-\\x7F]".toPattern() + private val minecraftColorCodesPattern = "(?i)(§[0-9a-fklmnor])+".toPattern() fun String.trimWhiteSpaceAndResets(): String = whiteSpaceResetPattern.matcher(this).replaceAll("") fun String.trimWhiteSpace(): String = whiteSpacePattern.matcher(this).replaceAll("") @@ -556,7 +558,7 @@ object StringUtils { fun IChatComponent.contains(string: String): Boolean = formattedText.contains(string) - fun String.width(): Int { - return Minecraft.getMinecraft().fontRendererObj.getStringWidth(this) - } + fun String.width(): Int = Minecraft.getMinecraft().fontRendererObj.getStringWidth(this) + + fun String.lastColorCode(): String? = minecraftColorCodesPattern.findAll(this).lastOrNull() } |