121 lines
3.0 KiB
Kotlin
121 lines
3.0 KiB
Kotlin
|
package net.shadowfacts.phycon.util
|
||
|
|
||
|
import net.minecraft.text.*
|
||
|
import net.minecraft.util.Formatting
|
||
|
import java.util.*
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
fun text(build: TextContext.() -> Unit): Text {
|
||
|
val context = TextContext()
|
||
|
context.build()
|
||
|
return context.toText()
|
||
|
}
|
||
|
|
||
|
class TextContext(
|
||
|
private val texts: MutableList<Text> = mutableListOf(),
|
||
|
private val styles: Deque<Style> = LinkedList()
|
||
|
) {
|
||
|
|
||
|
fun toText(): MutableText {
|
||
|
val text = LiteralText("")
|
||
|
texts.forEach(text::append)
|
||
|
return text
|
||
|
}
|
||
|
|
||
|
operator fun String.unaryPlus() {
|
||
|
+LiteralText(this)
|
||
|
}
|
||
|
|
||
|
operator fun MutableText.unaryPlus() {
|
||
|
if (styles.isNotEmpty()) {
|
||
|
texts.add(this.setStyle(styles.peek()))
|
||
|
} else {
|
||
|
texts.add(this)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fun translate(key: String, vararg parameters: Any): TranslatableText {
|
||
|
return TranslatableText(key, *parameters)
|
||
|
}
|
||
|
|
||
|
fun withStyle(style: Style, build: TextContext.() -> Unit) {
|
||
|
styles.push(style)
|
||
|
build()
|
||
|
styles.pop()
|
||
|
}
|
||
|
|
||
|
fun withStyle(style: Style.() -> Style, build: TextContext.() -> Unit) {
|
||
|
withStyle(Style.EMPTY.style(), build)
|
||
|
}
|
||
|
|
||
|
fun withStyle(formatting: Formatting, build: TextContext.() -> Unit) {
|
||
|
withStyle(Style.EMPTY.withFormatting(formatting), build)
|
||
|
}
|
||
|
|
||
|
val bold = Formatting.BOLD
|
||
|
fun Style.bold(): Style = withBold(true)
|
||
|
|
||
|
val italic = Formatting.ITALIC
|
||
|
fun Style.italic(): Style = withItalic(true)
|
||
|
|
||
|
val strikethrough = Formatting.STRIKETHROUGH
|
||
|
fun Style.strikethrough(): Style = withFormatting(strikethrough)
|
||
|
|
||
|
val underline = Formatting.UNDERLINE
|
||
|
fun Style.underline(): Style = withFormatting(underline)
|
||
|
|
||
|
val obfuscated = Formatting.OBFUSCATED
|
||
|
fun Style.obfuscated(): Style = withFormatting(obfuscated)
|
||
|
|
||
|
val black = Formatting.BLACK
|
||
|
fun Style.black(): Style = withFormatting(black)
|
||
|
|
||
|
val darkBlue = Formatting.DARK_BLUE
|
||
|
fun Style.darkBlue(): Style = withFormatting(darkBlue)
|
||
|
|
||
|
val darkGreen = Formatting.DARK_GREEN
|
||
|
fun Style.darkGreen(): Style = withFormatting(darkGreen)
|
||
|
|
||
|
val darkAqua = Formatting.DARK_AQUA
|
||
|
fun Style.darkAqua(): Style = withFormatting(darkAqua)
|
||
|
|
||
|
val darkRed = Formatting.DARK_RED
|
||
|
fun Style.darkRed(): Style = withFormatting(darkRed)
|
||
|
|
||
|
val darkPurple = Formatting.DARK_PURPLE
|
||
|
fun Style.darkPurple(): Style = withFormatting(darkPurple)
|
||
|
|
||
|
val gold = Formatting.GOLD
|
||
|
fun Style.gold(): Style = withFormatting(gold)
|
||
|
|
||
|
val gray = Formatting.GRAY
|
||
|
fun Style.gray(): Style = withFormatting(gray)
|
||
|
|
||
|
val darkGray = Formatting.DARK_GRAY
|
||
|
fun Style.darkGray(): Style = withFormatting(darkGray)
|
||
|
|
||
|
val blue = Formatting.BLUE
|
||
|
fun Style.blue(): Style = withFormatting(blue)
|
||
|
|
||
|
val green = Formatting.GREEN
|
||
|
fun Style.green(): Style = withFormatting(green)
|
||
|
|
||
|
val aqua = Formatting.RED
|
||
|
fun Style.aqua(): Style = withFormatting(aqua)
|
||
|
|
||
|
val red = Formatting.RED
|
||
|
fun Style.red(): Style = withFormatting(red)
|
||
|
|
||
|
val lightPurple = Formatting.LIGHT_PURPLE
|
||
|
fun Style.lightPurple(): Style = withFormatting(lightPurple)
|
||
|
|
||
|
val yellow = Formatting.YELLOW
|
||
|
fun Style.yellow(): Style = withFormatting(yellow)
|
||
|
|
||
|
val white = Formatting.WHITE
|
||
|
fun Style.white(): Style = withFormatting(white)
|
||
|
|
||
|
}
|