Search in sources :

Example 1 with ArrayStackFilter

use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.

the class FlexibleRecipe method craft.

@Override
public CraftingResult<T> craft(IFlexibleCrafter baseCrafter, boolean preview) {
    if (output == null) {
        return null;
    }
    IFlexibleCrafter crafter = baseCrafter;
    if (preview) {
        crafter = new FakeFlexibleCrafter(baseCrafter);
    }
    CraftingResult<T> result = new CraftingResult<>();
    result.recipe = this;
    result.powerCost = powerCost;
    result.craftingTime = craftingTime;
    for (ItemStack requirement : inputItems) {
        IStackFilter filter = new ArrayStackFilter(requirement);
        int amount = requirement.stackSize;
        if (consumeItems(crafter, result, filter, amount) != 0) {
            return null;
        }
    }
    for (List<ItemStack> requirements : inputItemsWithAlternatives) {
        IStackFilter filter = new ArrayStackFilter(requirements.toArray(new ItemStack[requirements.size()]));
        int amount = requirements.get(0).stackSize;
        if (consumeItems(crafter, result, filter, amount) != 0) {
            return null;
        }
    }
    for (FluidStack requirement : inputFluids) {
        int amount = requirement.amount;
        for (int tankid = 0; tankid < crafter.getCraftingFluidStackSize(); tankid++) {
            FluidStack fluid = crafter.getCraftingFluidStack(tankid);
            if (fluid != null && fluid.isFluidEqual(requirement)) {
                int amountUsed;
                if (fluid.amount > amount) {
                    amountUsed = amount;
                    if (!preview) {
                        crafter.decrCraftingFluidStack(tankid, amount);
                    }
                    amount = 0;
                } else {
                    amountUsed = fluid.amount;
                    if (!preview) {
                        crafter.decrCraftingFluidStack(tankid, fluid.amount);
                    }
                    amount -= fluid.amount;
                }
                result.usedFluids.add(new FluidStack(requirement.getFluid(), amountUsed));
            }
            if (amount == 0) {
                break;
            }
        }
        if (amount != 0) {
            return null;
        }
    }
    // Output generation
    result.crafted = output;
    return result;
}
Also used : ArrayStackFilter(buildcraft.lib.inventory.filter.ArrayStackFilter) FluidStack(net.minecraftforge.fluids.FluidStack) IStackFilter(buildcraft.api.core.IStackFilter) IFlexibleCrafter(buildcraft.api.recipes.IFlexibleCrafter) ItemStack(net.minecraft.item.ItemStack) CraftingResult(buildcraft.api.recipes.CraftingResult)

Example 2 with ArrayStackFilter

use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.

the class WorkbenchCrafting method hasExactStacks.

private boolean hasExactStacks() {
    TObjectIntMap<ItemStackKey> required = new TObjectIntHashMap<>(getSizeInventory());
    for (int s = 0; s < getSizeInventory(); s++) {
        ItemStack req = invBlueprint.getStackInSlot(s);
        if (!req.isEmpty()) {
            int count = req.getCount();
            if (count != 1) {
                req = req.copy();
                req.setCount(1);
            }
            ItemStackKey key = new ItemStackKey(req);
            required.adjustOrPutValue(key, count, count);
        }
    }
    return required.forEachEntry((stack, count) -> {
        ArrayStackFilter filter = new ArrayStackFilter(stack.baseStack);
        ItemStack inInventory = invMaterials.extract(filter, count, count, true);
        return !inInventory.isEmpty() && inInventory.getCount() == count;
    });
}
Also used : ArrayStackFilter(buildcraft.lib.inventory.filter.ArrayStackFilter) TObjectIntHashMap(gnu.trove.map.hash.TObjectIntHashMap) ItemStack(net.minecraft.item.ItemStack) ItemStackKey(buildcraft.lib.misc.ItemStackKey)

Example 3 with ArrayStackFilter

use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.

the class WorkbenchCrafting method craftExact.

/**
 * Implementation of {@link #craft()}, assuming nothing about the current recipe.
 */
private boolean craftExact() {
    // 4 steps:
    // - Move everything out of this inventory (Just to check: state correction operation)
    // - Attempt to move every exact item from invMaterials to this inventory
    // - Call normal crafting stuffs
    // - Move everything from the inventory back to materials
    // Step 1
    clearInventory();
    // Step 2
    for (int s = 0; s < getSizeInventory(); s++) {
        ItemStack bpt = invBlueprint.getStackInSlot(s);
        if (!bpt.isEmpty()) {
            ItemStack stack = invMaterials.extract(new ArrayStackFilter(bpt), 1, 1, false);
            if (stack.isEmpty()) {
                clearInventory();
                return false;
            }
            setInventorySlotContents(s, stack);
        }
    }
    // result of matches for getCraftingResult and getResult.
    if (!currentRecipe.matches(this, tile.getWorld())) {
        return false;
    }
    ItemStack result = currentRecipe.getCraftingResult(this);
    if (result.isEmpty()) {
        // what?
        clearInventory();
        return false;
    }
    ItemStack leftover = invResult.insert(result, false, false);
    if (!leftover.isEmpty()) {
        InventoryUtil.addToBestAcceptor(tile.getWorld(), tile.getPos(), null, leftover);
    }
    NonNullList<ItemStack> remainingStacks = currentRecipe.getRemainingItems(this);
    for (int s = 0; s < remainingStacks.size(); s++) {
        ItemStack inSlot = getStackInSlot(s);
        ItemStack remaining = remainingStacks.get(s);
        if (!inSlot.isEmpty()) {
            decrStackSize(s, 1);
            inSlot = getStackInSlot(s);
        }
        if (!remaining.isEmpty()) {
            if (inSlot.isEmpty()) {
                setInventorySlotContents(s, remaining);
            } else if (ItemStack.areItemsEqual(inSlot, remaining) && ItemStack.areItemStackTagsEqual(inSlot, remaining)) {
                remaining.grow(inSlot.getCount());
                setInventorySlotContents(s, remaining);
            } else {
                leftover = invMaterials.insert(remaining, false, false);
                if (!leftover.isEmpty()) {
                    InventoryUtil.addToBestAcceptor(tile.getWorld(), tile.getPos(), null, leftover);
                }
            }
        }
    }
    // Some ingredients really need to be removed (like empty buckets)
    for (int s = 0; s < getSizeInventory(); s++) {
        ItemStack inSlot = super.getStackInSlot(s);
        if (!inSlot.isEmpty()) {
            leftover = invMaterials.insert(inSlot, false, false);
            decrStackSize(s, inSlot.getCount() - (leftover.isEmpty() ? 0 : leftover.getCount()));
            if (!leftover.isEmpty()) {
                InventoryUtil.addToBestAcceptor(tile.getWorld(), tile.getPos(), null, leftover);
            }
        }
    }
    return true;
}
Also used : ArrayStackFilter(buildcraft.lib.inventory.filter.ArrayStackFilter) ItemStack(net.minecraft.item.ItemStack)

Example 4 with ArrayStackFilter

use of buildcraft.lib.inventory.filter.ArrayStackFilter in project BuildCraft by BuildCraft.

the class BoardRobotBuilder method update.

@Override
public void update() {
    if (launchingDelay > 0) {
        launchingDelay--;
        return;
    }
    if (markerToBuild == null) {
        markerToBuild = findClosestMarker();
        if (markerToBuild == null) {
            if (robot.containsItems()) {
                startDelegateAI(new AIRobotDisposeItems(robot));
            } else {
                startDelegateAI(new AIRobotGotoSleep(robot));
            }
            return;
        }
    }
    if (!markerToBuild.needsToBuild()) {
        markerToBuild = null;
        currentBuildingSlot = null;
        return;
    }
    if (currentBuildingSlot == null) {
        currentBuildingSlot = markerToBuild.bluePrintBuilder.reserveNextSlot(robot.worldObj);
        if (currentBuildingSlot == null) {
            // No slots available yet
            launchingDelay = 40;
            return;
        }
    }
    if (requirementsToLookFor == null) {
        if (robot.containsItems()) {
            startDelegateAI(new AIRobotDisposeItems(robot));
        }
        if (robot.worldObj.getWorldInfo().getGameType() != WorldSettings.GameType.CREATIVE) {
            requirementsToLookFor = currentBuildingSlot.getRequirements(markerToBuild.getContext());
        } else {
            requirementsToLookFor = new LinkedList<>();
        }
        if (requirementsToLookFor == null) {
            launchingDelay = 40;
            return;
        }
        if (requirementsToLookFor.size() > 4) {
            currentBuildingSlot.built = true;
            currentBuildingSlot = null;
            requirementsToLookFor = null;
            return;
        }
    }
    if (requirementsToLookFor.size() > 0) {
        startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ArrayStackFilter(requirementsToLookFor.get(0)), requirementsToLookFor.get(0).stackSize));
        return;
    }
    if (requirementsToLookFor.size() == 0) {
        if (currentBuildingSlot.stackConsumed == null) {
            // Once all the element are in, if not already, use them to
            // prepare the slot.
            markerToBuild.bluePrintBuilder.useRequirements(robot, currentBuildingSlot);
        }
        if (!hasEnoughEnergy()) {
            startDelegateAI(new AIRobotRecharge(robot));
        } else {
            startDelegateAI(new AIRobotGotoBlock(robot, Utils.convertFloor(currentBuildingSlot.getDestination()), 8));
        }
    // TODO: take into account cases where the robot can't reach the
    // destination - go to work on another block
    }
}
Also used : ArrayStackFilter(buildcraft.lib.inventory.filter.ArrayStackFilter) AIRobotGotoStationAndLoad(buildcraft.robotics.ai.AIRobotGotoStationAndLoad) AIRobotGotoSleep(buildcraft.robotics.ai.AIRobotGotoSleep) AIRobotDisposeItems(buildcraft.robotics.ai.AIRobotDisposeItems) AIRobotGotoBlock(buildcraft.robotics.ai.AIRobotGotoBlock) AIRobotRecharge(buildcraft.robotics.ai.AIRobotRecharge)

Aggregations

ArrayStackFilter (buildcraft.lib.inventory.filter.ArrayStackFilter)4 ItemStack (net.minecraft.item.ItemStack)3 IStackFilter (buildcraft.api.core.IStackFilter)1 CraftingResult (buildcraft.api.recipes.CraftingResult)1 IFlexibleCrafter (buildcraft.api.recipes.IFlexibleCrafter)1 ItemStackKey (buildcraft.lib.misc.ItemStackKey)1 AIRobotDisposeItems (buildcraft.robotics.ai.AIRobotDisposeItems)1 AIRobotGotoBlock (buildcraft.robotics.ai.AIRobotGotoBlock)1 AIRobotGotoSleep (buildcraft.robotics.ai.AIRobotGotoSleep)1 AIRobotGotoStationAndLoad (buildcraft.robotics.ai.AIRobotGotoStationAndLoad)1 AIRobotRecharge (buildcraft.robotics.ai.AIRobotRecharge)1 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)1 FluidStack (net.minecraftforge.fluids.FluidStack)1