From 140e391ce5630b3696ceec9245d7f9450326aba1 Mon Sep 17 00:00:00 2001 From: someone <98275381+someone005@users.noreply.github.com> Date: Wed, 28 Feb 2024 22:32:59 +0100 Subject: [PATCH] feat(client/server): GetItemCount export --- client/main.lua | 25 +++++++++++++++++++++++++ server/main.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/client/main.lua b/client/main.lua index 51000ee92..95111ff7d 100644 --- a/client/main.lua +++ b/client/main.lua @@ -55,6 +55,31 @@ end exports('HasItem', HasItem) +function GetItemCount(items) + assert((type(items) == 'table' or type(items) == 'string') and table.type(table) == 'hash', "Table in ^3GetItemCount^7 cannot be dictionary!") + + local check = type(items) == 'table' and 1 or 2 + local count = 0 + + for _,item in pairs(PlayerData.items) do + if check == 1 then + for k,v in pairs(items) do + if v == item.name then + count+=item.amount + end + end + elseif check == 2 then + if items == item.name then + count+=item.amount + end + end + end + + return count +end + +exports('GetItemCount', GetItemCount) + ---Gets the closest vending machine object to the client ---@return integer closestVendingMachine local function GetClosestVending() diff --git a/server/main.lua b/server/main.lua index 3050efe54..bed160df2 100644 --- a/server/main.lua +++ b/server/main.lua @@ -446,6 +446,33 @@ end exports('HasItem', HasItem) +function GetItemCount(source, items) + assert((type(items) == 'table' or type(items) == 'string') and table.type(table) == 'hash', "Table in ^3GetItemCount^7 cannot be dictionary!") + + local Player = QBCore.Functions.GetPlayer(source) + if not Player then return 0 end + local check = type(items) == 'table' and 1 or 2 + local count = 0 + + for _,item in pairs(Player.PlayerData.items) do + if check == 1 then + for k,v in pairs(items) do + if v == item.name then + count+=item.amount + end + end + else + if items == item.name then + count+=item.amount + end + end + end + + return count +end + +exports('GetItemCount', GetItemCount) + ---Create a usable item with a callback on use ---@param itemName string The name of the item to make usable ---@param data any