diff --git a/gradle.properties b/gradle.properties index 2eb808f..90aab70 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ archives_base_name = universalcomponents # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api fabric_version=0.11.7+build.356-1.16 cardinal_version=2.4.0-nightly.1.16-pre2 -lba_version=0.6.6 +lba_version=0.7.0 fluidity_version=0.11 item_inventory_version=1.1.0 tr_energy_version=0.1.0 diff --git a/src/main/java/io/github/cottonmc/component/compat/lba/AttributeWrapper.java b/src/main/java/io/github/cottonmc/component/compat/lba/AttributeWrapper.java index 744e538..1dbc7ec 100644 --- a/src/main/java/io/github/cottonmc/component/compat/lba/AttributeWrapper.java +++ b/src/main/java/io/github/cottonmc/component/compat/lba/AttributeWrapper.java @@ -14,6 +14,9 @@ public class AttributeWrapper implements FixedItemInv, ItemTransferable { private InventoryComponent component; + // TODO: (LBA 0.7.1) Remove this field as the default impl handles no-op listeners + private int changes; + public AttributeWrapper(InventoryComponent component) { this.component = component; } @@ -43,26 +46,64 @@ public boolean isItemValidForSlot(int slot, ItemStack stack) { return component.isAcceptableStack(slot, stack); } + @Override + public int getMaxAmount(int slot, ItemStack stack) { + return component.getMaxStackSize(slot); + } + @Override public boolean setInvStack(int slot, ItemStack to, Simulation simulation) { + // InventoryComponent doesn't return whether it was successful or not + // so this just checks *everything* + ItemStack current = getInvStack(slot); + if (ItemStackUtil.areEqualIgnoreAmounts(to, current)) { + + if (to.getCount() > current.getCount()) { + if (!component.canInsert(slot)) { + return false; + } + } else if (to.getCount() < current.getCount()) { + if (!component.canExtract(slot)) { + return false; + } + } else { + return true; + } + + } else { + if (!current.isEmpty() && !component.canExtract(slot)) { + return false; + } + if (!to.isEmpty() && !component.canInsert(slot)) { + return false; + } + } + + if (!component.isAcceptableStack(slot, to)) { + return false; + } if (simulation.isAction()) component.setStack(slot, to); return true; } @Override public int getChangeValue() { - return 0; //TODO: impl? + // TODO: (LBA 0.7.1) Remove this method as the default impl handles no-op listeners + return changes++; } @Nullable @Override public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) { - return null; //TODO: impl? + // Not possible: InventoryComponent has no way to call the removal token + // TODO: (LBA 0.7.1) Remove this method as the default impl handles no-op listeners + return null; } @Override public ItemStack attemptExtraction(ItemFilter filter, int maxAmount, Simulation simulation) { - return component.takeStack(0, maxAmount, actionForSim(simulation)); //TODO: fix + // This is the simplest way to implement this. + return getGroupedInv().attemptExtraction(filter, maxAmount, simulation); } @Override diff --git a/src/main/java/io/github/cottonmc/component/compat/lba/LBAInvHook.java b/src/main/java/io/github/cottonmc/component/compat/lba/LBAInvHook.java index 2ea7133..a9d336e 100644 --- a/src/main/java/io/github/cottonmc/component/compat/lba/LBAInvHook.java +++ b/src/main/java/io/github/cottonmc/component/compat/lba/LBAInvHook.java @@ -41,10 +41,12 @@ public InventoryComponent getInvComponent(BlockView world, BlockPos pos, @Nullab return null; } + @Override public boolean hasInvComponent(ItemStack stack) { return ItemAttributes.FIXED_INV.get(stack) != EmptyFixedItemInv.INSTANCE; } + @Override @Nullable public InventoryComponent getInvComponent(ItemStack stack) { FixedItemInv inv = ItemAttributes.FIXED_INV.get(stack); diff --git a/src/main/java/io/github/cottonmc/component/compat/lba/WrappedInvAttributeComponent.java b/src/main/java/io/github/cottonmc/component/compat/lba/WrappedInvAttributeComponent.java index a739e77..fad63a0 100644 --- a/src/main/java/io/github/cottonmc/component/compat/lba/WrappedInvAttributeComponent.java +++ b/src/main/java/io/github/cottonmc/component/compat/lba/WrappedInvAttributeComponent.java @@ -2,10 +2,14 @@ import alexiil.mc.lib.attributes.Simulation; import alexiil.mc.lib.attributes.item.FixedItemInv; +import alexiil.mc.lib.attributes.item.FixedItemInv.CopyingFixedItemInv; import alexiil.mc.lib.attributes.item.FixedItemInvView; import alexiil.mc.lib.attributes.item.ItemExtractable; import alexiil.mc.lib.attributes.item.ItemInsertable; +import alexiil.mc.lib.attributes.item.ItemInvUtil; import alexiil.mc.lib.attributes.item.compat.InventoryFixedWrapper; +import alexiil.mc.lib.attributes.item.filter.ConstantItemFilter; +import alexiil.mc.lib.attributes.item.filter.ExactItemFilter; import alexiil.mc.lib.attributes.item.filter.ItemFilter; import io.github.cottonmc.component.api.ActionType; import io.github.cottonmc.component.item.InventoryComponent; @@ -25,7 +29,7 @@ public class WrappedInvAttributeComponent implements InventoryComponent { //TODO: other wrappers for extractable-only, insertable-only, etc? public WrappedInvAttributeComponent(FixedItemInv inv) { this.inv = inv; - this.view = inv.getFixedView(); + this.view = inv; this.extractable = inv.getExtractable(); this.insertable = inv.getInsertable(); } @@ -58,6 +62,9 @@ public DefaultedList getMutableStacks() { @Override public ItemStack getStack(int slot) { + if (view instanceof CopyingFixedItemInv) { + return view.getInvStack(slot); + } return view.getInvStack(slot).copy(); } @@ -73,12 +80,12 @@ public boolean canExtract(int slot) { @Override public ItemStack takeStack(int slot, int amount, ActionType action) { - return extractable.attemptExtraction(createFilterForSlot(slot), amount, simForAction(action)); + return inv.getSlot(slot).attemptAnyExtraction(amount, simForAction(action)); } @Override public ItemStack removeStack(int slot, ActionType action) { - return extractable.attemptExtraction(createFilterForSlot(slot), getStack(slot).getCount(), simForAction(action)); + return takeStack(slot, Integer.MAX_VALUE, action); } @Override @@ -86,10 +93,9 @@ public void setStack(int slot, ItemStack stack) { inv.setInvStack(slot, stack, Simulation.ACTION); } - //TODO: any way to force slot? @Override public ItemStack insertStack(int slot, ItemStack stack, ActionType action) { - return insertable.attemptInsertion(stack, simForAction(action)); + return ItemInvUtil.insertSingle(inv, slot, stack, simForAction(action)); } @Override @@ -114,23 +120,22 @@ public boolean isAcceptableStack(int slot, ItemStack stack) { @Override public int amountOf(Item item) { - return amountOf(Collections.singleton(item)); + return view.getGroupedInv().getAmount(new ExactItemFilter(item)); } @Override public int amountOf(Set items) { - int ret = 0; - for (ItemStack stack : view.stackIterable()) { - if (items.contains(stack.getItem())) { - ret += stack.getCount(); - } - } - return ret; + return view.getGroupedInv().getAmount(ExactItemFilter.anyOf(items)); } @Override public boolean contains(Item item) { - return contains(Collections.singleton(item)); + for (ItemStack stack : view.stackIterable()) { + if (stack.getItem() == item) { + return true; + } + } + return false; } @Override