From 07455f20917013ad14dcf4c78b2204c919305630 Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 30 Jul 2023 20:39:46 +0800 Subject: [PATCH] feat: dynamic config push --- build.gradle.kts | 3 ++ .../kotlin/quaedam/config/QuaedamConfig.kt | 51 ++++++++++++++----- .../quaedam/config/SimpleQuaedamConfigPush.kt | 2 +- .../kotlin/quaedam/projector/Projector.kt | 2 +- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b8a2100..8318f6e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,7 @@ import net.fabricmc.loom.api.LoomGradleExtensionAPI plugins { java kotlin("jvm") version "1.9.0" + kotlin("plugin.serialization") version "1.9.0" id("architectury-plugin") version "3.4-SNAPSHOT" id("dev.architectury.loom") version "1.3-SNAPSHOT" apply false id("com.github.johnrengelman.shadow") version "8.1.1" apply false @@ -31,6 +32,7 @@ subprojects { allprojects { apply(plugin = "java") apply(plugin = "kotlin") + apply(plugin = "kotlinx-serialization") apply(plugin = "architectury-plugin") apply(plugin = "maven-publish") @@ -47,6 +49,7 @@ allprojects { dependencies { compileOnly("org.jetbrains.kotlin:kotlin-stdlib") + compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") } tasks.withType { diff --git a/common/src/main/kotlin/quaedam/config/QuaedamConfig.kt b/common/src/main/kotlin/quaedam/config/QuaedamConfig.kt index 395d1de..7488ed8 100644 --- a/common/src/main/kotlin/quaedam/config/QuaedamConfig.kt +++ b/common/src/main/kotlin/quaedam/config/QuaedamConfig.kt @@ -1,12 +1,13 @@ package quaedam.config -import com.google.gson.Gson -import com.google.gson.GsonBuilder import dev.architectury.event.events.client.ClientPlayerEvent import dev.architectury.platform.Platform import dev.architectury.utils.GameInstance +import kotlinx.serialization.Serializable +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import net.fabricmc.api.EnvType -import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.* import quaedam.Quaedam import java.nio.file.Path import kotlin.io.path.exists @@ -14,12 +15,26 @@ import kotlin.io.path.notExists import kotlin.io.path.readText import kotlin.io.path.writeText +@Serializable data class QuaedamConfig( - val projectorEffectRadius: Int = 4 + val valuesInt: Map = mapOf(), + val valuesFloat: Map = mapOf(), + val valuesDouble: Map = mapOf(), ) { companion object { - const val TAG_PROJECTOR_EFFECT_RADIUS = "ProjectorEffectRadius" + + private val localJson = Json { + isLenient = true + prettyPrint = true + encodeDefaults = true + ignoreUnknownKeys = true + } + + private val pushJson = Json { + encodeDefaults = true + ignoreUnknownKeys = true + } private val localFile: Path = Platform.getConfigFolder().resolve("quaedam.json") private var local0 = loadLocalConfig() @@ -29,7 +44,7 @@ data class QuaedamConfig( local0 = value writeLocalConfig() } - var remote: QuaedamConfig? = null + private var remote: QuaedamConfig? = null val current get() = remote ?: local0 init { @@ -48,13 +63,13 @@ data class QuaedamConfig( } private fun loadLocalConfig(): QuaedamConfig = if (localFile.exists()) { - Gson().fromJson(localFile.readText(), QuaedamConfig::class.java) + localJson.decodeFromString(localFile.readText()) } else { QuaedamConfig() } private fun writeLocalConfig() { - localFile.writeText(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(local0)) + localFile.writeText(localJson.encodeToString(local0)) } fun applyRemoteConfig(config: QuaedamConfig?) { @@ -62,15 +77,25 @@ data class QuaedamConfig( remote = config } - fun fromPushNbt(tag: CompoundTag) = QuaedamConfig( - projectorEffectRadius = tag.getInt(TAG_PROJECTOR_EFFECT_RADIUS) - ) + const val TAG_VALUES_INT = "ValuesInt" + const val TAG_VALUES_FLOAT = "ValuesFloat" + const val TAG_VALUES_DOUBLE = "ValuesDouble" + + fun fromPushNbt(tag: CompoundTag): QuaedamConfig { + return QuaedamConfig( + valuesInt = pushJson.decodeFromString(tag.getString(TAG_VALUES_INT)), + valuesFloat = pushJson.decodeFromString(tag.getString(TAG_VALUES_FLOAT)), + valuesDouble = pushJson.decodeFromString(tag.getString(TAG_VALUES_DOUBLE)), + ) + } } fun toPushNbt(tag: CompoundTag) { - tag.putInt(TAG_PROJECTOR_EFFECT_RADIUS, projectorEffectRadius) + tag.putString(TAG_VALUES_INT, pushJson.encodeToString(valuesInt)) + tag.putString(TAG_VALUES_FLOAT, pushJson.encodeToString(valuesFloat)) + tag.putString(TAG_VALUES_DOUBLE, pushJson.encodeToString(valuesDouble)) } - fun toPushNbt(forPush: Boolean) = CompoundTag().also { toPushNbt(it) } + fun toPushNbt() = CompoundTag().also { toPushNbt(it) } } diff --git a/common/src/main/kotlin/quaedam/config/SimpleQuaedamConfigPush.kt b/common/src/main/kotlin/quaedam/config/SimpleQuaedamConfigPush.kt index 8982e7c..f30538e 100644 --- a/common/src/main/kotlin/quaedam/config/SimpleQuaedamConfigPush.kt +++ b/common/src/main/kotlin/quaedam/config/SimpleQuaedamConfigPush.kt @@ -27,7 +27,7 @@ object SimpleQuaedamConfigPush { fun sendCurrent(player: ServerPlayer) = send(player, QuaedamConfig.current) - fun send(player: ServerPlayer, config: QuaedamConfig) = send(player, config.toPushNbt(forPush = true)) + fun send(player: ServerPlayer, config: QuaedamConfig) = send(player, config.toPushNbt()) private fun send(player: ServerPlayer, data: CompoundTag) { val buf = FriendlyByteBuf(Unpooled.buffer()) diff --git a/common/src/main/kotlin/quaedam/projector/Projector.kt b/common/src/main/kotlin/quaedam/projector/Projector.kt index 081f93c..012107d 100644 --- a/common/src/main/kotlin/quaedam/projector/Projector.kt +++ b/common/src/main/kotlin/quaedam/projector/Projector.kt @@ -30,7 +30,7 @@ object Projector { BlockEntityType.Builder.of(::ProjectorBlockEntity, block.get()).build(null) }!! - val currentEffectRadius get() = QuaedamConfig.current.projectorEffectRadius + val currentEffectRadius get() = QuaedamConfig.current.valuesInt["projector.effect_radius"] ?: 4 fun findNearbyProjectors(level: Level, pos: BlockPos) = level.getChunksNearby(pos, currentEffectRadius) .flatMap {