io.github.aecsocket.glossa.configurate.ModelExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glossa-configurate Show documentation
Show all versions of glossa-configurate Show documentation
Localization library for Adventure components
package io.github.aecsocket.glossa.configurate
import io.github.aecsocket.glossa.*
import net.kyori.adventure.serializer.configurate4.ConfigurateComponentSerializer
import net.kyori.adventure.text.format.Style
import org.spongepowered.configurate.ConfigurationNode
import org.spongepowered.configurate.ConfigurationOptions
import org.spongepowered.configurate.kotlin.extensions.get
import org.spongepowered.configurate.loader.ConfigurationLoader
import org.spongepowered.configurate.serialize.SerializationException
import java.util.Locale
private fun validateKey(key: Any, node: ConfigurationNode) = try {
validateGlossaKey(key.toString())
} catch (ex: GlossaBuildException) {
throw SerializationException(node, String::class.java, "Invalid key", ex)
}
fun GlossaStandard.Model.substitutionsFromConfig(node: ConfigurationNode) {
substitutions {
node.childrenMap().forEach { (rawKey, child) ->
val key = validateKey(rawKey, child)
val substitution = child.string
?: throw SerializationException(child, String::class.java, "Requires substitution")
miniMessageSubstitution(key, substitution)
}
}
}
fun GlossaStandard.Model.stylesFromConfig(node: ConfigurationNode) {
styles {
node.childrenMap().forEach { (rawKey, child) ->
val key = validateKey(rawKey, child)
val style = child.get