From 4c303fbdd8650f345615f58fade23b1e81784b84 Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Tue, 21 May 2024 20:43:11 -0500 Subject: [PATCH] Requested export --- server/functions.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/server/functions.lua b/server/functions.lua index 46f6684cd..9474938bf 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -258,6 +258,35 @@ end exports('GetItemsByName', GetItemsByName) +--- Retrieves the total count of used and free slots for a player or an inventory. +--- @param identifier number|string The player's identifier or the identifier of an inventory or drop. +--- @return number, number - The total count of used slots and the total count of free slots. If no inventory is found, returns 0 and the maximum slots. +function GetSlots(identifier) + local inventory, maxSlots + local player = QBCore.Functions.GetPlayer(identifier) + if player then + inventory = player.PlayerData.items + maxSlots = Config.MaxSlots + elseif Inventories[identifier] then + inventory = Inventories[identifier].items + maxSlots = Inventories[identifier].slots + elseif Drops[identifier] then + inventory = Drops[identifier].items + maxSlots = Drops[identifier].slots + end + if not inventory then return 0, maxSlots end + local slotsUsed = 0 + for _, v in pairs(inventory) do + if v then + slotsUsed = slotsUsed + 1 + end + end + local slotsFree = maxSlots - slotsUsed + return slotsUsed, slotsFree +end + +exports('GetSlots', GetSlots) + --- Retrieves the total count of specified items for a player. --- @param source number The player's source ID. --- @param items table|string The items to count. Can be either a table of item names or a single item name.