use of net.minecraftforge.fluids.capability.IFluidHandler.FluidAction in project assemblylinemachines by HaydenBelanger.
the class BathCrafting method assemble.
@Override
public ItemStack assemble(Container inv) {
if (inv instanceof IMachineDataBridge) {
IMachineDataBridge data = (IMachineDataBridge) inv;
IFluidHandler handler = data.getCraftingFluidHandler(Optional.empty());
BiFunction<Integer, FluidAction, FluidStack> drain = handler instanceof IFluidHandlerBypass ? (i, a) -> ((IFluidHandlerBypass) handler).drainBypassRestrictions(i, a) : (i, a) -> handler.drain(i, a);
if (handler == null || handler.getFluidInTank(0).getFluid() != fluid.getAssocFluid() || drain.apply(percent.getMB(), FluidAction.SIMULATE).getAmount() != percent.getMB())
return ItemStack.EMPTY;
int rand = RAND.nextInt(9) * data.getUpgradeAmount(Upgrades.MACHINE_CONSERVATION);
int cons = percent.getMB();
if (rand > 21) {
cons = 0;
} else if (rand > 15) {
cons = (int) Math.round((double) cons * 0.25d);
} else if (rand > 10) {
cons = (int) Math.round((double) cons * 0.5d);
} else if (rand > 5) {
cons = (int) Math.round((double) cons * 0.75d);
}
drain.apply(cons, FluidAction.EXECUTE);
inv.getItem(1).shrink(1);
inv.getItem(2).shrink(1);
data.setCycles((float) stirs * 3.6f);
}
return this.output.copy();
}
use of net.minecraftforge.fluids.capability.IFluidHandler.FluidAction in project Create by Creators-of-Create.
the class FluidNetwork method tick.
public void tick() {
if (pauseBeforePropagation > 0) {
pauseBeforePropagation--;
return;
}
for (int cycle = 0; cycle < CYCLES_PER_TICK; cycle++) {
boolean shouldContinue = false;
for (Iterator<BlockFace> iterator = queued.iterator(); iterator.hasNext(); ) {
BlockFace blockFace = iterator.next();
if (!isPresent(blockFace))
continue;
PipeConnection pipeConnection = get(blockFace);
if (pipeConnection != null) {
if (blockFace.equals(start))
transferSpeed = (int) Math.max(1, pipeConnection.pressure.get(true) / 2f);
frontier.add(Pair.of(blockFace, pipeConnection));
}
iterator.remove();
}
for (Iterator<Pair<BlockFace, PipeConnection>> iterator = frontier.iterator(); iterator.hasNext(); ) {
Pair<BlockFace, PipeConnection> pair = iterator.next();
BlockFace blockFace = pair.getFirst();
PipeConnection pipeConnection = pair.getSecond();
if (!pipeConnection.hasFlow())
continue;
Flow flow = pipeConnection.flow.get();
if (!fluid.isEmpty() && !flow.fluid.isFluidEqual(fluid)) {
iterator.remove();
continue;
}
if (!flow.inbound) {
if (pipeConnection.comparePressure() >= 0)
iterator.remove();
continue;
}
if (!flow.complete)
continue;
if (fluid.isEmpty())
fluid = flow.fluid;
boolean canRemove = true;
for (Direction side : Iterate.directions) {
if (side == blockFace.getFace())
continue;
BlockFace adjacentLocation = new BlockFace(blockFace.getPos(), side);
PipeConnection adjacent = get(adjacentLocation);
if (adjacent == null)
continue;
if (!adjacent.hasFlow()) {
// Branch could potentially still appear
if (adjacent.hasPressure() && adjacent.pressure.getSecond() > 0)
canRemove = false;
continue;
}
Flow outFlow = adjacent.flow.get();
if (outFlow.inbound) {
if (adjacent.comparePressure() > 0)
canRemove = false;
continue;
}
if (!outFlow.complete) {
canRemove = false;
continue;
}
// Give pipe end a chance to init connections
if (!adjacent.source.isPresent() && !adjacent.determineSource(world, blockFace.getPos())) {
canRemove = false;
continue;
}
if (adjacent.source.isPresent() && adjacent.source.get().isEndpoint()) {
targets.add(Pair.of(adjacentLocation, adjacent.source.get().provideHandler()));
continue;
}
if (visited.add(adjacentLocation.getConnectedPos())) {
queued.add(adjacentLocation.getOpposite());
shouldContinue = true;
}
}
if (canRemove)
iterator.remove();
}
if (!shouldContinue)
break;
}
if (!source.isPresent())
source = sourceSupplier.get();
if (!source.isPresent())
return;
keepPortableFluidInterfaceEngaged();
if (targets.isEmpty())
return;
for (Pair<BlockFace, LazyOptional<IFluidHandler>> pair : targets) {
if (pair.getSecond().isPresent() && world.getGameTime() % 40 != 0)
continue;
PipeConnection pipeConnection = get(pair.getFirst());
if (pipeConnection == null)
continue;
pipeConnection.source.ifPresent(fs -> {
if (fs.isEndpoint())
pair.setSecond(fs.provideHandler());
});
}
int flowSpeed = transferSpeed;
for (boolean simulate : Iterate.trueAndFalse) {
FluidAction action = simulate ? FluidAction.SIMULATE : FluidAction.EXECUTE;
IFluidHandler handler = source.orElse(null);
if (handler == null)
return;
FluidStack transfer = FluidStack.EMPTY;
for (int i = 0; i < handler.getTanks(); i++) {
FluidStack contained = handler.getFluidInTank(i);
if (contained.isEmpty())
continue;
if (!contained.isFluidEqual(fluid))
continue;
FluidStack toExtract = FluidHelper.copyStackWithAmount(contained, flowSpeed);
transfer = handler.drain(toExtract, action);
}
if (transfer.isEmpty()) {
FluidStack genericExtract = handler.drain(flowSpeed, action);
if (!genericExtract.isEmpty() && genericExtract.isFluidEqual(fluid))
transfer = genericExtract;
}
if (transfer.isEmpty())
return;
if (simulate)
flowSpeed = transfer.getAmount();
List<Pair<BlockFace, LazyOptional<IFluidHandler>>> availableOutputs = new ArrayList<>(targets);
while (!availableOutputs.isEmpty() && transfer.getAmount() > 0) {
int dividedTransfer = transfer.getAmount() / availableOutputs.size();
int remainder = transfer.getAmount() % availableOutputs.size();
for (Iterator<Pair<BlockFace, LazyOptional<IFluidHandler>>> iterator = availableOutputs.iterator(); iterator.hasNext(); ) {
Pair<BlockFace, LazyOptional<IFluidHandler>> pair = iterator.next();
int toTransfer = dividedTransfer;
if (remainder > 0) {
toTransfer++;
remainder--;
}
if (transfer.isEmpty())
break;
IFluidHandler targetHandler = pair.getSecond().orElse(null);
if (targetHandler == null) {
iterator.remove();
continue;
}
FluidStack divided = transfer.copy();
divided.setAmount(toTransfer);
int fill = targetHandler.fill(divided, action);
transfer.setAmount(transfer.getAmount() - fill);
if (fill < toTransfer)
iterator.remove();
}
}
flowSpeed -= transfer.getAmount();
transfer = FluidStack.EMPTY;
}
}
use of net.minecraftforge.fluids.capability.IFluidHandler.FluidAction in project Create by Creators-of-Create.
the class BasinTileEntity method tryClearingSpoutputOverflow.
private void tryClearingSpoutputOverflow() {
BlockState blockState = getBlockState();
if (!(blockState.getBlock() instanceof BasinBlock))
return;
Direction direction = blockState.getValue(BasinBlock.FACING);
BlockEntity te = level.getBlockEntity(worldPosition.below().relative(direction));
FilteringBehaviour filter = null;
InvManipulationBehaviour inserter = null;
if (te != null) {
filter = TileEntityBehaviour.get(level, te.getBlockPos(), FilteringBehaviour.TYPE);
inserter = TileEntityBehaviour.get(level, te.getBlockPos(), InvManipulationBehaviour.TYPE);
}
IItemHandler targetInv = te == null ? null : te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(inserter == null ? null : inserter.getInventory());
IFluidHandler targetTank = te == null ? null : te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null);
boolean update = false;
for (Iterator<ItemStack> iterator = spoutputBuffer.iterator(); iterator.hasNext(); ) {
ItemStack itemStack = iterator.next();
if (direction == Direction.DOWN) {
Block.popResource(level, worldPosition, itemStack);
iterator.remove();
update = true;
continue;
}
if (targetInv == null)
break;
if (!ItemHandlerHelper.insertItemStacked(targetInv, itemStack, true).isEmpty())
continue;
if (filter != null && !filter.test(itemStack))
continue;
update = true;
ItemHandlerHelper.insertItemStacked(targetInv, itemStack.copy(), false);
iterator.remove();
visualizedOutputItems.add(IntAttached.withZero(itemStack));
}
for (Iterator<FluidStack> iterator = spoutputFluidBuffer.iterator(); iterator.hasNext(); ) {
FluidStack fluidStack = iterator.next();
if (direction == Direction.DOWN) {
iterator.remove();
update = true;
continue;
}
if (targetTank == null)
break;
for (boolean simulate : Iterate.trueAndFalse) {
FluidAction action = simulate ? FluidAction.SIMULATE : FluidAction.EXECUTE;
int fill = targetTank instanceof SmartFluidTankBehaviour.InternalFluidHandler ? ((SmartFluidTankBehaviour.InternalFluidHandler) targetTank).forceFill(fluidStack.copy(), action) : targetTank.fill(fluidStack.copy(), action);
if (fill != fluidStack.getAmount())
break;
if (simulate)
continue;
update = true;
iterator.remove();
visualizedOutputFluids.add(IntAttached.withZero(fluidStack));
}
}
if (update) {
notifyChangeOfContents();
sendData();
}
}
use of net.minecraftforge.fluids.capability.IFluidHandler.FluidAction in project Create by Creators-of-Create.
the class BasinTileEntity method acceptFluidOutputsIntoBasin.
private boolean acceptFluidOutputsIntoBasin(List<FluidStack> outputFluids, boolean simulate, IFluidHandler targetTank) {
for (FluidStack fluidStack : outputFluids) {
FluidAction action = simulate ? FluidAction.SIMULATE : FluidAction.EXECUTE;
int fill = targetTank instanceof SmartFluidTankBehaviour.InternalFluidHandler ? ((SmartFluidTankBehaviour.InternalFluidHandler) targetTank).forceFill(fluidStack.copy(), action) : targetTank.fill(fluidStack.copy(), action);
if (fill != fluidStack.getAmount())
return false;
}
return true;
}
Aggregations