Search in sources :

Example 6 with FluidIdentifier

use of logisticspipes.utils.FluidIdentifier 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();
    int containedAmount = 0;
    for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
        boolean fallback = true;
        if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
            ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
            if (handler instanceof ISpecialTankAccessHandler) {
                fallback = false;
                Map<FluidIdentifier, Long> map = ((ISpecialTankAccessHandler) handler).getAvailableLiquid(pair.getValue1());
                if (map.containsKey(fluid)) {
                    long addition = (containedAmount) + map.get(fluid);
                    containedAmount = addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition;
                }
            }
        }
        if (fallback) {
            FluidTankInfo[] tanks = ((IFluidHandler) pair.getValue1()).getTankInfo(pair.getValue2().getOpposite());
            if (tanks != null) {
                for (FluidTankInfo tank : tanks) {
                    if (tank == null) {
                        continue;
                    }
                    FluidStack liquid;
                    if ((liquid = tank.fluid) != null && liquid.getFluidID() != 0) {
                        if (fluid.equals(FluidIdentifier.get(liquid))) {
                            if (((IFluidHandler) pair.getValue1()).canDrain(pair.getValue2().getOpposite(), liquid.getFluid())) {
                                if (((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), 1, false) != null) {
                                    long addition = ((long) containedAmount) + liquid.amount;
                                    containedAmount = addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    FluidLogisticsPromise promise = new FluidLogisticsPromise();
    promise.liquid = fluid;
    promise.amount = Math.min(tree.getMissingAmount(), containedAmount - root.getAllPromissesFor(this, fluid.getItemIdentifier()));
    promise.sender = this;
    promise.type = ResourceType.PROVIDER;
    if (promise.amount > 0) {
        tree.addPromise(promise);
    }
}
Also used : ISpecialTankHandler(logisticspipes.interfaces.ISpecialTankHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) FluidStack(net.minecraftforge.fluids.FluidStack) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) FluidResource(logisticspipes.request.resources.FluidResource) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) FluidLogisticsPromise(logisticspipes.routing.FluidLogisticsPromise)

Example 7 with FluidIdentifier

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

the class PipeFluidProvider method getSpecificInterests.

@Override
public //work in progress, currently not active code.
Set<ItemIdentifier> getSpecificInterests() {
    Set<ItemIdentifier> l1 = new TreeSet<>();
    for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
        boolean fallback = true;
        if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
            ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
            if (handler instanceof ISpecialTankAccessHandler) {
                fallback = false;
                Map<FluidIdentifier, Long> map = ((ISpecialTankAccessHandler) handler).getAvailableLiquid(pair.getValue1());
                l1.addAll(map.keySet().stream().map(FluidIdentifier::getItemIdentifier).collect(Collectors.toList()));
            }
        }
        if (fallback) {
            FluidTankInfo[] tanks = ((IFluidHandler) pair.getValue1()).getTankInfo(pair.getValue2().getOpposite());
            if (tanks != null) {
                for (FluidTankInfo tank : tanks) {
                    if (tank == null) {
                        continue;
                    }
                    FluidStack liquid;
                    if ((liquid = tank.fluid) != null && liquid.getFluidID() != 0) {
                        if (((IFluidHandler) pair.getValue1()).canDrain(pair.getValue2().getOpposite(), liquid.getFluid())) {
                            if (((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), 1, false) != null) {
                                FluidIdentifier ident = FluidIdentifier.get(liquid);
                                l1.add(ident.getItemIdentifier());
                            }
                        }
                    }
                }
            }
        }
    }
    return l1;
}
Also used : ISpecialTankHandler(logisticspipes.interfaces.ISpecialTankHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) FluidStack(net.minecraftforge.fluids.FluidStack) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) TreeSet(java.util.TreeSet) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 8 with FluidIdentifier

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

the class PipeFluidSupplierMk2 method throttledUpdateEntity.

@Override
public void throttledUpdateEntity() {
    if (!isEnabled()) {
        return;
    }
    if (MainProxy.isClient(container.getWorld())) {
        return;
    }
    super.throttledUpdateEntity();
    if (dummyInventory.getStackInSlot(0) == null) {
        return;
    }
    WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
    //@formatter:off
    worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IFluidHandler).filter(adjacent -> SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> new Pair<>((IFluidHandler) adjacent.tileEntity, adjacent.direction)).forEach(fluidHandlerDirectionPair -> {
        FluidTankInfo[] tankInfo = fluidHandlerDirectionPair.getValue1().getTankInfo(ForgeDirection.UNKNOWN);
        if (tankInfo == null || tankInfo.length == 0) {
            return;
        }
        Map<FluidIdentifier, Integer> wantFluids = new HashMap<>();
        FluidIdentifier fIdent = FluidIdentifier.get(dummyInventory.getIDStackInSlot(0).getItem());
        wantFluids.put(fIdent, amount);
        HashMap<FluidIdentifier, Integer> haveFluids = new HashMap<>();
        FluidTankInfo[] result = container.getTankInfo(ForgeDirection.UNKNOWN);
        for (FluidTankInfo slot : result) {
            if (slot == null || slot.fluid == null || slot.fluid.getFluidID() == 0 || !wantFluids.containsKey(FluidIdentifier.get(slot.fluid))) {
                continue;
            }
            Integer liquidWant = haveFluids.get(FluidIdentifier.get(slot.fluid));
            if (liquidWant == null) {
                haveFluids.put(FluidIdentifier.get(slot.fluid), slot.fluid.amount);
            } else {
                haveFluids.put(FluidIdentifier.get(slot.fluid), liquidWant + slot.fluid.amount);
            }
        }
        if (fluidHandlerDirectionPair.getValue2().ordinal() < ((PipeFluidTransportLogistics) transport).sideTanks.length) {
            FluidTank centerTank = ((PipeFluidTransportLogistics) transport).sideTanks[fluidHandlerDirectionPair.getValue2().ordinal()];
            if (centerTank != null && centerTank.getFluid() != null && wantFluids.containsKey(FluidIdentifier.get(centerTank.getFluid()))) {
                Integer liquidWant = haveFluids.get(FluidIdentifier.get(centerTank.getFluid()));
                if (liquidWant == null) {
                    haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), centerTank.getFluid().amount);
                } else {
                    haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), liquidWant + centerTank.getFluid().amount);
                }
            }
        }
        FluidTank centerTank = ((PipeFluidTransportLogistics) transport).internalTank;
        if (centerTank != null && centerTank.getFluid() != null && wantFluids.containsKey(FluidIdentifier.get(centerTank.getFluid()))) {
            Integer liquidWant = haveFluids.get(FluidIdentifier.get(centerTank.getFluid()));
            if (liquidWant == null) {
                haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), centerTank.getFluid().amount);
            } else {
                haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), liquidWant + centerTank.getFluid().amount);
            }
        }
        for (Entry<FluidIdentifier, Integer> liquidId : wantFluids.entrySet()) {
            Integer haveCount = haveFluids.get(liquidId.getKey());
            if (haveCount != null) {
                liquidId.setValue(liquidId.getValue() - haveCount);
            }
            _requestedItems.entrySet().stream().filter(requestedItem -> requestedItem.getKey().equals(liquidId.getKey())).forEach(requestedItem -> liquidId.setValue(liquidId.getValue() - requestedItem.getValue()));
        }
        setRequestFailed(false);
        for (FluidIdentifier need : wantFluids.keySet()) {
            int countToRequest = wantFluids.get(need);
            if (countToRequest < 1) {
                continue;
            }
            if (_bucketMinimum.getAmount() != 0 && countToRequest < _bucketMinimum.getAmount()) {
                continue;
            }
            if (!useEnergy(11)) {
                break;
            }
            boolean success = false;
            if (_requestPartials) {
                countToRequest = RequestTree.requestFluidPartial(need, countToRequest, this, null);
                if (countToRequest > 0) {
                    success = true;
                }
            } else {
                success = RequestTree.requestFluid(need, countToRequest, this, null);
            }
            if (success) {
                Integer currentRequest = _requestedItems.get(need);
                if (currentRequest == null) {
                    _requestedItems.put(need, countToRequest);
                } else {
                    _requestedItems.put(need, currentRequest + countToRequest);
                }
            } else {
                setRequestFailed(true);
            }
        }
    });
}
Also used : IRequireReliableFluidTransport(logisticspipes.interfaces.routing.IRequireReliableFluidTransport) Getter(lombok.Getter) IRequestFluid(logisticspipes.interfaces.routing.IRequestFluid) Textures(logisticspipes.textures.Textures) LogisticsPipes(logisticspipes.LogisticsPipes) Item(net.minecraft.item.Item) HashMap(java.util.HashMap) PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) MainProxy(logisticspipes.proxy.MainProxy) FluidIdentifier(logisticspipes.utils.FluidIdentifier) Map(java.util.Map) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ConnectionPipeType(logisticspipes.routing.pathfinder.IPipeInformationProvider.ConnectionPipeType) ItemIdentifierInventory(logisticspipes.utils.item.ItemIdentifierInventory) TextureType(logisticspipes.textures.Textures.TextureType) PacketHandler(logisticspipes.network.PacketHandler) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) RequestTree(logisticspipes.request.RequestTree) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) FluidSupplierAmount(logisticspipes.network.packets.pipe.FluidSupplierAmount) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Pair(logisticspipes.utils.tuples.Pair) IInventory(net.minecraft.inventory.IInventory) Entry(java.util.Map.Entry) FluidTank(net.minecraftforge.fluids.FluidTank) GuiIDs(logisticspipes.network.GuiIDs) FluidRoutedPipe(logisticspipes.pipes.basic.fluid.FluidRoutedPipe) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) HashMap(java.util.HashMap) PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) FluidTank(net.minecraftforge.fluids.FluidTank) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) Pair(logisticspipes.utils.tuples.Pair)

Example 9 with FluidIdentifier

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

the class PipeItemsFluidSupplier method throttledUpdateEntity.

@Override
public void throttledUpdateEntity() {
    if (!isEnabled()) {
        return;
    }
    if (MainProxy.isClient(getWorld())) {
        return;
    }
    super.throttledUpdateEntity();
    //@formatter:off
    Iterator<IFluidHandler> iterator = new WorldCoordinatesWrapper(container).getConnectedAdjacentTileEntities().filter(adjacent -> adjacent.tileEntity instanceof IFluidHandler).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> (IFluidHandler) adjacent.tileEntity).iterator();
    while (iterator.hasNext()) {
        IFluidHandler next = iterator.next();
        FluidTankInfo[] result = next.getTankInfo(ForgeDirection.UNKNOWN);
        if (result == null || result.length == 0) {
            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 = FluidContainerRegistry.getFluidForFilledItem(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<>();
        for (FluidTankInfo slot : result) {
            if (slot == null || slot.fluid == null || slot.fluid.getFluidID() == 0 || !wantFluids.containsKey(FluidIdentifier.get(slot.fluid))) {
                continue;
            }
            Integer liquidWant = haveFluids.get(FluidIdentifier.get(slot.fluid));
            if (liquidWant == null) {
                haveFluids.put(FluidIdentifier.get(slot.fluid), slot.fluid.amount);
            } else {
                haveFluids.put(FluidIdentifier.get(slot.fluid), liquidWant + slot.fluid.amount);
            }
        }
        //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 = FluidContainerRegistry.getFluidForFilledItem(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) container.pipe).setRequestFailed(false);
        for (ItemIdentifier need : wantContainers.keySet()) {
            FluidStack requestedFluidId = FluidContainerRegistry.getFluidForFilledItem(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 : FluidContainerRegistry(net.minecraftforge.fluids.FluidContainerRegistry) LogisticsModule(logisticspipes.modules.abstractmodules.LogisticsModule) Textures(logisticspipes.textures.Textures) LogisticsPipes(logisticspipes.LogisticsPipes) Item(net.minecraft.item.Item) HashMap(java.util.HashMap) MainProxy(logisticspipes.proxy.MainProxy) IRequireReliableTransport(logisticspipes.interfaces.routing.IRequireReliableTransport) ItemStack(net.minecraft.item.ItemStack) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IRequestItems(logisticspipes.interfaces.routing.IRequestItems) Map(java.util.Map) LPTravelingItemServer(logisticspipes.transport.LPTravelingItem.LPTravelingItemServer) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Iterator(java.util.Iterator) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) ItemIdentifierInventory(logisticspipes.utils.item.ItemIdentifierInventory) TextureType(logisticspipes.textures.Textures.TextureType) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) RequestTree(logisticspipes.request.RequestTree) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) PipeTransportLogistics(logisticspipes.transport.PipeTransportLogistics) CacheTypes(logisticspipes.utils.CacheHolder.CacheTypes) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Entry(java.util.Map.Entry) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) GuiIDs(logisticspipes.network.GuiIDs) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) IRequestItems(logisticspipes.interfaces.routing.IRequestItems) ItemStack(net.minecraft.item.ItemStack)

Example 10 with FluidIdentifier

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

the class AETankHandler method getAvailableLiquid.

@SuppressWarnings("unused")
@Override
public Map<FluidIdentifier, Long> getAvailableLiquid(TileEntity tile) {
    Map<FluidIdentifier, Long> map = new HashMap<>();
    if (tile instanceof ITileStorageMonitorable) {
        ITileStorageMonitorable mon = (ITileStorageMonitorable) tile;
        if (mon == null) {
            return map;
        }
        for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
            MachineSource source = new MachineSource(new LPActionHost(((IGridHost) tile).getGridNode(dir)));
            IStorageMonitorable monitor = mon.getMonitorable(dir, source);
            if (monitor == null || monitor.getFluidInventory() == null) {
                continue;
            }
            IMEMonitor<IAEFluidStack> fluids = monitor.getFluidInventory();
            for (IAEFluidStack stack : fluids.getStorageList()) {
                if (SimpleServiceLocator.extraCellsProxy.canSeeFluidInNetwork(stack.getFluid())) {
                    map.put(FluidIdentifier.get(stack.getFluid(), stack.getTagCompound() != null ? stack.getTagCompound().getNBTTagCompoundCopy() : null, null), stack.getStackSize());
                }
            }
            return map;
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ITileStorageMonitorable(appeng.api.implementations.tiles.ITileStorageMonitorable) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) IGridHost(appeng.api.networking.IGridHost) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IStorageMonitorable(appeng.api.storage.IStorageMonitorable) MachineSource(appeng.api.networking.security.MachineSource) IAEFluidStack(appeng.api.storage.data.IAEFluidStack)

Aggregations

FluidIdentifier (logisticspipes.utils.FluidIdentifier)13 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)7 FluidStack (net.minecraftforge.fluids.FluidStack)7 IFluidHandler (net.minecraftforge.fluids.IFluidHandler)7 HashMap (java.util.HashMap)6 FluidTankInfo (net.minecraftforge.fluids.FluidTankInfo)6 TileEntity (net.minecraft.tileentity.TileEntity)5 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)4 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 LogisticsPipes (logisticspipes.LogisticsPipes)3 ISpecialTankAccessHandler (logisticspipes.interfaces.ISpecialTankAccessHandler)3 ISpecialTankHandler (logisticspipes.interfaces.ISpecialTankHandler)3 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)3 ItemStack (net.minecraft.item.ItemStack)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 TreeSet (java.util.TreeSet)2 GuiIDs (logisticspipes.network.GuiIDs)2 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)2 MainProxy (logisticspipes.proxy.MainProxy)2