Search in sources :

Example 1 with ISpecialTankHandler

use of logisticspipes.interfaces.ISpecialTankHandler 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);
    int amountToSend, attemptedAmount;
    amountToSend = attemptedAmount = Math.min(order.getAmount(), 5000);
    for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
        if (amountToSend <= 0) {
            break;
        }
        boolean fallback = true;
        if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
            ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
            if (handler instanceof ISpecialTankAccessHandler) {
                fallback = false;
                FluidStack drained = ((ISpecialTankAccessHandler) handler).drainFrom(pair.getValue1(), order.getFluid(), amountToSend, false);
                if (drained != null && drained.amount > 0 && order.getFluid().equals(FluidIdentifier.get(drained))) {
                    drained = ((ISpecialTankAccessHandler) handler).drainFrom(pair.getValue1(), order.getFluid(), amountToSend, true);
                    int amount = drained.amount;
                    amountToSend -= amount;
                    ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(drained);
                    IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
                    item.setDestination(order.getRouter().getSimpleID());
                    item.setTransportMode(TransportMode.Active);
                    this.queueRoutedItem(item, pair.getValue2());
                    getFluidOrderManager().sendSuccessfull(amount, false, item);
                    if (amountToSend <= 0) {
                        break;
                    }
                }
            }
        }
        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 (order.getFluid().equals(FluidIdentifier.get(liquid))) {
                            int amount = Math.min(liquid.amount, amountToSend);
                            FluidStack drained = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amount, false);
                            if (drained != null && drained.amount > 0 && order.getFluid().equals(FluidIdentifier.get(drained))) {
                                drained = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amount, true);
                                while (drained.amount < amountToSend) {
                                    FluidStack addition = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amountToSend - drained.amount, false);
                                    if (addition != null && addition.amount > 0 && order.getFluid().equals(FluidIdentifier.get(addition))) {
                                        addition = ((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), amountToSend - drained.amount, true);
                                        drained.amount += addition.amount;
                                    } else {
                                        break;
                                    }
                                }
                                amount = drained.amount;
                                amountToSend -= amount;
                                ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(drained);
                                IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
                                item.setDestination(order.getRouter().getSimpleID());
                                item.setTransportMode(TransportMode.Active);
                                this.queueRoutedItem(item, pair.getValue2());
                                getFluidOrderManager().sendSuccessfull(amount, false, item);
                                if (amountToSend <= 0) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (amountToSend >= attemptedAmount) {
        getFluidOrderManager().sendFailed();
    }
}
Also used : ISpecialTankHandler(logisticspipes.interfaces.ISpecialTankHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) FluidStack(net.minecraftforge.fluids.FluidStack) LogisticsFluidOrder(logisticspipes.routing.order.LogisticsFluidOrder) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack)

Example 2 with ISpecialTankHandler

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

the class PipeFluidProvider method getAvailableFluids.

@Override
public Map<FluidIdentifier, Integer> getAvailableFluids() {
    Map<FluidIdentifier, Integer> map = new HashMap<>();
    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> tmp = ((ISpecialTankAccessHandler) handler).getAvailableLiquid(pair.getValue1());
                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) {
            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) {
                        FluidIdentifier ident = FluidIdentifier.get(liquid);
                        if (((IFluidHandler) pair.getValue1()).canDrain(pair.getValue2().getOpposite(), liquid.getFluid())) {
                            if (((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), 1, false) != null) {
                                if (map.containsKey(ident)) {
                                    long addition = ((long) map.get(ident)) + tank.fluid.amount;
                                    map.put(ident, addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
                                } else {
                                    map.put(ident, tank.fluid.amount);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    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 : ISpecialTankHandler(logisticspipes.interfaces.ISpecialTankHandler) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) ISpecialTankAccessHandler(logisticspipes.interfaces.ISpecialTankAccessHandler) FluidIdentifier(logisticspipes.utils.FluidIdentifier) IFluidHandler(net.minecraftforge.fluids.IFluidHandler) TileEntity(net.minecraft.tileentity.TileEntity) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 3 with ISpecialTankHandler

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

use of logisticspipes.interfaces.ISpecialTankHandler 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)

Aggregations

ISpecialTankAccessHandler (logisticspipes.interfaces.ISpecialTankAccessHandler)4 ISpecialTankHandler (logisticspipes.interfaces.ISpecialTankHandler)4 TileEntity (net.minecraft.tileentity.TileEntity)4 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)4 FluidStack (net.minecraftforge.fluids.FluidStack)4 FluidTankInfo (net.minecraftforge.fluids.FluidTankInfo)4 IFluidHandler (net.minecraftforge.fluids.IFluidHandler)4 FluidIdentifier (logisticspipes.utils.FluidIdentifier)3 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)1 FluidResource (logisticspipes.request.resources.FluidResource)1 FluidLogisticsPromise (logisticspipes.routing.FluidLogisticsPromise)1 LogisticsFluidOrder (logisticspipes.routing.order.LogisticsFluidOrder)1 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)1 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)1