-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEasyDisenchantRevamped.lua
576 lines (500 loc) · 15.9 KB
/
EasyDisenchantRevamped.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
--[[
EasyDisenchantRevamped (C) FHeerdink <[email protected]>
Forked from EasyDisenchant with Approval from Kruithne <[email protected]>
Licensed under GNU General Public Licence version 3.BlacklistItem
https://github.com/FHeerdink/EasyDisenchantRevamped
EasyDisenchantRevamped.lua - Contains the core functionality of the addon.
]]--
do
-- [[ Globals ]] --
local PlaySound = PlaySound;
local HideUIPanel = HideUIPanel;
local math_floor = math.floor;
local string_match = string.match;
local tonumber = tonumber;
local strlower = strlower;
local pairs = pairs;
local _K = Krutilities;
local _M = {
ADDON_NAME = "EasyDisenchantRevamped",
chatFormat = "Easy Disenchant Revamped: %s",
eventFrame = CreateFrame("FRAME"),
itemButtons = {},
eventMap = {}, -- Used for internal mapping of events.
isTradeSkillFrameHooked = false,
tradeSkillID = 333, -- CHARACTER_PROFESSION_ENCHANTING
maxButtons = 89,
buttonRenderingCache = {}
};
BINDING_HEADER_EASY_DISENCHANT = _M.ADDON_NAME;
BINDING_NAME_EASY_DISENCHANT_OPEN = SHOW;
-- Set table __index call to pass-through strings.
setmetatable(_M, { __index = function(t, k) return t.Strings[k]; end });
_M.ApplyLocalization = function(self, locale)
local strings = self.Strings;
for key, str in pairs(locale) do
strings[key] = str;
end
end
_M.GetItemIDFromLink = function(itemLink)
return tonumber(string_match(itemLink, "Hitem:(%d+)"));
end
_M.IsBlacklisted = function(self, itemID)
return self.blacklist[itemID];
end
_M.BlacklistItem = function(self, itemID, itemLink)
self.blacklist[itemID] = true;
self.lastBlacklistedItem = itemID;
self.lastBlacklistedItemLink = itemLink;
self:Print(self.BLACKLIST_ADD_ITEM:format(itemLink));
self:Print(self.BLACKLIST_INFO);
end
_M.ResetBlacklist = function(self)
self.blacklist = {};
EasyDisenchantBlacklist = self.blacklist;
self:Print(self.BLACKLIST_RESET);
self.InvokeWindowOpen(); -- Refresh display.
end
_M.UndoBlacklist = function(self)
if self.lastBlacklistedItem and self:IsBlacklisted(self.lastBlacklistedItem) then
self.blacklist[self.lastBlacklistedItem] = nil;
self:Print(self.BLACKLIST_REMOVE_ITEM:format(self.lastBlacklistedItemLink));
self.lastBlacklistedItem = nil;
self.lastBlacklistedItemLink = nil;
self.InvokeWindowOpen(); -- Refresh display.
end
end
_M.Print = function(self, message)
DEFAULT_CHAT_FRAME:AddMessage(self.chatFormat:format(message));
end
_M.SetEventHandler = function(self, event, func)
self.eventMap[event] = func;
self.eventFrame:RegisterEvent(event);
end
_M.RemoveEventHandler = function(self, event)
self.eventMap[event] = nil;
self.eventFrame:UnregisterEvent(event);
end
_M.HookTradeSkillFrame = function(self)
self:SetEventHandler("TRADE_SKILL_DATA_SOURCE_CHANGED", _M.CheckTradeSkillButton);
self.isTradeSkillFrameHooked = true;
end
_M.CreateTradeSkillButton = function(self)
self.tradeSkillButton = _K:Frame({
name = "EasyDisenchantRevampedTradeSkillButton",
parent = TradeSkillFrame,
enableMouse = true,
size = {256, 78},
points = {
point = "TOPRIGHT",
relativePoint = "BOTTOMRIGHT",
},
textures = {
{
texture = [[Interface\Transmogrify\TransmogToast]],
texCoord = {0, 1, 0, 0.609375}
},
{
texture = [[Interface\ICONS\INV_Enchant_Disenchant]],
subLevel = -1,
size = 45,
points = {
point = "LEFT", x = 15,
}
}
},
texts = {
text = "EasyDisenchantRevamped",
inherit = "GameFontHighlight",
points = {
point = "LEFT", x = 72
}
},
scripts = {
OnMouseUp = self.InvokeWindowOpen
}
});
end
_M.CheckTradeSkillButton = function(self)
if C_TradeSkillUI.GetTradeSkillLine() == self.tradeSkillID then
if not self.tradeSkillButton then
self:CreateTradeSkillButton();
end
self.tradeSkillButton:Show();
else
if self.tradeSkillButton then
self.tradeSkillButton:Hide();
end
end
end
_M.GetItemButtonRenderingCache = function(self)
local cache = self.buttonRenderingCache;
if not cache.hasCreated then
local frame = self.disenchantFrame;
-- Called when an item button is clicked, as a post-event.
cache.func_clickHook = function(self, key)
if InCombatLockdown() then
frame.header:SetText(ERR_NOT_IN_COMBAT);
frame.header:SetTextColor(1, 0, 0);
else
if key == "RightButton" then
_M:BlacklistItem(self.itemID, self.link);
end
self:Hide();
end
end
-- Called when the player's cursor leaves an item button.
cache.func_mouseLeave = function(self)
frame.glow:Hide();
GameTooltip:Hide();
end
-- Called when the player's cursor enters an item button.
cache.func_mouseEnter = function(self)
frame.glow:SetPoint("CENTER", self);
frame.glow:Show();
GameTooltip:SetOwner(self, "ANCHOR_LEFT");
GameTooltip:SetHyperlink(self.link);
GameTooltip:Show();
end
cache.factory = function(index)
return {
type = "ItemButton",
parent = self.disenchantFrame,
parentName = "ItemButton" .. index,
inherit = "SecureActionButtonTemplate",
textures = {
injectSelf = "backdrop",
layer = "BACKGROUND",
texture = [[Interface\Buttons\UI-EmptySlot-Disabled]],
size = 54,
},
points = {
point = "TOPLEFT",
x = 38 + (38 * (index % 9)),
y = -73 + (math_floor(index / 9) * -38)
},
scripts = {
OnEnter = cache.func_mouseEnter,
OnLeave = cache.func_mouseLeave
},
};
end
-- Prevent this scope being run again.
cache.hasCreated = true;
end
return cache;
end
_M.GetItemButton = function(self, index)
local buttons = self.itemButtons;
if buttons[index + 1] then
return buttons[index + 1];
end
local cache = self:GetItemButtonRenderingCache();
local button = _K:Frame(cache.factory(index));
button:HookScript("OnClick", cache.func_clickHook);
button:RegisterForClicks("LeftButtonDown", "RightButtonDown");
buttons[#buttons + 1] = button;
return button;
end
_M.ScanEquipmentManager = function(self)
local numOutfits = C_EquipmentSet.GetNumEquipmentSets();
local equipmentCache = {};
-- Equipment sets appear to be zero-index since 8.0?
for index = 0, numOutfits - 1 do
local _, _, setID = C_EquipmentSet.GetEquipmentSetInfo(index);
if setID ~= nil then
local itemLocations = C_EquipmentSet.GetItemLocations(setID);
for slotIndex, itemLocation in pairs(itemLocations) do
local isInBags = (bit.band(itemLocation, ITEM_INVENTORY_LOCATION_BAGS) ~= 0);
if isInBags then
local _, _, _, _, itemSlotIndex, itemBagIndex = EquipmentManager_UnpackLocation(itemLocation);
if itemSlotIndex ~= nil and itemBagIndex ~= nil then
local itemID = C_Container.GetContainerItemID(itemBagIndex, itemSlotIndex);
equipmentCache[table.concat({itemBagIndex, itemSlotIndex, itemID}, "-")] = true;
end
end
end
else
self:Print(self.BROKEN_ITEM_SET);
end
end
self.equipmentCache = equipmentCache;
end
_M.ResetEquipmentManagerCache = function(self)
self.equipmentCache = nil;
end
_M.IsItemInOutfit = function(self, bagID, slotID, itemID)
-- Check Outfitter if installed.
if Outfitter then
local inventoryCache = Outfitter:GetInventoryCache();
-- Call this method to mark items with UsedInOutfit
inventoryCache:CompiledUnusedItemsList();
local vItems = inventoryCache.ItemsByCode[itemID];
if vItems ~= nil then
for _, vItemInfo in ipairs(vItems) do
-- We found our item now check if its used in an outfit.
if vItemInfo.UsedInOutfit == true then
return true
end
end
end
else
if self.equipmentCache == nil then
-- Only process outfit data once per window open.
self:ScanEquipmentManager();
end
return self.equipmentCache[table.concat({bagID, slotID, itemID}, "-")] or false;
end
return false;
end
_M.UpdateItems = function(self)
-- Hide buttons.
local buttons = _M.itemButtons;
local nButtons = #buttons;
for i = 1, nButtons do
buttons[i]:Hide();
end
local disenchantName = GetSpellInfo(13262);
local macroFormat = "/stopmacro [combat][btn:2]\n/stopcasting\n/cast %s\n/cast %s %s";
self:ResetEquipmentManagerCache();
local useButton = 0;
for bagID = 0, NUM_BAG_SLOTS do
for slotID = 1, C_Container.GetContainerNumSlots(bagID) do
local item = C_Container.GetContainerItemInfo(bagID, slotID);
-- Skip non-existant items or legendary+.
if item ~= nul and item.hyperlink ~= nil and (item.quality ~= nil and item.quality < 5 and item.quality > 1) then
local itemName, _, _, _, _, itemClass, itemSubClass = GetItemInfo(item.hyperlink);
-- Avoid breaking on M+ keys
if itemSubClass ~= nil then
-- Check Blacklist
local itemID = item.itemID;
if not self:IsBlacklisted(itemID) and self:IsItemInOutfit(bagID, slotID, itemID) == false then
-- Only disenchant weapons and armour.
if itemClass == WEAPON or itemClass == ARMOR or itemSubClass:find(ITEM_QUALITY6_DESC) then
local button = self:GetItemButton(useButton);
SetItemButtonTexture(button, item.iconFileID);
SetItemButtonQuality(button, item.quality, item.hyperlink);
button:SetAttribute("type", "macro");
-- button:SetAttribute("macrotext", string.format('/cast %s\n/cast %s %s', disenchantName, bagID, slotID));
button:SetAttribute("macrotext", macroFormat:format(disenchantName, bagID, slotID));
button.link = item.hyperlink;
button.itemID = itemID;
-- button.bagID = bagID;
-- button.slotID = slotID;
button:Show();
if useButton == self.maxButtons then
return;
end
useButton = useButton + 1;
end
end
end
end
end
end
end
_M.CreateDisenchantFrame = function(self)
local bgAnchor = {
{ point = "TOPLEFT", x = 8, y = -8 },
{ point = "BOTTOMRIGHT", x = -8, y = 8 }
};
local cornerMixin = {
layer = "BACKGROUND",
size = 64,
subLevel = -2,
texture = [[Interface\Transmogrify\Textures]]
};
local edgeXMixin = {
tileX = true,
subLevel = -3,
size = {64, 23},
layer = "BACKGROUND",
texture = [[Interface\Transmogrify\HorizontalTiles]]
};
local edgeYMixin = {
tileY = true,
subLevel = -3,
size = {23, 64},
layer = "BACKGROUND",
texture = [[Interface\Transmogrify\VerticalTiles]]
};
self.disenchantFrame = _K:Frame({
name = "Easy Disenchant Revamped Frame",
size = {418, 472},
frames = {
{ -- Close Button
type = "BUTTON",
parentName = "CloseButton",
inherit = "UIPanelCloseButton",
points = { point = "TOPRIGHT", x = -20, y = -25 }
},
{ -- Glow used by the item buttons.
size = 37,
hidden = true,
injectSelf = "glow",
textures = {
{
parentName = "InnerGlow", injectSelf = "innerGlow",
texture = [[Interface\SpellActivationOverlay\IconAlert]],
size = 53, points = { point = "CENTER" },
texCoord = { 0.00781250, 0.50781250, 0.27734375, 0.52734375 }
},
{
layer = "OVERLAY", parentName = "Ants", injectSelf = "ants",
texture = [[Interface\SpellActivationOverlay\IconAlertAnts]],
size = 44, points = { point = "CENTER" }
}
},
scripts = {
OnUpdate = function(self, elapsed) AnimateTexCoords(self.ants, 256, 256, 48, 48, 22, elapsed, 0.01); end
}
}
},
texts = {
{
inherit = "GameFontHighlightMedium",
text = "Easy Disenchant Revamped",
injectSelf = "header",
points = { point = "TOPLEFT", x = 35, y = -40 }
},
{
inherit = "GameFontHighlightMedium",
text = self.INFO,
justifyH = "CENTER",
points = { point = "BOTTOM", y = 30 }
}
},
textures = {
{ -- Background.
layer = "BACKGROUND",
subLevel = -6,
texture = [[Interface\FrameGeneral\UI-Background-Marble]],
tile = true,
points = bgAnchor,
color = {0.302, 0.102, 0.204, 0.8}
},
{ -- Corner: Top Left
parentName = "CornerTL",
mixin = cornerMixin,
points = "TOPLEFT",
texCoord = {0.00781250, 0.50781250, 0.00195313, 0.12695313}
},
{ -- Corner: Top Right
parentName = "CornerTR",
mixin = cornerMixin,
points = "TOPRIGHT",
texCoord = {0.00781250, 0.50781250, 0.38476563, 0.50781250}
},
{ -- Corner: Bottom Left
parentName = "CornerBL",
mixin = cornerMixin,
points = "BOTTOMLEFT",
texCoord = {0.0078125, 0.5078125, 0.2578125, 0.38085938}
},
{ -- Corner: Bottom Right
parentName = "CornerBR",
mixin = cornerMixin,
points = "BOTTOMRIGHT",
texCoord = {0.0078125, 0.5078125, 0.13085938, 0.25390625}
},
{ -- Edge: Top
parentName = "TopEdge",
mixin = edgeXMixin,
points = {
{ point = "TOPLEFT", relativeTo = "$parentCornerTL", relativePoint = "TOPRIGHT", x = -30, y = -5 },
{ point = "TOPRIGHT", relativeTo = "$parentCornerTR", relativePoint = "TOPLEFT", x = 30, y = -5 }
},
texCoord = {0, 1, 0.40625, 0.765625}
},
{ -- Edge: Bottom
parentName = "BottomEdge",
mixin = edgeXMixin,
points = {
{ point = "BOTTOMLEFT", relativeTo = "$parentCornerBL", relativePoint = "BOTTOMRIGHT", x = -30, y = 4 },
{ point = "BOTTOMRIGHT", relativeTo = "$parentCornerBR", relativePoint = "BOTTOMLEFT", x = 30, y = 4 }
},
texCoord = {0, 1, 0.015625, 0.375}
},
{ -- Edge: Left
parentName = "LeftEdge",
mixin = edgeYMixin,
points = {
{ point = "TOPLEFT", relativeTo = "$parentCornerTL", relativePoint = "BOTTOMLEFT", x = 4, y = 16 },
{ point = "BOTTOMLEFT", relativeTo = "$parentCornerBL", relativePoint = "TOPLEFT", x = 4, y = -16 }
},
texCoord = {0.40625, 0.765625, 0, 1}
},
{ -- Edge: Right
parentName = "RightEdge",
mixin = edgeYMixin,
points = {
{ point = "TOPRIGHT", relativeTo = "$parentCornerTR", relativePoint = "BOTTOMRIGHT", x = -4, y = 16 },
{ point = "BOTTOMRIGHT", relativeTo = "$parentCornerBR", relativePoint = "TOPRIGHT", x = -4, y = -16 }
},
texCoord = {0.015625, 0.375, 0, 1}
}
},
scripts = {
OnHide = function() PlaySound(SOUNDKIT.UI_ETHEREAL_WINDOW_CLOSE); end
}
});
end
_M.OpenWindow = function(self)
HideUIPanel(TradeSkillFrame);
if not self.disenchantFrame then
self:CreateDisenchantFrame();
end
self:UpdateItems();
self.disenchantFrame:Show();
PlaySound(SOUNDKIT.UI_ETHEREAL_WINDOW_CLOSE);
end
_M.OnCommand = function(msg)
msg = strlower(msg);
if msg == "reset" then
-- Reset the entire blacklist.
_M:ResetBlacklist();
elseif msg == "undo" then
-- Revert last addition to the blacklist.
_M:UndoBlacklist();
else
-- Everything else just opens the window.
_M:InvokeWindowOpen();
end
end
_M.OnLoad = function(self)
-- Register command.
SLASH_DISENCHANT1, SLASH_DISENCHANT2 = "/disenchant", "/de";
SlashCmdList["DISENCHANT"] = _M.OnCommand;
-- Created store blacklist table if it doesn't exist.
if not EasyDisenchantBlacklist then
EasyDisenchantBlacklist = {};
end
-- Store local reference to our stored table.
self.blacklist = EasyDisenchantBlacklist;
-- Hook to TradeSkillFrame.
if not self.isTradeSkillFrameHooked and IsAddOnLoaded("Blizzard_TradeSkillUI") then
self:HookTradeSkillFrame();
end
end
_M.OnEvent = function(self, event, ...)
-- Note: self is not _M in this instance, it's eventFrame.
local handler = _M.eventMap[event];
if handler then
handler(_M, ...);
end
end
_M.InvokeWindowOpen = function()
_M:OpenWindow();
end
_M.OnAddonLoaded = function(self, addonName)
if addonName == self.ADDON_NAME then
self:OnLoad();
elseif addonName == "Blizzard_TradeSkillUI" then
_M:HookTradeSkillFrame();
end
end
_M.eventFrame:SetScript("OnEvent", _M.OnEvent);
_M:SetEventHandler("ADDON_LOADED", _M.OnAddonLoaded);
EasyDisenchantShowWindow = _M.InvokeWindowOpen; -- Expose window open function.
EasyDisenchant = _M; -- Expose addon container.
end