use of buildcraft.api.inventory.IItemHandlerFiltered in project BuildCraft by BuildCraft.
the class TriggerInventoryLevel method isTriggerActive.
@Override
public boolean isTriggerActive(TileEntity tile, EnumFacing side, IStatementContainer container, IStatementParameter[] parameters) {
IItemHandler itemHandler = tile.getCapability(CapUtil.CAP_ITEMS, side.getOpposite());
if (itemHandler == null) {
return false;
}
IItemHandlerFiltered filters = ObjectUtilBC.castOrNull(itemHandler, IItemHandlerFiltered.class);
StatementParameterItemStack param = getParam(0, parameters, new StatementParameterItemStack());
ItemStack searchStack = param.getItemStack();
int itemSpace = 0;
int foundItems = 0;
for (int slot = 0; slot < itemHandler.getSlots(); slot++) {
ItemStack stackInSlot = itemHandler.getStackInSlot(slot);
if (stackInSlot.isEmpty()) {
if (searchStack.isEmpty()) {
itemSpace += itemHandler.getSlotLimit(slot);
} else {
if (searchStack.getItem() instanceof IList) {
// Unfortunately lists are too generic to work properly
// without a simple filtered inventory.
ItemStack filter = filters == null ? ItemStack.EMPTY : filters.getFilter(slot);
if (StackUtil.matchesStackOrList(searchStack, filter)) {
itemSpace += Math.min(filter.getMaxStackSize(), itemHandler.getSlotLimit(slot));
}
} else {
ItemStack stack = searchStack.copy();
int count = Math.min(itemHandler.getSlotLimit(slot), searchStack.getMaxStackSize());
stack.setCount(count);
ItemStack leftOver = itemHandler.insertItem(slot, stack, true);
if (leftOver.isEmpty()) {
itemSpace += count;
} else {
itemSpace += count - leftOver.getCount();
}
}
}
} else {
if (searchStack.isEmpty() || StackUtil.matchesStackOrList(searchStack, stackInSlot)) {
itemSpace += Math.min(stackInSlot.getMaxStackSize(), itemHandler.getSlotLimit(slot));
foundItems += stackInSlot.getCount();
}
}
}
if (itemSpace > 0) {
float percentage = foundItems / (float) itemSpace;
return percentage < type.level;
}
return false;
}
use of buildcraft.api.inventory.IItemHandlerFiltered in project BuildCraft by BuildCraft.
the class CombinedItemHandlerWrapper method getFilter.
@Override
public ItemStack getFilter(int slot) {
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(slot, index);
if (handler instanceof IItemHandlerFiltered) {
return ((IItemHandlerFiltered) handler).getFilter(slot);
}
return handler.getStackInSlot(slot);
}
Aggregations