Search in sources :

Example 1 with IItemHandlerFiltered

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;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) IItemHandlerFiltered(buildcraft.api.inventory.IItemHandlerFiltered) StatementParameterItemStack(buildcraft.api.statements.StatementParameterItemStack) StatementParameterItemStack(buildcraft.api.statements.StatementParameterItemStack) ItemStack(net.minecraft.item.ItemStack) IList(buildcraft.api.items.IList)

Example 2 with IItemHandlerFiltered

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);
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) IItemHandlerFiltered(buildcraft.api.inventory.IItemHandlerFiltered)

Aggregations

IItemHandlerFiltered (buildcraft.api.inventory.IItemHandlerFiltered)2 IList (buildcraft.api.items.IList)1 StatementParameterItemStack (buildcraft.api.statements.StatementParameterItemStack)1 ItemStack (net.minecraft.item.ItemStack)1 IItemHandler (net.minecraftforge.items.IItemHandler)1 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)1