Files

176 lines
6.7 KiB
Lua
Raw Permalink Normal View History

2025-05-09 02:00:02 -04:00
---@diagnostic disable: duplicate-set-field
if GetResourceState('ps-inventory') == 'missing' then return end
local sloth = exports['ps-inventory']
Inventory = Inventory or {}
---@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
---@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)
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})
return success or false
end
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
---@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
---@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)
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})
return success or false
2025-06-04 15:25:20 -04:00
end
---@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
---@description Returns the specified slot data as a table.
2025-03-03 01:09:24 -05:00
---@param src number
---@param slot number
---@return table {weight, name, metadata, slot, label, count}
Inventory.GetItemBySlot = function(src, slot)
local item = sloth:GetItemBySlot(src, slot)
if not item then return {} end
2025-05-09 02:00:02 -04:00
return {
name = item.name,
label = item.label or item.name,
weight = item.weight,
2025-05-09 02:00:02 -04:00
slot = slot,
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
---@description This will open the specified stash for the src passed.
2025-03-03 01:09:24 -05:00
---@param src number
---@param _type string "stash", "trunk", "glovebox"
---@param id string
2025-05-09 06:03:09 -04:00
---@return nil
Inventory.OpenStash = function(src, _type, id)
_type = _type or "stash"
sloth:OpenInventory(_type, id, nil, src)
2025-02-15 06:13:38 -05:00
end
---@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
---@param requiredCount number (optional)
2025-03-03 01:09:24 -05:00
---@return boolean
Inventory.HasItem = function(src, item, requiredCount)
return sloth:HasItem(src, item, requiredCount or 1)
2025-02-15 06:13:38 -05:00
end
---@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
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)
-- 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
---@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
---@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
---@description This will register a shop, if it already exists it will return true.
---@param shopTitle string
---@param inventory table
---@param shopCoords table
---@param shopGroups table
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 = {}
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
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
end
---@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
return Inventory