From 5a935ae17007feea4713360d2a0dceefd39a68a6 Mon Sep 17 00:00:00 2001 From: eggohito <66972816+eggohito@users.noreply.github.com> Date: Tue, 13 Feb 2024 16:29:06 +0800 Subject: [PATCH] Tweak impl. of `copy_to_storage` item action type Now uses the codec for item stacks for serializing the item stack into NBT --- .../action/item/CopyToStorageAction.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/eggohito/eggolib/action/item/CopyToStorageAction.java b/src/main/java/io/github/eggohito/eggolib/action/item/CopyToStorageAction.java index cabce06..1ea806f 100644 --- a/src/main/java/io/github/eggohito/eggolib/action/item/CopyToStorageAction.java +++ b/src/main/java/io/github/eggohito/eggolib/action/item/CopyToStorageAction.java @@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.loot.function.CopyNbtLootFunction; import net.minecraft.nbt.NbtCompound; -import net.minecraft.registry.Registries; +import net.minecraft.nbt.NbtOps; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; import net.minecraft.util.Pair; @@ -33,18 +33,19 @@ public static void action(SerializableData.Instance data, Pair List operations = data.get("ops"); DataCommandStorage commandStorage = server.getDataCommandStorage(); - NbtCompound stackNbt = new NbtCompound(); - NbtCompound itemNbt = new NbtCompound(); - - stackNbt.putString("id", Registries.ITEM.getId(stack.getItem()).toString()); - stackNbt.putByte("Count", (byte) stack.getCount()); - stackNbt.put("tag", stack.getOrCreateNbt()); + NbtCompound stackNbt = ItemStack.CODEC.encodeStart(NbtOps.INSTANCE, stack) + .result() + .filter(nbt -> nbt instanceof NbtCompound) + .map(nbt -> (NbtCompound) nbt) + .orElse(new NbtCompound()); + NbtCompound itemNbt = new NbtCompound(); itemNbt.put("Item", stackNbt); - NbtCompound storageNbt = commandStorage.get(storageId); - operations.forEach(op -> op.execute(() -> storageNbt, itemNbt)); - commandStorage.set(storageId, storageNbt); + NbtCompound commandStorageNbt = commandStorage.get(storageId); + operations.forEach(op -> op.execute(() -> commandStorageNbt, itemNbt)); + + commandStorage.set(storageId, commandStorageNbt); }