Skip to content

Commit

Permalink
Tweak impl. of copy_to_storage item action type
Browse files Browse the repository at this point in the history
Now uses the codec for item stacks for serializing the item stack into NBT
  • Loading branch information
eggohito committed Feb 13, 2024
1 parent ef397fd commit 5a935ae
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,18 +33,19 @@ public static void action(SerializableData.Instance data, Pair<World, ItemStack>
List<CopyNbtLootFunction.Operation> 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);

}

Expand Down

0 comments on commit 5a935ae

Please sign in to comment.