Search in sources :

Example 1 with FluidAction

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();
}
Also used : IMachineDataBridge(me.haydenb.assemblylinemachines.block.helpers.MachineBuilder.MachineBlockEntityBuilder.IMachineDataBridge) FluidStack(net.minecraftforge.fluids.FluidStack) IFluidHandlerBypass(me.haydenb.assemblylinemachines.registry.Utils.IFluidHandlerBypass) FluidAction(net.minecraftforge.fluids.capability.IFluidHandler.FluidAction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 2 with FluidAction

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;
    }
}
Also used : BlockFace(com.simibubi.create.foundation.utility.BlockFace) FluidStack(net.minecraftforge.fluids.FluidStack) ArrayList(java.util.ArrayList) FluidAction(net.minecraftforge.fluids.capability.IFluidHandler.FluidAction) Direction(net.minecraft.core.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) Flow(com.simibubi.create.content.contraptions.fluids.PipeConnection.Flow) LazyOptional(net.minecraftforge.common.util.LazyOptional) Pair(com.simibubi.create.foundation.utility.Pair)

Example 3 with FluidAction

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();
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) SmartFluidTankBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.fluid.SmartFluidTankBehaviour) FluidStack(net.minecraftforge.fluids.FluidStack) InvManipulationBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour) FluidAction(net.minecraftforge.fluids.capability.IFluidHandler.FluidAction) Direction(net.minecraft.core.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) BlockState(net.minecraft.world.level.block.state.BlockState) FilteringBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour) ItemStack(net.minecraft.world.item.ItemStack) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 4 with FluidAction

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;
}
Also used : SmartFluidTankBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.fluid.SmartFluidTankBehaviour) FluidStack(net.minecraftforge.fluids.FluidStack) FluidAction(net.minecraftforge.fluids.capability.IFluidHandler.FluidAction)

Aggregations

FluidStack (net.minecraftforge.fluids.FluidStack)4 FluidAction (net.minecraftforge.fluids.capability.IFluidHandler.FluidAction)4 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)3 SmartFluidTankBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.fluid.SmartFluidTankBehaviour)2 Direction (net.minecraft.core.Direction)2 Flow (com.simibubi.create.content.contraptions.fluids.PipeConnection.Flow)1 FilteringBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour)1 InvManipulationBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour)1 BlockFace (com.simibubi.create.foundation.utility.BlockFace)1 Pair (com.simibubi.create.foundation.utility.Pair)1 ArrayList (java.util.ArrayList)1 IMachineDataBridge (me.haydenb.assemblylinemachines.block.helpers.MachineBuilder.MachineBlockEntityBuilder.IMachineDataBridge)1 IFluidHandlerBypass (me.haydenb.assemblylinemachines.registry.Utils.IFluidHandlerBypass)1 ItemStack (net.minecraft.world.item.ItemStack)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 LazyOptional (net.minecraftforge.common.util.LazyOptional)1 IItemHandler (net.minecraftforge.items.IItemHandler)1