From f913089d368681c1dcafe81e59129d60ae830904 Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 5 May 2025 19:24:17 -0300 Subject: [PATCH 1/4] add Client State Bag --- lib/init.lua | 4 +-- lib/statebags/client/client.lua | 48 +++++++++++++++++++++++++++++++++ lib/statebags/server/server.lua | 0 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 lib/statebags/client/client.lua create mode 100644 lib/statebags/server/server.lua diff --git a/lib/init.lua b/lib/init.lua index 7d8cc5a..3d3df81 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -73,5 +73,5 @@ cLib.Particle = Particle or Require("lib/particles/client/particles.lua") cLib.Cache = Cache or Require("lib/cache/client/cache.lua") cLib.ClientEntity = ClientEntity or Require("lib/entities/client/client_entity.lua") cLib.ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua") - -return cLib \ No newline at end of file +cLib.ClientStateBag = ClientStateBag or Require("lib/statebags/client/client.lua") +return cLib diff --git a/lib/statebags/client/client.lua b/lib/statebags/client/client.lua new file mode 100644 index 0000000..0668718 --- /dev/null +++ b/lib/statebags/client/client.lua @@ -0,0 +1,48 @@ +ClientStateBag = {} + +---Gets an entity from a statebag name +---@param entityId string The entity ID or statebag name +---@return number|nil entity The entity handle or nil if not found +local function getStateBagEntity(entityId) + local _entity = GetEntityFromStateBagName(entityId) + return _entity +end + +local function getPlayerFromStateBagName(stateBagName) + return getPlayerFromStateBagName(stateBagName) +end + +---Adds a handler for entity statebag changes +---@param keyName string The statebag key to watch for changes +---@param entityId string|nil The specific entity ID to watch, or nil for all entities +---@param callback function The callback function to handle changes (function(entityId, key, value, lastValue, replicated)) +---@return number handler The handler ID +function ClientStateBag.AddEntityChangeHandler(keyName, entityId, callback) + return AddStateBagChangeHandler(keyName, entityId or nil, function(bagName, key, value, lastValue, replicated) + local entity = getStateBagEntity(bagName) + if not DoesEntityExist(entity) then return end + if entity then + return callback(entity, key, value, lastValue, replicated) + end + end) +end + +---Adds a handler for player statebag changes +---@param keyName string The statebag key to watch for changes +---@param filter boolean|nil If true, only watch for changes from the current player +---@param callback function The callback function to handle changes (function(playerId, key, value, lastValue, replicated)) +---@return number handler The handler ID +function ClientStateBag.AddPlayerChangeHandler(keyName, filter, callback) + return AddStateBagChangeHandler(keyName, filter and GetPlayerServerId(PlayerId()) or nil, + function(bagName, key, value, lastValue, replicated) + local actualPlayerId = getPlayerFromStateBagName(bagName) + if DoesEntityExist(actualPlayerId) and (not actualPlayerId == PlayerPedId()) then -- you cant have a statebag value if you are not the player + return false + end + if actualPlayerId and actualPlayerId ~= 0 then + return callback(tonumber(actualPlayerId), key, value, lastValue, replicated) + end + end) +end + +return ClientStateBag diff --git a/lib/statebags/server/server.lua b/lib/statebags/server/server.lua new file mode 100644 index 0000000..e69de29 From dc91f98e37d17519e7b84397281d4fe735b734b4 Mon Sep 17 00:00:00 2001 From: JericoFX Date: Mon, 5 May 2025 19:29:37 -0300 Subject: [PATCH 2/4] a little fix for the bag name --- lib/statebags/client/client.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/statebags/client/client.lua b/lib/statebags/client/client.lua index 0668718..2c2c364 100644 --- a/lib/statebags/client/client.lua +++ b/lib/statebags/client/client.lua @@ -33,10 +33,10 @@ end ---@param callback function The callback function to handle changes (function(playerId, key, value, lastValue, replicated)) ---@return number handler The handler ID function ClientStateBag.AddPlayerChangeHandler(keyName, filter, callback) - return AddStateBagChangeHandler(keyName, filter and GetPlayerServerId(PlayerId()) or nil, + return AddStateBagChangeHandler(keyName, filter and ("player:%s"):format(GetPlayerServerId(PlayerId())) or nil, function(bagName, key, value, lastValue, replicated) local actualPlayerId = getPlayerFromStateBagName(bagName) - if DoesEntityExist(actualPlayerId) and (not actualPlayerId == PlayerPedId()) then -- you cant have a statebag value if you are not the player + if DoesEntityExist(actualPlayerId) and (actualPlayerId ~= PlayerPedId()) then -- you cant have a statebag value if you are not the player return false end if actualPlayerId and actualPlayerId ~= 0 then 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 3/4] 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 From 97c7d290b9892d250f409fc1e49dc917664ebaec Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Tue, 6 May 2025 14:48:22 -0400 Subject: [PATCH 4/4] feat: add new features and functionality, bump version for upcoming main push --- fxmanifest.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index e9fec41..4668f29 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -3,8 +3,9 @@ game 'gta5' lua54 'yes' use_experimental_fxv2_oal 'yes' author 'The Order of the Sacred Framework' -description 'A bridge made for the community to use as a base for their own projects. This bridge will allow a single format to work with nearly all commonly used systems while still offering expandability.' -version '0.6.1' +name 'community_bridge' +description 'A Universal Bridge for Our Community, created by a group of contributors with a shared vision to enhance both user and developer experiences. This bridge connects various frameworks, inventories, target systems, notification systems, and more, fostering compatibility and seamless integration.' +version '0.7.0' shared_scripts { '@ox_lib/init.lua',