All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.aecsocket.glossa.configurate.ModelExtensions.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
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