Actually implement WrapperFilteredItemHandler.
This commit is contained in:
@@ -18,64 +18,64 @@
|
|||||||
|
|
||||||
package appeng.util.inv;
|
package appeng.util.inv;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
|
|
||||||
import alexiil.mc.lib.attributes.ListenerRemovalToken;
|
|
||||||
import alexiil.mc.lib.attributes.ListenerToken;
|
|
||||||
import alexiil.mc.lib.attributes.Simulation;
|
import alexiil.mc.lib.attributes.Simulation;
|
||||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||||
import alexiil.mc.lib.attributes.item.InvMarkDirtyListener;
|
import alexiil.mc.lib.attributes.item.ItemStackUtil;
|
||||||
import alexiil.mc.lib.attributes.item.filter.ItemFilter;
|
import alexiil.mc.lib.attributes.item.filter.ItemFilter;
|
||||||
|
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
|
||||||
import appeng.util.inv.filter.IAEItemFilter;
|
import appeng.util.inv.filter.IAEItemFilter;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
// FIXME: Needs to be double checked, LBA has better ways of doing this
|
// FIXME: Needs to be double checked, LBA has better ways of doing this
|
||||||
public class WrapperFilteredItemHandler implements FixedItemInv {
|
public class WrapperFilteredItemHandler extends DelegatingFixedItemInv {
|
||||||
private final FixedItemInv handler;
|
|
||||||
private final IAEItemFilter filter;
|
private final IAEItemFilter filter;
|
||||||
|
|
||||||
public WrapperFilteredItemHandler(@Nonnull FixedItemInv handler, @Nonnull IAEItemFilter filter) {
|
public WrapperFilteredItemHandler(@Nonnull FixedItemInv handler, @Nonnull IAEItemFilter filter) {
|
||||||
this.handler = handler;
|
super(handler);
|
||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ItemStack getInvStack(int slot) {
|
|
||||||
return this.handler.getInvStack(slot);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
|
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
|
||||||
return this.handler.setInvStack(slot, to, simulation);
|
ItemStack current = this.getInvStack(slot);
|
||||||
}
|
boolean same = ItemStackUtil.areEqualIgnoreAmounts(current, to);
|
||||||
|
boolean isExtracting = !current.isEmpty() && (!same || to.getCount() < current.getCount());
|
||||||
|
boolean isInserting = !to.isEmpty() && (!same || to.getCount() > current.getCount());
|
||||||
|
|
||||||
@Override
|
if (isExtracting) {
|
||||||
public int getSlotCount() {
|
// We may be extracting by "exchanging" the current item for something else,
|
||||||
return this.handler.getSlotCount();
|
// or we might be setting it to air
|
||||||
|
int extractAmount = current.getCount();
|
||||||
|
if (same && !to.isEmpty()) {
|
||||||
|
extractAmount -= to.getCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!filter.allowExtract(delegate, slot, extractAmount)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isInserting) {
|
||||||
|
if (!filter.allowInsert(delegate, slot, to)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.setInvStack(slot, to, simulation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
||||||
return filter.allowInsert(handler, slot, stack);
|
return (stack.isEmpty() || this.getFilterForSlot(slot).matches(stack)) && super.isItemValidForSlot(slot, stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemFilter getFilterForSlot(int slot) {
|
public ItemFilter getFilterForSlot(int slot) {
|
||||||
return stack -> filter.allowInsert(handler, slot, stack);
|
ItemFilter parentFilter = super.getFilterForSlot(slot);
|
||||||
}
|
ItemFilter thisFilter = stack -> filter.allowInsert(delegate, slot, stack);
|
||||||
|
return thisFilter.and(parentFilter);
|
||||||
@Override
|
|
||||||
public int getChangeValue() {
|
|
||||||
return this.handler.getChangeValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) {
|
|
||||||
return this.handler.addListener(listener, removalToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user