From b0b139b179b8a813e6a2efe4b71244b585027a79 Mon Sep 17 00:00:00 2001 From: Jim Shield Date: Wed, 7 May 2025 12:58:39 +0100 Subject: [PATCH] Rework ps-inventory support PS-Inv used to be exactly the same as QB-Inv so alot of the function calls were shared This separates them and makes it easier to make changes to support their inventory Also unified the calls for server side functions into single functions instead of having separate per inventory Also moves: - `invImg()` to inventories.lua - `registerStash()` to stashcontrol.lua --- shared/coreloader.lua | 2 +- shared/inventories.lua | 51 +++++++++++ shared/itemcontrol.lua | 85 ++++++------------ shared/shops.lua | 46 +++++++--- shared/stashcontrol.lua | 166 ++++++++++++++++++++++++++++-------- shared/wrapperfunctions.lua | 73 +--------------- 6 files changed, 242 insertions(+), 181 deletions(-) diff --git a/shared/coreloader.lua b/shared/coreloader.lua index 934a9ef..28a1949 100644 --- a/shared/coreloader.lua +++ b/shared/coreloader.lua @@ -15,7 +15,7 @@ Items, Vehicles, Jobs, Gangs, Core, ESX = {}, nil, nil, nil, nil, nil -- Correct QB Inventory Export ------------------------------------------------------------- -- Ensure that the QB inventory export is corrected from 'qb-inventory' to 'ps-inventory' or 'lj-inventory' if needed. -Exports.QBInv = (isStarted("ps-inventory") and "ps-inventory") or (isStarted("lj-inventory") and "lj-inventory") or Exports.QBInv +Exports.PSInv = (isStarted("ps-inventory") and "ps-inventory") or (isStarted("lj-inventory") and "lj-inventory") ------------------------------------------------------------- -- Framework Exports and Inventory Identifiers diff --git a/shared/inventories.lua b/shared/inventories.lua index 85a49c3..f9826ae 100644 --- a/shared/inventories.lua +++ b/shared/inventories.lua @@ -206,4 +206,55 @@ function isInventoryOpen() return LocalPlayer.state.inv_busy end +end + +------------------------------------------------------------- +-- Item Image Retrieval +------------------------------------------------------------- + +--- Retrieves the NUI link for an item's image from the active inventory system. +--- +--- @param item string The item name. +--- @return string string A `nui://` link to the item's image, or an empty string if not found. +--- +--- @usage +--- ```lua +--- local imageLink = invImg("health_potion") +--- if imageLink ~= "" then print(imageLink) end +--- ``` +function invImg(item) + local imgLink = "" + if item ~= "" and Items[item] then + if isStarted(OXInv) then + imgLink = "nui://"..OXInv.."/web/images/"..(Items[item].image or "") + + elseif isStarted(QSInv) then + imgLink = "nui://"..QSInv.."/html/images/"..(Items[item].image or "") + + elseif isStarted(CoreInv) then + imgLink = "nui://"..CoreInv.."/html/img/"..(Items[item].image or "") + + elseif isStarted(CodeMInv) then + imgLink = "nui://"..CodeMInv.."/html/itemimages/"..(Items[item].image or "") + + elseif isStarted(OrigenInv) then + imgLink = "nui://"..OrigenInv.."/html/img/"..(Items[item].image or "") + + elseif isStarted(QBInv) then + imgLink = "nui://"..QBInv.."/html/images/"..(Items[item].image or "") + + elseif isStarted(PSInv) then + imgLink = "nui://"..PSInv.."/html/images/"..(Items[item].image or "") + + elseif isStarted(TgiannInv) then + imgLink = "nui://inventory_images/images/"..(Items[item].image or "") + + elseif isStarted(RSGInv) then + imgLink = "nui://"..RSGInv.."/html/images/"..(Items[item].image or "") + + else + print("^4ERROR^7: ^2No Inventory detected for invImg - Check starter.lua") + end + end + return imgLink end \ No newline at end of file diff --git a/shared/itemcontrol.lua b/shared/itemcontrol.lua index 4104147..db43311 100644 --- a/shared/itemcontrol.lua +++ b/shared/itemcontrol.lua @@ -52,53 +52,6 @@ function createUseableItem(item, funct) end end -------------------------------------------------------------- --- Item Image Retrieval -------------------------------------------------------------- - ---- Retrieves the NUI link for an item's image from the active inventory system. ---- ---- @param item string The item name. ---- @return string string A `nui://` link to the item's image, or an empty string if not found. ---- ---- @usage ---- ```lua ---- local imageLink = invImg("health_potion") ---- if imageLink ~= "" then print(imageLink) end ---- ``` -function invImg(item) - local imgLink = "" - if item ~= "" and Items[item] then - if isStarted(OXInv) then - imgLink = "nui://"..OXInv.."/web/images/"..(Items[item].image or "") - - elseif isStarted(QSInv) then - imgLink = "nui://"..QSInv.."/html/images/"..(Items[item].image or "") - - elseif isStarted(CoreInv) then - imgLink = "nui://"..CoreInv.."/html/img/"..(Items[item].image or "") - - elseif isStarted(CodeMInv) then - imgLink = "nui://"..CodeMInv.."/html/itemimages/"..(Items[item].image or "") - - elseif isStarted(OrigenInv) then - imgLink = "nui://"..OrigenInv.."/html/img/"..(Items[item].image or "") - - elseif isStarted(QBInv) then - imgLink = "nui://"..QBInv.."/html/images/"..(Items[item].image or "") - - elseif isStarted(TgiannInv) then - imgLink = "nui://inventory_images/images/"..(Items[item].image or "") - - elseif isStarted(RSGInv) then - imgLink = "nui://"..RSGInv.."/html/images/"..(Items[item].image or "") - - else - print("^4ERROR^7: ^2No Inventory detected for invImg - Check starter.lua") - end - end - return imgLink -end ------------------------------------------------------------- -- Adding and Removing Items @@ -209,25 +162,32 @@ RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount, dupeWarn(src, item, amount) else - if isStarted(OXInv) then invName = OXInv + if isStarted(OXInv) then + invName = OXInv exports[OXInv]:RemoveItem(src, item, remamount, nil) - elseif isStarted(QSInv) then invName = QSInv + elseif isStarted(QSInv) then + invName = QSInv exports[QSInv]:RemoveItem(src, item, remamount) - elseif isStarted(CoreInv) then invName = CoreInv + elseif isStarted(CoreInv) then + invName = CoreInv exports[CoreInv]:removeItem(src, item, remamount) - elseif isStarted(OrigenInv) then invName = OrigenInv + elseif isStarted(OrigenInv) then + invName = OrigenInv exports[OrigenInv]:removeItem(src, item, remamount) - elseif isStarted(CodeMInv) then invName = CodeMInv + elseif isStarted(CodeMInv) then + invName = CodeMInv exports[CodeMInv]:RemoveItem(src, item, remamount) - elseif isStarted(TgiannInv) then invName = TgiannInv + elseif isStarted(TgiannInv) then + invName = TgiannInv exports[TgiannInv]:RemoveItem(src, item, remamount) - elseif isStarted(QBInv) then invName = QBInv + elseif isStarted(QBInv) then + invName = QBInv while remamount > 0 do if Core.Functions.GetPlayer(src).Functions.RemoveItem(item, 1, slot) then remamount -= 1 @@ -239,7 +199,9 @@ RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount, if Config.Crafting.showItemBox then TriggerClientEvent((isStarted(QBInv) and QBInvNew and "qb-" or "").."inventory:client:ItemBox", src, Items[item], "remove", amount or 1) end - elseif isStarted(PSInv) then invName = PSInv + + elseif isStarted(PSInv) then + invName = PSInv while remamount > 0 do if Core.Functions.GetPlayer(src).Functions.RemoveItem(item, 1, slot) then remamount -= 1 @@ -312,17 +274,18 @@ RegisterNetEvent(getScript()..":server:toggleItem", function(give, item, amount, TriggerClientEvent((isStarted(QBInv) and QBInvNew and "qb-" or "").."inventory:client:ItemBox", src, Items[item], "add", amountToAdd) end + elseif isStarted(PSInv) then invName = PSInv + if Core.Functions.GetPlayer(src).Functions.AddItem(item, amountToAdd, nil, info) then + if Config.Crafting.showItemBox then + TriggerClientEvent("ps-inventory:client:ItemBox", src, Items[item], "add", amountToAdd) + end + end + elseif isStarted(RSGInv) then invName = RSGInv if Core.Functions.GetPlayer(src).Functions.AddItem(item, amountToAdd, nil, info) then TriggerClientEvent("rsg-inventory:client:ItemBox", src, Items[item], "add", amountToAdd) end - elseif isStarted(PSInv) then invName = PSInv - if Core.Functions.GetPlayer(src).Functions.AddItem(item, amountToAdd, nil, info) then - if Config.Crafting.showItemBox then - TriggerClientEvent("inventory:client:ItemBox", src, Items[item], "add", amountToAdd) - end - end end if invName == "" then diff --git a/shared/shops.lua b/shared/shops.lua index dd3c486..3583492 100644 --- a/shared/shops.lua +++ b/shared/shops.lua @@ -161,31 +161,41 @@ function openShop(data) TriggerServerEvent("inventory:server:OpenInventory", "shop", data.items.label, data.items) elseif isStarted(TgiannInv) then - TriggerServerEvent(getScript()..':server:OpenShopTgiann', data.shop) + TriggerServerEvent(getScript()..':server:openServerShop', data.shop) elseif isStarted(QBInv) then if QBInvNew then - TriggerServerEvent(getScript()..':server:OpenShopNewQB', data.shop) + TriggerServerEvent(getScript()..':server:openServerShop', data.shop) + else + TriggerServerEvent("inventory:server:OpenInventory", "shop", data.items.label, data.items) + end + + elseif isStarted(PSInv) then + if QBInvNew then + TriggerServerEvent(getScript()..':server:openServerShop', data.shop) else TriggerServerEvent("inventory:server:OpenInventory", "shop", data.items.label, data.items) end elseif isStarted(RSGInv) then - TriggerServerEvent(getScript()..':server:OpenShopRSG', data.shop) + TriggerServerEvent(getScript()..':server:openServerShop', data.shop) end lookEnt(data.coords) end -RegisterNetEvent(getScript()..':server:OpenShopNewQB', function(data) - exports[QBInv]:OpenShop(source, data) -end) - -RegisterNetEvent(getScript()..':server:OpenShopTgiann', function(data) - exports[TgiannInv]:OpenShop(source, data) -end) - -RegisterNetEvent(getScript()..':server:OpenShopRSG', function(data) - exports[RSGInv]:OpenShop(source, data) +RegisterNetEvent(getScript()..':server:openServerShop', function(data) + if isStarted(QBInv) then + exports[QBInv]:OpenShop(source, data) + end + if isStarted(PSInv) then + exports[PSInv]:OpenShop(source, data) + end + if isStarted(TgiannInv) then + exports[TgiannInv]:OpenShop(source, data) + end + if isStarted(RSGInv) then + exports[RSGInv]:OpenShop(source, data) + end end) --- Registers a shop with the active inventory system. @@ -222,6 +232,16 @@ function registerShop(name, label, items, society) society = society, }) + elseif isStarted(PSInv) and QBInvNew then + debugPrint("^6Bridge^7: ^2Registering ^3PS ^2Store^7:", name, label) + exports[PSInv]:CreateShop({ + name = name, + label = label, + slots = #items, + items = items, + society = society, + }) + elseif isStarted(RSGInv) then debugPrint("^6Bridge^7: ^2Registering ^3RSG ^2Store^7:", name, label) exports[RSGInv]:CreateShop({ diff --git a/shared/stashcontrol.lua b/shared/stashcontrol.lua index b2b2cb7..d7aa7f9 100644 --- a/shared/stashcontrol.lua +++ b/shared/stashcontrol.lua @@ -144,7 +144,7 @@ function openStash(data) exports[OrigenInv]:openInventory('stash', data.stash, { label = data.label }) elseif isStarted(TgiannInv) then - TriggerServerEvent(getScript()..':server:OpenStashTgiann', { + TriggerServerEvent(getScript()..':server:openServerStash', { stashName = data.stash, label = data.label, maxweight = data.maxWeight or 600000, @@ -153,7 +153,7 @@ function openStash(data) elseif isStarted(QBInv) then if QBInvNew then - TriggerServerEvent(getScript()..':server:OpenStashQB', { + TriggerServerEvent(getScript()..':server:openServerStash', { stashName = data.stash, label = data.label, maxweight = data.maxWeight or 600000, @@ -164,8 +164,21 @@ function openStash(data) TriggerServerEvent("inventory:server:OpenInventory", "stash", data.stash, data.stashOptions) end + elseif isStarted(PSInv) then + if QBInvNew then + TriggerServerEvent(getScript()..':server:openServerStash', { + stashName = data.stash, + label = data.label, + maxweight = data.maxWeight or 600000, + slots = data.slots or 40 + }) + else + TriggerEvent("inventory:client:SetCurrentStash", data.stash) + TriggerServerEvent("ps-inventory:server:OpenInventory", "stash", data.stash, data.stashOptions) + end + elseif isStarted(RSGInv) then - TriggerServerEvent(getScript()..':server:OpenStashRSG', { + TriggerServerEvent(getScript()..':server:openServerStash', { stashName = data.stash, label = data.label, maxweight = data.maxWeight or 600000, @@ -181,16 +194,23 @@ function openStash(data) lookEnt(data.coords) end -RegisterNetEvent(getScript()..':server:OpenStashTgiann', function(data) - exports[TgiannInv]:OpenInventory(source, 'stash', data.stashName, data) -end) -RegisterNetEvent(getScript()..':server:OpenStashQB', function(data) - exports[QBInv]:OpenInventory(source, data.stashName, data) -end) - -RegisterNetEvent(getScript()..':server:OpenStashRSG', function(data) - exports[RSGInv]:OpenInventory(source, data.stashName, data) +-- Wrapper function for opening stash from the server. +-- Messy but not much else I can do about it. +RegisterNetEvent(getScript()..":server:openServerStash", function(data) + local src = source + if isStarted(TgiannInv) then + exports[TgiannInv]:OpenInventory(source, 'stash', data.stashName, data) + end + if isStarted(QBInv) then + exports[QBInv]:OpenInventory(source, data.stashName, data) + end + if isStarted(PSInv) then + exports[PSInv]:OpenInventory(source, data.stashName, data) + end + if isStarted(RSGInv) then + exports[RSGInv]:OpenInventory(source, data.stashName, data) + end end) @@ -250,7 +270,12 @@ function getStash(stashName) elseif isStarted(QBInv) then stashResource = QBInv - local result = MySQL.scalar.await("SELECT items FROM "..(QBInvNew and "inventories" or "stashitem").." WHERE identifier = ?", { stashName }) + local result = MySQL.scalar.await("SELECT items FROM "..(QBInvNew and "inventories" or "stashitems").." WHERE identifier = ?", { stashName }) + if result then stashItems = json.decode(result) end + + elseif isStarted(PSInv) then + stashResource = PSInv + local result = MySQL.scalar.await("SELECT items FROM stashitems WHERE identifier = ?", { stashName }) if result then stashItems = json.decode(result) end elseif isStarted(RSGInv) then @@ -330,7 +355,7 @@ function stashRemoveItem(stashItems, stashName, items) debugPrint("^6Bridge^7: ^2None of this item left in stash ^3Stash^7", k, v) stashItems[l] = nil else - debugPrint("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..QBInv, k, v) + debugPrint("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..QSInv, k, v) exports[QSInv]:RemoveItemIntoStash(stashName[1], k, v, l) end end @@ -373,26 +398,6 @@ function stashRemoveItem(stashItems, stashName, items) debugPrint("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..TgiannInv, k, v) end - elseif isStarted(PSInv) then - for k, v in pairs(items) do - for l in pairs(stashItems) do - if stashItems[l].name == k then - if (stashItems[l].amount - v) <= 0 then - debugPrint("^6Bridge^7: ^2None of this item left in stash ^3Stash^7", k, v) - stashItems[l] = nil - else - debugPrint("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..QBInv, k, v) - stashItems[l].amount -= v - end - end - end - end - debugPrint("^6Bridge^7: ^3saveStash^7: ^2Saving ^3QB^2 stash ^7'^6"..stashName.."^7'") - MySQL.Async.insert('INSERT INTO stashitems (stash, items) VALUES (:stash, :items) ON DUPLICATE KEY UPDATE items = :items', { - ['stash'] = stashName, - ['items'] = json.encode(stashItems) - }) - elseif isStarted(QBInv) then if QBInvNew then for k, v in pairs(items) do @@ -425,6 +430,27 @@ function stashRemoveItem(stashItems, stashName, items) }) end + + elseif isStarted(PSInv) then + for k, v in pairs(items) do + for l in pairs(stashItems) do + if stashItems[l].name == k then + if (stashItems[l].amount - v) <= 0 then + debugPrint("^6Bridge^7: ^2None of this item left in stash ^3Stash^7", k, v) + stashItems[l] = nil + else + debugPrint("^6Bridge^7: ^2Removing item from ^3Stash^2 with ^7"..PSInv, k, v) + stashItems[l].amount -= v + end + end + end + end + debugPrint("^6Bridge^7: ^3saveStash^7: ^2Saving ^3QB^2 stash ^7'^6"..stashName.."^7'") + MySQL.Async.insert('INSERT INTO stashitems (stash, items) VALUES (:stash, :items) ON DUPLICATE KEY UPDATE items = :items', { + ['stash'] = stashName, + ['items'] = json.encode(stashItems) + }) + elseif isStarted(RSGInv) then for k, v in pairs(items) do exports[RSGInv]:RemoveItem(stashName[1], k, v, false, 'crafting') @@ -490,3 +516,75 @@ function stashhasItem(stashItems, items, amount) return true, hasTable end + + +--- Registers a stash with the active inventory system. +--- Supports either OXInv or QSInv. +--- +--- @param name string Unique stash identifier. +--- @param label string Display name for the stash. +--- @param slots number|nil (Optional) Number of slots (default 50). +--- @param weight number|nil (Optional) Maximum weight (default 4000000). +--- @param owner string|nil (Optional) Owner identifier for personal stashes. +--- @param coords table|nil (Optional) Coordinates for the stash location. +--- @usage +--- ```lua +--- registerStash( +--- "playerStash", +--- "Player Stash", +--- 100, +--- 8000000, +--- "player123", +--- { x = 100.0, y = 200.0, z = 30.0 } +--- ) +--- ``` +function registerStash(name, label, slots, weight, owner, coords) + if isStarted(OXInv) then + debugPrint("^6Bridge^7: ^2Registering ^3OX ^2Stash^7:", name, label, owner or nil) + exports[OXInv]:RegisterStash(name, label, slots or 50, weight or 4000000, owner or nil) + + elseif isStarted(QSInv) then + debugPrint("^6Bridge^7: ^2Registering ^3QS ^2Stash^7:", name, label) + exports[QSInv]:RegisterStash(name, label, slots or 50, weight or 4000000) + + --elseif isStarted(CoreInv) then + -- debugPrint("^6Bridge^7: ^2Registering ^3CoreInv ^2Stash^7:", name, label) + -- exports[CoreInv]:openHolder(nil, name, 'stash', nil, nil, false, nil) + + elseif isStarted(OrigenInv) then + debugPrint("^6Bridge^7: ^2Registering ^3OrigenInv ^2Stash^7:", name, label) + exports["origen_inventory"]:registerStash(name, label, slots or 50, weight or 4000000) + + elseif isStarted(TgiannInv) then + debugPrint("^6Bridge^7: ^2Registering ^3TgiannInv ^2Stash^7:", name, label) + exports[TgiannInv]:RegisterStash(name, label, slots or 50, weight or 4000000) + + end +end + +if isServer() then + --- Registers an event to create an OX stash from the server. + --- When triggered, it calls registerStash with the provided parameters. + --- + --- @event server:makeOXStash + --- @param name string Unique stash identifier. + --- @param label string Display name for the stash. + --- @param slots number|nil (Optional) Number of slots. + --- @param weight number|nil (Optional) Maximum weight. + --- @param owner string|nil (Optional) Owner identifier. + --- @param coords table|nil (Optional) Stash coordinates. + --- @usage + --- ```lua + --- TriggerEvent(getScript()..":server:makeOXStash", name, label, slots, weight, owner, coords, token) + --- ``` + RegisterNetEvent(getScript()..":server:makeOXStash", function(name, label, slots, weight, owner, coords, token) + local src = source or nil + if src then + if not checkToken(src, token, "stash", name) then + return + end + end + + registerStash(name, label, slots, weight, owner, coords) + end) +end diff --git a/shared/wrapperfunctions.lua b/shared/wrapperfunctions.lua index ebed372..a7b15f6 100644 --- a/shared/wrapperfunctions.lua +++ b/shared/wrapperfunctions.lua @@ -41,75 +41,4 @@ function registerCommand(command, options) end, false, { help = options[1] }) end -end - ---- Registers a stash with the active inventory system. ---- Supports either OXInv or QSInv. ---- ---- @param name string Unique stash identifier. ---- @param label string Display name for the stash. ---- @param slots number|nil (Optional) Number of slots (default 50). ---- @param weight number|nil (Optional) Maximum weight (default 4000000). ---- @param owner string|nil (Optional) Owner identifier for personal stashes. ---- @param coords table|nil (Optional) Coordinates for the stash location. ---- @usage ---- ```lua ---- registerStash( ---- "playerStash", ---- "Player Stash", ---- 100, ---- 8000000, ---- "player123", ---- { x = 100.0, y = 200.0, z = 30.0 } ---- ) ---- ``` -function registerStash(name, label, slots, weight, owner, coords) - if isStarted(OXInv) then - debugPrint("^6Bridge^7: ^2Registering ^3OX ^2Stash^7:", name, label, owner or nil) - exports[OXInv]:RegisterStash(name, label, slots or 50, weight or 4000000, owner or nil) - - elseif isStarted(QSInv) then - debugPrint("^6Bridge^7: ^2Registering ^3QS ^2Stash^7:", name, label) - exports[QSInv]:RegisterStash(name, label, slots or 50, weight or 4000000) - - --elseif isStarted(CoreInv) then - -- debugPrint("^6Bridge^7: ^2Registering ^3CoreInv ^2Stash^7:", name, label) - -- exports[CoreInv]:openHolder(nil, name, 'stash', nil, nil, false, nil) - - elseif isStarted(OrigenInv) then - debugPrint("^6Bridge^7: ^2Registering ^3OrigenInv ^2Stash^7:", name, label) - exports["origen_inventory"]:registerStash(name, label, slots or 50, weight or 4000000) - - elseif isStarted(TgiannInv) then - debugPrint("^6Bridge^7: ^2Registering ^3TgiannInv ^2Stash^7:", name, label) - exports[TgiannInv]:RegisterStash(name, label, slots or 50, weight or 4000000) - - end -end - -if isServer() then - --- Registers an event to create an OX stash from the server. - --- When triggered, it calls registerStash with the provided parameters. - --- - --- @event server:makeOXStash - --- @param name string Unique stash identifier. - --- @param label string Display name for the stash. - --- @param slots number|nil (Optional) Number of slots. - --- @param weight number|nil (Optional) Maximum weight. - --- @param owner string|nil (Optional) Owner identifier. - --- @param coords table|nil (Optional) Stash coordinates. - --- @usage - --- ```lua - --- TriggerEvent(getScript()..":server:makeOXStash", name, label, slots, weight, owner, coords, token) - --- ``` - RegisterNetEvent(getScript()..":server:makeOXStash", function(name, label, slots, weight, owner, coords, token) - local src = source or nil - if src then - if not checkToken(src, token, "stash", name) then - return - end - end - - registerStash(name, label, slots, weight, owner, coords) - end) -end +end \ No newline at end of file