Skip to content

Commit

Permalink
Refactor(serialization): Update itemstack serializer to use NBT API
Browse files Browse the repository at this point in the history
  • Loading branch information
Uni0305 committed Dec 3, 2024
1 parent aab14a5 commit 874c388
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
package me.uni0305.mokoko.library.serialization.json;

import com.google.gson.JsonArray;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import de.tr7zw.nbtapi.NBT;
import de.tr7zw.nbtapi.iface.ReadWriteNBT;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JsonItemStackArraySerializer implements JsonSerializer<ItemStack[]> {
private final JsonItemStackSerializer serializer = new JsonItemStackSerializer();
private final Gson gson = new Gson();

@Override
public @NotNull JsonElement serialize(ItemStack @NotNull [] items) {
JsonArray array = new JsonArray();
for (ItemStack item : items) {
JsonElement element = serializer.serialize(item);
array.add(element);
public @NotNull JsonElement serialize(@Nullable ItemStack @NotNull [] obj) throws RuntimeException {
try {
ReadWriteNBT nbt = NBT.itemStackArrayToNBT(obj);
String json = nbt.toString();
return gson.toJsonTree(json);
} catch (Exception e) {
throw new RuntimeException(e);
}
return array;
}

@Override
public ItemStack @NotNull [] deserialize(@NotNull JsonElement json) {
JsonArray array = json.getAsJsonArray();
int length = array.size();
ItemStack[] items = new ItemStack[length];
for (int i = 0; i < length; i++) {
JsonElement element = array.get(i);
items[i] = serializer.deserialize(element);
public @Nullable ItemStack @Nullable [] deserialize(@NotNull JsonElement src) throws RuntimeException {
try {
String json = src.toString();
ReadWriteNBT nbt = NBT.parseNBT(json);
return NBT.itemStackArrayFromNBT(nbt);
} catch (Exception e) {
throw new RuntimeException(e);
}
return items;
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package me.uni0305.mokoko.library.serialization.json;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack;
import de.tr7zw.nbtapi.NBT;
import de.tr7zw.nbtapi.iface.ReadWriteNBT;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JsonItemStackSerializer implements JsonSerializer<ItemStack> {
private final Gson gson = new Gson();

@Override
public @NotNull JsonElement serialize(@NotNull ItemStack obj) throws RuntimeException {
public @NotNull JsonElement serialize(@NotNull ItemStack obj) {
try {
net.minecraft.world.item.ItemStack nms = CraftItemStack.asNMSCopy(obj);
CompoundTag nbt = nms.save(new CompoundTag());
Dynamic<Tag> tag = new Dynamic<>(NbtOps.INSTANCE, nbt);
Dynamic<JsonElement> json = tag.convert(JsonOps.INSTANCE);
return json.getValue();
ReadWriteNBT nbt = NBT.itemStackToNBT(obj);
String json = nbt.toString();
return gson.toJsonTree(json);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public @NotNull ItemStack deserialize(@NotNull JsonElement src) throws RuntimeException {
public @Nullable ItemStack deserialize(@NotNull JsonElement src) throws RuntimeException {
try {
Dynamic<JsonElement> json = new Dynamic<>(JsonOps.INSTANCE, src);
Dynamic<Tag> tag = json.convert(NbtOps.INSTANCE);
CompoundTag nbt = (CompoundTag) tag.getValue();
net.minecraft.world.item.ItemStack nms = net.minecraft.world.item.ItemStack.of(nbt);
return CraftItemStack.asBukkitCopy(nms);
String json = src.toString();
ReadWriteNBT nbt = NBT.parseNBT(json);
return NBT.itemStackFromNBT(nbt);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 874c388

Please sign in to comment.