-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.lua
More file actions
330 lines (284 loc) · 11.7 KB
/
Core.lua
File metadata and controls
330 lines (284 loc) · 11.7 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
local addonName, Addon = ...
-------------------------------------------------------------------------
-- 1. Helpers
-------------------------------------------------------------------------
local GetContainerNumSlots = C_Container and C_Container.GetContainerNumSlots or GetContainerNumSlots
local GetContainerItemInfo = C_Container and C_Container.GetContainerItemInfo or GetContainerItemInfo
local PickupContainerItem = C_Container and C_Container.PickupContainerItem or PickupContainerItem
local format, insert, floor, ipairs = string.format, table.insert, math.floor, ipairs
function Addon:Print(message)
DEFAULT_CHAT_FRAME:AddMessage(self.BrandPrefix .. message)
end
function Addon:FormatCommaNumber(number)
return tostring(number):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
end
function Addon:FormatCurrency(rawValue)
local value = math.max(rawValue or 0, 0)
local gold = floor(value / 10000)
local silver = floor((value % 10000) / 100)
local copper = value % 100
local parts = {}
local goldColor = Addon.CurrencyColors.GOLD
local silverColor = Addon.CurrencyColors.SILVER
local copperColor = Addon.CurrencyColors.COPPER
if gold > 0 then
insert(parts, format("|cffffffff%s|r|cff%sg|r", Addon:FormatCommaNumber(gold), goldColor))
end
if gold > 0 then
insert(parts, format("|cffffffff%02d|r|cff%ss|r", silver, silverColor))
elseif silver > 0 then
insert(parts, format("|cffffffff%d|r|cff%ss|r", silver, silverColor))
end
if gold > 0 or silver > 0 then
insert(parts, format("|cffffffff%02d|r|cff%sc|r", copper, copperColor))
else
insert(parts, format("|cffffffff%d|r|cff%sc|r", copper, copperColor))
end
return table.concat(parts, " ")
end
function Addon:IsQuestCompleted(questId)
if C_QuestLog and C_QuestLog.IsQuestFlaggedCompleted then
return C_QuestLog.IsQuestFlaggedCompleted(questId)
end
return false
end
-------------------------------------------------------------------------
-- 2. Ignore List
-------------------------------------------------------------------------
function Addon:IsIgnored(itemId)
return MagicEraserCharDB and MagicEraserCharDB.ignoreList and MagicEraserCharDB.ignoreList[itemId]
end
function Addon:ToggleIgnore(itemId)
if not itemId then
return
end
if MagicEraserCharDB.ignoreList[itemId] then
MagicEraserCharDB.ignoreList[itemId] = nil
else
MagicEraserCharDB.ignoreList[itemId] = true
end
Addon:InvalidateCache()
Addon:RefreshDisplay()
end
function Addon:ClearIgnoreList()
wipe(MagicEraserCharDB.ignoreList)
Addon:InvalidateCache()
Addon:RefreshDisplay()
end
-------------------------------------------------------------------------
-- 3. Scan Cache
-------------------------------------------------------------------------
local cachedItem = nil
local isCacheValid = false
function Addon:InvalidateCache()
isCacheValid = false
cachedItem = nil
end
-------------------------------------------------------------------------
-- 4. Scanning & Evaluation Logic
-------------------------------------------------------------------------
function Addon:GetItemDeleteReason(itemId, rarity, sellPrice, requiredLevel)
local playerLevel = UnitLevel("player")
local questItemDatabase = Addon.AllowedDeleteQuestItems or {}
local consumableDatabase = Addon.AllowedDeleteConsumables or {}
local equipmentDatabase = Addon.AllowedDeleteEquipment or {}
if questItemDatabase[itemId] then
for _, questId in ipairs(questItemDatabase[itemId]) do
if self:IsQuestCompleted(questId) then
return "quest"
end
end
elseif consumableDatabase[itemId] then
if (playerLevel - (requiredLevel or 1)) >= 10 then
return "consumable"
end
elseif equipmentDatabase[itemId] then
return "equipment"
elseif rarity == 0 and (sellPrice or 0) > 0 then
return "gray"
end
return nil
end
local function isBetterDeletionCandidate(candidate, current)
if candidate.value < current.value then
return true
end
if candidate.value == current.value then
return Addon.DeletePriority[candidate.deleteReason] < Addon.DeletePriority[current.deleteReason]
end
return false
end
function Addon:FindItemToDelete()
if isCacheValid then
return cachedItem
end
local best = nil
local _, playerClass = UnitClass("player")
local isDataMissing = false
local classReagentExclusions = (Addon.ClassReagentExclusions and Addon.ClassReagentExclusions[playerClass]) or {}
for bag = 0, 4 do
local slotCount = GetContainerNumSlots(bag) or 0
for slot = 1, slotCount do
local itemInfo = GetContainerItemInfo(bag, slot)
if itemInfo and itemInfo.hyperlink then
local itemId = itemInfo.itemID
if not Addon:IsIgnored(itemId) and not classReagentExclusions[itemId] then
local name, _, rarity, _, requiredLevel, _, _, _, _, icon, sellPrice =
GetItemInfo(itemInfo.hyperlink)
if not name then
isDataMissing = true
if C_Item and C_Item.RequestLoadItemDataByID then
C_Item.RequestLoadItemDataByID(itemId)
end
else
local count = itemInfo.stackCount or 1
local totalValue = (sellPrice or 0) * count
local deleteReason = self:GetItemDeleteReason(itemId, rarity, sellPrice, requiredLevel)
if deleteReason then
local candidate = {
link = itemInfo.hyperlink,
itemId = itemId,
count = count,
value = totalValue,
icon = icon,
bag = bag,
slot = slot,
deleteReason = deleteReason
}
if not best or isBetterDeletionCandidate(candidate, best) then
best = candidate
end
end
end
end
end
end
end
if isDataMissing then
C_Timer.After(
1.0,
function()
Addon:RefreshDisplay()
end
)
end
cachedItem = best
isCacheValid = true
return best
end
-------------------------------------------------------------------------
-- 5. Deletion
-------------------------------------------------------------------------
function Addon:RunEraser()
if InCombatLockdown() then
self:Print(Addon.Colors.TEXT .. "Cannot erase items while in combat.|r")
return
end
local item = self:FindItemToDelete()
if item then
if CursorHasItem() then
ClearCursor()
end
PickupContainerItem(item.bag, item.slot)
local cursorType, cursorItemId = GetCursorInfo()
if cursorType == "item" and cursorItemId == item.itemId then
DeleteCursorItem()
PlaySound(5156)
local stackString = (item.count > 1) and format(" x%d", item.count) or ""
local valueString
if item.deleteReason == "quest" then
valueString = ", this item was associated with a quest you have completed"
elseif item.value > 0 then
valueString = format(", worth %s", Addon:FormatCurrency(item.value))
else
valueString = ""
end
self:Print(Addon.Colors.TEXT .. format("Erased %s%s%s.|r", item.link, stackString, valueString))
Addon:InvalidateCache()
C_Timer.After(
0.2,
function()
Addon:RefreshDisplay()
end
)
return
else
self:Print(Addon.Colors.TEXT .. "Slow down! You are clicking faster than the game can erase items.|r")
ClearCursor()
end
else
self:Print(
Addon.Colors.TEXT ..
"Congratulations, your bags are full of good stuff! You'll have to manually erase something if you want to free up more space.|r"
)
end
Addon:RefreshDisplay()
end
-------------------------------------------------------------------------
-- 6. Events
-------------------------------------------------------------------------
local updatePending = false
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:RegisterEvent("BAG_UPDATE_DELAYED")
eventFrame:RegisterEvent("QUEST_TURNED_IN")
eventFrame:SetScript(
"OnEvent",
function(self, event, ...)
if event == "QUEST_TURNED_IN" then
local questId = ...
C_Timer.After(
1.0,
function()
local questItemDatabase = Addon.AllowedDeleteQuestItems or {}
local alertedItems = {}
for bag = 0, 4 do
local slotCount = GetContainerNumSlots(bag) or 0
for slot = 1, slotCount do
local itemInfo = GetContainerItemInfo(bag, slot)
if itemInfo then
local itemId = itemInfo.itemID
if questItemDatabase[itemId] and not alertedItems[itemId] then
for _, trackedQuestId in ipairs(questItemDatabase[itemId]) do
if trackedQuestId == questId then
Addon:Print(
Addon.Colors.TEXT ..
format("%s can now be safely erased!|r", itemInfo.hyperlink)
)
alertedItems[itemId] = true
break
end
end
end
end
end
end
Addon:InvalidateCache()
Addon:RefreshDisplay()
end
)
elseif event == "PLAYER_LOGIN" then
MagicEraserDB = MagicEraserDB or {}
MagicEraserDB.minimap = MagicEraserDB.minimap or {}
MagicEraserCharDB = MagicEraserCharDB or {}
MagicEraserCharDB.ignoreList = MagicEraserCharDB.ignoreList or {}
local LibDBIcon = LibStub("LibDBIcon-1.0", true)
if LibDBIcon and Addon.LDBObject then
LibDBIcon:Register(addonName, Addon.LDBObject, MagicEraserDB.minimap)
end
Addon:RefreshDisplay()
else
if not updatePending then
updatePending = true
C_Timer.After(
0.1,
function()
Addon:InvalidateCache()
Addon:RefreshDisplay()
updatePending = false
end
)
end
end
end
)