Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -58,6 +62,9 @@ public DefaultedList<ItemStack> getMutableStacks() {

@Override
public ItemStack getStack(int slot) {
if (view instanceof CopyingFixedItemInv) {
return view.getInvStack(slot);
}
return view.getInvStack(slot).copy();
}

Expand All @@ -73,23 +80,22 @@ 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
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
Expand All @@ -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<Item> 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
Expand Down