Search in sources :

Example 1 with FluidIdentifierStack

use of logisticspipes.utils.FluidIdentifierStack in project LogisticsPipes by RS485.

the class LogisticsFluidManager method getBestReply.

@Override
@Nullable
public Pair<Integer, FluidSinkReply> getBestReply(FluidIdentifierStack stack, IRouter sourceRouter, List<Integer> jamList) {
    Optional<Pair<Integer, FluidSinkReply>> bestReply = sourceRouter.getIRoutersByCost().stream().filter(it -> it.containsFlag(PipeRoutingConnectionType.canRouteTo) && it.destination.getId() != sourceRouter.getId() && !jamList.contains(it.destination.getSimpleID()) && it.destination.getPipe() instanceof IFluidSink && it.destination.getPipe().isEnabled() && !it.destination.getPipe().isOnSameContainer(sourceRouter.getPipe())).sorted().map(it -> new Pair<>(it.destination.getSimpleID(), ((IFluidSink) it.destination.getPipe()).sinkAmount(stack))).filter(pair -> pair.getValue2() != null && pair.getValue2().sinkAmount != 0L).reduce((left, right) -> left.getValue2().fixedPriority.compareTo(right.getValue2().fixedPriority) < 0 ? right : left);
    bestReply.ifPresent(pair -> {
        ServerRouter serverRouter = SimpleServiceLocator.routerManager.getServerRouter(pair.getValue1());
        if (serverRouter == null)
            return;
        CoreRoutedPipe pipe = serverRouter.getPipe();
        if (pipe != null)
            pipe.spawnParticle(Particles.BlueParticle, 10);
    });
    return bestReply.orElse(null);
}
Also used : IFluidSink(logisticspipes.interfaces.routing.IFluidSink) IRouter(logisticspipes.routing.IRouter) IFluidSink(logisticspipes.interfaces.routing.IFluidSink) Particles(logisticspipes.pipefxhandlers.Particles) HashMap(java.util.HashMap) LogisticsFluidContainer(logisticspipes.items.LogisticsFluidContainer) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) LPItems(logisticspipes.LPItems) FluidIdentifier(logisticspipes.utils.FluidIdentifier) FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) FluidSinkReply(logisticspipes.utils.FluidSinkReply) Map(java.util.Map) IProvideFluids(logisticspipes.interfaces.routing.IProvideFluids) Nullable(javax.annotation.Nullable) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) PipeRoutingConnectionType(logisticspipes.routing.PipeRoutingConnectionType) Collectors(java.util.stream.Collectors) ExitRoute(logisticspipes.routing.ExitRoute) ServerRouter(logisticspipes.routing.ServerRouter) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) Pair(logisticspipes.utils.tuples.Pair) Entry(java.util.Map.Entry) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) Optional(java.util.Optional) FluidStack(net.minecraftforge.fluids.FluidStack) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) ServerRouter(logisticspipes.routing.ServerRouter) Pair(logisticspipes.utils.tuples.Pair) Nullable(javax.annotation.Nullable)

Example 2 with FluidIdentifierStack

use of logisticspipes.utils.FluidIdentifierStack in project LogisticsPipes by RS485.

the class LogisticsFluidContainer method addInformation.

@Override
@SideOnly(Side.CLIENT)
public void addInformation(@Nonnull ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
    super.addInformation(stack, worldIn, tooltip, flagIn);
    if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
        FluidIdentifierStack fluidStack = SimpleServiceLocator.logisticsFluidManager.getFluidFromContainer(ItemIdentifierStack.getFromStack(stack));
        if (fluidStack != null) {
            tooltip.add("Type:  " + fluidStack.makeFluidStack().getFluid().getLocalizedName(fluidStack.makeFluidStack()));
            tooltip.add("Value: " + fluidStack.getAmount() + "mB");
        }
    }
}
Also used : FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 3 with FluidIdentifierStack

use of logisticspipes.utils.FluidIdentifierStack in project LogisticsPipes by RS485.

the class FluidRoutedPipe method endReached.

public boolean endReached(LPTravelingItemServer arrivingItem, TileEntity tile) {
    if (canInsertToTanks() && MainProxy.isServer(getWorld())) {
        getCacheHolder().trigger(CacheTypes.Inventory);
        if (arrivingItem.getItemIdentifierStack() == null || !(arrivingItem.getItemIdentifierStack().getItem().isFluidContainer())) {
            return false;
        }
        if (getRouter().getSimpleID() != arrivingItem.getDestination()) {
            return false;
        }
        int filled;
        FluidIdentifierStack liquid = SimpleServiceLocator.logisticsFluidManager.getFluidFromContainer(arrivingItem.getItemIdentifierStack());
        if (isConnectableTank(tile, arrivingItem.output, false)) {
            // Try to put liquid into all adjacent tanks.
            for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> util : PipeFluidUtil.INSTANCE.getAdjacentTanks(this, false)) {
                filled = util.getValue2().fill(liquid, true);
                liquid.lowerAmount(filled);
                if (liquid.getAmount() != 0) {
                    continue;
                }
                return true;
            }
            // Try inserting the liquid into the pipe side tank
            filled = ((PipeFluidTransportLogistics) transport).sideTanks[arrivingItem.output.ordinal()].fill(liquid.makeFluidStack(), true);
            if (filled == liquid.getAmount()) {
                return true;
            }
            liquid.lowerAmount(filled);
        }
        // Try inserting the liquid into the pipe internal tank
        filled = ((PipeFluidTransportLogistics) transport).internalTank.fill(liquid.makeFluidStack(), true);
        if (filled == liquid.getAmount()) {
            return true;
        }
        // If liquids still exist,
        liquid.lowerAmount(filled);
        // TODO: FIX THIS
        if (this instanceof IRequireReliableFluidTransport) {
            ((IRequireReliableFluidTransport) this).liquidNotInserted(liquid.getFluid(), liquid.getAmount());
        }
        IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(SimpleServiceLocator.logisticsFluidManager.getFluidContainer(liquid));
        Pair<Integer, FluidSinkReply> replies = SimpleServiceLocator.logisticsFluidManager.getBestReply(liquid, getRouter(), routedItem.getJamList());
        if (replies == null) {
            // clear destination without marking item as lost
            routedItem.setDestination(0);
        } else {
            int dest = replies.getValue1();
            routedItem.setDestination(dest);
        }
        routedItem.setTransportMode(TransportMode.Passive);
        this.queueRoutedItem(routedItem, arrivingItem.output.getOpposite());
        return true;
    }
    return false;
}
Also used : FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) ITankUtil(logisticspipes.interfaces.ITankUtil) FluidSinkReply(logisticspipes.utils.FluidSinkReply) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) IRequireReliableFluidTransport(logisticspipes.interfaces.routing.IRequireReliableFluidTransport)

Example 4 with FluidIdentifierStack

use of logisticspipes.utils.FluidIdentifierStack 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)

Example 5 with FluidIdentifierStack

use of logisticspipes.utils.FluidIdentifierStack in project LogisticsPipes by RS485.

the class LogisticsFluidManager method getAvailableFluid.

@Override
public TreeSet<FluidIdentifierStack> getAvailableFluid(List<ExitRoute> validDestinations) {
    Map<FluidIdentifier, Integer> allAvailableItems = new HashMap<>();
    for (ExitRoute r : validDestinations) {
        if (r == null) {
            continue;
        }
        if (!r.containsFlag(PipeRoutingConnectionType.canRequestFrom)) {
            continue;
        }
        if (!(r.destination.getPipe() instanceof IProvideFluids)) {
            continue;
        }
        IProvideFluids provider = (IProvideFluids) r.destination.getPipe();
        Map<FluidIdentifier, Integer> allItems = provider.getAvailableFluids();
        for (Entry<FluidIdentifier, Integer> liquid : allItems.entrySet()) {
            Integer amount = allAvailableItems.get(liquid.getKey());
            if (amount == null) {
                allAvailableItems.put(liquid.getKey(), liquid.getValue());
            } else {
                long addition = ((long) amount) + liquid.getValue();
                if (addition > Integer.MAX_VALUE) {
                    addition = Integer.MAX_VALUE;
                }
                allAvailableItems.put(liquid.getKey(), (int) addition);
            }
        }
    }
    return allAvailableItems.entrySet().stream().map(item -> new FluidIdentifierStack(item.getKey(), item.getValue())).collect(Collectors.toCollection(TreeSet::new));
}
Also used : IRouter(logisticspipes.routing.IRouter) IFluidSink(logisticspipes.interfaces.routing.IFluidSink) Particles(logisticspipes.pipefxhandlers.Particles) HashMap(java.util.HashMap) LogisticsFluidContainer(logisticspipes.items.LogisticsFluidContainer) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) LPItems(logisticspipes.LPItems) FluidIdentifier(logisticspipes.utils.FluidIdentifier) FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) FluidSinkReply(logisticspipes.utils.FluidSinkReply) Map(java.util.Map) IProvideFluids(logisticspipes.interfaces.routing.IProvideFluids) Nullable(javax.annotation.Nullable) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) PipeRoutingConnectionType(logisticspipes.routing.PipeRoutingConnectionType) Collectors(java.util.stream.Collectors) ExitRoute(logisticspipes.routing.ExitRoute) ServerRouter(logisticspipes.routing.ServerRouter) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) Pair(logisticspipes.utils.tuples.Pair) Entry(java.util.Map.Entry) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) Optional(java.util.Optional) FluidStack(net.minecraftforge.fluids.FluidStack) FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) HashMap(java.util.HashMap) IProvideFluids(logisticspipes.interfaces.routing.IProvideFluids) FluidIdentifier(logisticspipes.utils.FluidIdentifier) ExitRoute(logisticspipes.routing.ExitRoute)

Aggregations

FluidIdentifierStack (logisticspipes.utils.FluidIdentifierStack)8 FluidStack (net.minecraftforge.fluids.FluidStack)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 ITankUtil (logisticspipes.interfaces.ITankUtil)3 IProvideFluids (logisticspipes.interfaces.routing.IProvideFluids)3 LogisticsFluidContainer (logisticspipes.items.LogisticsFluidContainer)3 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)3 FluidIdentifier (logisticspipes.utils.FluidIdentifier)3 FluidSinkReply (logisticspipes.utils.FluidSinkReply)3 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)3 Pair (logisticspipes.utils.tuples.Pair)3 Optional (java.util.Optional)2 TreeSet (java.util.TreeSet)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 LPItems (logisticspipes.LPItems)2 IFluidSink (logisticspipes.interfaces.routing.IFluidSink)2