From 05a4a5b3817ea3e70662d3834fd56a1b249fa70e Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 4 Aug 2022 04:26:51 +0200 Subject: screen padding --- .../moe/nea/notenoughupdates/util/ConfigHolder.kt | 34 ++++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src/main/kotlin/moe/nea/notenoughupdates/util') diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt index 003de6c..07a9b4c 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.SerializationException import moe.nea.notenoughupdates.NotEnoughUpdates import moe.nea.notenoughupdates.events.NEUScreenEvents +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents import net.minecraft.client.Minecraft import net.minecraft.commands.CommandSource import net.minecraft.network.chat.Component @@ -15,9 +16,11 @@ import kotlin.io.path.readText import kotlin.io.path.writeText import kotlin.reflect.KClass -abstract class ConfigHolder(val serializer: KSerializer, - val name: String, - val default: () -> T) { +abstract class ConfigHolder( + val serializer: KSerializer, + val name: String, + val default: () -> T +) { var config: T private set @@ -33,15 +36,21 @@ abstract class ConfigHolder(val serializer: KSerializer, if (file.exists()) try { return NotEnoughUpdates.json.decodeFromString( - serializer, - file.readText() + serializer, + file.readText() ) } catch (e: IOException) { badLoads.add(name) - NotEnoughUpdates.logger.error("IO exception during loading of config file $name. This will reset this config.", e) + NotEnoughUpdates.logger.error( + "IO exception during loading of config file $name. This will reset this config.", + e + ) } catch (e: SerializationException) { badLoads.add(name) - NotEnoughUpdates.logger.error("Serialization exception during loading of config file $name. This will reset this config.", e) + NotEnoughUpdates.logger.error( + "Serialization exception during loading of config file $name. This will reset this config.", + e + ) } return default() } @@ -95,8 +104,12 @@ abstract class ConfigHolder(val serializer: KSerializer, private fun warnForResetConfigs(player: CommandSource) { if (badLoads.isNotEmpty()) { - player.sendSystemMessage(Component.literal("The following configs have been reset: ${badLoads.joinToString(", ")}. " + - "This can be intentional, but probably isn't.")) + player.sendSystemMessage( + Component.literal( + "The following configs have been reset: ${badLoads.joinToString(", ")}. " + + "This can be intentional, but probably isn't." + ) + ) badLoads.clear() } } @@ -110,6 +123,9 @@ abstract class ConfigHolder(val serializer: KSerializer, } false }) + ClientLifecycleEvents.CLIENT_STOPPING.register(ClientLifecycleEvents.ClientStopping { + performSaves() + }) } -- cgit