blob: f34efe47322360a44d7a6b35e1663d5d777e610e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package at.hannibal2.skyhanni.utils.compat
import net.minecraft.util.IChatComponent
//#if MC > 1.16
//$$ import net.minecraft.ChatFormatting
//$$ import net.minecraft.network.chat.TextColor
//#endif
//#if MC > 1.20
//$$ import net.minecraft.text.MutableText
//$$ import net.minecraft.text.PlainTextContent
//#endif
fun IChatComponent.getDirectlyContainedText() =
//#if MC < 1.16
this.unformattedTextForChat
//#elseif MC < 1.20
//$$ this.contents
//#else
//$$ (this.content as? PlainTextContent)?.string().orEmpty()
//#endif
fun IChatComponent.getFormattedTextCompat() =
//#if MC < 1.16
this.formattedText
//#else
//$$run {
//$$ val sb = StringBuilder()
//$$ for (component in iterator()) {
//$$ sb.append(component.style.color?.toChatFormatting()?.toString() ?: "§r")
//$$ sb.append(component.getDirectlyContainedText())
//$$ sb.append("§r")
//$$ }
//$$ sb.toString()
//$$}
//$$
//$$private val textColorLUT = ChatFormatting.entries
//$$ .mapNotNull { formatting -> formatting.color?.let { it to formatting } }
//$$ .toMap()
//$$
//$$fun TextColor.toChatFormatting(): ChatFormatting? {
//$$ return textColorLUT[this.value]
//$$}
//$$
//$$fun Component.iterator(): Sequence<Component> {
//$$ return sequenceOf(this) + siblings.asSequence().flatMap { it.iterator() } // TODO: in theory we want to properly inherit styles here
//$$}
//#endif
//#if MC > 1.20
//$$fun MutableText.withColor(formatting: Formatting): Text {
//$$ return this.styled { it.withColor(formatting) }
//$$}
//#endif
|