From 7cc0f17fc4df18393bc515c9dd8987b8bb6fef7f Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Thu, 29 Apr 2021 20:46:40 +0200 Subject: [PATCH] Use the default configuration as a fallback in Configuration.kt --- .../snowygui/config/Configuration.kt | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index 628b70f..3427b9a 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -2,7 +2,7 @@ package fr.username404.snowygui.config import com.typesafe.config.Config import com.typesafe.config.ConfigException -import com.typesafe.config.ConfigFactory +import com.typesafe.config.ConfigFactory.* import com.typesafe.config.ConfigRenderOptions import fr.username404.snowygui.Snowy import io.github.config4k.extract @@ -14,12 +14,6 @@ import java.io.File object Configuration { private val file: File = File(Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "snowy.conf") - private val base = """ - |Snowy { - | displayInitMessage = true - |} - """.trimMargin() - private val baseConf = ConfigFactory.parseString(base) private suspend fun writeConfig(c: Config) = coroutineScope { launch { file.writeText( @@ -40,19 +34,27 @@ object Configuration { } @JvmField val obtained: Config = run { - var result: Config = baseConf + var result: Config = empty() with(file) { if (!exists()) { createNewFile() setWritable(true) setReadable(true) } else try { - result = ConfigFactory.parseFile(file) + result = parseFile(file) } catch (e: ConfigException) { Snowy.logs.warn("Could not parse the snowy configuration file, the default configuration will be used instead.") } } - ConfigFactory.load(result).extract("Snowy").resolveWith(baseConf) + load(result).withFallback( + parseString( + """ + |Snowy { + | displayInitMessage = true + |} + """.trimMargin() + ) + ).extract("Snowy") }.also { runBlocking { writeConfig(it)