diff --git a/src/main/kotlin/com/peasenet/config/FullbrightConfig.kt b/src/main/kotlin/com/peasenet/config/FullbrightConfig.kt index c8808e3..fc30b98 100644 --- a/src/main/kotlin/com/peasenet/config/FullbrightConfig.kt +++ b/src/main/kotlin/com/peasenet/config/FullbrightConfig.kt @@ -51,12 +51,7 @@ open class FullbrightConfig : Config() { key = "fullbright" } - val maxGamma: Float - /** - * Gets the maximum gamma value allowed. - * If XRAY is enabled, this will be 16. - * If it is not, then it will be between 1 and 4 inclusive. - * @return max gamma value. - */ - get() = if (Mods.isActive("xray")) 16F else 1 + 15 * gamma + fun maxGamma(): Float { + return if (Mods.isActive("xray")) 16F else 1 + 15 * gamma + } } \ No newline at end of file diff --git a/src/main/kotlin/com/peasenet/config/XrayConfig.kt b/src/main/kotlin/com/peasenet/config/XrayConfig.kt index ab66772..7e4e89e 100644 --- a/src/main/kotlin/com/peasenet/config/XrayConfig.kt +++ b/src/main/kotlin/com/peasenet/config/XrayConfig.kt @@ -23,10 +23,11 @@ */ package com.peasenet.config -import net.minecraft.block.Block +import com.google.gson.TypeAdapter +import com.google.gson.stream.JsonReader +import com.google.gson.stream.JsonToken +import com.google.gson.stream.JsonWriter import net.minecraft.block.ExperienceDroppingBlock -import net.minecraft.registry.Registries -import java.util.function.Consumer /** * The configuration for xray. @@ -44,8 +45,53 @@ class XrayConfig : BlockListConfig({ it is ExperienceDroppingBlock } field = value saveConfig() } - + init { key = "xray" } +} + + +class XrayConfigGsonAdapter : TypeAdapter() { + override fun write(out: JsonWriter?, value: XrayConfig?) { + // Write the block list + out?.beginObject() + out?.name("blocks") + out?.beginArray() + value?.blocks?.forEach { + out?.value(it) + } + out?.endArray() + out?.name("blockCulling") + out?.value(value?.blockCulling) + out?.endObject() + } + + override fun read(reader: JsonReader?): XrayConfig { + val config = XrayConfig() + reader?.beginObject() + while (reader?.hasNext() == true) { + val token = reader.peek() + var fieldName = "" + if (token.equals(JsonToken.NAME)) + fieldName = reader.nextName() + + when (fieldName) { + "blocks" -> { + reader.beginArray() + while (reader.hasNext()) { + config.blocks.add(reader.nextString()) + } + reader.endArray() + } + + "blockCulling" -> { + config.blockCulling = reader.nextBoolean() + } + } + } + reader?.endObject() + return config + } + } \ No newline at end of file diff --git a/src/main/kotlin/com/peasenet/main/Settings.kt b/src/main/kotlin/com/peasenet/main/Settings.kt index 2657171..93bd482 100644 --- a/src/main/kotlin/com/peasenet/main/Settings.kt +++ b/src/main/kotlin/com/peasenet/main/Settings.kt @@ -5,6 +5,8 @@ import com.google.gson.ToNumberPolicy import com.google.gson.stream.JsonReader import com.peasenet.annotations.GsonExclusionStrategy import com.peasenet.config.Config +import com.peasenet.config.XrayConfig +import com.peasenet.config.XrayConfigGsonAdapter import java.io.* import java.nio.file.Files import java.nio.file.Path @@ -38,12 +40,14 @@ object Settings { if (!file.exists()) { save() } - val json = GsonBuilder().setPrettyPrinting().create() + val json = GsonBuilder().setPrettyPrinting() + .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) + .registerTypeAdapter(XrayConfig::class.java, XrayConfigGsonAdapter()) + .create() try { val reader = JsonReader(InputStreamReader(FileInputStream(file))) val data = json.fromJson>>( - reader, - HashMap::class.java + reader, HashMap::class.java ) if (data == null) loadDefault() reader.close() @@ -66,10 +70,12 @@ object Settings { * @param key - The key of the configuration. * @return - The configuration. */ - private fun fetchConfig(clazz: Class>, key: String?): Config<*> { + private fun fetchConfig(clazz: Config<*>, key: String?): Config<*> { // open the settings file val cfgFile = filePath - val json = GsonBuilder().create() + val json = GsonBuilder().registerTypeAdapter(XrayConfig::class.java, XrayConfigGsonAdapter()) + .setExclusionStrategies(GsonExclusionStrategy()).setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) + .create() val map: Any? try { map = json.fromJson(FileReader(cfgFile), HashMap::class.java)[key] @@ -81,7 +87,8 @@ object Settings { } try { val jsonObject = json.toJsonTree(map).asJsonObject - return json.fromJson(jsonObject, clazz) + // parse the json object to the configuration class + return json.fromJson(jsonObject, clazz::class.java) } catch (e: IllegalStateException) { settings[key] = defaultSettings[key]!! save() @@ -97,7 +104,9 @@ object Settings { val cfgFile = filePath // ensure the settings file exists ensureCfgCreated(cfgFile) - val json = GsonBuilder().setPrettyPrinting().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) + val json = GsonBuilder().setPrettyPrinting().setObjectToNumberStrategy( + ToNumberPolicy.LONG_OR_DOUBLE + ).registerTypeAdapter(XrayConfig::class.java, XrayConfigGsonAdapter()) .setExclusionStrategies(GsonExclusionStrategy()).create() try { val writer = Files.newBufferedWriter(Paths.get(cfgFile)) @@ -121,8 +130,7 @@ object Settings { */ fun addConfig(config: Config<*>) { defaultSettings[config.key] = config - settings[config.key] = - fetchConfig(config.javaClass, config.key) + settings[config.key] = fetchConfig(config, config.key) } /** diff --git a/src/main/kotlin/com/peasenet/mods/render/ModFullBright.kt b/src/main/kotlin/com/peasenet/mods/render/ModFullBright.kt index bd71fa4..cfda449 100644 --- a/src/main/kotlin/com/peasenet/mods/render/ModFullBright.kt +++ b/src/main/kotlin/com/peasenet/mods/render/ModFullBright.kt @@ -107,7 +107,7 @@ class ModFullBright : RenderMod( if (fullbrightConfig.gammaFade) { fadeGammaUp() } else { - gamma = fullbrightConfig.maxGamma.toDouble() + gamma = fullbrightConfig.maxGamma().toDouble() } } @@ -141,7 +141,7 @@ class ModFullBright : RenderMod( */ set(gamma) { val newValue = gamma - val maxGamma = fullbrightConfig.maxGamma + val maxGamma = fullbrightConfig.maxGamma() newValue.coerceAtLeast(0.0) .coerceAtMost(maxGamma.toDouble()) val newGamma = GavinsModClient.minecraftClient.options.gamma