Skip to content

Commit a22f236

Browse files
UTextComponent: fix inconsistencies in toFormattedString
1 parent ba14689 commit a22f236

File tree

1 file changed

+21
-44
lines changed

1 file changed

+21
-44
lines changed

src/main/kotlin/gg/essential/universal/utils/textUtils.kt

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,46 @@ package gg.essential.universal.utils
33
import net.minecraft.util.IChatComponent
44

55
//#if MC>=11600
6-
//$$ import net.minecraft.util.ICharacterConsumer
76
//$$ import net.minecraft.util.text.Color
87
//$$ import net.minecraft.util.text.Style
98
//$$ import net.minecraft.util.text.TextFormatting
9+
//$$ import java.util.Optional
1010
//#endif
1111

1212
//#if MC>=11602
13-
//$$ private class TextBuilder(private val isFormatted: Boolean) : ICharacterConsumer {
14-
//$$ private val builder = StringBuilder()
15-
//$$ private var cachedStyle: Style? = null
13+
//$$ private val colorToFormatChar = TextFormatting.values().mapNotNull { format ->
14+
//$$ Color.fromTextFormatting(format)?.let { it to format }
15+
//$$ }.toMap()
1616
//$$
17-
//$$ override fun accept(index: Int, style: Style, codePoint: Int): Boolean {
18-
//$$ if (isFormatted && style != cachedStyle) {
19-
//$$ cachedStyle = style
20-
//$$ builder.append(formatString(style))
21-
//$$ }
22-
//$$
23-
//$$ builder.append(codePoint.toChar())
24-
//$$ return true
25-
//$$ }
26-
//$$
27-
//$$ fun getString() = builder.toString()
28-
//$$
29-
//$$ private fun formatString(style: Style): String {
30-
//$$ val builder = StringBuilder("§r")
31-
//$$
32-
//$$ when {
33-
//$$ style.bold -> builder.append("§l")
34-
//$$ style.italic -> builder.append("§o")
35-
//$$ style.underlined -> builder.append("§n")
36-
//$$ style.strikethrough -> builder.append("§m")
37-
//$$ style.obfuscated -> builder.append("§k")
38-
//$$ }
39-
//$$
40-
//$$ style.color?.let(colorToFormatChar::get)?.let {
41-
//$$ builder.append(it)
42-
//$$ }
43-
//$$ return builder.toString()
44-
//$$ }
45-
//$$
46-
//$$ companion object {
47-
//$$ private val colorToFormatChar = TextFormatting.values().mapNotNull { format ->
48-
//$$ Color.fromTextFormatting(format)?.let { it to format }
49-
//$$ }.toMap()
50-
//$$ }
17+
//$$ private fun formatString(style: Style): String = buildString {
18+
//$$ style.color?.let(colorToFormatChar::get)?.let(::append)
19+
//$$ if (style.isBold) append("§l")
20+
//$$ if (style.isItalic) append("§o")
21+
//$$ if (style.isUnderlined) append("§n")
22+
//$$ if (style.isObfuscated) append("§k")
23+
//$$ if (style.isStrikethrough) append("§m")
5124
//$$ }
5225
//#endif
5326

5427
fun IChatComponent.toUnformattedString(): String {
5528
//#if MC>=11600
56-
//$$ val builder = TextBuilder(false)
57-
//$$ func_241878_f().accept(builder)
58-
//$$ return builder.getString()
29+
//$$ return string
5930
//#else
6031
return unformattedText
6132
//#endif
6233
}
6334

6435
fun IChatComponent.toFormattedString(): String {
6536
//#if MC>=11600
66-
//$$ val builder = TextBuilder(true)
67-
//$$ func_241878_f().accept(builder)
68-
//$$ return builder.getString()
37+
//$$ return buildString {
38+
//$$ append(formatString(style))
39+
//$$ [email protected]<String> {
40+
//$$ append(it)
41+
//$$ Optional.empty()
42+
//$$ }
43+
//$$ append("§r")
44+
//$$ siblings.forEach { append(it.toFormattedString()) }
45+
//$$ }
6946
//#else
7047
return formattedText
7148
//#endif

0 commit comments

Comments
 (0)