Search in sources :

Example 11 with NeighborTileEntity

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

the class FluidRoutedPipe method enabledUpdateEntity.

@Override
public void enabledUpdateEntity() {
    super.enabledUpdateEntity();
    if (canInsertFromSideToTanks()) {
        int validDirections = 0;
        final List<Pair<NeighborTileEntity<TileEntity>, ITankUtil>> list = PipeFluidUtil.INSTANCE.getAdjacentTanks(this, true);
        for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : list) {
            if (pair.getValue2() instanceof LogisticsTileGenericPipe) {
                if (((LogisticsTileGenericPipe) pair.getValue2()).pipe instanceof CoreRoutedPipe) {
                    continue;
                }
            }
            FluidTank internalTank = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue1().getDirection().ordinal()];
            validDirections++;
            if (internalTank.getFluid() == null) {
                continue;
            }
            int filled = pair.getValue2().fill(FluidIdentifierStack.getFromStack(internalTank.getFluid()), true);
            if (filled == 0) {
                continue;
            }
            FluidStack drain = internalTank.drain(filled, true);
            if (drain == null || filled != drain.amount) {
                if (LogisticsPipes.isDEBUG()) {
                    throw new UnsupportedOperationException("Fluid Multiplication");
                }
            }
        }
        if (validDirections == 0) {
            return;
        }
        FluidTank tank = ((PipeFluidTransportLogistics) transport).internalTank;
        FluidStack stack = tank.getFluid();
        if (stack == null) {
            return;
        }
        for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : list) {
            if (pair.getValue1().isLogisticsPipe()) {
                if (((LogisticsTileGenericPipe) pair.getValue1().getTileEntity()).pipe instanceof CoreRoutedPipe) {
                    continue;
                }
            }
            FluidTank tankSide = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue1().getDirection().ordinal()];
            stack = tank.getFluid();
            if (stack == null) {
                continue;
            }
            stack = stack.copy();
            int filled = tankSide.fill(stack, true);
            if (filled == 0) {
                continue;
            }
            FluidStack drain = tank.drain(filled, true);
            if (drain == null || filled != drain.amount) {
                if (LogisticsPipes.isDEBUG()) {
                    throw new UnsupportedOperationException("Fluid Multiplication");
                }
            }
        }
    }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) FluidTank(net.minecraftforge.fluids.FluidTank) ITankUtil(logisticspipes.interfaces.ITankUtil) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) Pair(logisticspipes.utils.tuples.Pair)

Example 12 with NeighborTileEntity

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

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

Example 14 with NeighborTileEntity

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

the class PipeLogisticsChassis method nextOrientation.

@Override
public void nextOrientation() {
    final SingleAdjacent pointedAdjacent = this.pointedAdjacent;
    Pair<NeighborTileEntity<TileEntity>, ConnectionType> newNeighbor;
    if (pointedAdjacent == null) {
        newNeighbor = nextPointedOrientation(null);
    } else {
        newNeighbor = nextPointedOrientation(pointedAdjacent.getDir());
    }
    final ChassisOrientationPacket packet = PacketHandler.getPacket(ChassisOrientationPacket.class);
    if (newNeighbor == null) {
        this.pointedAdjacent = null;
        packet.setDir(null);
    } else {
        this.pointedAdjacent = new SingleAdjacent(this, newNeighbor.getValue1().getDirection(), newNeighbor.getValue2());
        packet.setDir(newNeighbor.getValue1().getDirection());
    }
    MainProxy.sendPacketToAllWatchingChunk(_module, packet.setTilePos(container));
    refreshRender(true);
}
Also used : RequestChassisOrientationPacket(logisticspipes.network.packets.pipe.RequestChassisOrientationPacket) ChassisOrientationPacket(logisticspipes.network.packets.pipe.ChassisOrientationPacket) ConnectionType(network.rs485.logisticspipes.connection.ConnectionType) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) SingleAdjacent(network.rs485.logisticspipes.connection.SingleAdjacent)

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