forked from Jaliborc/Combuctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
executable file
·297 lines (233 loc) · 6.67 KB
/
main.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
--[[
Combuctor.lua
Some sort of crazy visual inventory management system
--]]
local ADDON, Addon = ...
LibStub('AceAddon-3.0'):NewAddon(Addon, ADDON, 'AceEvent-3.0', 'AceConsole-3.0')
Addon.__call = Addon.GetModule
_G[ADDON] = setmetatable(Addon, Addon)
--[[ Constants ]]--
local L = LibStub('AceLocale-3.0'):GetLocale(ADDON)
local CURRENT_VERSION = GetAddOnMetadata(ADDON, 'Version')
BINDING_HEADER_COMBUCTOR = ADDON
BINDING_NAME_COMBUCTOR_TOGGLE_INVENTORY = L.ToggleInventory
BINDING_NAME_COMBUCTOR_TOGGLE_BANK = L.ToggleBank
--[[ Startup ]]--
function Addon:OnEnable()
self.profile = self:InitDB()
-- version update
if self.db.version ~= CURRENT_VERSION then
self:UpdateSettings()
self:UpdateVersion()
end
-- slash commands
self:RegisterChatCommand('combuctor', 'OnSlashCommand')
self:RegisterChatCommand('cbt', 'OnSlashCommand')
-- startup frames
self.Frame:New(L.InventoryTitle, {0, 1, 2, 3, 4}, self.profile.inventory, 'inventory')
self.Frame:New(L.BankTitle, {BANK_CONTAINER, 5, 6, 7, 8, 9, 10, 11, REAGENTBANK_CONTAINER}, self.profile.bank, 'bank')
self:HookBagEvents()
self:HookTooltips()
-- option frame loader
local f = CreateFrame('Frame', nil, InterfaceOptionsFrame)
f:SetScript('OnShow', function(self)
self:SetScript('OnShow', nil)
LoadAddOn('Combuctor_Config')
end)
end
--[[ Settings ]]--
function Addon:InitDB()
CombuctorDB2 = CombuctorDB2 or {
version = CURRENT_VERSION,
global = {}, profiles = {}
}
self.db = CombuctorDB2
self.sets = self.db.global
return self:GetProfile() or self:InitProfile()
end
function Addon:UpdateSettings(major, minor, bugfix)
local expansion, patch, release = strsplit('.', self.db.version)
local version = tonumber(expansion) * 10000 + tonumber(patch or 0) * 100 + tonumber(release or 0)
-- nothing to do, yay!
end
function Addon:UpdateVersion()
self.db.version = CURRENT_VERSION
self:Print(format(L.Updated, self.db.version))
end
function Addon:ToggleSetting(set)
self.sets[set] = not self.sets[set] or nil
end
function Addon:GetSetting(set)
return self.sets[set]
end
--[[ Profiles ]]--
function Addon:GetProfile(player)
return self.db.profiles[(player or UnitName('player')) .. ' - ' .. GetRealmName()]
end
local function addSet(sets, exclude, name, ...)
if sets then
tinsert(sets, name)
else
sets = {name}
end
if select('#', ...) > 0 then
if exclude then
tinsert(exclude, {[name] = {...}})
else
exclude = {[name] = {...}}
end
end
return sets, exclude
end
local function getDefaultInventorySets(class)
local sets, exclude = addSet(sets, exclude, ALL, ALL)
return sets, exclude
end
local function getDefaultBankSets(class)
local sets, exclude = addSet(sets, exclude, ALL, ALL)
sets, exclude = addSet(sets, exclude, L.Equipment)
sets, exclude = addSet(sets, exclude, L.TradeGood)
sets, exclude = addSet(sets, exclude, L.Misc)
return sets, exclude
end
function Addon:InitProfile()
local player, realm = UnitName('player'), GetRealmName()
local class = select(2, UnitClass('player'))
local profile = self:GetBaseProfile()
profile.inventory.sets, profile.inventory.exclude = getDefaultInventorySets(class)
profile.bank.sets, profile.bank.exclude = getDefaultBankSets(class)
self.db.profiles[player .. ' - ' .. realm] = profile
return profile
end
function Addon:GetBaseProfile()
return {
inventory = {
position = {'RIGHT'},
showBags = false,
leftSideFilter = true,
w = 384,
h = 512,
},
bank = {
showBags = false,
w = 512,
h = 512,
}
}
end
--[[ Events ]]--
function Addon:HookBagEvents()
local AutoShowInventory = function()
self:Show(BACKPACK_CONTAINER, true)
end
local AutoHideInventory = function()
self:Hide(BACKPACK_CONTAINER, true)
end
--auto magic display code
OpenBackpack = AutoShowInventory
hooksecurefunc('CloseBackpack', AutoHideInventory)
ToggleBag = function(bag)
self:Toggle(bag)
end
OpenBag = function(bag)
self:Show(bag)
end
ToggleBackpack = function()
self:Toggle(BACKPACK_CONTAINER)
end
OpenAllBags = function(frame)
self:Show(BACKPACK_CONTAINER)
end
if _G['ToggleAllBags'] then
ToggleAllBags = function()
self:Toggle(BACKPACK_CONTAINER)
end
end
--closing the game menu triggers this function, and can be done in combat,
hooksecurefunc('CloseAllBags', function()
self:Hide(BACKPACK_CONTAINER)
end)
BankFrame:UnregisterAllEvents()
self.BagEvents.Listen(self, 'BANK_OPENED', function()
LibStub('LibItemCache-1.1').AtBank = true
self:Show(BANK_CONTAINER, true)
self:Show(BACKPACK_CONTAINER, true)
end)
self.BagEvents.Listen(self, 'BANK_CLOSED', function()
LibStub('LibItemCache-1.1').AtBank = false
self:Hide(BANK_CONTAINER, true)
self:Hide(BACKPACK_CONTAINER, true)
end)
self:RegisterEvent('MAIL_CLOSED', AutoHideInventory)
self:RegisterEvent('TRADE_SHOW', AutoShowInventory)
self:RegisterEvent('TRADE_CLOSED', AutoHideInventory)
self:RegisterEvent('TRADE_SKILL_SHOW', AutoShowInventory)
self:RegisterEvent('TRADE_SKILL_CLOSE', AutoHideInventory)
self:RegisterEvent('AUCTION_HOUSE_SHOW', AutoShowInventory)
self:RegisterEvent('AUCTION_HOUSE_CLOSED', AutoHideInventory)
self:RegisterEvent('AUCTION_HOUSE_SHOW', AutoShowInventory)
self:RegisterEvent('AUCTION_HOUSE_CLOSED', AutoHideInventory)
end
--[[ Frames ]]--
function Addon:Show(bag, auto)
for _,frame in pairs(self.frames) do
for _,bagID in pairs(frame.bags) do
if bagID == bag then
frame:ShowFrame(auto)
return
end
end
end
end
function Addon:Hide(bag, auto)
for _,frame in pairs(self.frames) do
for _,bagID in pairs(frame.bags) do
if bagID == bag then
frame:HideFrame(auto)
return
end
end
end
end
function Addon:Toggle(bag, auto)
for _,frame in pairs(self.frames) do
for _,bagID in pairs(frame.bags) do
if bagID == bag then
frame:ToggleFrame(auto)
return
end
end
end
end
function Addon:UpdateFrames()
for _,frame in pairs(self.frames or {}) do
frame.itemFrame:Regenerate()
end
end
function Addon:GetFrame(key)
return self.frames[key]
end
--[[ Extras ]]--
function Addon:ShowOptions()
if LoadAddOn('Combuctor_Config') then
InterfaceOptionsFrame_OpenToCategory(ADDON)
InterfaceOptionsFrame_OpenToCategory(ADDON) -- sometimes once not enough
end
end
function Addon:OnSlashCommand(msg)
local msg = msg and msg:lower()
if msg == 'bank' then
self:Toggle(BANK_CONTAINER)
elseif msg == 'bags' then
self:Toggle(BACKPACK_CONTAINER)
elseif msg == '' or msg == 'config' or msg == 'options' then
self:ShowOptions()
elseif msg == 'version' then
self:Print(self.db.version)
else
self:Print('Commands (/cbt or /combuctor)\n- bank: Toggle bank\n- bags: Toggle inventory\n- options: Shows the options menu')
end
end
function Addon:Print(...)
return print('|cffFFBA00'.. ADDON .. '|r:', ...)
end