Skip to content

Commit

Permalink
feat: gson adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidJoker committed Feb 3, 2025
1 parent 97397ae commit bd53ce8
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.modlabs.kpaper.utils
package cc.modlabs.kpaper.file

import java.io.File

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.modlabs.kpaper.utils.config
package cc.modlabs.kpaper.file.config

import org.bukkit.configuration.InvalidConfigurationException
import org.bukkit.configuration.file.YamlConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.modlabs.kpaper.utils.config
package cc.modlabs.kpaper.file.config

import cc.modlabs.kpaper.main.PluginInstance

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.modlabs.kpaper.utils.config
package cc.modlabs.kpaper.file.config

import org.bukkit.Bukkit

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cc.modlabs.kpaper.file.gson

import com.google.gson.*
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.inventory.ItemFactory
import org.bukkit.inventory.ItemStack
import org.bukkit.configuration.serialization.ConfigurationSerializable
import java.lang.reflect.Type

class ItemStackTypeAdapter : JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
override fun serialize(src: ItemStack, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
val jsonObject = JsonObject()
jsonObject.addProperty("item", src.type.key.toString())
jsonObject.addProperty("count", src.amount)

if (src.hasItemMeta()) {
val metaMap = src.itemMeta?.serialize() ?: emptyMap()
val metaJson = context.serialize(metaMap)
jsonObject.add("meta", metaJson)
}

return jsonObject
}

@Throws(JsonParseException::class)
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): ItemStack {
val jsonObject = json.asJsonObject
val item = Material.matchMaterial(jsonObject["item"].asString) ?: throw JsonParseException("Invalid material")
val count = jsonObject["count"].asInt

val stack = ItemStack(item, count)

if (jsonObject.has("meta")) {
val metaJson = jsonObject["meta"]
val metaMap = context.deserialize<Map<String, Any>>(metaJson, Map::class.java)

val itemFactory: ItemFactory = Bukkit.getItemFactory()
val meta = itemFactory.getItemMeta(item)
if (meta is ConfigurationSerializable) {
metaMap.forEach { (key, value) -> meta.javaClass.getMethod("set$key", value::class.java)?.invoke(meta, value) }
}
stack.itemMeta = meta
}

return stack
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cc.modlabs.kpaper.file.gson

import com.google.gson.*
import java.lang.reflect.Type
import java.time.LocalDate

class LocalDateAdapter : JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> {

override fun serialize(
src: LocalDate?,
typeOfSrc: Type?,
context: JsonSerializationContext?
): JsonElement {
// If you don't want null to appear at all, you can handle it differently
return if (src == null) JsonNull.INSTANCE else JsonPrimitive(src.toString())
}

override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): LocalDate? {
if (json == null || json.isJsonNull) return null
return LocalDate.parse(json.asString)
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/cc/modlabs/kpaper/file/gson/MaterialTypeAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cc.modlabs.kpaper.file.gson

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import org.bukkit.Material

class MaterialTypeAdapter : TypeAdapter<Material>() {
override fun write(out: JsonWriter?, value: Material?) {
if (value == null) {
out?.nullValue()
return
}
out?.value(value.name.lowercase())
}

override fun read(`in`: JsonReader?): Material {
if (`in`?.peek() == JsonToken.NULL) return Material.STONE

return Material.valueOf(`in`!!.nextString().uppercase())
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cc.modlabs.kpaper.translation

import cc.modlabs.kpaper.extensions.getInternalLogger
import cc.modlabs.kpaper.extensions.getLogger
import cc.modlabs.kpaper.translation.interfaces.TranslationSource
import cc.modlabs.kpaper.utils.TempStorage
import cc.modlabs.kpaper.file.TempStorage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down

0 comments on commit bd53ce8

Please sign in to comment.