From 7bcc247bd3f27735bbfff207ce72b3e2cffb512e Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Fri, 23 Aug 2024 09:52:47 +0200 Subject: Fix: Double Color in ScoreboardData Part 2 (#2382) --- .../at/hannibal2/skyhanni/data/ScoreboardData.kt | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'src/main/java/at') diff --git a/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt b/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt index 26915faf3..2287ae358 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ScoreboardData.kt @@ -22,13 +22,7 @@ object ScoreboardData { private var sidebarLines: List = emptyList() // TODO rename to raw var sidebarLinesRaw: List = emptyList() // TODO delete - val objectiveTitle: String get() = grabObjectiveTitle() - - fun grabObjectiveTitle(): String { - val scoreboard = Minecraft.getMinecraft().theWorld?.scoreboard ?: return "" - val objective = scoreboard.getObjectiveInDisplaySlot(1) ?: return "" - return objective.displayName - } + val objectiveTitle: String get() = Minecraft.getMinecraft().theWorld?.scoreboard?.getObjectiveInDisplaySlot(1)?.displayName ?: "" private var dirty = false @@ -39,16 +33,32 @@ object ScoreboardData { val start = split[0] var end = if (split.size > 1) split[1] else "" + /** + * If the line is split into two parts, we need to remove the color code prefixes from the end part + * to prevent the color from being applied to the start of `end`, which would cause the color to be + * duplicated in the final output. + * + * This fucks up different Regex checks if not working correctly, like here: + * ``` + * Pattern: '§8- (§.)+[\w\s]+Dragon§a [\w,.]+§.❤' + * Lines: - '§8- §c§aApex Dra§agon§a 486M§c❤' + * - '§8- §c§6Flame Dr§6agon§a 460M§c❤' + * ``` + */ val lastColor = start.lastColorCode() ?: "" - // Determine the longest prefix of "end" that matches any suffix of "lastColor" + // Generate the list of color suffixes val colorSuffixes = generateSequence(lastColor) { it.dropLast(2) } .takeWhile { it.isNotEmpty() } - .toList() - - val matchingPrefix = colorSuffixes.find { end.startsWith(it) } ?: "" - if (matchingPrefix.isNotEmpty()) { - end = end.removePrefix(matchingPrefix) + .toMutableList() + + // Iterate through the colorSuffixes to remove matching prefixes from 'end' + for (suffix in colorSuffixes.toList()) { + if (end.startsWith(suffix)) { + end = end.removePrefix(suffix) + colorSuffixes.remove(suffix) + break + } } add(start + end) -- cgit