85 lines
2.6 KiB
Lua
85 lines
2.6 KiB
Lua
-- EGM mail: bag counts. Only unlocked stacks in bags 0-4 count as mailable.
|
|
EGM = EGM or {}
|
|
EGM.Bags = EGM.Bags or {}
|
|
local B = EGM.Bags
|
|
|
|
local function idFromLink(link)
|
|
if not link then return nil end
|
|
if EGM.Util and EGM.Util.itemIdFromLink then
|
|
local id = EGM.Util.itemIdFromLink(link)
|
|
return tonumber(id)
|
|
end
|
|
local _, _, id = string.find(link, "item:(%d+)")
|
|
return id and tonumber(id) or nil
|
|
end
|
|
|
|
-- a soulbound stack can never be mailed (ERR_MAIL_BOUND_ITEM). Only equippable items get bound in bags (equipped once, or a
|
|
-- bind-on-pickup copy), so only those are read from a hidden tooltip; trade goods never are.
|
|
local scan
|
|
function B.isBound(bag, slot)
|
|
local link = GetContainerItemLink(bag, slot)
|
|
local id = link and idFromLink(link)
|
|
if not id or not GetItemInfo then return false end
|
|
local _, _, _, _, _, _, _, equipLoc = GetItemInfo(id)
|
|
if not equipLoc or equipLoc == "" then return false end
|
|
if not scan then
|
|
scan = CreateFrame("GameTooltip", "EGMBindScanTip", nil, "GameTooltipTemplate")
|
|
scan:SetOwner(WorldFrame, "ANCHOR_NONE")
|
|
end
|
|
scan:ClearLines()
|
|
scan:SetBagItem(bag, slot)
|
|
for i = 2, 4 do
|
|
local fs = getglobal("EGMBindScanTipTextLeft" .. i)
|
|
local t = fs and fs:GetText()
|
|
if t and (t == ITEM_SOULBOUND or t == "Soulbound") then return true end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- array of {bag, slot, count} for item id (unlocked, mailable slots only); second value = soulbound stacks skipped
|
|
function B.stacks(item)
|
|
local out, bound = {}, 0
|
|
item = tonumber(item)
|
|
if not item then return out, 0 end
|
|
for bag = 0, 4 do
|
|
local n = GetContainerNumSlots(bag) or 0
|
|
for slot = 1, n do
|
|
local link = GetContainerItemLink(bag, slot)
|
|
if link and idFromLink(link) == item then
|
|
local _, count, locked = EGM.Util.real("GetContainerItemInfo")(bag, slot)
|
|
if count and not locked then
|
|
if B.isBound(bag, slot) then bound = bound + 1
|
|
else table.insert(out, { bag = bag, slot = slot, count = count }) end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return out, bound
|
|
end
|
|
|
|
function B.count(item)
|
|
local s = B.stacks(item)
|
|
local total = 0
|
|
for i = 1, table.getn(s) do total = total + s[i].count end
|
|
return total
|
|
end
|
|
|
|
-- first empty slot -> bag, slot (or nil)
|
|
function B.freeSlot()
|
|
for bag = 0, 4 do
|
|
local n = GetContainerNumSlots(bag) or 0
|
|
for slot = 1, n do
|
|
if not GetContainerItemLink(bag, slot) then return bag, slot end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
if CreateFrame then
|
|
local f = CreateFrame("Frame", "EGMBagsFrame")
|
|
f:RegisterEvent("BAG_UPDATE")
|
|
f:SetScript("OnEvent", function()
|
|
if EGM.Fire then EGM.Fire("BAGS") end
|
|
end)
|
|
end
|