Search in sources :

Example 1 with BuildingSlotBlock

use of buildcraft.core.builders.BuildingSlotBlock 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)

Example 2 with BuildingSlotBlock

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

the class BptBuilderBlueprint method recomputeNeededItems.

private void recomputeNeededItems() {
    neededItems.clear();
    HashMap<StackKey, Integer> computeStacks = new HashMap<>();
    for (List<BuildingSlotBlock> lb : buildList.values()) {
        for (BuildingSlotBlock slot : lb) {
            if (slot == null) {
                continue;
            }
            List<ItemStack> stacks = new ArrayList<>();
            try {
                stacks = slot.getRequirements(context);
            } catch (Throwable t) {
                // Defensive code against errors in implementers
                t.printStackTrace();
                BCLog.logger.throwing(t);
            }
            for (ItemStack stack : stacks) {
                if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
                    continue;
                }
                StackKey key = new StackKey(stack);
                if (!computeStacks.containsKey(key)) {
                    computeStacks.put(key, stack.stackSize);
                } else {
                    Integer num = computeStacks.get(key);
                    num += stack.stackSize;
                    computeStacks.put(key, num);
                }
            }
        }
    }
    for (BuildingSlotEntity slot : entityList) {
        LinkedList<ItemStack> stacks = new LinkedList<>();
        try {
            stacks = slot.getRequirements(context);
        } catch (Throwable t) {
            // Defensive code against errors in implementers
            t.printStackTrace();
            BCLog.logger.throwing(t);
        }
        for (ItemStack stack : stacks) {
            if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
                continue;
            }
            StackKey key = new StackKey(stack);
            if (!computeStacks.containsKey(key)) {
                computeStacks.put(key, stack.stackSize);
            } else {
                Integer num = computeStacks.get(key);
                num += stack.stackSize;
                computeStacks.put(key, num);
            }
        }
    }
    for (Entry<StackKey, Integer> e : computeStacks.entrySet()) {
        neededItems.add(new RequirementItemStack(e.getKey().stack.copy(), e.getValue()));
    }
    sortNeededItems();
}
Also used : HashMap(java.util.HashMap) StackKey(buildcraft.api.core.StackKey) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock) BuildingSlotEntity(buildcraft.core.builders.BuildingSlotEntity) ItemStack(net.minecraft.item.ItemStack)

Example 3 with BuildingSlotBlock

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

the class BptBuilderTemplate method internalInit.

@Override
protected void internalInit() {
    BlockPos worldOffset = pos.subtract(blueprint.anchor);
    BlockPos bptMin = BlockPos.ORIGIN;
    if (worldOffset.getY() < 0)
        bptMin = VecUtil.replaceValue(bptMin, Axis.Y, -worldOffset.getY());
    BlockPos bptMax = blueprint.size.subtract(Utils.POS_ONE);
    if (worldOffset.add(bptMax).getY() >= context.world().getHeight()) {
        bptMax = VecUtil.replaceValue(bptMax, Axis.Y, context.world().getHeight() - worldOffset.getY());
    }
    /* Check to make sure the max is bigger than the min- if its not it means that the size was 0 for one of the
         * axis */
    if (Utils.min(bptMin, bptMax).equals(bptMin) && Utils.max(bptMin, bptMax).equals(bptMax)) {
        if (blueprint.excavate) {
            for (BlockPos bptOffset : BlockPos.getAllInBox(bptMin, bptMax)) {
                BlockPos pointWorldOffset = worldOffset.add(bptOffset);
                SchematicBlockBase slot = blueprint.get(bptOffset);
                if (slot == null && !isLocationUsed(pointWorldOffset)) {
                    BuildingSlotBlock b = new BuildingSlotBlock();
                    b.schematic = null;
                    b.pos = pointWorldOffset;
                    b.mode = Mode.ClearIfInvalid;
                    b.buildStage = 0;
                    clearList.add(b);
                }
            }
        }
        for (BlockPos bptOffset : BlockPos.getAllInBox(bptMin, bptMax)) {
            BlockPos pointWorldOffset = worldOffset.add(bptOffset);
            SchematicBlockBase slot = blueprint.get(bptOffset);
            if (slot != null && !isLocationUsed(pointWorldOffset)) {
                BuildingSlotBlock b = new BuildingSlotBlock();
                b.schematic = slot;
                b.pos = pointWorldOffset;
                b.mode = Mode.Build;
                b.buildStage = 1;
                buildList.add(b);
            }
        }
    }
    iteratorBuild = new BuildingSlotIterator(buildList);
    iteratorClear = new BuildingSlotIterator(clearList);
}
Also used : SchematicBlockBase(buildcraft.api.blueprints.SchematicBlockBase) BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock) BuildingSlotIterator(buildcraft.core.builders.BuildingSlotIterator) BlockPos(net.minecraft.util.math.BlockPos)

Example 4 with BuildingSlotBlock

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

the class BptBuilderTemplate method getNextBlock.

@Override
public BuildingSlot getNextBlock(World world, TileAbstractBuilder inv) {
    if (buildList.size() != 0 || clearList.size() != 0) {
        BuildingSlotBlock slot = internalGetNextBlock(world, inv);
        checkDone();
        if (slot != null) {
            return slot;
        }
    } else {
        checkDone();
    }
    return null;
}
Also used : BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock)

Example 5 with BuildingSlotBlock

use of buildcraft.core.builders.BuildingSlotBlock 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)

Aggregations

BuildingSlotBlock (buildcraft.core.builders.BuildingSlotBlock)11 LinkedList (java.util.LinkedList)4 ItemStack (net.minecraft.item.ItemStack)4 BuildingSlotEntity (buildcraft.core.builders.BuildingSlotEntity)3 IInvSlot (buildcraft.api.core.IInvSlot)2 BlockPos (net.minecraft.util.math.BlockPos)2 SchematicBlock (buildcraft.api.blueprints.SchematicBlock)1 SchematicBlockBase (buildcraft.api.blueprints.SchematicBlockBase)1 SchematicEntity (buildcraft.api.blueprints.SchematicEntity)1 StackKey (buildcraft.api.core.StackKey)1 BuilderItemMetaPair (buildcraft.core.builders.BuilderItemMetaPair)1 BuildingSlotIterator (buildcraft.core.builders.BuildingSlotIterator)1 TileAbstractBuilder (buildcraft.core.builders.TileAbstractBuilder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ItemBlock (net.minecraft.item.ItemBlock)1 Fluid (net.minecraftforge.fluids.Fluid)1 FluidStack (net.minecraftforge.fluids.FluidStack)1