forked from Jaliborc/Wildpants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltipCounts.lua
187 lines (147 loc) · 4.59 KB
/
tooltipCounts.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--[[
tooltipCounts.lua
Adds item counts to tooltips
]]--
local ADDON, Addon = ...
local L = LibStub('AceLocale-3.0'):GetLocale(ADDON)
local TooltipCounts = Addon:NewModule('TooltipCounts')
local SILVER = '|cffc7c7cf%s|r'
local LAST_BANK_SLOT = NUM_BAG_SLOTS + NUM_BANKBAGSLOTS
local FIRST_BANK_SLOT = NUM_BAG_SLOTS + 1
local TOTAL = SILVER:format(L.Total)
local Cache = LibStub('LibItemCache-2.0')
local ItemText, ItemCount, Hooked = {}, {}
--[[ Adding Text ]]--
local function FindItemCount(owner, bag, itemID)
local count = 0
local info = Cache:GetBagInfo(owner, bag)
for slot = 1, (info.count or 0) do
local id = Cache:GetItemID(owner, bag, slot)
if id == itemID then
count = count + (Cache:GetItemInfo(owner, bag, slot).count or 1)
end
end
return count
end
local function FormatCounts(color, ...)
local total, places = 0, 0
local text = ''
for i = 1, select('#', ...), 2 do
local title, count = select(i, ...)
if count > 0 then
text = text .. L.TipDelimiter .. title:format(count)
total = total + count
places = places + 1
end
end
text = text:sub(#L.TipDelimiter + 1)
if places > 1 then
text = color:format(total) .. ' ' .. SILVER:format('('.. text .. ')')
else
text = color:format(text)
end
return total, total > 0 and text
end
local function AddOwners(tooltip, link)
if not Addon.sets.tipCount or tooltip.__tamedCounts then
return
end
local itemID = tonumber(link and GetItemInfo(link) and link:match('item:(%d+)')) -- Blizzard doing craziness when doing GetItemInfo
if not itemID or itemID == HEARTHSTONE_ITEM_ID then
return
end
local players = 0
local total = 0
for owner in Cache:IterateOwners() do
local info = Cache:GetOwnerInfo(owner)
local color = Addon:GetOwnerColorString(info)
local count, text = ItemCount[owner] and ItemCount[owner][itemID]
if count then
text = ItemText[owner][itemID]
else
if not info.isguild then
local equip = FindItemCount(owner, 'equip', itemID)
local vault = FindItemCount(owner, 'vault', itemID)
local bags, bank = 0,0
if info.cached then
for i = BACKPACK_CONTAINER, NUM_BAG_SLOTS do
bags = bags + FindItemCount(owner, i, itemID)
end
for i = FIRST_BANK_SLOT, LAST_BANK_SLOT do
bank = bank + FindItemCount(owner, i, itemID)
end
if REAGENTBANK_CONTAINER then
bank = bank + FindItemCount(owner, REAGENTBANK_CONTAINER, itemID)
end
bank = bank + FindItemCount(owner, BANK_CONTAINER, itemID)
else
local owned = GetItemCount(itemID, true)
local carrying = GetItemCount(itemID)
bags = carrying - equip
bank = owned - carrying
end
count, text = FormatCounts(color, L.TipCountEquip, equip, L.TipCountBags, bags, L.TipCountBank, bank, L.TipCountVault, vault)
elseif Addon.sets.countGuild then
local guild = 0
for i = 1, GetNumGuildBankTabs() do
guild = guild + FindItemCount(owner, i, itemID)
end
count, text = FormatCounts(color, L.TipCountGuild, guild)
else
count = 0
end
if info.cached then
ItemText[owner] = ItemText[owner] or {}
ItemText[owner][itemID] = text
ItemCount[owner] = ItemCount[owner] or {}
ItemCount[owner][itemID] = count
end
end
if count > 0 then
tooltip:AddDoubleLine(Addon:GetOwnerIconString(info, 12,0,0) .. ' ' .. color:format(info.name), text)
total = total + count
players = players + 1
end
end
if players > 1 and total > 0 then
tooltip:AddDoubleLine(TOTAL, SILVER:format(total))
end
tooltip.__tamedCounts = true
tooltip:Show()
end
--[[ Hooking ]]--
local function OnItem(tooltip)
local name, link = tooltip:GetItem()
if name ~= '' then -- Blizzard broke tooltip:GetItem() in 6.2
AddOwners(tooltip, link)
end
end
local function OnTradeSkill(tooltip, recipe, reagent)
AddOwners(tooltip, reagent and C_TradeSkillUI.GetRecipeReagentItemLink(recipe, reagent) or C_TradeSkillUI.GetRecipeItemLink(recipe))
end
local function OnQuest(tooltip, type, quest)
AddOwners(tooltip, GetQuestItemLink(type, quest))
end
local function OnClear(tooltip)
tooltip.__tamedCounts = false
end
local function HookTip(tooltip)
tooltip:HookScript('OnTooltipCleared', OnClear)
tooltip:HookScript('OnTooltipSetItem', OnItem)
hooksecurefunc(tooltip, 'SetQuestItem', OnQuest)
hooksecurefunc(tooltip, 'SetQuestLogItem', OnQuest)
if C_TradeSkillUI then
hooksecurefunc(tooltip, 'SetRecipeReagentItem', OnTradeSkill)
hooksecurefunc(tooltip, 'SetRecipeResultItem', OnTradeSkill)
end
end
--[[ Startup ]]--
function TooltipCounts:OnEnable()
if Addon.sets.tipCount then
if not Hooked then
HookTip(GameTooltip)
HookTip(ItemRefTooltip)
Hooked = true
end
end
end