Skip to content

Commit

Permalink
Fix handling of item variant nbt
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 22, 2024
1 parent 999272a commit 40495c1
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,22 @@ private static ItemStack silentItemStackFromNbt(RegistryWrapper.WrapperLookup lo
if (nbt.isEmpty()) {
return null;
}

var ops = lookup.getOps(NbtOps.INSTANCE);
var result = ItemStack.CODEC.parse(ops, nbt);
if (result.isSuccess()) {
return result.getOrThrow();
}
result = ITEM_VARIANT_FORMATTED_ITEM_STACK_CODEC.parse(ops, nbt);
return null;
}

@Nullable
private static ItemStack silentItemVariantFromNbt(RegistryWrapper.WrapperLookup lookup, NbtCompound nbt) {
if (nbt.isEmpty()) {
return null;
}

var ops = lookup.getOps(NbtOps.INSTANCE);
var result = ITEM_VARIANT_FORMATTED_ITEM_STACK_CODEC.parse(ops, nbt);
if (result.isSuccess()) {
return result.getOrThrow();
}
Expand Down Expand Up @@ -198,13 +207,23 @@ public static NbtCompound transformBlockEntityNbt(PacketContext context, BlockEn
}

if (original.contains("item", NbtElement.COMPOUND_TYPE)) {
var stack = silentItemStackFromNbt(lookup, original.getCompound("item"));
var nbt = original.getCompound("item");
var stack = silentItemStackFromNbt(lookup, nbt);
boolean variant = false;
if (stack == null) {
stack = silentItemVariantFromNbt(lookup, nbt);
variant = stack != null;
}

if (stack != null && PolymerItemUtils.isPolymerServerItem(stack, context)) {
if (override == null) {
override = original.copy();
}
stack = PolymerItemUtils.getPolymerItemStack(stack, context);
override.put("item", stack.toNbtAllowEmpty(lookup));
override.put("item", variant
? ITEM_VARIANT_FORMATTED_ITEM_STACK_CODEC.encodeStart(lookup.getOps(NbtOps.INSTANCE), stack).getOrThrow()
: stack.toNbtAllowEmpty(lookup)
);
}
}

Expand Down

0 comments on commit 40495c1

Please sign in to comment.