Skip to content

Commit

Permalink
Fix inventory item matching with components in fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimordialMoros committed Oct 3, 2024
1 parent 027fbec commit 0b08794
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ public int selectedSlot() {

@Override
public boolean has(Item type, int amount) {
return handle.contains(new ItemStack(PlatformAdapter.toFabricItemType(type), amount));
if (amount <= 0) {
return false;
}
ItemStack toMatch = new ItemStack(PlatformAdapter.toFabricItemType(type), amount);
return hasItemInSubContainer(handle.items, toMatch)
|| hasItemInSubContainer(handle.armor, toMatch)
|| hasItemInSubContainer(handle.offhand, toMatch);
}

private boolean hasItemInSubContainer(Iterable<ItemStack> container, ItemStack neededItem) {
for (ItemStack item : container) {
if (!item.isEmpty() && ItemStack.isSameItem(item, neededItem)) {
int count = item.getCount();
if (count >= neededItem.getCount()) {
return true;
} else {
neededItem.shrink(count);
}
}
}
return false;
}

@Override
Expand Down

0 comments on commit 0b08794

Please sign in to comment.