2025-05-09 02:00:02 -04:00
---@diagnostic disable: duplicate-set-field
2025-12-14 16:22:35 -05:00
if GetResourceState ( 'jpr-inventory' ) == 'missing' then return end
2025-01-31 19:15:33 -05:00
local jpr = exports [ 'jpr-inventory' ]
2025-05-09 06:03:09 -04:00
local registeredShops = {}
2025-01-31 19:15:33 -05:00
Inventory = Inventory or {}
2025-05-19 07:20:23 -05:00
Inventory.Stashes = Inventory.Stashes or {}
2025-01-31 19:15:33 -05:00
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 "jpr-inventory"
end
---@description This will add an item, and return true or false based on success
2025-03-07 04:08:40 -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-03-07 04:08:40 -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 canCarry = Inventory.CanCarryItem ( src , item , count )
if not canCarry then return false end
local success = exports [ 'jpr-inventory' ]: AddItem ( src , item , count , slot , metadata , 'community_bridge Item added' )
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 or 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
2025-05-09 06:03:09 -04:00
---@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.RemoveItem = function ( src , item , count , slot , metadata )
2025-12-14 16:22:35 -05:00
local success = exports [ 'jpr-inventory' ]: RemoveItem ( src , item , count , slot , 'community_bridge Item removed' )
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 or false
2025-01-31 19:15:33 -05: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-03 01:09:24 -05:00
---@param item string
2025-05-09 06:03:09 -04:00
---@return table
Inventory.GetItemInfo = function ( item )
local itemTable = Framework.GetItemInfo ( item )
itemTable.image = Inventory.GetImagePath ( item )
return itemTable
2025-01-31 19:15:33 -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 )
local slotData = jpr : GetItemBySlot ( src , slot )
if not slotData then return {} end
2025-05-09 02:00:02 -04:00
return {
name = slotData.name ,
label = slotData.label ,
weight = slotData.weight ,
slot = slot ,
count = slotData.amount ,
metadata = slotData.info ,
stack = slotData.unique ,
description = slotData.description
}
2025-02-16 07:14:27 -05:00
end
2025-12-14 16:22:35 -05:00
---@description This will open the specified stash for the src passed.
2025-05-09 06:03:09 -04: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
local tbl = Inventory.Stashes [ id ] or { weight = 1000000 , slots = 50 }
2025-06-07 21:55:14 -04:00
TriggerClientEvent ( 'community_bridge:client:jpr-inventory:openStash' , src , id , { weight = tbl.weight , slots = tbl.slots })
2025-05-09 06:03:09 -04:00
end
2025-12-14 16:22:35 -05:00
---@description This will return a boolean if the player has the item.
2025-05-09 06:03:09 -04:00
---@param src number
---@param item string
2025-12-14 16:22:35 -05:00
---@param requiredCount number (optional)
2025-05-09 06:03:09 -04:00
---@return boolean
2025-12-14 16:22:35 -05:00
Inventory.HasItem = function ( src , item , requiredCount )
return jpr : HasItem ( src , item , requiredCount or 1 )
2025-05-09 06:03:09 -04:00
end
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 )
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 ( "jpr-inventory" , string.format ( "html/images/%s.png" , item ))
local imagePath = file and string.format ( "nui://jpr-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 )
2025-05-09 06:03:09 -04:00
jpr : OpenShop ( src , shopTitle )
2025-03-07 04:08:40 -05:00
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 )
if not shopTitle or not inventory or not shopCoords then return end
2025-03-07 04:08:40 -05:00
if registeredShops [ shopTitle ] then return true end
local repackItems = {}
2025-12-14 16:22:35 -05:00
local repackedShopItems = { name = shopTitle , label = shopTitle , coords = shopCoords , items = repackItems , slots = # inventory , }
for k , v in pairs ( inventory ) do
table.insert ( repackItems , { name = v.name , price = v.price or 1000 , amount = v.count or v.amount or 1 , slot = k })
2025-03-07 04:08:40 -05:00
end
jpr : CreateShop ( repackedShopItems )
registeredShops [ shopTitle ] = true
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
2025-07-28 00:10:51 -05:00
Inventory.OpenPlayerInventory = function ( src , target )
assert ( src , "OpenPlayerInventory: src is required" )
2025-12-14 16:22:35 -05:00
assert ( target , "OpenPlayerInventory: target is required" )
2025-07-28 00:10:51 -05:00
local identifier = Framework.GetPlayerIdentifier ( target )
if not identifier then return false end
exports [ 'jpr-inventory' ]: OpenInventory ( src , identifier )
end
2025-05-10 00:01:56 -04:00
return Inventory