-
Notifications
You must be signed in to change notification settings - Fork 8
/
Baggins.lua
4385 lines (3942 loc) · 152 KB
/
Baggins.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local LibStub = LibStub
Baggins = LibStub("AceAddon-3.0"):NewAddon("Baggins", "AceEvent-3.0", "AceHook-3.0", "AceBucket-3.0", "AceTimer-3.0", "AceConsole-3.0")
local Baggins = Baggins
local L = LibStub("AceLocale-3.0"):GetLocale("Baggins")
local LBU = LibStub("LibBagUtils-1.0")
local LSM = LibStub:GetLibrary("LibSharedMedia-3.0", true)
local qt = LibStub('LibQTip-1.0')
local dbIcon = LibStub("LibDBIcon-1.0")
local console = LibStub("AceConsole-3.0")
local iui = LibStub("LibItemUpgradeInfo-1.0")
local next, pairs, ipairs, tonumber, select, strmatch, wipe, type, time, print =
next, pairs, ipairs, tonumber, select, strmatch, wipe, type, time, print
local min, max, ceil, floor, mod =
min, max, ceil, floor, mod
local tinsert, tremove, tsort, tconcat =
tinsert, tremove, table.sort, table.concat
local format =
string.format
local band =
bit.band
local IsAddOnLoaded = C_AddOns and C_AddOns.IsAddOnLoaded and C_AddOns.IsAddOnLoaded or IsAddOnLoaded
local BlizzSortBags = C_Container and C_Container.SortBags
local CloseBankFrame = C_Bank and C_Bank.CloseBankFrame or CloseBankFrame
local GetItemCount, GetItemInfo, GetInventoryItemLink, GetItemQualityColor, GetItemFamily, BankButtonIDToInvSlotID, GetNumBankSlots =
GetItemCount, GetItemInfo, GetInventoryItemLink, GetItemQualityColor, GetItemFamily, BankButtonIDToInvSlotID, GetNumBankSlots
local GetContainerItemInfo, GetContainerItemLink, GetContainerNumFreeSlots, GetContainerItemCooldown =
C_Container and C_Container.GetContainerItemInfo or GetContainerItemInfo, C_Container and C_Container.GetContainerItemLink or GetContainerItemLink, C_Container and C_Container.GetContainerNumFreeSlots or GetContainerNumFreeSlots, C_Container and C_Container.GetContainerItemCooldown or GetContainerItemCooldown
local BANK_PANELS = BANK_PANELS
local ItemButtonUtil = ItemButtonUtil
local IsBagOpen = IsBagOpen
local ShowInspectCursor = ShowInspectCursor
local ShowContainerSellCursor = C_Container and C_Container.ShowContainerSellCursor or ShowContainerSellCursor
local Enum = Enum and Enum
local ReagentBankButtonIDToInvSlotID, GetContainerItemQuestInfo, DepositReagentBank, IsReagentBankUnlocked =
ReagentBankButtonIDToInvSlotID, C_Container and C_Container.GetContainerItemQuestInfo or GetContainerItemQuestInfo, DepositReagentBank and DepositReagentBank, IsReagentBankUnlocked and IsReagentBankUnlocked
local IsContainerItemAnUpgrade = IsContainerItemAnUpgrade and IsContainerItemAnUpgrade
local C_ItemUpgrade = C_ItemUpgrade and C_ItemUpgrade
local C_Item, ItemLocation, InCombatLockdown, IsModifiedClick, GetDetailedItemLevelInfo, GetContainerItemID, InRepairMode, KeyRingButtonIDToInvSlotID, C_PetJournal, C_NewItems, PlaySound =
C_Item, ItemLocation, InCombatLockdown, IsModifiedClick, GetDetailedItemLevelInfo, C_Container and C_Container.GetContainerItemID or GetContainerItemID, InRepairMode, KeyRingButtonIDToInvSlotID, C_PetJournal, C_NewItems, PlaySound
local UseContainerItem = C_Container and C_Container.UseContainerItem or UseContainerItem
local WOW_PROJECT_ID = WOW_PROJECT_ID
local WOW_PROJECT_CLASSIC = WOW_PROJECT_CLASSIC
local WOW_PROJECT_BURNING_CRUSADE_CLASSIC = WOW_PROJECT_BURNING_CRUSADE_CLASSIC
local WOW_PROJECT_WRATH_CLASSIC = WOW_PROJECT_WRATH_CLASSIC
local WOW_PROJECT_MAINLINE = WOW_PROJECT_MAINLINE
local LE_EXPANSION_LEVEL_CURRENT = LE_EXPANSION_LEVEL_CURRENT
local LE_EXPANSION_BURNING_CRUSADE = LE_EXPANSION_BURNING_CRUSADE
local LE_EXPANSION_WRATH_OF_THE_LICH_KING = LE_EXPANSION_WRATH_OF_THE_LICH_KING
-- Bank tab locals, for auto reagent deposit
local BANK_TAB = BANK_PANELS[1].name
local REAGENT_BANK_TAB = BANK_PANELS and BANK_PANELS[2] and BANK_PANELS[2].name
Baggins.hasIcon = "Interface\\Icons\\INV_Jewelry_Ring_03"
Baggins.cannotDetachTooltip = true
Baggins.clickableTooltip = true
Baggins.independentProfile = true
Baggins.hideWithoutStandby = true
-- number of item buttons that should be kept in the pool, so that none need to be created in combat
Baggins.minSpareItemButtons = 10
BINDING_HEADER_BAGGINS = L["Baggins"]
BINDING_NAME_BAGGINS_TOGGLEALL = L["Toggle All Bags"]
BINDING_NAME_BAGGINS_ITEMBUTTONMENU = "Item Menu"
BINDING_NAME_BAGGINS_TOGGLECOMPRESSALL= "Toggle " .. L["Compress All"]
local equiplocs = {
INVTYPE_AMMO = 0,
INVTYPE_HEAD = 1,
INVTYPE_NECK = 2,
INVTYPE_SHOULDER = 3,
INVTYPE_BODY = 4,
INVTYPE_CHEST = 5,
INVTYPE_ROBE = 5,
INVTYPE_WAIST = 6,
INVTYPE_LEGS = 7,
INVTYPE_FEET = 8,
INVTYPE_WRIST = 9,
INVTYPE_HAND = 10,
INVTYPE_FINGER = 11,
INVTYPE_TRINKET = 13,
INVTYPE_CLOAK = 15,
INVTYPE_WEAPON = 16,
INVTYPE_SHIELD = 17,
INVTYPE_2HWEAPON = 16,
INVTYPE_WEAPONMAINHAND = 16,
INVTYPE_WEAPONOFFHAND = 17,
INVTYPE_HOLDABLE = 17,
INVTYPE_RANGED = 18,
INVTYPE_THROWN = 18,
INVTYPE_RANGEDRIGHT = 18,
INVTYPE_RELIC = 18,
INVTYPE_TABARD = 19,
INVTYPE_BAG = 20,
}
Baggins.itemcounts = {}
function Baggins:Debug(str, ...) --luacheck: ignore 212
if not str or strlen(str) == 0 then return end
if (...) then
if strfind(str, "%%%.%d") or strfind(str, "%%[dfqsx%d]") then
str = format(str, ...)
else
str = strjoin(" ", str, tostringall(...))
end
end
local name = "Baggins"
DEFAULT_CHAT_FRAME:AddMessage(format("|cffff9933%s:|r %s", name, str))
end
function Baggins:IsClassicWow() --luacheck: ignore 212
return WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
end
function Baggins:IsTBCWow() --luacheck: ignore 212
return WOW_PROJECT_ID == WOW_PROJECT_BURNING_CRUSADE_CLASSIC and LE_EXPANSION_LEVEL_CURRENT == LE_EXPANSION_BURNING_CRUSADE
end
function Baggins:IsWrathWow() --luacheck: ignore 212
return WOW_PROJECT_ID == WOW_PROJECT_WRATH_CLASSIC and LE_EXPANSION_LEVEL_CURRENT == LE_EXPANSION_WRATH_OF_THE_LICH_KING
end
function Baggins:IsCataWow() --luacheck: ignore 212
return WOW_PROJECT_ID == WOW_PROJECT_CATACLYSM_CLASSIC and LE_EXPANSION_LEVEL_CURRENT == LE_EXPANSION_CATACLYSM
end
function Baggins:IsRetailWow() --luacheck: ignore 212
return WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
end
local timers = {}
function Baggins:ScheduleNamedTimer(name, callback, delay, arg)
local alreadyScheduled = timers[name]
if alreadyScheduled and self:TimeLeft(alreadyScheduled) then
self:CancelTimer(alreadyScheduled, true)
end
timers[name] = self:ScheduleTimer(callback, delay, arg)
end
function Baggins:CancelNamedTimer(name)
local timer = timers[name]
if timer then
timers[name] = nil
self:CancelTimer(timer, true)
end
end
local nextFrameTimers = {}
local timerFrame = CreateFrame('Frame')
timerFrame:SetScript("OnUpdate", function(self)
while next(nextFrameTimers) do
local func = next(nextFrameTimers)
local args = nextFrameTimers[func]
if type(args) == 'table' then
Baggins[func](Baggins, unpack(args))
wipe(args)
else
Baggins[func](Baggins)
end
nextFrameTimers[func] = nil
end
self:Hide()
end)
function Baggins:ScheduleForNextFrame(callback, arg, ...) --luacheck: ignore 212
nextFrameTimers[callback] = arg and { arg, ... } or true
timerFrame:Show()
end
-- internal signalling minilibrary
local signals = {}
function Baggins:RegisterSignal(name, handler, arg1) --luacheck: ignore 212 -- Example: RegisterSignal("MySignal", self.SomeHandler, self)
if not arg1 then error("Usage: Baggins:RegisterSignal(name, handler, arg1)") end
if not signals[name] then
signals[name] = {}
end
signals[name][handler]=arg1;
end
function Baggins:FireSignal(name, ...) --luacheck: ignore 212 -- Example: FireSignal("MySignal", 1, 2, 3);
if signals[name] then
for handler,arg1 in pairs(signals[name]) do
handler(arg1, ...);
end
end
end
local tooltip
local ldbDropDownFrame = CreateFrame("Frame", "Baggins_DropDownFrame", UIParent, "UIDropDownMenuTemplate")
local ldbDropDownMenu
local spacer = { text = "", disabled = true, notCheckable = true, notClickable = true}
local function initDropdownMenu()
if Baggins:IsRetailWow() then
ldbDropDownMenu = {
{
text = "Run Blizzard Bag Sort",
tooltipText = "Runs Blizzard bag Sort",
func = function()
Baggins:CloseAllBags()
BlizzSortBags()
end,
notCheckable = true,
},
{
text = L["Force Full Refresh"],
tooltipText = L["Forces a Full Refresh of item sorting"],
func = function()
Baggins:ForceFullRefresh()
Baggins:Baggins_RefreshBags()
end,
notCheckable = true,
},
spacer,
{
text = L["Hide Default Bank"],
tooltipText = L["Hide the default bank window."],
checked = Baggins.db.profile.hidedefaultbank,
keepShownOnClick = true,
func = function()
Baggins.db.profile.hidedefaultbank = not Baggins.db.profile.hidedefaultbank
end,
},
{
text = L["Override Default Bags"],
tooltipText = L["Baggins will open instead of the default bags"],
checked = Baggins.db.profile.overridedefaultbags,
keepShownOnClick = true,
func = function()
Baggins.db.profile.overridedefaultbags = not Baggins.db.profile.overridedefaultbags
Baggins:UpdateBagHooks()
end,
},
spacer,
{
text = L["Config Window"],
func = function() Baggins:OpenConfig() end,
notCheckable = true,
},
{
text = L["Bag/Category Config"],
func = function() Baggins:OpenEditConfig() end,
notCheckable = true,
},
}
else
ldbDropDownMenu = {
{
text = L["Force Full Refresh"],
tooltipText = L["Forces a Full Refresh of item sorting"],
func = function()
Baggins:ForceFullRefresh()
Baggins:Baggins_RefreshBags()
end,
notCheckable = true,
},
spacer,
{
text = L["Hide Default Bank"],
tooltipText = L["Hide the default bank window."],
checked = Baggins.db.profile.hidedefaultbank,
keepShownOnClick = true,
func = function()
Baggins.db.profile.hidedefaultbank = not Baggins.db.profile.hidedefaultbank
end,
},
{
text = L["Override Default Bags"],
tooltipText = L["Baggins will open instead of the default bags"],
checked = Baggins.db.profile.overridedefaultbags,
keepShownOnClick = true,
func = function()
Baggins.db.profile.overridedefaultbags = not Baggins.db.profile.overridedefaultbags
Baggins:UpdateBagHooks()
end,
},
spacer,
{
text = L["Config Window"],
func = function() Baggins:OpenConfig() end,
notCheckable = true,
},
{
text = L["Bag/Category Config"],
func = function() Baggins:OpenEditConfig() end,
notCheckable = true,
},
}
end
end
local function updateMenu()
if not ldbDropDownMenu then
initDropdownMenu()
return
end
if Baggins:IsRetailWow() then
ldbDropDownMenu[4].checked = Baggins.db.profile.hidedefaultbank
ldbDropDownMenu[5].checked = Baggins.db.profile.overridedefaultbags
else
ldbDropDownMenu[3].checked = Baggins.db.profile.hidedefaultbank
ldbDropDownMenu[4].checked = Baggins.db.profile.overridedefaultbags
end
end
local ldbdata = {
type = "data source",
icon = "Interface\\Icons\\INV_Jewelry_Ring_03",
OnClick = function(_, message)
if message == "RightButton" then
tooltip:Hide()
updateMenu()
Baggins:EasyMenu(ldbDropDownMenu, ldbDropDownFrame, "cursor", 0, 0, "MENU")
-- Baggins:OpenConfig()
else
Baggins:OnClick()
end
end,
label = "Baggins",
text = "",
OnEnter = function(self)
tooltip = qt:Acquire('BagginsTooltip', 1)
tooltip:SetHeaderFont(GameFontNormalLarge)
tooltip:SetScript("OnHide", function(self) --luacheck: ignore 432
qt:Release(self)
end)
Baggins:UpdateTooltip(true)
self.tooltip = tooltip
tooltip:SmartAnchorTo(self)
tooltip:SetAutoHideDelay(0.2, self)
tooltip:Show()
end,
}
Baggins.obj = LibStub("LibDataBroker-1.1"):NewDataObject("Baggins", ldbdata)
do
local buttonCount = 0
local buttonPool = {}
local function createItemButton()
local frameType
if Baggins:IsRetailWow() then
frameType = "ItemButton"
else
frameType = "Button"
end
local frame = CreateFrame(frameType,"BagginsPooledItemButton"..buttonCount,nil,"ContainerFrameItemButtonTemplate")
frame.GetItemContextMatchResult = nil
buttonCount = buttonCount + 1
if InCombatLockdown() then
Baggins:Debug("Baggins: WARNING: item-frame will be tainted")
Baggins:RegisterEvent("PLAYER_REGEN_ENABLED")
frame.tainted = true
end
return frame
end
function Baggins:RepopulateButtonPool(num) --luacheck: ignore 212
if InCombatLockdown() then
Baggins:RegisterEvent("PLAYER_REGEN_ENABLED")
return
end
while #buttonPool < num do
local frame = createItemButton()
tinsert(buttonPool, frame)
end
end
local usedButtons = 0
function Baggins:GetItemButton()
usedButtons = usedButtons + 1
self.db.char.lastNumItemButtons = usedButtons
local frame
if next(buttonPool) then
frame = tremove(buttonPool, 1)
else
frame = createItemButton()
end
self:ScheduleTimer("RepopulateButtonPool", 0, Baggins.minSpareItemButtons)
return frame
end
function Baggins:ReleaseItemButton(button) --luacheck: ignore 212
button.glow:Hide()
button.newtext:Hide()
tinsert(buttonPool, button)
end
end
function Baggins:PLAYER_REGEN_ENABLED()
for _,bagframe in ipairs(Baggins.bagframes) do
for _,section in ipairs(bagframe.sections) do
for i,item in ipairs(section.items) do
if item.tainted then
local tainted = section.items[i]
tainted:Hide()
section.items[i] = self:CreateItemButton()
end
end
end
end
self:ForceFullUpdate()
self:RepopulateButtonPool(Baggins.minSpareItemButtons)
end
function Baggins:OnInitialize()
self.bagframes = {}
self.colors = {
black = {r=0,g=0,b=0,hex="|cff000000"},
white = {r=1,g=1,b=1,hex="|cffffffff"},
blue = {r=0,g=0.5,b=1,hex="|cff007fff"},
purple = {r=1,g=0.4,b=1,hex="|cffff66ff"},
}
self:InitOptions()
local buttonsToPool = (self.db.char.lastNumItemButtons or 90) + Baggins.minSpareItemButtons -- create a few spare buttons
self:RepopulateButtonPool(buttonsToPool)
self:InitBagCategoryOptions()
self:RegisterChatCommand("baggins", "OpenConfig")
self.OnMenuRequest = self.opts
if Baggins:IsClassicWow() or Baggins:IsTBCWow() or Baggins:IsWrathWow() or Baggins:IsCataWow() then
dbIcon:Register("Baggins", ldbdata, self.db.profile.minimap)
end
-- self:RegisterChatCommand({ "/baggins" }, self.opts, "BAGGINS")
end
function Baggins:IsActive() --luacheck: ignore 212
return true
end
--deep copy of a table, will NOT handle tables as keys or circular references
local function deepCopy(to, from)
for k in pairs(to) do
to[k] = nil
end
for k, v in pairs(from) do
if type(v) == "table" then
to[k] = {}
deepCopy(to[k], from[k])
else
to[k] = from[k]
end
end
end
function Baggins:OnProfileEnable()
local p = self.db.profile
--check if this profile has been setup before, if not add the default bags and categories
--cant leave these in the defaults since removing a bag would have it come back on reload
local refresh = false
if not next(p.categories) then
deepCopy(p.categories, self.defaultcategories)
refresh = true
end
if #p.bags == 0 then
local templateName = self.db.global.template
local templates = {}
local template = templates[templateName]
deepCopy(p.bags, template.bags)
refresh = true
end
if refresh then
self:ChangeProfile()
end
self:CreateAllBags()
self:SetCategoryTable(self.db.profile.categories)
self:ResortSections()
self:ForceFullRefresh()
self:Baggins_RefreshBags()
self:BuildMoneyBagOptions()
self:BuildBankControlsBagOptions()
end
function Baggins:OnEnable()
--self:SetBagUpdateSpeed();
self:RegisterEvent("BAG_CLOSED", "ForceFullRefresh")
self:RegisterEvent("BAG_UPDATE","OnBagUpdate")
self:RegisterEvent("BAG_UPDATE_COOLDOWN", "UpdateItemButtonCooldowns")
self:RegisterEvent("ITEM_LOCK_CHANGED", "UpdateItemButtonLocks")
self:RegisterEvent("QUEST_ACCEPTED", "UpdateItemButtons")
self:RegisterEvent("UNIT_QUEST_LOG_CHANGED", "UpdateItemButtons")
self:RegisterEvent("PLAYERBANKSLOTS_CHANGED", "OnBankChanged")
if Baggins:IsRetailWow() then
self:RegisterEvent("PLAYERREAGENTBANKSLOTS_CHANGED", "OnReagentBankChanged")
self:RegisterEvent("REAGENTBANK_PURCHASED", "OnReagentBankChanged")
end
self:RegisterEvent("PLAYERBANKBAGSLOTS_CHANGED", "OnBankSlotPurchased")
self:RegisterEvent("BANKFRAME_CLOSED", "OnBankClosed")
self:RegisterEvent("BANKFRAME_OPENED", "OnBankOpened")
self:RegisterEvent("PLAYER_MONEY", "UpdateMoneyFrame")
self:RegisterEvent('AUCTION_HOUSE_SHOW', "AuctionHouse")
self:RegisterEvent('AUCTION_HOUSE_CLOSED', "CloseAllBags")
-- Patch 10.0 Added Later Added To Classic
self:RegisterEvent('PLAYER_INTERACTION_MANAGER_FRAME_SHOW', "PlayerInteractionManager")
self:RegisterEvent('PLAYER_INTERACTION_MANAGER_FRAME_HIDE', "PlayerInteractionManager")
self:RegisterEvent('SOCKET_INFO_UPDATE', "OpenAllBags")
self:RegisterSignal('CategoryMatchAdded', self.CategoryMatchAdded, self)
self:RegisterSignal('CategoryMatchRemoved', self.CategoryMatchRemoved, self)
self:RegisterSignal('SlotMoved', self.SlotMoved, self)
self:UpdateBagHooks()
self:UpdateBackpackHook()
self:RawHook("CloseSpecialWindows", true)
--self:RawHookScript(BankFrame,"OnEvent","BankFrame_OnEvent")
-- hook blizzard PLAYERBANKSLOTS_CHANGED function to filter inactive table
-- this is required to prevent a nil error when working with a tab that the
-- default UI is not currently showing
self:RawHook("BankFrameItemButton_Update", true)
--force an update of all bags on first opening
self.doInitialUpdate = true
self.doInitialBankUpdate = true
self:ResortSections()
self:UpdateText()
--self:SetDebugging(true)
if self.db.profile.hideduplicates == true then
self.db.profile.hideduplicates = "global"
end
self:CreateMoneyFrame()
self:UpdateMoneyFrame()
self:CreateBankControlFrame()
self:UpdateBankControlFrame()
local skin = self:GetSkin(self.db.profile.skin)
if not skin then -- if skin doesn't exist anymore, reset to default
console:Print("|cFFFF0000Baggins|r "..L["Skin '%s' not found, resetting to default"]:format(self.db.profile.skin))
self.db.profile.skin = "default"
end
self:EnableSkin(self.db.profile.skin)
self:OnProfileEnable()
self:RunBagUpdates()
end
function Baggins:Baggins_CategoriesChanged()
self:ReallyUpdateBags()
self.doInitialBankUpdate = true
end
function Baggins:BankFrame_OnEvent(...)
if not self:IsActive() or not self.db.profile.hidedefaultbank then
self.hooks[BankFrame].OnEvent(...)
end
end
function Baggins:UpdateBagHooks()
if self.db.profile.overridedefaultbags then
self:UnhookBagHooks()
self:RawHook("OpenAllBags", "ToggleAllBags", true)
self:RawHook("ToggleAllBags", true)
self:RawHook('ToggleBackpack', 'ToggleAllBags', true)
self:RawHook("CloseAllBags", true)
self:RawHook('OpenBag', 'ToggleAllBags', true)
self:RawHook('OpenBackpack', 'ToggleAllBags', true)
self:RawHook('ToggleBag', 'ToggleAllBags', true)
if Baggins:IsRetailWow() then
self:RawHook("OpenAllBagsMatchingContext", "ToggleAllBags", true)
--self:RawHook("OpenAndFilterBags", "ToggleAllBags", true)
end
--self:RawHook('ToggleBag', 'ToggleBags', true)
--self:RawHook('OpenBackpack', 'OpenBags', true)
--self:RawHook('CloseBackpack', 'CloseBags', true)
else
self:UnhookBagHooks()
end
end
function Baggins:UnhookBagHooks()
if self:IsHooked("OpenAllBags") then
self:Unhook("OpenAllBags")
end
if self:IsHooked("ToggleAllBags") then
self:Unhook("ToggleAllBags")
end
if self:IsHooked("ToggleBackpack") then
self:Unhook("ToggleBackpack")
end
if self:IsHooked("CloseAllBags") then
self:Unhook("CloseAllBags")
end
if Baggins:IsRetailWow() then
if self:IsHooked("OpenAllBagsMatchingContext") then
self:Unhook("OpenAllBagsMatchingContext")
end
end
end
function Baggins:UpdateBackpackHook()
if self.db.profile.overridebackpack then
if not self:IsHooked(MainMenuBarBackpackButton, "OnClick") then
self:RawHookScript(MainMenuBarBackpackButton, "OnClick", "MainMenuBarBackpackButtonOnClick")
end
if not self:IsHooked(CharacterBag0Slot, "OnClick") then
self:RawHookScript(CharacterBag0Slot, "OnClick", "MainMenuBarBackpackButtonOnClick")
end
if not self:IsHooked(CharacterBag1Slot, "OnClick") then
self:RawHookScript(CharacterBag1Slot, "OnClick", "MainMenuBarBackpackButtonOnClick")
end
if not self:IsHooked(CharacterBag2Slot, "OnClick") then
self:RawHookScript(CharacterBag2Slot, "OnClick", "MainMenuBarBackpackButtonOnClick")
end
if not self:IsHooked(CharacterBag3Slot, "OnClick") then
self:RawHookScript(CharacterBag3Slot, "OnClick", "MainMenuBarBackpackButtonOnClick")
end
if Baggins:IsRetailWow() and not self:IsHooked(CharacterReagentBag0Slot, "OnClick") then
self:RawHookScript(CharacterReagentBag0Slot, "OnClick", "MainMenuBarBackpackButtonOnClick")
end
else
self:UnhookBackpack()
end
end
function Baggins:UnhookBackpack()
if self:IsHooked(MainMenuBarBackpackButton, "OnClick") then
self:Unhook(MainMenuBarBackpackButton, "OnClick")
end
if self:IsHooked(CharacterBag0Slot, "OnClick") then
self:RawHookScript(CharacterBag0Slot, "OnClick")
end
if self:IsHooked(CharacterBag1Slot, "OnClick") then
self:RawHookScript(CharacterBag1Slot, "OnClick")
end
if self:IsHooked(CharacterBag2Slot, "OnClick") then
self:RawHookScript(CharacterBag2Slot, "OnClick")
end
if self:IsHooked(CharacterBag3Slot, "OnClick") then
self:RawHookScript(CharacterBag3Slot, "OnClick")
end
if Baggins:IsRetailWow() and self:IsHooked(CharacterReagentBag0Slot, "OnClick") then
self:RawHookScript(CharacterReagentBag0Slot, "OnClick")
end
end
function Baggins:OnDisable()
self:CloseAllBags()
end
local INVSLOT_LAST_EQUIPPED, CONTAINER_BAG_OFFSET, NUM_BAG_SLOTS =
INVSLOT_LAST_EQUIPPED, CONTAINER_BAG_OFFSET, NUM_TOTAL_EQUIPPED_BAG_SLOTS or NUM_BAG_SLOTS
function Baggins:SaveItemCounts()
local itemcounts = self.itemcounts
wipe(itemcounts)
for _,_,link in LBU:Iterate("BAGS") do -- includes keyring
if link then
local id = tonumber(link:match("item:(%d+)"))
if id and not itemcounts[id] then
itemcounts[id] = { count = GetItemCount(id), ts = time() }
end
end
end
if Baggins:IsRetailWow() then
for _,_,link in LBU:Iterate("REAGENTBANK") do
if link then
local id = tonumber(link:match("item:(%d+)"))
if id and not itemcounts[id] then
itemcounts[id] = { count = GetItemCount(id), ts = time() }
end
end
end
end
for slot = 0, INVSLOT_LAST_EQUIPPED do -- 0--19
local link = GetInventoryItemLink("player",slot)
if link then
local id = tonumber(link:match("item:(%d+)"))
if id and not itemcounts[id] then
itemcounts[id] = { count = GetItemCount(id), ts = time() }
end
end
end
for slot = 1+CONTAINER_BAG_OFFSET, NUM_BAG_SLOTS+CONTAINER_BAG_OFFSET do -- 20--23
local link = GetInventoryItemLink("player",slot)
if link then
local id = tonumber(link:match("item:(%d+)"))
if id and not itemcounts[id] then
itemcounts[id] = { count = GetItemCount(id), ts = time() }
end
end
end
end
function Baggins:RunItemCountUpdates()
if self.db.profile.newitemduration > 0 then
Baggins:ForceFullUpdate()
end
end
function Baggins:IsCompressed(itemID)
local p = self.db.profile
if self.tempcompressnone then
return false
end
--slot sorting will break compression horribly
--if p.sort == "slot" then
-- return false
--end
if p.compressall then
return true
end
--string id's here are empty slots
if type(itemID) == "string" and p.compressempty then
return true
end
if type(itemID) == "number" then
local itemFamily = GetItemFamily(itemID)
local _, _, _, _, _, _, _, itemStackCount, itemEquipLoc = GetItemInfo(itemID)
if itemFamily then -- likes to be nil during login
if Baggins:IsRetailWow() then
if p.CompressShards and band(itemFamily,4)~=0 and itemEquipLoc~="INVTYPE_BAG" then
return true
end
if p.compressammo and band(itemFamily,3)~=0 and itemEquipLoc~="INVTYPE_BAG" then
return true
end
end
if Baggins:IsClassicWow() or Baggins:IsTBCWow() or Baggins:IsWrathWow() or Baggins:IsCataWow() then
if p.CompressShards and itemFamily ~=3 and itemEquipLoc~="INVTYPE_BAG" then
return true
end
if p.compressammo and itemFamily ~=2 and itemEquipLoc~="INVTYPE_BAG" then
return true
end
end
end
if p.compressstackable and itemStackCount and itemStackCount>1 then
--local charBags = {}
--for i=0, NUM_BAG_SLOTS do
-- tinsert(charBags, i);
--end
--if Baggins:IsClassicWow() or Baggins:IsTBCWow() then
-- tinsert(charBags, KEYRING_CONTAINER)
--end
----local bankBags = { BANK_CONTAINER }
----for i=NUM_BAG_SLOTS+1, NUM_BAG_SLOTS+NUM_BANKBAGSLOTS do
---- tinsert(bankBags, i);
----end
----local bags = bank and bankBags or charBags
--local bags = charBags
--for _,bag in ipairs(bags) do
-- for slot=1,(GetContainerNumSlots(bag) or 0) do
-- local _, itemCount, locked, _, _ = GetContainerItemInfo(bag, slot)
-- local link = GetContainerItemLink(bag, slot)
-- if link then
-- local _, _, _, _, _, _, _, iMaxStack = GetItemInfo(link)
-- if iMaxStack and itemCount < iMaxStack then
-- end
-- end
-- end
--end
return true
end
end
end
function Baggins:OnBankClosed()
-- don't remove the test, it prevents infinite recursion loop on CloseBankFrame()
if self.bankIsOpen then
self.bankIsOpen = nil
self:CloseAllBags()
end
end
function Baggins:OnBankOpened()
if self.doInitialBankUpdate then
self.doInitialBankUpdate = false
Baggins:ForceFullBankUpdate()
end
self.bankIsOpen = true
self:OpenAllBags()
end
function Baggins:OnBankChanged()
self:OnBagUpdate(nil,-1)
end
function Baggins:OnReagentBankChanged()
self:OnBagUpdate(nil,REAGENTBANK_CONTAINER)
end
function Baggins:OnReagentBankPurchased()
self:UpdateBankControlFrame()
self:ForceFullBankUpdate()
self:UpdateBags()
end
function Baggins:OnBankSlotPurchased()
self:UpdateBankControlFrame()
self:ForceFullBankUpdate()
self:UpdateBags()
end
-------------------------
-- Update Bag Contents --
-------------------------
local scheduled_refresh = false
function Baggins:ScheduleRefresh()
if not scheduled_refresh then
scheduled_refresh = self:ScheduleForNextFrame('Baggins_RefreshBags')
end
end
function Baggins:Baggins_RefreshBags()
if self.dirtyBags then
--Baggins:Debug('Updating bags')
self:ReallyUpdateBags()
end
for bagid,bagframe in pairs(self.bagframes) do
for _,sectionframe in pairs(bagframe.sections) do
if sectionframe.used and sectionframe.dirty then
--Baggins:Debug('Updating section #%d-%d', bagid, secid)
self:ReallyLayoutSection(sectionframe)
end
end
if bagframe.dirty then
self:ReallyUpdateBagFrameSize(bagid)
end
end
if self.dirtyBagLayout then
self:ReallyLayoutBagFrames()
end
scheduled_refresh = nil
self:FireSignal("Baggins_RefreshBags")
end
function Baggins:UpdateBags()
self.dirtyBags = true
self:ScheduleRefresh()
end
local function CheckSection(bagframe, secid)
for i = 1,secid do
if not bagframe.sections[i] then
bagframe.sections[i] = Baggins:CreateSectionFrame(bagframe,i)
if i == 1 then
bagframe.sections[i]:SetPoint("TOPLEFT",bagframe,"TOPLEFT",10,-36)
else
bagframe.sections[i]:SetPoint("TOPLEFT",bagframe.sections[i-1],"BOTTOMLEFT",0,1)
end
end
end
end
local function GetSlotInfo(item)
local bag, slot = item:match("^(-?%d+):(%d+)$")
local bagType = Baggins:IsSpecialBag(bag)
local itemID
local cacheditem = Baggins:GetCachedItem(item)
if cacheditem then
itemID = tonumber(cacheditem:match("^(%d+)"))
end
return bag, slot, itemID, bagType
end
local function new() return {} end
local function del(t) wipe(t) end
--local rdel = del
function Baggins:CategoryMatchAdded(category, slot, isbank)
local p = self.db.profile
for bagid, bag in pairs(p.bags) do
local bagframe = self.bagframes[bagid]
if bagframe then
for sectionid, section in pairs(bag.sections) do
for _, catname in pairs(section.cats) do
if catname == category and ((not bag.isBank) == (not isbank)) then
CheckSection(bagframe, sectionid)
local secframe = bagframe.sections[sectionid]
secframe.slots[slot] = ( secframe.slots[slot] or 0 ) + 1
local layout = secframe.layout
local _, _, itemid, bagtype = GetSlotInfo(slot)
if not itemid then
itemid = (bagtype or "")
end
local found
--check for an existing stack to add the slot to
for k, entry in ipairs(layout) do
if type(entry) == "table" then
if entry.itemid == itemid and entry.slots[slot] then
found = true
elseif entry.itemid == itemid then
if self:IsCompressed(itemid) then
if not entry.slots[slot] then
entry.slots[slot] = true
entry.slotcount = entry.slotcount + 1
end
found = true
end
else
if entry.slots[slot] then
entry.slots[slot] = nil
entry.slotcount = entry.slotcount - 1
if entry.slotcount == 0 then
del(entry.slots)
del(entry)
layout[k] = slot
end
end
end
end
end
if not found then
local newentry = new()
newentry.slots = new()
newentry.slots[slot] = true
newentry.slotcount = 1
newentry.itemid = itemid
tinsert(layout, newentry)
secframe.needssorting = true
end
end
end
end
end
end
end
function Baggins:CategoryMatchRemoved(category, slot, isbank)
local p = self.db.profile
for bagid, bag in pairs(p.bags) do
local bagframe = self.bagframes[bagid]
if bagframe then
for sectionid, section in pairs(bag.sections) do
for _, catname in pairs(section.cats) do
if catname == category and ((not bag.isBank) == (not isbank)) then
CheckSection(bagframe, sectionid)
local secframe = bagframe.sections[sectionid]
secframe.slots[slot] = ( secframe.slots[slot] or 1 ) - 1
if secframe.slots[slot] == 0 then
secframe.slots[slot] = false
end
local layout = secframe.layout
--remove the slot from any stacks that contain it
for k, entry in ipairs(layout) do
if type(entry) == "table" then
if entry.slots[slot] then
entry.slots[slot] = nil
entry.slotcount = entry.slotcount - 1
end
if entry.slotcount == 0 then
del(entry.slots)
del(entry)
layout[k] = slot
end
end
end
end
end
end
end
end
end
function Baggins:SlotMoved(category, slot, isbank)
local p = self.db.profile
for bagid, bag in pairs(p.bags) do
local bagframe = self.bagframes[bagid]
if bagframe then
for sectionid, section in pairs(bag.sections) do
for _, catname in pairs(section.cats) do