From 11114bf52911a66550400cf8213b491ae2eb387a Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Tue, 6 May 2025 14:39:29 -0400 Subject: [PATCH] core_inventory compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- modules/framework/es_extended/server.lua | 1 + modules/framework/qb-core/server.lua | 1 + modules/framework/qbx_core/server.lua | 1 + modules/inventory/core_inventory/client.lua | 28 +++++ modules/inventory/core_inventory/server.lua | 115 ++++++++++++++++++++ 5 files changed, 146 insertions(+) diff --git a/modules/framework/es_extended/server.lua b/modules/framework/es_extended/server.lua index 31fc9a8..4a1cfea 100644 --- a/modules/framework/es_extended/server.lua +++ b/modules/framework/es_extended/server.lua @@ -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) diff --git a/modules/framework/qb-core/server.lua b/modules/framework/qb-core/server.lua index 6146517..1d81bc3 100644 --- a/modules/framework/qb-core/server.lua +++ b/modules/framework/qb-core/server.lua @@ -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) diff --git a/modules/framework/qbx_core/server.lua b/modules/framework/qbx_core/server.lua index 2674f9d..aa614a6 100644 --- a/modules/framework/qbx_core/server.lua +++ b/modules/framework/qbx_core/server.lua @@ -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) diff --git a/modules/inventory/core_inventory/client.lua b/modules/inventory/core_inventory/client.lua index 2e9cce0..f59f174 100644 --- a/modules/inventory/core_inventory/client.lua +++ b/modules/inventory/core_inventory/client.lua @@ -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 diff --git a/modules/inventory/core_inventory/server.lua b/modules/inventory/core_inventory/server.lua index f4aa30d..65792dc 100644 --- a/modules/inventory/core_inventory/server.lua +++ b/modules/inventory/core_inventory/server.lua @@ -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