[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:
local function OpenCraftingMenu(benchType)
local PlayerData = QBCore.Functions.GetPlayerData()
if not Config[benchType] then
print("No Crafting "..benchType.." defined")
return
end
local xpType = Config[benchType].xpType
local recipes = Config[benchType].recipes
local currentXP = PlayerData.metadata[xpType] or 0
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
INSERT NEW CODE AT THE BOTTOM OF THE FILE:
RegisterNetEvent('inventory:client:craftTarget',function(benchType)
OpenCraftingMenu(benchType)
end)
In HRS_base_building
hrs_base_building\config.lua
["prop_tool_bench02_ld"] = {
item = "prop_tool_bench02_ld",
life = 10000.0,
type = "crafting",
subtype = "findGroud",
noFoundationNeed = true,
TriggerEvent = {
type = "client",
event = "inventory:client:craftTarget",
args = {'item_bench'}, --- agrs in order
entityAsArg = "hrs_base_entity" --- in the arguments, this word will be replaced by the Entity
},
crafting = {
{name = "wood",count = 20}
}
},
["gr_prop_gr_bench_02a"] = {
item = "gr_prop_gr_bench_02a",
life = 20000.0,
type = "crafting",
subtype = "findGroud",
noFoundationNeed = true,
TriggerEvent = {
type = "client",
event = "inventory:client:craftTarget",
args = {'attachment_bench'}, --- agrs in order
entityAsArg = "hrs_base_entity" --- in the arguments, this word will be replaced by the Entity
},
crafting = {
{name = "wood",count = 20},
{name = "metalscrap",count = 20}
}
},
Last updated