Search in sources :

Example 1 with LogisticsFluidOrder

use of logisticspipes.routing.order.LogisticsFluidOrder in project LogisticsPipes by RS485.

the class PipeFluidProvider method enabledUpdateEntity.

@Override
public void enabledUpdateEntity() {
    super.enabledUpdateEntity();
    if (!getFluidOrderManager().hasOrders(ResourceType.PROVIDER) || !isNthTick(6)) {
        return;
    }
    LogisticsFluidOrder order = getFluidOrderManager().peekAtTopRequest(ResourceType.PROVIDER);
    AtomicInteger amountToSend = new AtomicInteger();
    AtomicInteger attemptedAmount = new AtomicInteger();
    amountToSend.set(Math.min(order.getAmount(), 5000));
    attemptedAmount.set(Math.min(order.getAmount(), 5000));
    for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : PipeFluidUtil.INSTANCE.getAdjacentTanks(this, false)) {
        if (amountToSend.get() <= 0) {
            break;
        }
        boolean fallback = true;
        if (pair.getValue2() instanceof ISpecialTankUtil) {
            ISpecialTankUtil util = (ISpecialTankUtil) pair.getValue2();
            fallback = false;
            ISpecialTankAccessHandler handler = util.getSpecialHandler();
            FluidStack drained = handler.drainFrom(pair.getValue1().getTileEntity(), order.getFluid(), amountToSend.get(), false);
            if (drained != null && drained.amount > 0 && order.getFluid().equals(FluidIdentifier.get(drained))) {
                drained = handler.drainFrom(pair.getValue1().getTileEntity(), order.getFluid(), amountToSend.get(), true);
                int amount = drained.amount;
                amountToSend.addAndGet(-amount);
                ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(FluidIdentifierStack.getFromStack(drained));
                IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
                item.setDestination(order.getRouter().getSimpleID());
                item.setTransportMode(TransportMode.Active);
                this.queueRoutedItem(item, pair.getValue1().getDirection());
                getFluidOrderManager().sendSuccessfull(amount, false, item);
                if (amountToSend.get() <= 0) {
                    break;
                }
            }
        }
        if (fallback) {
            if (pair.getValue2().containsTanks()) {
                pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(fluidStack -> {
                    if (amountToSend.get() <= 0) {
                        return;
                    }
                    if (fluidStack != null && fluidStack.getFluid() != null) {
                        if (order.getFluid().equals(fluidStack.getFluid())) {
                            int amount = Math.min(fluidStack.getAmount(), amountToSend.get());
                            FluidIdentifierStack drained = pair.getValue2().drain(amount, false);
                            if (drained != null && drained.getAmount() > 0 && order.getFluid().equals(drained.getFluid())) {
                                drained = pair.getValue2().drain(amount, true);
                                while (drained.getAmount() < amountToSend.get()) {
                                    FluidIdentifierStack addition = pair.getValue2().drain(amountToSend.get() - drained.getAmount(), false);
                                    if (addition != null && addition.getAmount() > 0 && order.getFluid().equals(addition.getFluid())) {
                                        addition = pair.getValue2().drain(amountToSend.get() - drained.getAmount(), true);
                                        drained.raiseAmount(addition.getAmount());
                                    } else {
                                        break;
                                    }
                                }
                                amount = drained.getAmount();
                                amountToSend.addAndGet(-amount);
                                ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(drained);
                                IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
                                item.setDestination(order.getRouter().getSimpleID());
                                item.setTransportMode(TransportMode.Active);
                                queueRoutedItem(item, pair.getValue1().getDirection());
                                getFluidOrderManager().sendSuccessfull(amount, false, item);
                            }
                        }
                    }
                });
            }
        }
    }
    if (amountToSend.get() >= attemptedAmount.get()) {
        getFluidOrderManager().sendFailed();
    }
}
Also used : ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) LogisticsFluidOrder(logisticspipes.routing.order.LogisticsFluidOrder) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) IRequestFluid(logisticspipes.interfaces.routing.IRequestFluid) IOrderInfoProvider(logisticspipes.routing.order.IOrderInfoProvider) Textures(logisticspipes.textures.Textures) Item(net.minecraft.item.Item) HashMap(java.util.HashMap) ResourceType(logisticspipes.routing.order.IOrderInfoProvider.ResourceType) FluidIdentifier(logisticspipes.utils.FluidIdentifier) FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) IProvideFluids(logisticspipes.interfaces.routing.IProvideFluids) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation) ISpecialTankUtil(logisticspipes.interfaces.ISpecialTankUtil) Collection(java.util.Collection) FluidResource(logisticspipes.request.resources.FluidResource) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) TextureType(logisticspipes.textures.Textures.TextureType) RequestTreeNode(logisticspipes.request.RequestTreeNode) TransportMode(logisticspipes.logisticspipes.IRoutedItem.TransportMode) RequestTree(logisticspipes.request.RequestTree) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) IFilter(logisticspipes.interfaces.routing.IFilter) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) ITankUtil(logisticspipes.interfaces.ITankUtil) Pair(logisticspipes.utils.tuples.Pair) Entry(java.util.Map.Entry) TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) FluidLogisticsPromise(logisticspipes.routing.FluidLogisticsPromise) FluidRoutedPipe(logisticspipes.pipes.basic.fluid.FluidRoutedPipe) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) FluidStack(net.minecraftforge.fluids.FluidStack) LogisticsFluidOrder(logisticspipes.routing.order.LogisticsFluidOrder) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) ITankUtil(logisticspipes.interfaces.ITankUtil) ISpecialTankUtil(logisticspipes.interfaces.ISpecialTankUtil) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack)

Aggregations

Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Nonnull (javax.annotation.Nonnull)1 ISpecialTankAccessHandler (logisticspipes.interfaces.ISpecialTankAccessHandler)1 ISpecialTankUtil (logisticspipes.interfaces.ISpecialTankUtil)1 ITankUtil (logisticspipes.interfaces.ITankUtil)1 IAdditionalTargetInformation (logisticspipes.interfaces.routing.IAdditionalTargetInformation)1 IFilter (logisticspipes.interfaces.routing.IFilter)1 IProvideFluids (logisticspipes.interfaces.routing.IProvideFluids)1 IRequestFluid (logisticspipes.interfaces.routing.IRequestFluid)1 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)1 TransportMode (logisticspipes.logisticspipes.IRoutedItem.TransportMode)1 FluidRoutedPipe (logisticspipes.pipes.basic.fluid.FluidRoutedPipe)1 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)1 RequestTree (logisticspipes.request.RequestTree)1 RequestTreeNode (logisticspipes.request.RequestTreeNode)1