2025-03-24 14:09:19 -04:00
ItemsBuilder = ItemsBuilder or {}
ItemsBuilder = {}
2025-04-02 07:27:13 -04:00
---This will generate the items in the formats of qb_core, qb_core_old and ox_inventory. It will then build a lua file in the generateditems folder of the community_bridge.
---@param invoking string
---@param itemsTable table
2025-04-21 01:48:27 -05:00
ItemsBuilder.Generate = function ( invoking , outputPrefix , itemsTable , useQB )
if not itemsTable then return end
invoking = invoking or GetInvokingResource () or GetCurrentResourceName () or "community_bridge"
2025-03-24 14:09:19 -04:00
2025-04-21 01:48:27 -05:00
local resourcePath = string.format ( "%s/%s/" , GetResourcePath ( invoking ), outputPrefix or "generated_items" )
2025-03-24 14:09:19 -04:00
2025-04-21 01:48:27 -05:00
-- remove any doubles slashes after initial double slash
-- resourcePath = resourcePath:gsub("//", "/")
-- check if directory exists
local folder = io.open ( resourcePath , "r" )
if not folder then
local createDirectoryCMD = string.format ( "if not exist \" %s \" mkdir \" %s \" " , resourcePath , resourcePath )
local returned , err = io.popen ( createDirectoryCMD )
if not returned then
print ( "🌮 Failed to create directory: " , err )
return
2025-03-24 14:09:19 -04:00
end
2025-04-21 01:48:27 -05:00
returned : close ()
print ( "🌮 Created Directory: " , resourcePath )
else
folder : close ()
2025-03-24 14:09:19 -04:00
end
local qbOld = {}
local qbNew = {}
local oxInventory = {}
2025-04-21 01:48:27 -05:00
if useQB then
for key , item in pairs ( itemsTable ) do
qbOld [ key ] = string.format (
"['%s'] = {name = '%s', label = '%s', weight = %s, type = 'item', image = '%s', unique = %s, useable = %s, shouldClose = %s, description = '%s'}," ,
key , key , item.label , item.weight , item.image or key .. 'png' , item.unique , item.useable , item.shouldClose , item.description
)
qbNew [ key ] = string.format (
"['%s'] = {['name'] = '%s', ['label'] = '%s', ['weight'] = %s, ['type'] = 'item',['image'] = '%s', ['unique'] = %s, ['useable'] = %s, ['shouldClose'] = %s, ['description'] = '%s'}," ,
key , key , item.label , item.weight , item.image or key .. 'png' , item.unique , item.useable , item.shouldClose , item.description
)
2025-04-21 02:04:01 -05:00
imagewithoutpng = item ? . image and item.image : gsub ( ".png" , "" )
shouldRenderImage = imagewithoutpng and imagewithoutpng ~= key
2025-04-21 01:48:27 -05:00
oxInventory [ key ] = string.format (
[[
["%s"] = {
label = "%s",
description = "%s",
weight = %s,
stack = %s,
close = %s,
%s
}, ]] ,
key , item.label , item.description , item.weight , not item.unique , item.shouldClose ,
2025-04-21 02:04:01 -05:00
shouldRenderImage and item.image and string.format ( [[client = {
image = '%s'
}]] , item.image ) or ""
2025-04-21 01:48:27 -05:00
)
end
2025-03-24 14:09:19 -04:00
2025-04-21 01:48:27 -05:00
else
for key , item in pairs ( itemsTable ) do
-- ['peanut_butter'] = {['name'] = 'peanut_butter',['label'] = 'Peanut Butter',['weight'] = 1000,['type'] = 'item',['image'] = 'peanut_butter.png',['unique'] = false,['useable'] = false,['shouldClose'] = true,['combinable'] = nil,['description'] = 'A cooking ingredient'},
qbOld [ key ] = string.format (
"['%s'] = {name = '%s', label = '%s', weight = %s, type = 'item', image = '%s', unique = %s, useable = %s, shouldClose = %s, description = '%s'}," ,
key , key , item.label , item.weight , item ? . client ? . image or key .. 'png' , not item.stack , true , item.close , item.description
)
qbNew [ key ] = string.format (
"['%s'] = {['name'] = '%s', ['label'] = '%s', ['weight'] = %s, ['type'] = 'item', ['image'] = '%s', ['unique'] = %s, ['useable'] = %s, ['shouldClose'] = %s, ['description'] = '%s'}," ,
key , key , item.label , item.weight , item ? . client ? . image or key .. 'png' , not item.stack , true , item.close , item.description
)
2025-04-21 02:04:01 -05:00
imagewithoutpng = item ? . client ? . image and item.client . image : gsub ( ".png" , "" )
shouldRenderImage = imagewithoutpng and imagewithoutpng ~= key
2025-04-21 01:48:27 -05:00
oxInventory [ key ] = string.format (
[[
["%s"] = {
label = "%s",
description = "%s",
weight = %s,
stack = %s,
close = %s,
%s
}, ]] ,
key , item.label , item.description , item.weight , item.stack , item.close ,
2025-04-21 02:04:01 -05:00
shouldRenderImage and item ? . client ? . image and string.format ( [[client = {
image = '%s'
}]] , item.client . image ) or ""
2025-04-21 01:48:27 -05:00
)
2025-03-24 14:09:19 -04:00
end
end
2025-04-21 01:48:27 -05:00
local function write ( fileName , content )
2025-03-24 14:09:19 -04:00
local filePath = resourcePath .. fileName
local file = io.open ( filePath , "w" )
if file then
2025-04-21 01:48:27 -05:00
for key , value in pairs ( content ) do
file : write ( string.format ( "%s \n " , value ))
2025-03-24 14:09:19 -04:00
end
file : close ()
print ( "🌮 Items File Created: " .. filePath )
else
print ( "🌮 Something Broke for: " .. filePath )
end
end
2025-04-21 01:48:27 -05:00
write ( invoking .. "(" .. "qb_core_old).lua" , qbOld )
write ( invoking .. "(" .. "qb_core_new).lua" , qbNew )
write ( invoking .. "(" .. "ox_inventory).lua" , oxInventory )
2025-03-24 14:09:19 -04:00
end
exports ( 'ItemsBuilder' , ItemsBuilder )
return ItemsBuilder