TerraOceanPlugin/KotlinPlugin/src/main/kotlin/com/terraocean/plugin/Configuration.kt

51 lines
1.7 KiB
Kotlin

package com.terraocean.plugin
import hazae41.minecraft.kutils.bukkit.ConfigFile
import hazae41.minecraft.kutils.get
import java.io.File
import kotlin.reflect.KProperty
object Settings : TerraConfig(instance.dataFolder["config.yml"]) {
val socketURL by item("socket.url", "ws://localhost:8080/ws")
}
object Messages : TerraConfig(instance.dataFolder["strings.yml"]) {
val botConnectionLost by message("errors.botConnectionLost")
val voteNew by message("vote.new")
val voteWarn by message("vote.warn")
val voteTimeout by message("vote.timeout")
val kickVoiceChannel by message("kick.voiceChannelLeave")
val voteKick by message("kick.voteKick")
val kickRemovedFromWhitelist by message("kick.removedFromWhitelist")
}
open class TerraConfig(file: File) : ConfigFile(file) {
inner class message(val path: String) {
init {
if (!config.contains(path)) {
set(path, "Message not found: $path")
}
}
operator fun getValue(ref: Any?, prop: KProperty<*>): String =
config.getString(path, "Message not found: $path")!!
operator fun setValue(ref: Any?, prop: KProperty<*>, value: String) = set(path, value)
}
inner class item<T>(private val path: String, private val default: T) {
init {
if (!config.contains(path)) {
set(path, default)
}
}
@Suppress("UNCHECKED_CAST")
operator fun getValue(ref: Any?, prop: KProperty<*>): T =
config.get(path, default) as? T ?: run {
throw IllegalStateException("Path $path [value ${config["path"]}]] does not conform to type required")
}
}
}