Skip to content

Commit

Permalink
fix: null check components for ItemStack.container()
Browse files Browse the repository at this point in the history
  • Loading branch information
gabizou committed May 20, 2024
1 parent e5aeac0 commit b1ec627
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,18 @@ public ItemStack empty() {

private static <T> void fillComponents(DataContainer container, TypedDataComponent<T> component) {
final ResourceLocation componentTypeKey = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(component.type());
if (componentTypeKey == null) {
return;
}
final DataResult<Object> value = component.type().codec().encodeStart(JavaOps.INSTANCE, component.value());
container.set(DataQuery.of(':', componentTypeKey.toString()), value.result().orElse(null));
final var raw = value.result().orElse(null);
if (raw == null) {
return;
}
container.set(DataQuery.of(':', componentTypeKey.toString()), raw);

}

@NotNull
public static DataContainer getDataContainer(final net.minecraft.world.item.ItemStack mcStack) {
final ResourceLocation key = BuiltInRegistries.ITEM.getKey(mcStack.getItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@
public abstract class ItemStackMixin_API implements SerializableDataHolder.Mutable, ComponentLike, HoverEventSource<HoverEvent.ShowItem> { // conflict from overriding ValueContainer#copy() from DataHolder

// @formatter:off
@Shadow @Final private static Logger LOGGER;

@Shadow public abstract int shadow$getCount();
@Shadow public abstract void shadow$setCount(int size); // Do not use field directly as Minecraft tracks the empty state
@Shadow public abstract int shadow$getMaxStackSize();
Expand All @@ -92,10 +90,6 @@ public abstract class ItemStackMixin_API implements SerializableDataHolder.Mutab

// @formatter:on

@Shadow public abstract void enchant(final Enchantment $$0, final int $$1);

@Shadow public abstract Rarity getRarity();

public int itemStack$quantity() {
return this.shadow$getCount();
}
Expand Down

0 comments on commit b1ec627

Please sign in to comment.