Skip to content

Commit

Permalink
Add custom type serializer for XrayConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
GT3CH1 committed Nov 13, 2024
1 parent 1bd0d93 commit c6c32d2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
11 changes: 3 additions & 8 deletions src/main/kotlin/com/peasenet/config/FullbrightConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ open class FullbrightConfig : Config<FullbrightConfig>() {
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
}
}
54 changes: 50 additions & 4 deletions src/main/kotlin/com/peasenet/config/XrayConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -44,8 +45,53 @@ class XrayConfig : BlockListConfig<XrayConfig>({ it is ExperienceDroppingBlock }
field = value
saveConfig()
}

init {
key = "xray"
}
}


class XrayConfigGsonAdapter : TypeAdapter<XrayConfig>() {
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
}

}
26 changes: 17 additions & 9 deletions src/main/kotlin/com/peasenet/main/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<HashMap<String, Config<*>>>(
reader,
HashMap::class.java
reader, HashMap::class.java
)
if (data == null) loadDefault()
reader.close()
Expand All @@ -66,10 +70,12 @@ object Settings {
* @param key - The key of the configuration.
* @return - The configuration.
*/
private fun fetchConfig(clazz: Class<out Config<*>>, 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]
Expand All @@ -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()
Expand All @@ -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))
Expand All @@ -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)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/peasenet/mods/render/ModFullBright.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ModFullBright : RenderMod(
if (fullbrightConfig.gammaFade) {
fadeGammaUp()
} else {
gamma = fullbrightConfig.maxGamma.toDouble()
gamma = fullbrightConfig.maxGamma().toDouble()
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c6c32d2

Please sign in to comment.