diff options
Diffstat (limited to 'src/main/kotlin/com/examplemod/config')
-rw-r--r-- | src/main/kotlin/com/examplemod/config/Config.kt | 52 | ||||
-rw-r--r-- | src/main/kotlin/com/examplemod/config/PersistentData.kt | 35 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/main/kotlin/com/examplemod/config/Config.kt b/src/main/kotlin/com/examplemod/config/Config.kt new file mode 100644 index 0000000..3008de3 --- /dev/null +++ b/src/main/kotlin/com/examplemod/config/Config.kt @@ -0,0 +1,52 @@ +package com.examplemod.config + +import gg.essential.vigilance.Vigilant +import java.awt.Color +import java.io.File + + +object Config : Vigilant( + File(ExampleMod.configDirectory, "config.toml"), + ExampleMod.metadata.name +) { + var demoSwitch = false + var demoSelector = 0 + var demoColor : Color = Color.WHITE + var demoText = "" + + init { + category("One category") { + switch( + ::demoSwitch, + name = "Switch", + description = "This is a switch" + ) + + subcategory("An additional category") { + selector( + ::demoSelector, + name = "Selector", + description = "This is a selector", + options = listOf("Option 1", "Option 2", "Option 3") + ) + color( + ::demoColor, + name = "Color", + description = "This sets a color" + ) + } + } + + category("Another category") { + text( + ::demoText, + name = "Text", + description = "This is text", + placeholder = "This is some placeholder text." + ) + + } + } + + +}
\ No newline at end of file diff --git a/src/main/kotlin/com/examplemod/config/PersistentData.kt b/src/main/kotlin/com/examplemod/config/PersistentData.kt new file mode 100644 index 0000000..58e0ac0 --- /dev/null +++ b/src/main/kotlin/com/examplemod/config/PersistentData.kt @@ -0,0 +1,35 @@ +package com.examplemod.config + +import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient +import kotlinx.serialization.json.Json +import java.io.File +import kotlinx.serialization.encodeToString +import kotlinx.serialization.decodeFromString + +@Serializable +data class PersistentData( + var exampleData: Map<String, String> = mapOf("key1" to "entry1"), + var moreExampleData: Int = 5 +) { + + fun save() { + configFile.writeText(Json.encodeToString(this)) + } + + companion object { + private val configFile: File = File(ExampleMod.configDirectory,"data.json") + + fun load(): PersistentData { + val data = if (!configFile.exists()) { + configFile.createNewFile() + PersistentData() + } else configFile.runCatching { + Json.decodeFromString<PersistentData>(this.readText()) + }.getOrNull() ?: PersistentData() + return data.apply { + this.save() + } + } + } +}
\ No newline at end of file |