Search in sources :

Example 6 with NeighborTileEntity

use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.

the class PipeFluidProvider method collectSpecificInterests.

@Override
public // work in progress, currently not active code.
void collectSpecificInterests(@Nonnull Collection<ItemIdentifier> itemidCollection) {
    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();
            handler.getAvailableLiquid(tile).keySet().stream().map(FluidIdentifier::getItemIdentifier).forEach(itemidCollection::add);
        }
        if (fallback) {
            if (pair.getValue2().containsTanks()) {
                pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(liquid -> {
                    if (liquid != null && liquid.getFluid() != null) {
                        if (pair.getValue2().canDrain(liquid.getFluid())) {
                            if (pair.getValue2().drain(1, false) != null) {
                                FluidIdentifier ident = liquid.getFluid();
                                itemidCollection.add(ident.getItemIdentifier());
                            }
                        }
                    }
                });
            }
        }
    }
}
Also used : NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) 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) ITankUtil(logisticspipes.interfaces.ITankUtil) ISpecialTankUtil(logisticspipes.interfaces.ISpecialTankUtil) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier)

Example 7 with NeighborTileEntity

use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.

the class PipeFluidProvider method getAvailableFluids.

@Override
public Map<FluidIdentifier, Integer> getAvailableFluids() {
    Map<FluidIdentifier, Integer> map = new HashMap<>();
    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> tmp = handler.getAvailableLiquid(tile);
            for (Entry<FluidIdentifier, Long> entry : tmp.entrySet()) {
                if (map.containsKey(entry.getKey())) {
                    long addition = ((long) map.get(entry.getKey())) + entry.getValue();
                    map.put(entry.getKey(), addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
                } else {
                    map.put(entry.getKey(), entry.getValue() > Integer.MAX_VALUE ? Integer.MAX_VALUE : entry.getValue().intValue());
                }
            }
        }
        if (fallback) {
            if (pair.getValue2().containsTanks()) {
                pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(liquid -> {
                    if (liquid != null && liquid.getFluid() != null) {
                        FluidIdentifier ident = liquid.getFluid();
                        if (pair.getValue2().canDrain(ident)) {
                            if (pair.getValue2().drain(ident.makeFluidIdentifierStack(1), false) != null) {
                                if (map.containsKey(ident)) {
                                    long addition = ((long) map.get(ident)) + liquid.getAmount();
                                    map.put(ident, addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
                                } else {
                                    map.put(ident, liquid.getAmount());
                                }
                            }
                        }
                    }
                });
            }
        }
    }
    Map<FluidIdentifier, Integer> result = new HashMap<>();
    // Reduce what has been reserved, add.
    for (Entry<FluidIdentifier, Integer> fluid : map.entrySet()) {
        int remaining = fluid.getValue() - getFluidOrderManager().totalFluidsCountInOrders(fluid.getKey());
        if (remaining < 1) {
            continue;
        }
        result.put(fluid.getKey(), remaining);
    }
    return result;
}
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) HashMap(java.util.HashMap) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ITankUtil(logisticspipes.interfaces.ITankUtil) ISpecialTankUtil(logisticspipes.interfaces.ISpecialTankUtil) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity)

Example 8 with NeighborTileEntity

use of network.rs485.logisticspipes.connection.NeighborTileEntity 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 9 with NeighborTileEntity

use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.

the class LogisticsPowerProviderTileEntity method update.

@Override
public void update() {
    super.update();
    pauseRequesting = false;
    if (!init) {
        if (MainProxy.isClient(getWorld())) {
            LogisticsHUDRenderer.instance().add(this);
        }
        init = true;
    }
    double globalRequest = orders.values().stream().reduce(Double::sum).orElse(0.0);
    if (globalRequest > 0) {
        final double fullfillRatio = Math.min(1, Math.min(internalStorage, getMaxProvidePerTick()) / globalRequest);
        if (fullfillRatio > 0) {
            final Function<NeighborTileEntity<LogisticsTileGenericPipe>, CoreRoutedPipe> getPipe = (NeighborTileEntity<LogisticsTileGenericPipe> neighbor) -> (CoreRoutedPipe) neighbor.getTileEntity().pipe;
            orders.entrySet().stream().map(routerIdToOrderCount -> new Pair<>(SimpleServiceLocator.routerManager.getRouter(routerIdToOrderCount.getKey()), Math.min(internalStorage, routerIdToOrderCount.getValue() * fullfillRatio))).filter(destinationToPower -> destinationToPower.getValue1() != null && destinationToPower.getValue1().getPipe() != null).forEach(destinationToPower -> new WorldCoordinatesWrapper(this).allNeighborTileEntities().stream().flatMap(neighbor -> LPNeighborTileEntityKt.optionalIs(neighbor, LogisticsTileGenericPipe.class).map(Stream::of).orElseGet(Stream::empty)).filter(neighbor -> neighbor.getTileEntity().pipe instanceof CoreRoutedPipe && !getPipe.apply(neighbor).stillNeedReplace()).flatMap(neighbor -> getPipe.apply(neighbor).getRouter().getDistanceTo(destinationToPower.getValue1()).stream().map(exitRoute -> new Pair<>(neighbor, exitRoute))).filter(neighborToExit -> neighborToExit.getValue2().containsFlag(PipeRoutingConnectionType.canPowerSubSystemFrom) && neighborToExit.getValue2().filters.stream().noneMatch(IFilter::blockPower)).findFirst().ifPresent(neighborToSource -> {
                CoreRoutedPipe sourcePipe = getPipe.apply(neighborToSource.getValue1());
                if (sourcePipe.isInitialized()) {
                    sourcePipe.container.addLaser(neighborToSource.getValue1().getOurDirection(), 1, getLaserColor(), true, true);
                }
                sendPowerLaserPackets(sourcePipe.getRouter(), destinationToPower.getValue1(), neighborToSource.getValue2().exitOrientation, neighborToSource.getValue2().exitOrientation != neighborToSource.getValue1().getDirection());
                internalStorage -= destinationToPower.getValue2();
                // because calculations with floats
                if (internalStorage <= 0)
                    internalStorage = 0;
                handlePower(destinationToPower.getValue1().getPipe(), destinationToPower.getValue2());
            }));
        }
    }
    orders.clear();
    if (MainProxy.isServer(world)) {
        if (internalStorage != lastUpdateStorage) {
            updateClients();
            lastUpdateStorage = internalStorage;
        }
    }
}
Also used : PowerProviderGui(logisticspipes.network.guis.block.PowerProviderGui) IRouter(logisticspipes.routing.IRouter) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) LogisticsPipes(logisticspipes.LogisticsPipes) LogisticsSolidTileEntity(logisticspipes.blocks.LogisticsSolidTileEntity) HashMap(java.util.HashMap) CCType(logisticspipes.proxy.computers.interfaces.CCType) MainProxy(logisticspipes.proxy.MainProxy) Function(java.util.function.Function) IHeadUpDisplayRenderer(logisticspipes.interfaces.IHeadUpDisplayRenderer) HUDStartBlockWatchingPacket(logisticspipes.network.packets.hud.HUDStartBlockWatchingPacket) PlayerCollectionList(logisticspipes.utils.PlayerCollectionList) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) IHeadUpDisplayBlockRendererProvider(logisticspipes.interfaces.IHeadUpDisplayBlockRendererProvider) ISubSystemPowerProvider(logisticspipes.interfaces.ISubSystemPowerProvider) CrashReportCategory(net.minecraft.crash.CrashReportCategory) LogisticsHUDRenderer(logisticspipes.renderer.LogisticsHUDRenderer) Map(java.util.Map) IBlockWatchingHandler(logisticspipes.interfaces.IBlockWatchingHandler) LPNeighborTileEntityKt(network.rs485.logisticspipes.connection.LPNeighborTileEntityKt) LinkedList(java.util.LinkedList) CoordinatesGuiProvider(logisticspipes.network.abstractguis.CoordinatesGuiProvider) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NewGuiHandler(logisticspipes.network.NewGuiHandler) PipeRoutingConnectionType(logisticspipes.routing.PipeRoutingConnectionType) HUDStopBlockWatchingPacket(logisticspipes.network.packets.hud.HUDStopBlockWatchingPacket) IGuiOpenControler(logisticspipes.interfaces.IGuiOpenControler) EnumFacing(net.minecraft.util.EnumFacing) Triplet(logisticspipes.utils.tuples.Triplet) PacketHandler(logisticspipes.network.PacketHandler) ExitRoute(logisticspipes.routing.ExitRoute) PowerProviderLevel(logisticspipes.network.packets.block.PowerProviderLevel) ServerRouter(logisticspipes.routing.ServerRouter) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) Stream(java.util.stream.Stream) IFilter(logisticspipes.interfaces.routing.IFilter) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Pair(logisticspipes.utils.tuples.Pair) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) IGuiTileEntity(logisticspipes.interfaces.IGuiTileEntity) IPowerLevelDisplay(logisticspipes.interfaces.IPowerLevelDisplay) BitSet(java.util.BitSet) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) HUDPowerLevel(logisticspipes.gui.hud.HUDPowerLevel) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) Stream(java.util.stream.Stream) Pair(logisticspipes.utils.tuples.Pair)

Example 10 with NeighborTileEntity

use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.

the class PowerSupplierHandler method requestICPower.

private void requestICPower() {
    // Use Buffer
    final List<LPNeighborTileEntity<TileEntity>> adjacentTileEntities = new WorldCoordinatesWrapper(pipe.container).allNeighborTileEntities();
    double globalNeed = 0;
    double[] need = new double[adjacentTileEntities.size()];
    int i = 0;
    for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
        if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity())) {
            if (pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection())) {
                if (SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
                    // TODO pipe.container must be IEnergySource
                    globalNeed += need[i] = SimpleServiceLocator.IC2Proxy.demandedEnergyUnits(adjacent.getTileEntity());
                }
            }
        }
        ++i;
    }
    if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
        double fullfillable = Math.min(1, internalBufferIC2 / globalNeed);
        i = 0;
        for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
            if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity()) && pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection()) && SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
                // TODO pipe.container must be IEnergySource
                if (internalBufferIC2 + 1 < need[i] * fullfillable) {
                    return;
                }
                double toUse = Math.min(pipe.getUpgradeManager().getIC2PowerLevel(), need[i] * fullfillable);
                double unUsed = SimpleServiceLocator.IC2Proxy.injectEnergyUnits(adjacent.getTileEntity(), adjacent.getOurDirection(), toUse);
                double used = toUse - unUsed;
                if (used > 0) {
                    // MainProxy.sendPacketToAllWatchingChunk(this.pipe.getX(), this.pipe.getZ(), MainProxy.getDimensionForWorld(this.pipe.getWorld()), PacketHandler.getPacket(PowerPacketLaser.class).setColor(LogisticsPowerProviderTileEntity.IC2_COLOR).setPos(this.pipe.getLPPosition()).setRenderBall(true).setDir(adTile.orientation).setLength(0.5F));
                    pipe.container.addLaser(adjacent.getDirection(), 0.5F, LogisticsPowerProviderTileEntity.IC2_COLOR, false, true);
                    internalBufferIC2 -= used;
                }
                if (internalBufferIC2 < 0) {
                    internalBufferIC2 = 0;
                    return;
                }
            }
            ++i;
        }
    }
    // Rerequest Buffer
    List<Pair<ISubSystemPowerProvider, List<IFilter>>> provider = pipe.getRouter().getSubSystemPowerProvider();
    double available = 0;
    outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
        for (IFilter filter : pair.getValue2()) {
            if (filter.blockPower()) {
                continue outer;
            }
        }
        if (pair.getValue1().usePaused()) {
            continue;
        }
        if (!pair.getValue1().getBrand().equals("EU")) {
            continue;
        }
        available += pair.getValue1().getPowerLevel();
    }
    if (available > 0) {
        double neededPower = PowerSupplierHandler.INTERNAL_IC2_BUFFER_MAX - internalBufferIC2;
        if (neededPower > 0) {
            if (pipe.useEnergy((int) (neededPower / 10000), false)) {
                outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
                    for (IFilter filter : pair.getValue2()) {
                        if (filter.blockPower()) {
                            continue outer;
                        }
                    }
                    if (pair.getValue1().usePaused()) {
                        continue;
                    }
                    if (!pair.getValue1().getBrand().equals("EU")) {
                        continue;
                    }
                    double requestamount = neededPower * (pair.getValue1().getPowerLevel() / available);
                    pair.getValue1().requestPower(pipe.getRouterId(), requestamount);
                }
            }
        }
    }
}
Also used : LogisticsPowerProviderTileEntity(logisticspipes.blocks.powertile.LogisticsPowerProviderTileEntity) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) LPNeighborTileEntity(network.rs485.logisticspipes.connection.LPNeighborTileEntity) IFilter(logisticspipes.interfaces.routing.IFilter) LPNeighborTileEntity(network.rs485.logisticspipes.connection.LPNeighborTileEntity) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) Pair(logisticspipes.utils.tuples.Pair)

Aggregations

NeighborTileEntity (network.rs485.logisticspipes.connection.NeighborTileEntity)14 TileEntity (net.minecraft.tileentity.TileEntity)11 Pair (logisticspipes.utils.tuples.Pair)9 IFilter (logisticspipes.interfaces.routing.IFilter)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 ITankUtil (logisticspipes.interfaces.ITankUtil)7 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)7 Nonnull (javax.annotation.Nonnull)6 FluidIdentifier (logisticspipes.utils.FluidIdentifier)6 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)6 FluidStack (net.minecraftforge.fluids.FluidStack)6 Collection (java.util.Collection)5 IAdditionalTargetInformation (logisticspipes.interfaces.routing.IAdditionalTargetInformation)5 IRequestFluid (logisticspipes.interfaces.routing.IRequestFluid)5 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)5 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)5 WorldCoordinatesWrapper (network.rs485.logisticspipes.world.WorldCoordinatesWrapper)5 Entry (java.util.Map.Entry)4