core_inventory compatibility

The current implementation isn’t the cleanest, but it gets the job done. I’ve had to rig parts of it together, and it hasn’t yet had a proper cleanup pass. This update adds support for core_inventory on both ESX and QB frameworks. Previously, the QB fallback worked fine, but an issue affecting ESX users was discovered.

To resolve this, a change was needed in how framework-registered usable items are handled—allowing us to pass the item’s ID as the slot when using core_inventory, while maintaining slot-based behavior otherwise. This approach ensures compatibility without requiring any modifications to other resources.

I've personally tested this on ESX, but since I haven’t been able to coordinate with the core_inventory team, QB Core users will essentially be the first wave of testers for this integration.
This commit is contained in:
MrNewb committed 2025-05-06 14:39:29 -04:00
1 parent afb0e6b45e
commit 11114bf529
5 files changed
+146

No files matched your search

+1
View File
@@ -301,6 +301,7 @@ Framework.RegisterUsableItem = function(itemName, cb)
local func = function(src, item, itemData)
itemData = itemData or item
itemData.metadata = itemData.metadata or itemData.info or {}
itemData.slot = itemData.id or itemData.slot
cb(src, itemData)
end
ESX.RegisterUsableItem(itemName, func)
+1
View File
@@ -447,6 +447,7 @@ Framework.RegisterUsableItem = function(itemName, cb)
local func = function(src, item, itemData)
itemData = itemData or item
itemData.metadata = itemData.metadata or itemData.info or {}
itemData.slot = itemData.id or itemData.slot
cb(src, itemData)
end
QBCore.Functions.CreateUseableItem(itemName, func)
+1
View File
@@ -413,6 +413,7 @@ Framework.RegisterUsableItem = function(itemName, cb)
local func = function(src, item, itemData)
itemData = itemData or item
itemData.metadata = itemData.metadata or itemData.info or {}
itemData.slot = itemData.id or itemData.slot
cb(src, itemData)
end
return QBox:CreateUseableItem(itemName, func)
@@ -16,6 +16,34 @@ Inventory.GetImagePath = function(item)
return imagePath or "https://avatars.githubusercontent.com/u/47620135"
end
---comment
---@param item string
---@return boolean
Inventory.HasItem = function(item)
return core:hasItem(item, 1)
end
---comment
---@param item string
---@return number
Inventory.GetItemCount = function(item)
return core:getItemCount(item)
end
Inventory.GetPlayerInventory = function()
local playerItems = core:getInventory()
local repackedTable = {}
for _, v in pairs(playerItems) do
table.insert(repackedTable, {
name = v.name,
count = v.count,
metadata = v.metadata,
slot = v.id,
})
end
return repackedTable
end
Inventory.GetItemInfo = function(item)
local frameworkName = Framework.GetFrameworkName()
if not frameworkName then return {} end
+115
View File
@@ -14,6 +14,121 @@ Inventory.GetImagePath = function(item)
return imagePath or "https://avatars.githubusercontent.com/u/47620135"
end
---This will add 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.AddItem = function(src, item, count, slot, metadata)
TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "add", item = item, count = count, slot = slot, metadata = metadata})
local itemsAdd = core:addItem(src, item, count, metadata)
return itemsAdd
end
Inventory.GetPlayerInventory = function(src)
local playerItems = core:getInventory(src)
local repackedTable = {}
for _, v in pairs(playerItems) do
table.insert(repackedTable, {
name = v.name,
count = v.count,
metadata = v.metadata,
slot = v.id,
})
end
return repackedTable
end
Inventory.RemoveItem = function(src, item, count, slot, metadata)
if not slot and metadata then
local inv = Inventory.GetPlayerInventory(src)
if not inv then return false end
for _, v in pairs(inv) do
if v.name == item and v.metadata == metadata then
slot = v.slot
break
end
end
end
TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "remove", item = item, count = count, slot = slot, metadata = metadata})
if slot then
local identifier = Framework.GetPlayerIdentifier(src)
if not identifier then return false end
local framework = Bridge.Framework.GetFrameworkName()
if framework == 'es_extended' then
identifier = string.gsub(identifier, ":", "")
end
local weirdInventoryName = 'content-' .. identifier
return core:removeItemExact(weirdInventoryName, slot, count)
end
core:removeItem(src, item, count)
return true
-- I hate this inventory so much, I am so sorry for this.
end
---This will get the item from the specified slot.
---@param src number
---@param slot number
---@return table
Inventory.GetItemBySlot = function(src, slot)
local inv = Inventory.GetPlayerInventory(src)
if not inv then return {} end
for _, v in pairs(inv) do
print(v.slot, slot)
if v.slot == slot then
return {
name = v.name,
count = v.count,
metadata = v.metadata,
slot = v.slot,
}
end
end
return {}
end
---Returns the total count of an item in the inventory.
---@param src number
---@param item string
---@param metadata table
---@return number
Inventory.GetItemCount = function(src, item, metadata)
if metadata then
local inv = Inventory.GetPlayerInventory(src)
local deepCount = 0
if not inv then return 0 end
for _, v in pairs(inv) do
if v.name == item and v.metadata == metadata then
deepCount = deepCount + v.count
end
end
return deepCount
end
local count = core:getItemCount(src, item)
return count or 0
end
---Returns boolean if the player has the item in their inventory.
---@param src number
---@param item string
---@return boolean
Inventory.HasItem = function(src, item)
local count = core:getItemCount(src, item)
return count > 0
end
---This will set the metadata of an item in the inventory.
---@param src number
---@param item string
---@param slot number
---@param metadata table
---@return nil
Inventory.SetMetadata = function(src, item, slot, metadata)
core:setMetadata(src, slot, metadata)
end
---comment
---@param oldplate string
---@param newplate string