forked from Jaliborc/LibItemCache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibItemCache-2.0.lua
More file actions
417 lines (338 loc) · 12.1 KB
/
LibItemCache-2.0.lua
File metadata and controls
417 lines (338 loc) · 12.1 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
--[[
Copyright 2013-2022 João Cardoso
LibItemCache is distributed under the terms of the GNU General Public License (Version 3).
As a special exception, the copyright holders of this library give you permission to embed it
with independent modules to produce an addon, regardless of the license terms of these
independent modules, and to copy and distribute the resulting software under terms of your
choice, provided that you also meet, for each embedded independent module, the terms and
conditions of the license of that module. Permission is not granted to modify this library.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the library. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
This file is part of LibItemCache.
--]]
local Lib = LibStub:NewLibrary('LibItemCache-2.0', 32)
if not Lib then return end
local PLAYER, FACTION, REALM, REALMS
local COMPLETE_LINK = '|c.+|H.+|h.+|h|r'
local PET_LINK = '|c.+|Hbattlepet:.+|h.+|h|r'
local PET_LINK_FORMAT = '|c%s|Hbattlepet:%sx0|h[%s]|h|r'
local PET_STRING = '^' .. strrep('%d+:', 7) .. '%d+$'
local KEYSTONE_LINK = '|c.+|Hkeystone:.+|h.+|h|r'
local KEYSTONE_STRING = '^' .. strrep('%d+:', 6) .. '%d+$'
local EMPTY_FUNC = function() end
local FindRealms = function()
if not REALM then
PLAYER, REALM = UnitFullName('player')
FACTION = UnitFactionGroup('player')
REALMS = GetAutoCompleteRealms()
if not REALMS or #REALMS == 0 then
REALMS = {REALM}
end
end
end
local Events, Caches = LibStub('AceEvent-3.0'), {}
local AccessInterfaces = function(self, key)
for name, lib in LibStub:IterateLibraries() do
if lib.IsItemCache and lib[key] then
self[key] = lib[key]
return self[key]
end
end
return EMPTY_FUNC
end
setmetatable(Caches, { __index = AccessInterfaces })
Events:Embed(Lib)
Lib:RegisterEvent('BANKFRAME_OPENED', function() Lib.AtBank = true; Lib:SendMessage('CACHE_BANK_OPENED') end)
Lib:RegisterEvent('BANKFRAME_CLOSED', function() Lib.AtBank = false; Lib:SendMessage('CACHE_BANK_CLOSED') end)
if CanUseVoidStorage then
Lib:RegisterEvent('VOID_STORAGE_OPEN', function() Lib.AtVault = true; Lib:SendMessage('CACHE_VAULT_OPENED') end)
Lib:RegisterEvent('VOID_STORAGE_CLOSE', function() Lib.AtVault = false; Lib:SendMessage('CACHE_VAULT_CLOSED') end)
end
if CanGuildBankRepair then
Lib:RegisterEvent('GUILDBANKFRAME_OPENED', function() Lib.AtGuild = true; Lib:SendMessage('CACHE_GUILD_OPENED') end)
Lib:RegisterEvent('GUILDBANKFRAME_CLOSED', function() Lib.AtGuild = false; Lib:SendMessage('CACHE_GUILD_CLOSED') end)
end
--[[ Owners ]]--
function Lib:GetOwnerInfo(owner)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
local cached = Lib:IsOwnerCached(realm, name, isguild)
local api = isguild and 'GetGuild' or 'GetPlayer'
local owner = cached and Caches[api](Caches, realm, name) or {}
if not cached then
owner.faction = FACTION
if isguild then
owner.money = GetGuildBankMoney()
else
owner.money = (GetMoney() or 0) - GetCursorMoney() - GetPlayerTradeMoney()
owner.class = select(2, UnitClass('player'))
owner.race = select(2, UnitRace('player'))
owner.guild = GetGuildInfo('player')
owner.gender = UnitSex('player')
end
end
owner.guild = owner.guild and ('® ' .. owner.guild .. ' - ' .. realm)
owner.name, owner.realm, owner.isguild = name, realm, isguild
owner.cached = cached
return owner
end
function Lib:DeleteOwnerInfo(owner)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
local cached = Lib:IsOwnerCached(realm, name, isguild)
if cached then
if isguild then
Caches:DeleteGuild(realm, name)
else
Caches:DeletePlayer(realm, name)
end
end
end
function Lib:IterateOwners()
FindRealms()
local i, players, guilds, suffix = 0
return function()
while i <= #REALMS do
local owner = players and players()
if owner then
return owner .. suffix
else
players = nil
end
local owner = guilds and guilds()
if owner then
return '® ' .. owner .. suffix
end
i = i + 1
if i <= #REALMS then
players = Caches:GetPlayers(REALMS[i])
guilds = Caches:GetGuilds(REALMS[i])
suffix = REALMS[i] ~= REALM and ' - ' .. REALMS[i] or ''
end
end
end
end
--[[ Items and Bags ]]--
function Lib:GetBagInfo(owner, bag)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
local cached = Lib:IsBagCached(realm, name, isguild, bag)
local api = isguild and 'GetGuildTab' or 'GetBag'
local query = cached or isguild and bag ~= GetCurrentGuildBankTab()
local item = query and Caches[api](Caches, realm, name, bag) or {}
if cached then
item.cached = true
elseif isguild then
local name, icon, view, deposit, withdraw, remaining = GetGuildBankTabInfo(bag)
if not query then
item.deposit, item.withdraw, item.remaining = deposit, withdraw, remaining
end
item.name, item.icon, item.viewable = name, icon, view
elseif tonumber(bag) then
item.free = GetContainerNumFreeSlots(bag)
if bag == REAGENTBANK_CONTAINER then
item.cost = GetReagentBankCost()
item.owned = IsReagentBankUnlocked()
elseif bag == KEYRING_CONTAINER then
item.count = HasKey and HasKey() and GetContainerNumSlots(bag)
item.free = item.count and item.free and (item.count + item.free - 32)
elseif bag > BACKPACK_CONTAINER then
item.slot = ContainerIDToInventoryID(bag)
item.link = GetInventoryItemLink('player', item.slot)
item.icon = GetInventoryItemTexture('player', item.slot)
item.count = GetContainerNumSlots(bag)
if bag > NUM_BAG_SLOTS then
item.owned = (bag - NUM_BAG_SLOTS) <= GetNumBankSlots()
item.cost = GetBankSlotCost()
end
end
end
if isguild then
item.count = 98
item.family = 0
elseif bag == 'vault' then
item.count = 160
elseif bag == 'equip' then
item.count = INVSLOT_LAST_EQUIPPED
item.owned = true
else
item.owned = item.owned or (bag >= KEYRING_CONTAINER and bag <= NUM_BAG_SLOTS) or item.id or item.link
if bag == KEYRING_CONTAINER then
item.family = 9
elseif bag <= BACKPACK_CONTAINER then
item.count = item.count or item.owned and GetContainerNumSlots(bag)
item.family = bag ~= REAGENTBANK_CONTAINER and 0 or REAGENTBANK_CONTAINER
end
end
return Lib:RestoreItemData(item)
end
function Lib:GetItemInfo(owner, bag, slot)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
local cached = Lib:IsBagCached(realm, name, isguild, bag)
local api = isguild and 'GetGuildItem' or 'GetItem'
local item = cached and Caches[api](Caches, realm, name, bag, slot) or {}
if cached then
item.cached = true
elseif isguild then
item.link = GetGuildBankItemLink(bag, slot)
item.icon, item.count, item.locked = GetGuildBankItemInfo(bag, slot)
elseif bag == 'equip' then
item.link = GetInventoryItemLink('player', slot)
elseif bag == 'vault' then
item.id, item.icon, item.locked, item.recent, item.filtered, item.quality = GetVoidItemInfo(1, slot)
else
item.icon, item.count, item.locked, item.quality, item.readable, item.lootable, item.link, item.filtered, item.worthless, item.id = GetContainerItemInfo(bag, slot)
end
return Lib:RestoreItemData(item)
end
function Lib:GetItemID(owner, bag, slot)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
local cached = Lib:IsBagCached(realm, name, isguild, bag)
if cached then
local api = isguild and 'GetGuildItem' or 'GetItem'
local item = Caches[api](Caches, realm, name, bag, slot)
return item and (item.id or item.link and tonumber(item.link:match('(%d+)')))
elseif isguild then
local link = GetGuildBankItemLink(bag, slot)
return link and tonumber(link:match('item:(%d+)'))
elseif bag == 'equip' then
return GetInventoryItemID('player', slot)
elseif bag == 'vault' then
return GetVoidItemInfo(1, slot)
else
return GetContainerItemID(bag, slot)
end
end
function Lib:PickupItem(owner, bag, slot)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
local cached = Lib:IsBagCached(realm, name, isguild, bag)
if not cached then
if isguild then
PickupGuildBankItem(bag, slot)
elseif bag == 'equip' then
PickupInventoryItem(slot)
elseif bag == 'vault' then
ClickVoidStorageSlot(1, slot)
else
PickupContainerItem(bag, slot)
end
end
end
--[[ Advanced ]]--
function Lib:GetOwnerID(owner)
local realm, name, isguild = Lib:GetOwnerAddress(owner)
return (isguild and '® ' or '') .. name .. ' - ' .. realm
end
function Lib:GetOwnerAddress(owner)
FindRealms()
if not owner then
return REALM, PLAYER
end
local first, realm = strmatch(owner, '^(.-) *%- *(.+)$')
local isguild, name = strmatch(first or owner, '^(®) *(.+)')
return realm or REALM, name or first or owner, isguild and true
end
function Lib:IsOwnerCached(realm, name, isguild)
return realm ~= REALM or name ~= (isguild and GetGuildInfo('player') or PLAYER)
end
function Lib:IsBagCached(realm, name, isguild, bag)
if Lib:IsOwnerCached(realm, name, isguild) then
return true
end
if isguild then
return not Lib.AtGuild
end
local isBankBag = bag == BANK_CONTAINER or bag == REAGENTBANK_CONTAINER or type(bag) == 'number' and bag > NUM_BAG_SLOTS
return isBankBag and not Lib.AtBank or bag == 'vault' and not Lib.AtVault
end
function Lib:RestoreItemData(item)
local link, id = item.link, item.id
local query = link or id
local bind, class, complete, crafting, equip, expac, icon, level, minLevel, name, price, quality, stack, set, subclass
if type(link) == 'string' then
complete = link:find(COMPLETE_LINK)
if complete then
if link:find(PET_LINK) then
id, level, quality, name, icon = self:RestorePetLinkData(link)
class, subclass, query = Enum.ItemClass.Battlepet, 0
elseif link:find(KEYSTONE_LINK) then
query = 138019
end
else
if link:sub(1,9) == 'battlepet' or link:find(PET_STRING) then
id, level, quality, name, icon = self:RestorePetLinkData(link)
link = PET_LINK_FORMAT:format(select(4, GetItemQualityColor(quality)), link, name)
class, subclass, query = Enum.ItemClass.Battlepet, 0
elseif link:sub(1,8) == 'keystone' or link:find(KEYSTONE_STRING) then
query = 138019
elseif link:sub(1,5) ~= 'item:' then
query = 'item:' .. link
end
end
end
if query then
id, _, _, equip, icon, class, subclass = GetItemInfoInstant(query)
name, link, quality, level, minLevel, _, _, stack, _,_, price, _,_, bind, expac, set, crafting = GetItemInfo(query)
end
item.id = item.id or id
item.link = complete and item.link or link
item.family = item.family or item.id and GetItemFamily(item.id) or 0
item.quality = (item.quality and item.quality >= 0 and item.quality) or quality
item.bind = item.bind or bind
item.class = item.class or class
item.crafting = item.crafting or crafting
item.equip = item.equip or equip
item.expac = item.expac or expac
item.icon = item.icon or icon
item.level = item.level or level
item.minLevel = item.minLevel or minLevel
item.name = item.name or name
item.price = item.price or price
item.stack = item.stack or stack
item.set = item.set or set
item.subclass = item.subclass or subclass
return item
end
function Lib:RestorePetLinkData(link)
local id, level, quality = link:match('(%d+):(%d+):(%d+)')
local name, icon = C_PetJournal.GetPetInfoBySpeciesID(id)
return tonumber(id), tonumber(level), tonumber(quality), name, icon
end
--[[ Location ]]--
function Lib:InBank() -- naming for legacy purposes
return Lib.AtBank
end
function Lib:InVault()
return Lib.AtVault
end
function Lib:InGuild()
return Lib.AtGuild
end
--[[ Static ]]--
function Lib:IsBackpack(bag)
return bag == BACKPACK_CONTAINER
end
function Lib:IsBackpackBag(bag)
return bag > BACKPACK_CONTAINER and bag <= NUM_BAG_SLOTS
end
function Lib:IsKeyring(bag)
return bag == KEYRING_CONTAINER
end
function Lib:IsBank(bag)
return bag == BANK_CONTAINER
end
function Lib:IsBankBag(bag)
return bag > NUM_BAG_SLOTS and bag <= (NUM_BAG_SLOTS + NUM_BANKBAGSLOTS)
end
function Lib:IsReagents(bag)
return bag == REAGENTBANK_CONTAINER
end
--[[ Embedding ]]--
function Lib:Embed(object)
for k, v in pairs(Lib) do
if k ~= 'Embed' and type(v) == 'function' and type(Events[k]) ~= 'function' then
object[k] = v
end
end
end