Minigame map/arena library to hold & store data.
minecraft minecraft-lib minecraft-library
3

Configure Feed

Select the types of activity you want to include in your feed.

Add JsonWorldDataStore

* Added a json serializer/deserializer using Kotlinx's JSON

Lukiiy (Jul 12, 2026, 9:59 PM -0300) d2970ba3 088eb9a7

+76
+76
src/main/kotlin/me/lukiiy/mapling/provided/JsonWorldDataStore.kt
··· 1 + package me.lukiiy.mapling.provided 2 + 3 + import kotlinx.serialization.json.Json 4 + import kotlinx.serialization.json.JsonArray 5 + import kotlinx.serialization.json.JsonElement 6 + import kotlinx.serialization.json.JsonObject 7 + import kotlinx.serialization.json.JsonPrimitive 8 + import kotlinx.serialization.json.booleanOrNull 9 + import kotlinx.serialization.json.doubleOrNull 10 + import kotlinx.serialization.json.jsonObject 11 + import kotlinx.serialization.json.longOrNull 12 + import me.lukiiy.mapling.Position 13 + import me.lukiiy.mapling.WorldData 14 + import me.lukiiy.mapling.WorldDataStore 15 + import java.io.File 16 + import kotlin.collections.component1 17 + import kotlin.collections.component2 18 + import kotlin.collections.iterator 19 + import kotlin.collections.map 20 + 21 + class JsonWorldDataStore : WorldDataStore { 22 + override fun load(file: File): WorldData { 23 + if (!file.exists()) return WorldData() 24 + 25 + return deserialize(Json.parseToJsonElement(file.readText()).jsonObject) 26 + } 27 + 28 + override fun save(file: File, data: WorldData) { 29 + if (data.isEmpty()) return 30 + 31 + file.parentFile?.mkdirs() 32 + file.writeText(Json.encodeToString(JsonObject.serializer(), serialize(data))) 33 + } 34 + 35 + private fun serialize(data: WorldData): JsonObject { 36 + fun encode(value: Any): JsonElement = when (value) { 37 + is Position -> JsonPrimitive(value.serialize()) 38 + is Boolean -> JsonPrimitive(value) 39 + is Long -> JsonPrimitive(value) 40 + is Int -> JsonPrimitive(value) 41 + is Double -> JsonPrimitive(value) 42 + is String -> JsonPrimitive(value) 43 + is List<*> -> JsonArray(value.map { encode(requireNotNull(it)) }) 44 + else -> error("Unsupported value.") 45 + } 46 + 47 + return JsonObject(buildMap { 48 + for ((key, value) in data.values()) put(key, encode(value)) 49 + for ((key, section) in data.sections()) put(key, serialize(section)) 50 + }) 51 + } 52 + 53 + private fun deserialize(obj: JsonObject): WorldData { 54 + fun decode(element: JsonElement): Any = when (element) { 55 + is JsonPrimitive -> when { 56 + element.content.startsWith("pos:") -> Position.deserialize(element.content) 57 + !element.isString -> element.booleanOrNull ?: element.longOrNull ?: element.doubleOrNull ?: element.content 58 + else -> element.content 59 + } 60 + 61 + is JsonArray -> element.map(::decode) 62 + else -> error("Unsupported value.") 63 + } 64 + 65 + fun read(obj: JsonObject, target: WorldData) { 66 + for ((key, element) in obj) { 67 + when (element) { 68 + is JsonObject -> read(element, target.section(key)) 69 + else -> target.set(key, decode(element)) 70 + } 71 + } 72 + } 73 + 74 + return WorldData().also { read(obj, it) } 75 + } 76 + }