Search in sources :

Example 1 with TileAbstractBuilder

use of buildcraft.core.builders.TileAbstractBuilder in project BuildCraft by BuildCraft.

the class BptBuilderBlueprint method useRequirements.

@Override
public void useRequirements(IInventory inv, BuildingSlot slot) {
    if (slot instanceof BuildingSlotBlock && ((BuildingSlotBlock) slot).mode == Mode.ClearIfInvalid) {
        return;
    }
    LinkedList<ItemStack> tmpReq = new LinkedList<>();
    try {
        for (ItemStack stk : slot.getRequirements(context)) {
            if (stk != null) {
                tmpReq.add(stk.copy());
            }
        }
    } catch (Throwable t) {
        // Defensive code against errors in implementers
        t.printStackTrace();
        BCLog.logger.throwing(t);
    }
    if (context.world().getWorldInfo().getGameType() == GameType.CREATIVE) {
        for (ItemStack s : tmpReq) {
            slot.addStackConsumed(s);
        }
        return;
    }
    ListIterator<ItemStack> itr = tmpReq.listIterator();
    while (itr.hasNext()) {
        ItemStack reqStk = itr.next();
        boolean smallStack = reqStk.stackSize == 1;
        ItemStack usedStack = reqStk;
        boolean itemBlock = reqStk.getItem() instanceof ItemBlock;
        Fluid fluid = itemBlock ? FluidRegistry.lookupFluidForBlock(((ItemBlock) reqStk.getItem()).block) : null;
        if (fluid != null && inv instanceof TileAbstractBuilder && ((TileAbstractBuilder) inv).drainBuild(new FluidStack(fluid, FluidContainerRegistry.BUCKET_VOLUME), true)) {
            continue;
        }
        for (IInvSlot slotInv : InventoryIterator.getIterable(inv)) {
            if (inv instanceof TileAbstractBuilder && !((TileAbstractBuilder) inv).isBuildingMaterialSlot(slotInv.getIndex())) {
                continue;
            }
            ItemStack invStk = slotInv.getStackInSlot();
            if (invStk == null || invStk.stackSize == 0) {
                continue;
            }
            FluidStack fluidStack = fluid != null ? FluidContainerRegistry.getFluidForFilledItem(invStk) : null;
            boolean fluidFound = fluidStack != null && fluidStack.getFluid() == fluid && fluidStack.amount >= FluidContainerRegistry.BUCKET_VOLUME;
            if (fluidFound || slot.getSchematic().isItemMatchingRequirement(invStk, reqStk)) {
                try {
                    usedStack = slot.getSchematic().useItem(context, reqStk, slotInv);
                    slot.addStackConsumed(usedStack);
                } 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;
        }
        if (smallStack) {
            // set to the actual item used.
            itr.set(usedStack);
        }
    }
}
Also used : BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock) IInvSlot(buildcraft.api.core.IInvSlot) FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraftforge.fluids.Fluid) TileAbstractBuilder(buildcraft.core.builders.TileAbstractBuilder) ItemStack(net.minecraft.item.ItemStack) ItemBlock(net.minecraft.item.ItemBlock) LinkedList(java.util.LinkedList)

Aggregations

IInvSlot (buildcraft.api.core.IInvSlot)1 BuildingSlotBlock (buildcraft.core.builders.BuildingSlotBlock)1 TileAbstractBuilder (buildcraft.core.builders.TileAbstractBuilder)1 LinkedList (java.util.LinkedList)1 ItemBlock (net.minecraft.item.ItemBlock)1 ItemStack (net.minecraft.item.ItemStack)1 Fluid (net.minecraftforge.fluids.Fluid)1 FluidStack (net.minecraftforge.fluids.FluidStack)1