Search in sources :

Example 6 with IInvSlot

use of buildcraft.api.core.IInvSlot in project BuildCraft by BuildCraft.

the class BlockBuildCraft method getComparatorInputOverride.

@Override
public int getComparatorInputOverride(World world, BlockPos pos) {
    TileEntity tile = world.getTileEntity(pos);
    if (tile instanceof IInventory) {
        int count = 0;
        int countNonEmpty = 0;
        float power = 0.0F;
        for (EnumFacing face : EnumFacing.values()) {
            for (IInvSlot slot : InventoryIterator.getIterable((IInventory) tile, face)) {
                if (((IComparatorInventory) this).doesSlotCountComparator(tile, slot.getIndex(), slot.getStackInSlot())) {
                    count++;
                    if (slot.getStackInSlot() != null) {
                        countNonEmpty++;
                        power += (float) slot.getStackInSlot().stackSize / (float) Math.min(((IInventory) tile).getInventoryStackLimit(), slot.getStackInSlot().getMaxStackSize());
                    }
                }
            }
        }
        power /= count;
        return MathHelper.floor_float(power * 14.0F) + (countNonEmpty > 0 ? 1 : 0);
    }
    return 0;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory) IInvSlot(buildcraft.api.core.IInvSlot) EnumFacing(net.minecraft.util.EnumFacing)

Example 7 with IInvSlot

use of buildcraft.api.core.IInvSlot in project BuildCraft by BuildCraft.

the class BptBuilderTemplate method internalGetNextBlock.

private BuildingSlotBlock internalGetNextBlock(World world, TileAbstractBuilder builder) {
    BuildingSlotBlock result = null;
    IInvSlot firstSlotToConsume = null;
    for (IInvSlot invSlot : InventoryIterator.getIterable(builder)) {
        if (!builder.isBuildingMaterialSlot(invSlot.getIndex())) {
            continue;
        }
        ItemStack stack = invSlot.getStackInSlot();
        if (stack != null && stack.stackSize > 0) {
            firstSlotToConsume = invSlot;
            break;
        }
    }
    // Step 1: Check the cleared
    iteratorClear.startIteration();
    while (iteratorClear.hasNext()) {
        BuildingSlotBlock slot = iteratorClear.next();
        if (slot.buildStage > clearList.getFirst().buildStage) {
            iteratorClear.reset();
            break;
        }
        if (canDestroy(builder, context, slot)) {
            if (BlockUtil.isUnbreakableBlock(world, slot.pos) || isBlockBreakCanceled(world, slot.pos) || BuildCraftAPI.isSoftBlock(world, slot.pos)) {
                iteratorClear.remove();
                markLocationUsed(slot.pos);
            } else {
                consumeEnergyToDestroy(builder, slot);
                createDestroyItems(slot);
                result = slot;
                iteratorClear.remove();
                markLocationUsed(slot.pos);
                break;
            }
        }
    }
    if (result != null) {
        return result;
    }
    // Step 2: Check the built, but only if we have anything to place and enough energy
    if (firstSlotToConsume == null) {
        return null;
    }
    iteratorBuild.startIteration();
    while (iteratorBuild.hasNext()) {
        BuildingSlotBlock slot = iteratorBuild.next();
        if (slot.buildStage > buildList.getFirst().buildStage) {
            iteratorBuild.reset();
            break;
        }
        if (BlockUtil.isUnbreakableBlock(world, slot.pos) || isBlockPlaceCanceled(world, slot.pos, slot.schematic) || !BuildCraftAPI.isSoftBlock(world, slot.pos)) {
            iteratorBuild.remove();
            markLocationUsed(slot.pos);
        } else if (builder.consumeEnergy(BuilderAPI.BUILD_ENERGY)) {
            slot.addStackConsumed(firstSlotToConsume.decreaseStackInSlot(1));
            result = slot;
            iteratorBuild.remove();
            markLocationUsed(slot.pos);
            break;
        }
    }
    return result;
}
Also used : BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock) IInvSlot(buildcraft.api.core.IInvSlot) ItemStack(net.minecraft.item.ItemStack)

Example 8 with IInvSlot

use of buildcraft.api.core.IInvSlot in project BuildCraft by BuildCraft.

the class BptBuilderBlueprint method checkRequirements.

public boolean checkRequirements(TileAbstractBuilder builder, Schematic slot) {
    LinkedList<ItemStack> tmpReq = new LinkedList<>();
    try {
        LinkedList<ItemStack> req = new LinkedList<>();
        slot.getRequirementsForPlacement(context, req);
        for (ItemStack stk : req) {
            if (stk != null) {
                tmpReq.add(stk.copy());
            }
        }
    } catch (Throwable t) {
        // Defensive code against errors in implementers
        t.printStackTrace();
        BCLog.logger.throwing(t);
    }
    LinkedList<ItemStack> stacksUsed = new LinkedList<>();
    if (context.world().getWorldInfo().getGameType() == GameType.CREATIVE) {
        for (ItemStack s : tmpReq) {
            stacksUsed.add(s);
        }
        return !(builder.energyAvailable() < slot.getEnergyRequirement(stacksUsed));
    }
    for (ItemStack reqStk : tmpReq) {
        boolean itemBlock = reqStk.getItem() instanceof ItemBlock;
        Fluid fluid = itemBlock ? FluidRegistry.lookupFluidForBlock(((ItemBlock) reqStk.getItem()).block) : null;
        if (fluid != null && builder.drainBuild(new FluidStack(fluid, FluidContainerRegistry.BUCKET_VOLUME), true)) {
            continue;
        }
        for (IInvSlot slotInv : InventoryIterator.getIterable(new InventoryCopy(builder))) {
            if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
                continue;
            }
            ItemStack invStk = slotInv.getStackInSlot();
            if (invStk == null || invStk.stackSize == 0) {
                continue;
            }
            FluidStack fluidStack = fluid != null ? FluidContainerRegistry.getFluidForFilledItem(invStk) : null;
            boolean compatibleContainer = fluidStack != null && fluidStack.getFluid() == fluid && fluidStack.amount >= FluidContainerRegistry.BUCKET_VOLUME;
            if (slot.isItemMatchingRequirement(invStk, reqStk) || compatibleContainer) {
                try {
                    stacksUsed.add(slot.useItem(context, reqStk, slotInv));
                } catch (Throwable t) {
                    // Defensive code against errors in implementers
                    t.printStackTrace();
                    BCLog.logger.throwing(t);
                }
                if (reqStk.stackSize == 0) {
                    break;
                }
            }
        }
        if (reqStk.stackSize != 0) {
            return false;
        }
    }
    return builder.energyAvailable() >= slot.getEnergyRequirement(stacksUsed);
}
Also used : IInvSlot(buildcraft.api.core.IInvSlot) FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraftforge.fluids.Fluid) InventoryCopy(buildcraft.core.lib.inventory.InventoryCopy) ItemStack(net.minecraft.item.ItemStack) ItemBlock(net.minecraft.item.ItemBlock) LinkedList(java.util.LinkedList)

Example 9 with IInvSlot

use of buildcraft.api.core.IInvSlot in project BuildCraft by BuildCraft.

the class TransactorSimple method remove.

@Override
public ItemStack remove(IStackFilter filter, boolean doRemove) {
    for (IInvSlot slot : InventoryIterator.getIterable(inventory, orientation)) {
        ItemStack stack = slot.getStackInSlot();
        if (stack != null && slot.canTakeStackFromSlot(stack) && filter.matches(stack)) {
            if (doRemove) {
                return slot.decreaseStackInSlot(1);
            } else {
                ItemStack output = stack.copy();
                output.stackSize = 1;
                return output;
            }
        }
    }
    return null;
}
Also used : IInvSlot(buildcraft.api.core.IInvSlot) ItemStack(net.minecraft.item.ItemStack)

Example 10 with IInvSlot

use of buildcraft.api.core.IInvSlot in project BuildCraft by BuildCraft.

the class TileAdvancedCraftingTable method searchNeighborsForIngredients.

private void searchNeighborsForIngredients() {
    for (IInvSlot slot : InventoryIterator.getIterable(craftingSlots, EnumFacing.UP)) {
        ItemStack ingred = slot.getStackInSlot();
        if (ingred == null) {
            continue;
        }
        IStackFilter filter = new CraftingFilter(ingred);
        if (InvUtils.countItems(invInput, EnumFacing.UP, filter) < InvUtils.countItems(craftingSlots, EnumFacing.UP, filter)) {
            for (EnumFacing side : SEARCH_SIDES) {
                TileEntity tile = getTile(side);
                if (tile instanceof IInventory) {
                    IInventory inv = InvUtils.getInventory((IInventory) tile);
                    ItemStack result = InvUtils.moveOneItem(inv, side.getOpposite(), invInput, side, filter);
                    if (result != null) {
                        return;
                    }
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory) IInvSlot(buildcraft.api.core.IInvSlot) IStackFilter(buildcraft.api.core.IStackFilter) CraftingFilter(buildcraft.lib.inventory.filter.CraftingFilter) EnumFacing(net.minecraft.util.EnumFacing) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IInvSlot (buildcraft.api.core.IInvSlot)14 ItemStack (net.minecraft.item.ItemStack)11 IInventory (net.minecraft.inventory.IInventory)4 EnumFacing (net.minecraft.util.EnumFacing)3 BuildingSlotBlock (buildcraft.core.builders.BuildingSlotBlock)2 TileAutoWorkbench (buildcraft.factory.TileAutoWorkbench)2 ArrayStackOrListFilter (buildcraft.lib.inventory.filter.ArrayStackOrListFilter)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ItemBlock (net.minecraft.item.ItemBlock)2 TileEntity (net.minecraft.tileentity.TileEntity)2 Fluid (net.minecraftforge.fluids.Fluid)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 IStackFilter (buildcraft.api.core.IStackFilter)1 IRequestProvider (buildcraft.api.robots.IRequestProvider)1 IInjectable (buildcraft.api.transport.IInjectable)1 TileAbstractBuilder (buildcraft.core.builders.TileAbstractBuilder)1 ITransactor (buildcraft.core.lib.inventory.ITransactor)1 InventoryCopy (buildcraft.core.lib.inventory.InventoryCopy)1 CraftingFilter (buildcraft.lib.inventory.filter.CraftingFilter)1