Search in sources :

Example 41 with Pair

use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.

the class PipeFluidProvider method canProvide.

@Override
public void canProvide(RequestTreeNode tree, RequestTree root, List<IFilter> filter) {
    if (tree.isDone()) {
        return;
    }
    if (!(tree.getRequestType() instanceof FluidResource)) {
        return;
    }
    FluidIdentifier fluid = ((FluidResource) tree.getRequestType()).getFluid();
    AtomicInteger containedAmount = new AtomicInteger(0);
    for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : PipeFluidUtil.INSTANCE.getAdjacentTanks(this, false)) {
        boolean fallback = true;
        if (pair.getValue2() instanceof ISpecialTankUtil) {
            final ISpecialTankUtil util = (ISpecialTankUtil) pair.getValue2();
            fallback = false;
            ISpecialTankAccessHandler handler = util.getSpecialHandler();
            TileEntity tile = util.getTileEntity();
            Map<FluidIdentifier, Long> map = handler.getAvailableLiquid(tile);
            if (map.containsKey(fluid)) {
                long addition = (containedAmount.get()) + map.get(fluid);
                containedAmount.set(addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
            }
        }
        if (fallback) {
            if (pair.getValue2().containsTanks()) {
                pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(liquid -> {
                    if (liquid != null && liquid.getFluid() != null) {
                        if (fluid.equals(liquid.getFluid())) {
                            if (pair.getValue2().canDrain(liquid.getFluid())) {
                                if (pair.getValue2().drain(liquid.getFluid().makeFluidIdentifierStack(1), false) != null) {
                                    long addition = ((long) containedAmount.get()) + liquid.getAmount();
                                    containedAmount.set(addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
                                }
                            }
                        }
                    }
                });
            }
        }
    }
    FluidLogisticsPromise promise = new FluidLogisticsPromise();
    promise.liquid = fluid;
    promise.amount = Math.min(tree.getMissingAmount(), containedAmount.get() - root.getAllPromissesFor(this, fluid.getItemIdentifier()));
    promise.sender = this;
    promise.type = ResourceType.PROVIDER;
    if (promise.amount > 0) {
        tree.addPromise(promise);
    }
}
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) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ITankUtil(logisticspipes.interfaces.ITankUtil) FluidResource(logisticspipes.request.resources.FluidResource) ISpecialTankUtil(logisticspipes.interfaces.ISpecialTankUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) FluidLogisticsPromise(logisticspipes.routing.FluidLogisticsPromise)

Example 42 with Pair

use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.

the class PipeFluidInsertion method enabledUpdateEntity.

@Override
public void enabledUpdateEntity() {
    super.enabledUpdateEntity();
    List<Integer> tempJamList = new ArrayList<>();
    if (!localJamList.isEmpty()) {
        List<Pair<Integer, Integer>> toRemove = new ArrayList<>();
        for (Pair<Integer, Integer> part : localJamList) {
            part.setValue2(part.getValue2() - 1);
            if (part.getValue2() <= 0) {
                toRemove.add(part);
            } else {
                tempJamList.add(part.getValue1());
            }
        }
        if (!toRemove.isEmpty()) {
            localJamList.removeAll(toRemove);
        }
    }
    PipeFluidTransportLogistics transport = (PipeFluidTransportLogistics) this.transport;
    for (EnumFacing dir : EnumFacing.VALUES) {
        FluidStack stack = transport.sideTanks[dir.ordinal()].getFluid();
        if (stack == null) {
            continue;
        }
        stack = stack.copy();
        if (nextSendMax[dir.ordinal()] > 0 && stack.amount < transport.sideTanks[dir.ordinal()].getCapacity()) {
            nextSendMax[dir.ordinal()]--;
            continue;
        }
        if (nextSendMin[dir.ordinal()] > 0) {
            nextSendMin[dir.ordinal()]--;
            continue;
        }
        Pair<Integer, FluidSinkReply> result = SimpleServiceLocator.logisticsFluidManager.getBestReply(FluidIdentifierStack.getFromStack(stack), getRouter(), tempJamList);
        if (result == null || result.getValue2().sinkAmount <= 0) {
            nextSendMax[dir.ordinal()] = 100;
            nextSendMin[dir.ordinal()] = 10;
            continue;
        }
        if (!useEnergy((int) (0.01 * result.getValue2().getSinkAmountInt()))) {
            nextSendMax[dir.ordinal()] = 100;
            nextSendMin[dir.ordinal()] = 10;
            continue;
        }
        FluidStack toSend = transport.sideTanks[dir.ordinal()].drain(result.getValue2().getSinkAmountInt(), true);
        ItemIdentifierStack liquidContainer = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(FluidIdentifierStack.getFromStack(toSend));
        IRoutedItem routed = SimpleServiceLocator.routedItemHelper.createNewTravelItem(liquidContainer);
        routed.setDestination(result.getValue1());
        routed.setTransportMode(TransportMode.Passive);
        this.queueRoutedItem(routed, dir);
        nextSendMax[dir.ordinal()] = 100;
        nextSendMin[dir.ordinal()] = 5;
    }
}
Also used : PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) FluidSinkReply(logisticspipes.utils.FluidSinkReply) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) Pair(logisticspipes.utils.tuples.Pair)

Aggregations

Pair (logisticspipes.utils.tuples.Pair)42 List (java.util.List)21 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)19 IFilter (logisticspipes.interfaces.routing.IFilter)18 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)18 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)18 Map (java.util.Map)16 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)16 TileEntity (net.minecraft.tileentity.TileEntity)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)13 Collection (java.util.Collection)12 ExitRoute (logisticspipes.routing.ExitRoute)12 ItemStack (net.minecraft.item.ItemStack)12 Nonnull (javax.annotation.Nonnull)11 IAdditionalTargetInformation (logisticspipes.interfaces.routing.IAdditionalTargetInformation)11 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)11 MainProxy (logisticspipes.proxy.MainProxy)11 IRouter (logisticspipes.routing.IRouter)11 EntityPlayer (net.minecraft.entity.player.EntityPlayer)11