Skip to content

Commit 5ce94e9

Browse files
committed
feat: itemstack & text serializers
1 parent 3a09e94 commit 5ce94e9

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import com.mojang.authlib.GameProfile
3232
import com.mojang.blaze3d.systems.RenderSystem.recordRenderCall
3333
import net.minecraft.block.Block
3434
import net.minecraft.client.MinecraftClient
35+
import net.minecraft.item.ItemStack
36+
import net.minecraft.text.Text
3537
import net.minecraft.util.math.BlockPos
3638
import org.apache.logging.log4j.LogManager
3739
import org.apache.logging.log4j.Logger
@@ -61,6 +63,8 @@ object Lambda {
6163
.registerTypeAdapter(Block::class.java, BlockSerializer)
6264
.registerTypeAdapter(GameProfile::class.java, GameProfileSerializer)
6365
.registerTypeAdapter(Optional::class.java, OptionalSerializer)
66+
.registerTypeAdapter(ItemStack::class.java, ItemStackSerializer)
67+
.registerTypeAdapter(Text::class.java, Text.Serializer())
6468
.create()
6569

6670
fun initialize(block: (Long) -> Unit) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.config.serializer
19+
20+
import com.google.gson.JsonDeserializationContext
21+
import com.google.gson.JsonDeserializer
22+
import com.google.gson.JsonElement
23+
import com.google.gson.JsonObject
24+
import com.google.gson.JsonSerializationContext
25+
import com.google.gson.JsonSerializer
26+
import net.minecraft.item.ItemStack
27+
import net.minecraft.nbt.StringNbtReader
28+
import net.minecraft.nbt.visitor.StringNbtWriter
29+
import net.minecraft.registry.Registries
30+
import net.minecraft.util.Identifier
31+
import java.lang.reflect.Type
32+
33+
object ItemStackSerializer : JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
34+
override fun serialize(
35+
stack: ItemStack,
36+
typeOfSrc: Type,
37+
context: JsonSerializationContext
38+
): JsonElement =
39+
JsonObject().apply {
40+
addProperty("id", stack.item.registryEntry.key.get().value.toString())
41+
addProperty("count", stack.count)
42+
stack.nbt?.let { addProperty("tag", StringNbtWriter().apply(it)) }
43+
}
44+
45+
override fun deserialize(
46+
json: JsonElement,
47+
typeOfT: Type,
48+
context: JsonDeserializationContext
49+
): ItemStack {
50+
val id = json.asJsonObject.get("id").asString
51+
val count = json.asJsonObject.get("count").asInt
52+
val nbt = json.asJsonObject?.get("tag")?.asString
53+
54+
val item = Registries.ITEM.get(Identifier(id))
55+
56+
return ItemStack(item, count).apply { nbt?.let { setNbt(StringNbtReader.parse(it)) } }
57+
}
58+
}

0 commit comments

Comments
 (0)