aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt31
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/RegexUtils.kt12
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt8
3 files changed, 28 insertions, 23 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt b/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt
index b24f08b04..0ade6a946 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt
@@ -6,7 +6,7 @@ import at.hannibal2.skyhanni.events.RawScoreboardUpdateEvent
import at.hannibal2.skyhanni.events.ScoreboardUpdateEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketReceivedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
-import at.hannibal2.skyhanni.utils.RegexUtils.matches
+import at.hannibal2.skyhanni.utils.StringUtils.lastColorCode
import net.minecraft.client.Minecraft
import net.minecraft.network.play.server.S3CPacketUpdateScore
import net.minecraft.network.play.server.S3EPacketTeams
@@ -26,30 +26,21 @@ object ScoreboardData {
private var dirty = false
- private val minecraftColorCodesPattern = "(?i)[0-9a-fkmolnr]".toPattern()
-
- fun formatLines(rawList: List<String>): List<String> {
- val list = mutableListOf<String>()
+ private fun formatLines(rawList: List<String>) = buildList {
for (line in rawList) {
val separator = splitIcons.find { line.contains(it) } ?: continue
val split = line.split(separator)
val start = split[0]
- var end = split[1]
- // get last color code in start
- val lastColorIndex = start.lastIndexOf('§')
- val lastColor = if (lastColorIndex != -1
- && lastColorIndex + 1 < start.length
- && (minecraftColorCodesPattern.matches(start[lastColorIndex + 1].toString()))
- ) start.substring(lastColorIndex, lastColorIndex + 2)
- else ""
-
- // remove first color code from end, when it is the same as the last color code in start
- end = end.removePrefix(lastColor)
-
- list.add(start + end)
- }
+ var end = if (split.size > 1) split[1] else ""
+
+ val lastColor = start.lastColorCode() ?: ""
- return list
+ if (end.startsWith(lastColor)) {
+ end = end.removePrefix(lastColor)
+ }
+
+ add(start + end)
+ }
}
@HandleEvent(receiveCancelled = true)
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()
}