Search in sources :

Example 1 with InventoryCopy

use of buildcraft.core.lib.inventory.InventoryCopy 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)

Aggregations

IInvSlot (buildcraft.api.core.IInvSlot)1 InventoryCopy (buildcraft.core.lib.inventory.InventoryCopy)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