Add getLegacyFormatString method

[no changelog]
This commit is contained in:
Linnea Gräf
2024-10-24 14:49:21 +02:00
parent 6100b8c9a0
commit 0357245e88
5 changed files with 223 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package moe.nea.firmament.util
import net.minecraft.text.MutableText
import net.minecraft.text.PlainTextContent
import net.minecraft.text.Text
import net.minecraft.text.TextColor
import net.minecraft.text.TranslatableTextContent
import net.minecraft.util.Formatting
import moe.nea.firmament.Firmament
@@ -92,6 +93,30 @@ val Text.unformattedString: String
val Text.directLiteralStringContent: String? get() = (this.content as? PlainTextContent)?.string()
fun Text.getLegacyFormatString() =
run {
val sb = StringBuilder()
for (component in iterator()) {
sb.append(component.style.color?.toChatFormatting()?.toString() ?: "§r")
sb.append(component.directLiteralStringContent)
sb.append("§r")
}
sb.toString()
}
private val textColorLUT = Formatting.entries
.mapNotNull { formatting -> formatting.colorValue?.let { it to formatting } }
.toMap()
fun TextColor.toChatFormatting(): Formatting? {
return textColorLUT[this.rgb]
}
fun Text.iterator(): Sequence<Text> {
return sequenceOf(this) + siblings.asSequence()
.flatMap { it.iterator() } // TODO: in theory we want to properly inherit styles here
}
fun Text.allSiblings(): List<Text> = listOf(this) + siblings.flatMap { it.allSiblings() }
fun MutableText.withColor(formatting: Formatting) = this.styled { it.withColor(formatting).withItalic(false) }