[QB] Adding qb-crafting crafting in Base Building

This snippet will help you setup crafting from qb-crafting as events to use on other scripts like hrs_base_building

In qb-crafting

qb-crafting\client.lua

  • OLD CODE:

local function OpenCraftingMenu(benchType)
    local PlayerData = QBCore.Functions.GetPlayerData()
    local xpType = benchType == 'item_bench' and Config.item_bench.xpType or Config.attachment_bench.xpType
    local recipes = benchType == 'item_bench' and Config.item_bench.recipes or Config.attachment_bench.recipes
    local currentXP = PlayerData.metadata[xpType]

    QBCore.Functions.TriggerCallback('crafting:getPlayerInventory', function(inventory)
        local craftableItems = {}
        local nonCraftableItems = {}
        for _, recipe in pairs(recipes) do
            if currentXP >= recipe.xpRequired then
                local canCraft = true
                local itemsText = ''
                for _, reqItem in pairs(recipe.requiredItems) do
                    local hasItem = false
                    for _, invItem in pairs(inventory) do
                        if invItem.name == reqItem.item and invItem.amount >= reqItem.amount then
                            hasItem = true
                            break
                        end
                    end
                    local itemLabel = QBCore.Shared.Items[reqItem.item].label
                    itemsText = itemsText .. ' x' .. tostring(reqItem.amount) .. ' ' .. itemLabel .. '<br>'
                    if not hasItem then
                        canCraft = false
                    end
                end
                itemsText = string.sub(itemsText, 1, -5)
                local menuItem = {
                    header = QBCore.Shared.Items[recipe.item].label,
                    txt = itemsText,
                    icon = Config.ImageBasePath .. QBCore.Shared.Items[recipe.item].image,
                    params = {
                        isAction = true,
                        event = function()
                            CraftAmount(recipe.item, recipe.requiredItems, recipe.xpGain, xpType)
                        end,
                        args = {}
                    },
                    disabled = not canCraft
                }
                if canCraft then
                    craftableItems[#craftableItems + 1] = menuItem
                else
                    nonCraftableItems[#nonCraftableItems + 1] = menuItem
                end
            end
        end
        local menuItems = {
            {
                header = string.format(Lang:t('menus.header')),
                icon = 'fas fa-drafting-compass',
                isMenuHeader = true,
            }
        }
        for _, item in ipairs(craftableItems) do
            menuItems[#menuItems + 1] = item
        end
        for _, item in ipairs(nonCraftableItems) do
            menuItems[#menuItems + 1] = item
        end
        exports['qb-menu']:openMenu(menuItems)
    end)
end
  • NEW CODE:

  • INSERT THIS NEW EVENT AT THE BOTTOM OF THE FILE:

In HRS_base_building

hrs_base_building\config.lua

Last updated