Search in sources :

Example 6 with ITankUtil

use of logisticspipes.interfaces.ITankUtil 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 7 with ITankUtil

use of logisticspipes.interfaces.ITankUtil in project LogisticsPipes by RS485.

the class PipeItemsFluidSupplier method endReached.

public void endReached(LPTravelingItemServer data, TileEntity tile) {
    getCacheHolder().trigger(CacheTypes.Inventory);
    transport.markChunkModified(tile);
    notifyOfItemArival(data.getInfo());
    EnumFacing orientation = data.output.getOpposite();
    if (getOriginalUpgradeManager().hasSneakyUpgrade()) {
        orientation = getOriginalUpgradeManager().getSneakyOrientation();
    }
    ITankUtil util = SimpleServiceLocator.tankUtilFactory.getTankUtilForTE(tile, orientation);
    if (util == null) {
        return;
    }
    if (SimpleServiceLocator.pipeInformationManager.isItemPipe(tile)) {
        return;
    }
    if (data.getItemIdentifierStack() == null) {
        return;
    }
    FluidIdentifierStack liquidId = FluidIdentifierStack.getFromStack(data.getItemIdentifierStack());
    if (liquidId == null) {
        return;
    }
    while (data.getItemIdentifierStack().getStackSize() > 0 && util.fill(liquidId, false) == liquidId.getAmount() && this.useEnergy(5)) {
        util.fill(liquidId, true);
        data.getItemIdentifierStack().lowerStackSize(1);
        Item item = data.getItemIdentifierStack().getItem().item;
        if (item.hasContainerItem(data.getItemIdentifierStack().makeNormalStack())) {
            Item containerItem = Objects.requireNonNull(item.getContainerItem());
            transport.sendItem(new ItemStack(containerItem, 1));
        }
    }
}
Also used : FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) ITankUtil(logisticspipes.interfaces.ITankUtil) Item(net.minecraft.item.Item) EnumFacing(net.minecraft.util.EnumFacing) ItemStack(net.minecraft.item.ItemStack)

Example 8 with ITankUtil

use of logisticspipes.interfaces.ITankUtil in project LogisticsPipes by RS485.

the class PipeItemsFluidSupplier method throttledUpdateEntity.

@Override
public void throttledUpdateEntity() {
    if (!isEnabled()) {
        return;
    }
    if (MainProxy.isClient(getWorld())) {
        return;
    }
    super.throttledUpdateEntity();
    for (NeighborTileEntity<TileEntity> neighbor : getAdjacent().fluidTanks()) {
        final ITankUtil tankUtil = LPNeighborTileEntityKt.getTankUtil(neighbor);
        if (tankUtil == null || !tankUtil.containsTanks()) {
            continue;
        }
        // How much do I want?
        Map<ItemIdentifier, Integer> wantContainers = dummyInventory.getItemsAndCount();
        HashMap<FluidIdentifier, Integer> wantFluids = new HashMap<>();
        for (Entry<ItemIdentifier, Integer> item : wantContainers.entrySet()) {
            ItemStack wantItem = item.getKey().unsafeMakeNormalStack(1);
            FluidStack liquidstack = FluidUtil.getFluidContained(wantItem);
            if (liquidstack == null) {
                continue;
            }
            wantFluids.put(FluidIdentifier.get(liquidstack), item.getValue() * liquidstack.amount);
        }
        // How much do I have?
        HashMap<FluidIdentifier, Integer> haveFluids = new HashMap<>();
        tankUtil.tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).filter(Objects::nonNull).forEach(fluid -> {
            if (wantFluids.containsKey(fluid.getFluid())) {
                haveFluids.merge(fluid.getFluid(), fluid.getAmount(), Integer::sum);
            }
        });
        // Reduce what I have and what have been requested already
        for (Entry<FluidIdentifier, Integer> liquidId : wantFluids.entrySet()) {
            Integer haveCount = haveFluids.get(liquidId.getKey());
            if (haveCount != null) {
                liquidId.setValue(liquidId.getValue() - haveCount);
            }
        }
        for (Entry<ItemIdentifier, Integer> requestedItem : _requestedItems.entrySet()) {
            ItemStack wantItem = requestedItem.getKey().unsafeMakeNormalStack(1);
            FluidStack requestedFluidId = FluidUtil.getFluidContained(wantItem);
            if (requestedFluidId == null) {
                continue;
            }
            FluidIdentifier requestedFluid = FluidIdentifier.get(requestedFluidId);
            Integer want = wantFluids.get(requestedFluid);
            if (want != null) {
                wantFluids.put(requestedFluid, want - requestedItem.getValue() * requestedFluidId.amount);
            }
        }
        ((PipeItemsFluidSupplier) Objects.requireNonNull(container).pipe).setRequestFailed(false);
        for (ItemIdentifier need : wantContainers.keySet()) {
            FluidStack requestedFluidId = FluidUtil.getFluidContained(need.unsafeMakeNormalStack(1));
            if (requestedFluidId == null) {
                continue;
            }
            if (!wantFluids.containsKey(FluidIdentifier.get(requestedFluidId))) {
                continue;
            }
            int countToRequest = wantFluids.get(FluidIdentifier.get(requestedFluidId)) / requestedFluidId.amount;
            if (countToRequest < 1) {
                continue;
            }
            if (!useEnergy(11)) {
                break;
            }
            boolean success = false;
            if (_requestPartials) {
                countToRequest = RequestTree.requestPartial(need.makeStack(countToRequest), (IRequestItems) container.pipe, null);
                if (countToRequest > 0) {
                    success = true;
                }
            } else {
                success = RequestTree.request(need.makeStack(countToRequest), (IRequestItems) container.pipe, null, null);
            }
            if (success) {
                Integer currentRequest = _requestedItems.get(need);
                if (currentRequest == null) {
                    _requestedItems.put(need, countToRequest);
                } else {
                    _requestedItems.put(need, currentRequest + countToRequest);
                }
            } else {
                ((PipeItemsFluidSupplier) container.pipe).setRequestFailed(true);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) FluidIdentifier(logisticspipes.utils.FluidIdentifier) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ITankUtil(logisticspipes.interfaces.ITankUtil) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IRequestItems(logisticspipes.interfaces.routing.IRequestItems) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ITankUtil (logisticspipes.interfaces.ITankUtil)8 NeighborTileEntity (network.rs485.logisticspipes.connection.NeighborTileEntity)7 FluidIdentifierStack (logisticspipes.utils.FluidIdentifierStack)6 TileEntity (net.minecraft.tileentity.TileEntity)6 FluidStack (net.minecraftforge.fluids.FluidStack)6 HashMap (java.util.HashMap)5 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)5 FluidIdentifier (logisticspipes.utils.FluidIdentifier)5 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)5 Pair (logisticspipes.utils.tuples.Pair)5 Item (net.minecraft.item.Item)5 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Nonnull (javax.annotation.Nonnull)4 ISpecialTankAccessHandler (logisticspipes.interfaces.ISpecialTankAccessHandler)4 ISpecialTankUtil (logisticspipes.interfaces.ISpecialTankUtil)4 IAdditionalTargetInformation (logisticspipes.interfaces.routing.IAdditionalTargetInformation)4