Skip to content

Commit 6481cf5

Browse files
committed
Rework item cache to use links instead of IDs to better handle items with bonusIDs (like shadowlands legendaries)
1 parent 34e46ab commit 6481cf5

File tree

10 files changed

+209
-170
lines changed

10 files changed

+209
-170
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Bug Fixes
66
* #1840 - Talents granted by Torghast powers are now correctly reflected by the Talent Learned condition.
77
* #1844 - The Totem icon type has been updated for Monks to better support the wide range of "totems" that Monks have.
8+
* #1842 - Fixed handling of Shadowlands legendaries in item suggestion lists
89

910
## v9.0.3
1011
### Bug Fixes

Components/Core/Common/Item.lua

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ function ItemByID:GetName()
235235
return name
236236
end
237237
end
238+
function ItemByID:GetLink()
239+
local _, itemLink = GetItemInfo(self.itemID)
240+
return itemLink
241+
end
238242

239243

240244

@@ -271,7 +275,7 @@ function ItemByName:GetCooldown()
271275
end
272276

273277
function ItemByName:GetID()
274-
local _, itemLink = GetItemInfo(self.name)
278+
local itemLink = self:GetLink()
275279
if itemLink then
276280
return tonumber(strmatch(itemLink, ":(%d+)"))
277281
end
@@ -287,6 +291,10 @@ function ItemByName:GetName()
287291
return self.name
288292
end
289293
end
294+
function ItemByName:GetLink()
295+
local _, itemLink = GetItemInfo(self.name)
296+
return itemLink
297+
end
290298

291299

292300

@@ -297,6 +305,36 @@ end
297305

298306
local ItemBySlot = TMW:NewClass("ItemBySlot", Item)
299307

308+
309+
local slotNames = {}
310+
for _, slotName in pairs{
311+
"BackSlot",
312+
"ChestSlot",
313+
"FeetSlot",
314+
"Finger0Slot",
315+
"Finger1Slot",
316+
"HandsSlot",
317+
"HeadSlot",
318+
"LegsSlot",
319+
"MainHandSlot",
320+
"NeckSlot",
321+
"SecondaryHandSlot",
322+
"ShirtSlot",
323+
"ShoulderSlot",
324+
"TabardSlot",
325+
"Trinket0Slot",
326+
"Trinket1Slot",
327+
"WaistSlot",
328+
"WristSlot",
329+
} do
330+
local slotID = GetInventorySlotInfo(slotName)
331+
if slotID then
332+
slotNames[slotID] = slotName
333+
else
334+
TMW:Debug("Invalid slot name %s", slotName)
335+
end
336+
end
337+
300338
function ItemBySlot:OnNewInstance(itemSlot)
301339
TMW:ValidateType("2 (itemSlot)", "ItemByID:New(itemSlot)", itemSlot, "number")
302340

@@ -323,8 +361,12 @@ function ItemBySlot:GetID()
323361
return GetInventoryItemID("player", self.slot)
324362
end
325363
function ItemBySlot:GetName()
326-
local name = GetItemInfo(self:GetLink())
327-
return name
364+
local link = self:GetLink()
365+
if link then
366+
local name = GetItemInfo(link)
367+
if name then return name end
368+
end
369+
return slotNames[self.slot] and _G[slotNames[self.slot]:upper()] or nil
328370
end
329371
function ItemBySlot:GetLink()
330372
return GetInventoryItemLink("player", self.slot)
@@ -343,7 +385,7 @@ function ItemByLink:OnNewInstance(itemLink)
343385
TMW:ValidateType("2 (itemLink)", "ItemByID:New(itemLink)", itemLink, "string")
344386

345387
self.link = itemLink
346-
self.itemID = tonumber(strmatch(itemLink, ":(%d+)"))
388+
self.itemID = tonumber(strmatch(itemLink, "item:(%d+)"))
347389
self.name = GetItemInfo(itemLink)
348390
end
349391

@@ -367,9 +409,15 @@ end
367409

368410
function ItemByLink:GetName()
369411
local name = GetItemInfo(self.link)
412+
if not name then
413+
name = self.link:match("%[(.-)%]")
414+
end
370415
if name then
371416
self.name = name
372417
self.GetName = self.GetName_saved
373418
return name
374419
end
420+
end
421+
function ItemByLink:GetLink()
422+
return self.link
375423
end

Components/Core/Spells/Equivalencies.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ TMW.BE = {
515515
288024, -- Diamond Barrier (Diamond-Laced Refracting Prism, Battle of Dazar'alor raid)
516516
295271, -- Umbral Shell (Void Stone, Crucible of Storms raid)
517517
295431, -- Ephemeral Vigor (Abyssal Speaker's Gauntlets, Crucible of Storms raid)
518+
311444, -- Indomitable Deck (Shadowlands tank darkmoon deck)
519+
322507, -- Celestial Brew (Monk, brewmaster)
518520
324867, -- Fleshcraft (Necrolord)
519521
},
520522
ImmuneToMagicCC = {

Components/Core/Spells/ItemCache.lua

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TMW.IE:RegisterDatabaseDefaults{
4141
},
4242
}
4343

44-
TMW.IE:RegisterUpgrade(72310, {
44+
TMW.IE:RegisterUpgrade(90403, {
4545
locale = function(self, locale)
4646
locale.ItemCache = nil
4747
locale.XPac_ItemCache = nil
@@ -59,7 +59,7 @@ TMW.IE:RegisterUpgrade(62217, {
5959

6060
--[[ Returns the main cache table. Structure:
6161
Cache = {
62-
[itemID] = 1,
62+
[link] = 1,
6363
}
6464
]]
6565
function ItemCache:GetCache()
@@ -74,8 +74,8 @@ end
7474

7575
--[[ Returns a list of items that the player currently has. Structure:
7676
Cache = {
77-
[itemID] = name,
78-
[name] = itemID,
77+
[link] = 1,
78+
[name] = link,
7979
}
8080
]]
8181
function ItemCache:GetCurrentItems()
@@ -119,14 +119,14 @@ TMW:RegisterCallback("TMW_OPTIONS_LOADED", function()
119119
-- Compile all items from all timesegments into a cohesive table for
120120
-- fast lookups and easy iteration.
121121
for timestamp, items in pairs(Cache) do
122-
for id, name in pairs(items) do
123-
CompiledCache[id] = name
122+
for link in pairs(items) do
123+
CompiledCache[link] = 1
124124
end
125125
end
126126

127127
--Start requests so that we can validate itemIDs.
128-
for id in pairs(CompiledCache) do
129-
GetItemInfo(id)
128+
for link in pairs(CompiledCache) do
129+
GetItemInfo(link)
130130
end
131131

132132
ItemCache:RegisterEvent("BAG_UPDATE")
@@ -135,21 +135,36 @@ TMW:RegisterCallback("TMW_OPTIONS_LOADED", function()
135135
ItemCache:CacheItems()
136136
end)
137137

138-
local function cacheItem(itemID, name)
138+
local function cacheItem(itemID, link)
139+
-- Sanitize the link:
140+
-- strip out all gems/enchants (not an important distinction for TMW)
141+
-- LEAVE suffixID alone (makes sure that the name is correct)
142+
-- strip out uniqueID (not an important distinction for TMW)
143+
-- strip out linkLevel,specializationId
144+
link = link:gsub("(item:%d+):%-?%d*:%-?%d*:%-?%d*:%-?%d*:%-?%d*:(%-?%d*):%-?%d*:%-?%d*:%-?%d*", "%1:::::%2::::")
145+
146+
if link:find("|h%[%]|h") then
147+
-- Links that don't have the item name loaded
148+
-- will just have brackets in the position of the name and nothing else.
149+
return
150+
end
151+
152+
CurrentItems[link] = 1
153+
139154
-- The item is not cached at all.
140-
if not CompiledCache[itemID] then
141-
Cache[currentTimestamp][itemID] = name
142-
CompiledCache[itemID] = name
155+
if not CompiledCache[link] then
156+
Cache[currentTimestamp][link] = 1
157+
CompiledCache[link] = 1
143158

144159
-- The item is in an old cache timesegment.
145-
elseif not Cache[currentTimestamp][itemID] then
160+
elseif not Cache[currentTimestamp][link] then
146161
-- Remove the item from the old cache.
147162
for timestamp, items in pairs(Cache) do
148-
items[itemID] = nil
163+
items[link] = nil
149164
end
150165

151166
-- Add it to the current cache timesegment.
152-
Cache[currentTimestamp][itemID] = name
167+
Cache[currentTimestamp][link] = 1
153168
end
154169
end
155170

@@ -169,30 +184,29 @@ function ItemCache:CacheItems(force)
169184
for container = 0, NUM_BAG_SLOTS do
170185
for slot = 1, GetContainerNumSlots(container) do
171186
local id = GetContainerItemID(container, slot)
187+
local link = GetContainerItemLink(container, slot)
172188
if id then
173-
local name = GetItemInfo(id)
174-
name = name and strlower(name)
175-
176-
CurrentItems[id] = name
177-
cacheItem(id, name)
189+
cacheItem(id, link)
178190
end
179191
end
180192
end
181193

182194
-- Cache equipped items
183195
for slot = 1, 19 do
184196
local id = GetInventoryItemID("player", slot)
197+
local link = GetInventoryItemLink("player", slot)
185198
if id then
186-
local name = GetItemInfo(id)
187-
name = name and strlower(name)
188-
189-
CurrentItems[id] = name
190-
cacheItem(id, name)
199+
cacheItem(id, link)
191200
end
192201
end
193202

194-
for id, name in pairs(CurrentItems) do
195-
CurrentItems[name] = id
203+
local reverseMap = {}
204+
for link in pairs(CurrentItems) do
205+
local name = link:match("%[(.-)%]")
206+
reverseMap[strlower(name)] = link
207+
end
208+
for k, v in pairs(reverseMap) do
209+
CurrentItems[k] = v
196210
end
197211

198212
doUpdateCache = nil

0 commit comments

Comments
 (0)