Search in sources :

Example 1 with IRequestItems

use of logisticspipes.interfaces.routing.IRequestItems in project LogisticsPipes by RS485.

the class ModuleCrafter method addCrafting.

@Override
public ICraftingTemplate addCrafting(IResource toCraft) {
    List<ItemIdentifierStack> stack = getCraftedItems();
    if (stack == null) {
        return null;
    }
    IReqCraftingTemplate template = null;
    if (getUpgradeManager().isFuzzyUpgrade() && outputFuzzy().nextSetBit(0) != -1) {
        for (ItemIdentifierStack craftable : stack) {
            DictResource dict = new DictResource(craftable, null);
            dict.loadFromBitSet(outputFuzzy().copyValue());
            if (toCraft.matches(dict, IResource.MatchSettings.NORMAL)) {
                template = new DictCraftingTemplate(dict, this, priority.getValue());
                break;
            }
        }
    } else {
        for (ItemIdentifierStack craftable : stack) {
            if (toCraft.matches(craftable.getItem(), IResource.MatchSettings.NORMAL)) {
                template = new ItemCraftingTemplate(craftable, this, priority.getValue());
                break;
            }
        }
    }
    if (template == null) {
        return null;
    }
    IRequestItems[] target = new IRequestItems[9];
    for (int i = 0; i < 9; i++) {
        target[i] = this;
    }
    if (!isSatelliteConnected()) {
        // has a satellite configured and that one is unreachable
        return null;
    }
    if (!getUpgradeManager().isAdvancedSatelliteCrafter()) {
        IRouter r = getSatelliteRouter(-1);
        if (r != null) {
            IRequestItems sat = r.getPipe();
            for (int i = 6; i < 9; i++) {
                target[i] = sat;
            }
        }
    } else {
        for (int i = 0; i < 9; i++) {
            IRouter r = getSatelliteRouter(i);
            if (r != null) {
                target[i] = r.getPipe();
            }
        }
    }
    // Check all materials
    for (int i = 0; i < 9; i++) {
        ItemIdentifierStack resourceStack = dummyInventory.getIDStackInSlot(i);
        if (resourceStack == null || resourceStack.getStackSize() == 0) {
            continue;
        }
        IResource req;
        if (getUpgradeManager().isFuzzyUpgrade() && inputFuzzy(i).nextSetBit(0) != -1) {
            DictResource dict;
            req = dict = new DictResource(resourceStack, target[i]);
            dict.loadFromBitSet(inputFuzzy(i).copyValue());
        } else {
            req = new ItemResource(resourceStack, target[i]);
        }
        template.addRequirement(req, new CraftingChassisInformation(i, getPositionInt()));
    }
    int liquidCrafter = getUpgradeManager().getFluidCrafter();
    IRequestFluid[] liquidTarget = new IRequestFluid[liquidCrafter];
    if (!getUpgradeManager().isAdvancedSatelliteCrafter()) {
        IRouter r = getFluidSatelliteRouter(-1);
        if (r != null) {
            IRequestFluid sat = (IRequestFluid) r.getPipe();
            for (int i = 0; i < liquidCrafter; i++) {
                liquidTarget[i] = sat;
            }
        }
    } else {
        for (int i = 0; i < liquidCrafter; i++) {
            IRouter r = getFluidSatelliteRouter(i);
            if (r != null) {
                liquidTarget[i] = (IRequestFluid) r.getPipe();
            }
        }
    }
    for (int i = 0; i < liquidCrafter; i++) {
        FluidIdentifier liquid = getFluidMaterial(i);
        int amount = liquidAmounts.get(i);
        if (liquid == null || amount <= 0 || liquidTarget[i] == null) {
            continue;
        }
        template.addRequirement(new FluidResource(liquid, amount, liquidTarget[i]), null);
    }
    if (getUpgradeManager().hasByproductExtractor() && getByproductItem() != null) {
        template.addByproduct(getByproductItem());
    }
    return template;
}
Also used : DictResource(logisticspipes.request.resources.DictResource) DictCraftingTemplate(logisticspipes.request.DictCraftingTemplate) IReqCraftingTemplate(logisticspipes.request.IReqCraftingTemplate) FluidIdentifier(logisticspipes.utils.FluidIdentifier) ItemResource(logisticspipes.request.resources.ItemResource) FluidResource(logisticspipes.request.resources.FluidResource) IRouter(logisticspipes.routing.IRouter) IRequestFluid(logisticspipes.interfaces.routing.IRequestFluid) ItemCraftingTemplate(logisticspipes.request.ItemCraftingTemplate) IRequestItems(logisticspipes.interfaces.routing.IRequestItems) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) IResource(logisticspipes.request.resources.IResource)

Example 2 with IRequestItems

use of logisticspipes.interfaces.routing.IRequestItems 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

IRequestItems (logisticspipes.interfaces.routing.IRequestItems)2 FluidIdentifier (logisticspipes.utils.FluidIdentifier)2 HashMap (java.util.HashMap)1 ITankUtil (logisticspipes.interfaces.ITankUtil)1 IRequestFluid (logisticspipes.interfaces.routing.IRequestFluid)1 DictCraftingTemplate (logisticspipes.request.DictCraftingTemplate)1 IReqCraftingTemplate (logisticspipes.request.IReqCraftingTemplate)1 ItemCraftingTemplate (logisticspipes.request.ItemCraftingTemplate)1 DictResource (logisticspipes.request.resources.DictResource)1 FluidResource (logisticspipes.request.resources.FluidResource)1 IResource (logisticspipes.request.resources.IResource)1 ItemResource (logisticspipes.request.resources.ItemResource)1 IRouter (logisticspipes.routing.IRouter)1 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)1 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 FluidStack (net.minecraftforge.fluids.FluidStack)1 NeighborTileEntity (network.rs485.logisticspipes.connection.NeighborTileEntity)1