From 38686e56f9a35d2c229112fe87d86a1bbb3edf41 Mon Sep 17 00:00:00 2001 From: shagu Date: Sat, 29 Jul 2017 21:17:28 +0200 Subject: [PATCH] api: add itemcount function --- api/api.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/api/api.lua b/api/api.lua index df8f6667..abf17d09 100644 --- a/api/api.lua +++ b/api/api.lua @@ -98,6 +98,31 @@ function pfUI.api.GetItemLinkByName(name) end end +-- [ GetItemCount ] +-- Returns information about how many of a given item the player has. +-- 'itemName' [string] name of the item +-- returns: [int] the number of the given item +function pfUI.api.GetItemCount(itemName) + local count = 0 + for bag = 4, 0, -1 do + for slot = 1, GetContainerNumSlots(bag) do + local _, itemCount = GetContainerItemInfo(bag, slot); + if itemCount then + local itemLink = GetContainerItemLink(bag,slot) + local _, _, itemParse = strfind(itemLink, "(%d+):") + local queryName, _, _, _, _, _ = GetItemInfo(itemParse) + if queryName and queryName ~= "" then + if queryName == itemName then + count = count + itemCount; + end + end + end + end + end + + return count +end + -- [ Abbreviate ] -- Abbreviates a number from 1234 to 1.23k -- 'number' [number] the number that should be abbreviated