2025-05-09 02:00:02 -04:00
---@diagnostic disable: duplicate-set-field
2025-12-14 16:22:35 -05:00
if GetResourceState ( 'ps-inventory' ) == 'missing' then return end
2025-01-31 19:15:33 -05:00
local sloth = exports [ 'ps-inventory' ]
2025-01-10 16:36:20 -05:00
Inventory = Inventory or {}
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 "ps-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-03-03 01:09:24 -05:00
---@return boolean
2025-05-09 06:03:09 -04:00
Inventory.AddItem = function ( src , item , count , slot , metadata )
2025-12-14 16:22:35 -05:00
local success = sloth : AddItem ( src , item , count , slot , metadata , 'community_bridge' )
if not success then return false end
2025-05-09 06:03:09 -04:00
TriggerClientEvent ( 'ps-inventory:client:ItemBox' , src , QBCore.Shared . Items [ item ], 'add' , count )
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 or false
2025-01-10 16:36:20 -05:00
end
2025-12-14 16:22:35 -05:00
local function runMetadataSearch ( src , metadata )
local inv = Framework.GetPlayerInventory ( src ) or {}
for k , 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-12-14 16:22:35 -05:00
---@description This will remove an item, and return true or false based on success
2025-03-03 01:09:24 -05:00
---@param src number
---@param item string
2025-05-09 06:03:09 -04:00
---@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.RemoveItem = function ( src , item , count , slot , metadata )
2025-12-14 16:22:35 -05:00
if metadata then
local found , itemSlot = runMetadataSearch ( src , metadata )
if found then
slot = itemSlot
end
end
local success = sloth : RemoveItem ( src , item , count , slot , 'community_bridge' )
if not success then return false end
2025-05-09 06:03:09 -04:00
TriggerClientEvent ( 'ps-inventory:client:ItemBox' , src , QBCore.Shared . Items [ item ], 'remove' , count )
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 or false
2025-06-04 15:25:20 -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-03-07 04:08:40 -05:00
---@param item string
2025-05-09 06:03:09 -04:00
---@return table
Inventory.GetItemInfo = function ( item )
local itemData = QBCore.Shared . Items [ item ]
if not itemData then return {} end
return {
name = itemData.name or "Missing Name" ,
label = itemData.label or "Missing Label" ,
stack = itemData.stack or "true" ,
weight = itemData.weight or "0" ,
description = itemData.description or "none" ,
image = Inventory.GetImagePath ( item ),
}
2025-03-07 04:08:40 -05:00
end
2025-12-14 16:22:35 -05:00
---@description Returns the specified slot data as a table.
2025-03-03 01:09:24 -05:00
---@param src number
---@param slot number
2025-12-14 16:22:35 -05:00
---@return table {weight, name, metadata, slot, label, count}
2025-01-31 19:15:33 -05:00
Inventory.GetItemBySlot = function ( src , slot )
2025-12-14 16:22:35 -05:00
local item = sloth : GetItemBySlot ( src , slot )
if not item then return {} end
2025-05-09 02:00:02 -04:00
return {
2025-12-14 16:22:35 -05:00
name = item.name ,
label = item.label or item.name ,
weight = item.weight ,
2025-05-09 02:00:02 -04:00
slot = slot ,
2025-12-14 16:22:35 -05:00
count = item.amount ,
metadata = item.info ,
stack = item.unique ,
description = item.description
2025-05-09 02:00:02 -04:00
}
2025-02-15 06:13:38 -05:00
end
2025-12-14 16:22:35 -05:00
---@description This will open the specified stash for the src passed.
2025-03-03 01:09:24 -05:00
---@param src number
2025-12-14 16:22:35 -05:00
---@param _type string "stash", "trunk", "glovebox"
---@param id string
2025-05-09 06:03:09 -04:00
---@return nil
2025-06-07 21:55:14 -04:00
Inventory.OpenStash = function ( src , _type , id )
_type = _type or "stash"
2025-12-14 16:22:35 -05:00
sloth : OpenInventory ( _type , id , nil , src )
2025-02-15 06:13:38 -05:00
end
2025-12-14 16:22:35 -05:00
---@description This will return a boolean if the player has the item.
2025-03-03 01:09:24 -05:00
---@param src number
---@param item string
2025-12-14 16:22:35 -05:00
---@param requiredCount number (optional)
2025-03-03 01:09:24 -05:00
---@return boolean
2025-12-14 16:22:35 -05:00
Inventory.HasItem = function ( src , item , requiredCount )
return sloth : HasItem ( src , item , requiredCount or 1 )
2025-02-15 06:13:38 -05:00
end
2025-02-16 07:14:27 -05:00
2025-12-14 16:22:35 -05:00
---@description This will update the plate to the vehicle inside the inventory. (It will also update with jg-mechanic if using it)
2025-03-03 01:09:24 -05:00
---@param oldplate string
---@param newplate string
---@return boolean
2025-02-16 07:14:27 -05:00
Inventory.UpdatePlate = function ( oldplate , newplate )
local queries = {
'UPDATE gloveboxitems SET plate = @newplate WHERE plate = @oldplate' ,
'UPDATE trunkitems SET plate = @newplate WHERE plate = @oldplate' ,
}
local values = { newplate = newplate , oldplate = oldplate }
MySQL.transaction . await ( queries , values )
2025-12-14 16:22:35 -05:00
-- ox standard doesnt return anything so probably shouldnt return so everything else has the same standard. Yeaaaa probably should change this.
2026-03-13 12:35:30 -04:00
if GetResourceState ( 'jg-mechanic' ) == 'missing' then return true end
2025-05-09 06:03:09 -04:00
return true , exports [ "jg-mechanic" ]: vehiclePlateUpdated ( oldplate , newplate )
2025-03-07 04:08:40 -05: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
Inventory.GetImagePath = function ( item )
item = Inventory.StripPNG ( item )
local file = LoadResourceFile ( "ps-inventory" , string.format ( "html/images/%s.png" , item ))
local imagePath = file and string.format ( "nui://ps-inventory/html/images/%s.png" , item )
return imagePath or "https://avatars.githubusercontent.com/u/47620135"
end
2025-03-07 04:08:40 -05:00
2025-12-14 16:22:35 -05:00
---@description This will open the specified shop for the src passed.
2025-05-09 06:03:09 -04:00
---@param src number
---@param shopTitle string
2025-03-07 04:08:40 -05:00
Inventory.OpenShop = function ( src , shopTitle )
return sloth : OpenShop ( src , shopTitle )
end
2025-12-14 16:22:35 -05:00
---@description This will register a shop, if it already exists it will return true.
2025-06-07 22:50:13 -04:00
---@param shopTitle string
2025-12-14 16:22:35 -05:00
---@param inventory table
2025-06-07 22:50:13 -04:00
---@param shopCoords table
---@param shopGroups table
2025-12-14 16:22:35 -05:00
Inventory.RegisterShop = function ( shopTitle , inventory , shopCoords , shopGroups )
assert ( shopTitle , "RegisterShop: shopTitle is required" )
assert ( inventory , "RegisterShop: shopInventory is required" )
assert ( shopCoords , "RegisterShop: shopCoords is required" )
assert ( shopGroups , "RegisterShop: shopGroups is required" )
2025-03-07 04:08:40 -05:00
local repackItems = {}
2025-12-14 16:22:35 -05:00
for k , v in pairs ( inventory ) do
table.insert ( repackItems , { name = v.name , price = v.price or 1000 , amount = v.amount or v.count or 1 , slot = k })
2025-03-07 04:08:40 -05:00
end
2025-12-14 16:22:35 -05:00
local repackedShopItems = { name = shopTitle , label = shopTitle , coords = shopCoords , items = repackItems , slots = # inventory , }
2025-03-07 04:08:40 -05:00
sloth : CreateShop ( repackedShopItems )
return true
2025-05-10 00:01:56 -04:00
end
2025-12-14 16:22:35 -05:00
---@description This will open a players inventory, used for admin purposes and stuff.
---@param src number
---@param target number
Inventory.OpenPlayerInventory = function ( src , target )
assert ( src , "OpenPlayerInventory: src is required" )
assert ( target , "OpenPlayerInventory: target is required" )
sloth : OpenInventoryById ( src , target )
end
2025-05-10 00:01:56 -04:00
return Inventory