feat: dynamic config push

This commit is contained in:
xtex 2023-07-30 20:39:46 +08:00
parent 3ac5f3c9ec
commit 07455f2091
Signed by: xtex
GPG Key ID: B918086ED8045B91
4 changed files with 43 additions and 15 deletions

View File

@ -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<JavaCompile> {

View File

@ -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<String, Int> = mapOf(),
val valuesFloat: Map<String, Float> = mapOf(),
val valuesDouble: Map<String, Double> = 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) }
}

View File

@ -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())

View File

@ -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 {