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;
}
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;
}
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);
}
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;
}
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;
}
}
}
}
}
}
Aggregations