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', 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..2c2c364 --- /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 ("player:%s"):format(GetPlayerServerId(PlayerId())) or nil, + function(bagName, key, value, lastValue, replicated) + local actualPlayerId = getPlayerFromStateBagName(bagName) + 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 + 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 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