2025-05-09 02:00:02 -04:00
---@diagnostic disable: duplicate-set-field
2025-12-14 16:22:35 -05:00
if GetResourceState ( 'codem-inventory' ) == 'missing' then return end
2025-01-10 16:36:20 -05:00
2025-02-22 20:25:35 -05:00
Inventory = Inventory or {}
2025-05-19 07:20:23 -05:00
Inventory.Stashes = Inventory.Stashes or {}
2025-02-22 20:25:35 -05:00
2025-01-31 19:15:33 -05:00
local codem = exports [ 'codem-inventory' ]
2025-12-14 16:22:35 -05:00
---@description This will get the name of the in use resource.
---@return string
Inventory.GetResourceName = function ()
return "codem-inventory"
end
---@description This will add an item, and return true or false based on success
2025-05-09 06:03:09 -04:00
---@param src number
---@param item string
---@param count number
2025-12-14 16:22:35 -05:00
---@param slot number (optional)
---@param metadata table (optional)
2025-05-09 06:03:09 -04:00
---@return boolean
Inventory.AddItem = function ( src , item , count , slot , metadata )
2025-12-14 16:22:35 -05:00
local success = codem : AddItem ( src , item , count , slot , metadata )
if not success then return false end
2025-05-09 06:03:09 -04:00
TriggerClientEvent ( "community_bridge:client:inventory:updateInventory" , src , { action = "add" , item = item , count = count , slot = slot , metadata = metadata })
2025-12-14 16:22:35 -05:00
return success
2025-05-09 06:03:09 -04:00
end
2025-12-14 16:22:35 -05:00
---@description Internal function to search for items with specific metadata in player inventory
---@param src number
---@param metadata table
---@return boolean, number|nil
local function runMetadataSearch ( src , metadata )
local inv = Framework.GetPlayerInventory ( src ) or {}
for _ , v in pairs ( inv ) do
if v.metadata and v.metadata == metadata then
return true , v.slot
end
end
return false
2025-09-10 12:27:12 -04:00
end
2025-05-09 06:03:09 -04:00
---This will remove an item, and return true or false based on success
---@param src number
---@param item string
---@param count number
---@param slot number
---@param metadata table
---@return boolean
Inventory.RemoveItem = function ( src , item , count , slot , metadata )
2025-12-14 16:22:35 -05:00
if metadata then
local found , foundSlot = runMetadataSearch ( src , metadata )
if found then slot = foundSlot end
end
local success = codem : RemoveItem ( src , item , count , slot )
if not success then return false end
2025-05-09 06:03:09 -04:00
TriggerClientEvent ( "community_bridge:client:inventory:updateInventory" , src , { action = "remove" , item = item , count = count , slot = slot , metadata = metadata })
2025-12-14 16:22:35 -05:00
return success
2025-09-13 18:04:05 -04:00
end
2025-12-14 16:22:35 -05:00
---@description This will return a table with the item info, {name, label, stack, weight, description, image}
2025-05-09 06:03:09 -04:00
---@param item string
---@return table
2025-02-22 20:25:35 -05:00
Inventory.GetItemInfo = function ( item )
2025-09-13 18:04:05 -04:00
local itemData = Inventory.Items ()
if not itemData or not itemData [ item ] then return {} end
2025-05-09 02:00:02 -04:00
return {
2025-09-13 18:04:05 -04:00
name = itemData [ item ]. name or "Missing Name" ,
label = itemData [ item ]. label or "Missing Label" ,
stack = itemData [ item ]. unique or "false" ,
weight = itemData [ item ]. weight or "0" ,
description = itemData [ item ]. description or "none" ,
image = itemData [ item ]. image or Inventory.GetImagePath ( item ),
2025-02-22 20:25:35 -05:00
}
end
2025-12-14 16:22:35 -05:00
---@description This will return the entire items table from the inventory.
---@return table
Inventory.Items = function ()
return codem : GetItemList () or {}
end
---@description This will return the count of the item in the players inventory, if not found will return 0.
---@param src number
---@param item string
---@param metadata table (optional)
---@return number
Inventory.GetItemCount = function ( src , item , metadata )
return codem : GetItemsTotalAmount ( src , item ) or 0
end
---@description This wil return the players inventory.
2025-05-09 06:03:09 -04:00
---@param src number
---@return table
2025-02-22 20:25:35 -05:00
Inventory.GetPlayerInventory = function ( src )
local identifier = Framework.GetPlayerIdentifier ( src )
local playerItems = codem : GetInventory ( identifier , src )
local repackedTable = {}
for _ , v in pairs ( playerItems ) do
table.insert ( repackedTable , {
name = v.name ,
2025-12-14 16:22:35 -05:00
count = v.amount or v.count ,
metadata = v.info or v.metadata or {},
2025-02-22 20:25:35 -05:00
slot = v.slot ,
})
end
return repackedTable
end
2025-12-14 16:22:35 -05:00
---@description Returns the specified slot data as a table.
2025-05-09 06:03:09 -04:00
---@param src number
---@param slot number
2025-12-14 16:22:35 -05:00
---@return table {weight, name, metadata, slot, label, count}
2025-05-09 06:03:09 -04:00
Inventory.GetItemBySlot = function ( src , slot )
local slotData = codem : GetItemBySlot ( src , slot )
if not slotData then return {} end
return {
2025-09-13 18:04:05 -04:00
name = slotData.name ,
label = slotData.label or slotData.name ,
weight = slotData.weight ,
slot = slotData.slot ,
count = slotData.amount or slotData.count ,
2025-12-14 16:22:35 -05:00
metadata = slotData.info or slotData.metadata or {},
stack = slotData.unique or false ,
2025-09-13 18:04:05 -04:00
description = slotData.description
2025-05-09 06:03:09 -04:00
}
2025-02-22 20:25:35 -05:00
end
2025-12-14 16:22:35 -05:00
---@description This will set the metadata of an item in the inventory.
2025-05-09 06:03:09 -04:00
---@param src number
---@param item string
---@param slot number
---@param metadata table
---@return nil
2025-02-22 20:25:35 -05:00
Inventory.SetMetadata = function ( src , item , slot , metadata )
return codem : SetItemMetadata ( src , slot , metadata )
end
2025-12-14 16:22:35 -05:00
---@description This will return a boolean if the player has the item.
2025-03-07 04:08:40 -05:00
---@param src number
---@param item string
2025-12-14 16:22:35 -05:00
---@param requiredCount number (optional)
2025-03-07 04:08:40 -05:00
---@return boolean
2025-12-14 16:22:35 -05:00
Inventory.HasItem = function ( src , item , requiredCount )
return codem : HasItem ( src , item , requiredCount or 1 )
2025-02-22 20:25:35 -05:00
end
2025-01-10 16:36:20 -05:00
2025-12-14 16:22:35 -05:00
---@description This will clear the specified inventory, will always return true unless a value isnt passed correctly.
2025-06-04 15:25:20 -04:00
---@param id string
2025-12-14 16:22:35 -05:00
---@param _type string unused with ox_inventory
2025-06-04 15:25:20 -04:00
---@return boolean
Inventory.ClearStash = function ( id , _type )
if type ( id ) ~= "string" then return false end
2025-06-08 04:36:53 -04:00
if Inventory.Stashes [ id ] then Inventory.Stashes [ id ] = nil end
2025-12-14 16:22:35 -05:00
codem : UpdateStash ( id , {})
return true
2025-06-04 15:25:20 -04:00
end
2025-12-14 16:22:35 -05:00
---@description This will add items to a stash, and return true or false based on success
---@param id string
---@param items table {item, count, metadata}
2025-05-09 06:03:09 -04:00
---@return boolean
2025-12-14 16:22:35 -05:00
Inventory.AddStashItems = function ( id , items )
if type ( items ) ~= "table" then return false end
local repack = {}
for _ , v in pairs ( items ) do
table.insert ( repack , {
item = v.item ,
amount = v.count or v.amount ,
info = v.metadata or v.info or {},
unique = v.unique or v.stack or false ,
description = v.description or "none" ,
weight = Inventory.GetItemInfo ( v.item ). weight or 0 ,
type = 'item' ,
slot = # repack + 1 ,
})
end
codem : UpdateStash ( id , repack )
return true
2025-05-09 06:03:09 -04:00
end
2025-12-14 16:22:35 -05:00
---@description This will get the image path for an item, it is an alternate option to GetItemInfo. If a image isnt found will revert to community_bridge logo (useful for menus)
2025-05-09 06:03:09 -04:00
---@param item string
---@return string
2025-01-10 16:36:20 -05:00
Inventory.GetImagePath = function ( item )
2025-04-16 05:24:03 -05:00
item = Inventory.StripPNG ( item )
2025-12-14 16:22:35 -05:00
local file = LoadResourceFile ( "codem-inventory" , string.format ( "html/itemimages/%s.png" , item ))
local imagePath = file and string.format ( "nui://codem-inventory/html/itemimages/%s.png" , item )
2025-01-10 16:36:20 -05:00
return imagePath or "https://avatars.githubusercontent.com/u/47620135"
2025-03-07 04:08:40 -05:00
end
2025-05-10 00:01:56 -04:00
return Inventory