-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHatter.lua
339 lines (274 loc) · 8.54 KB
/
Hatter.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
local ADDON_NAME, Data = ...
Hatter = LibStub("AceAddon-3.0"):NewAddon(ADDON_NAME, "AceConsole-3.0", "AceEvent-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale(ADDON_NAME)
local AceConfig = LibStub"AceConfig-3.0"
local AceConfigDialog = LibStub"AceConfigDialog-3.0"
local AceConfigRegistry = LibStub"AceConfigRegistry-3.0"
local AceDB = LibStub"AceDB-3.0"
local AceDBOptions = LibStub"AceDBOptions-3.0"
local ENABLED = true
function Hatter:Toggle()
ENABLED = not ENABLED
end
function Hatter:IsHidable(slot)
return Data.HIDABLE_SLOTS[slot] or false
end
function Hatter:GetDB()
return self.db.profile
end
function Hatter:GetEnforceDefault()
return self:GetDB().DefaultVisibility.EnforceDefault
end
function Hatter:SetEnforceDefault(state)
self:GetDB().DefaultVisibility.EnforceDefault = state
end
function Hatter:GetDefaultVisibility(slot)
return self:GetDB().DefaultVisibility[slot]
end
function Hatter:SetDefaultVisibility(slot, visibility)
self:GetDB().DefaultVisibility[slot] = visibility
end
function Hatter:GetVisibility(slot, id)
return self:GetDB()[slot][id]
end
function Hatter:SetVisibility(slot, id, visibility)
if visibility == nil then
return self:ForgetVisibility(slot, id)
end
local oldVisibility = self:GetVisibility(slot, id)
self:GetDB()[slot][id] = visibility
if oldVisibility ~= visibility then
local function output()
local name, link = GetItemInfo(id)
if not link then
C_Timer.After(0.1, output)
return
end
self:Printf("%s %s: %s", L["Remembering"], link, visibility and L["Shown"] or L["Hidden"])
end
output()
end
end
function Hatter:ForgetVisibility(slot, id)
self:GetDB()[slot][id] = visibility
local function output()
local name, link = GetItemInfo(id)
if not link then
C_Timer.After(0.1, output)
return
end
self:Printf("%s %s", L["Forgotten"], link)
end
output()
end
function Hatter:ForgetAllVisibility(slot)
for id in pairs(self:GetDB()[slot]) do
self:ForgetVisibility(slot, id)
end
end
function Hatter:SetVisibilityDefault(slot, id)
if self:GetVisibility(slot, id) == nil then
if self:GetEnforceDefault() then
self:SetVisibility(slot, id, self:GetDefaultVisibility(slot))
else
self:SetVisibility(slot, id, self:IsShown(slot))
end
end
end
function Hatter:IsShown(slot)
if slot == Data.HEAD then
return ShowingHelm()
elseif slot == Data.BACK then
return ShowingCloak()
end
end
function Hatter:ShowSlot(slot, visibility)
if slot == Data.HEAD then
return self.ShowHelm(visibility)
elseif slot == Data.BACK then
return self.ShowCloak(visibility)
end
end
function Hatter:GetItemId(slot)
return GetInventoryItemID("player", slot)
end
function Hatter:GetItemLink(slot)
return GetInventoryItemLink("player", slot)
end
function Hatter:SetShown(slot, visibility)
if self:IsShown(slot) ~= visibility then
self:ShowSlot(slot, visibility)
end
end
function Hatter:OnEquipmentChanged(event, slot, isEmpty)
if not self:IsHidable(slot) then return end
if isEmpty then return end
local itemId = self:GetItemId(slot)
if itemId and self:GetItemLink(slot) then
self:SetVisibilityDefault(slot, itemId)
self:SetShown(slot, self:GetVisibility(slot, itemId))
end
end
function Hatter:OnShown(slot, isShown)
local itemId = self:GetItemId(slot)
if itemId and self:GetItemLink(slot) then
self:SetVisibility(slot, itemId, isShown)
end
end
function Hatter:PrintUsage()
self:Printf(L["Usage:"])
self:Printf(" /%s config", Data.CHAT_COMMAND)
self:Printf(" %s", L["Open options"])
self:Printf(" /%s list", Data.CHAT_COMMAND)
self:Printf(" %s", L["List items"])
self:Printf(" /%s forget [%s]", Data.CHAT_COMMAND, L["ItemLink"])
self:Printf(" %s", L["Forget an item"])
end
function Hatter:OpenConfig(category)
InterfaceAddOnsList_Update()
InterfaceOptionsFrame_OpenToCategory(category)
end
function Hatter:ListItems(slot, noHeader)
local items = {visible = {}, invisible = {}}
local count = 0
local cached = true
for itemId, visibility in pairs(self:GetDB()[slot]) do
local name, link = GetItemInfo(itemId)
if link then
table.insert(items[visibility and "visible" or "invisible"], {name = name, link = link, visibility = visibility})
count = count + 1
else
cached = false
end
end
if not cached then
C_Timer.After(0.1, function() self:ListItems(slot) end)
return false
end
table.sort(items.visible, function(a, b) return a.name < b.name end)
table.sort(items.invisible, function(a, b) return a.name < b.name end)
if count > 0 then
if #items.visible > 0 then
self:Printf(slot == 1 and L["Shown hats:"] or L["Shown cloaks:"])
for _, item in ipairs(items.visible) do
self:Printf(" %s", item.link)
end
end
if #items.invisible > 0 then
self:Printf(slot == 1 and L["Hidden hats:"] or L["Hidden cloaks:"])
for _, item in ipairs(items.invisible) do
self:Printf(" %s", item.link)
end
end
else
self:Printf(slot == 1 and L["No hats are being remembered"] or L["No cloaks are being remembered"])
end
return true
end
function Hatter:ParseChatCommand(input)
local command, link = self:GetArgs(input, 2)
command = command and command:lower() or nil
if command == "list" then
self:ListItems(Data.HEAD)
self:ListItems(Data.BACK)
return true
elseif command == "forget" then
if link then
local id = tonumber(link)
if not id then
id = link:match"item:(%d+)"
end
if id then
id = tonumber(id)
if id then
local found = false
for slot in pairs(Data.HIDABLE_SLOTS) do
if self:GetVisibility(slot, id) ~= nil then
self:SetVisibility(slot, id, nil)
found = true
break
end
end
if not found then
self:Printf(L["That item is not being remembered"])
end
return true
end
else
return false
end
end
elseif command == "clear" then
for slot in pairs(Data.HIDABLE_SLOTS) do
for k, v in pairs(self:GetDB()[slot]) do
self:SetVisibility(slot, itemId, nil)
end
end
elseif command == "config" or command == "options" then
self:OpenConfig(ADDON_NAME)
return true
end
return false
end
function Hatter:OnChatCommand(input)
if not self:ParseChatCommand(input) then
self:PrintUsage()
end
end
function Hatter:IsWeakAuraFound()
if WeakAuras then
if WeakAuras.IsAuraLoaded then
if WeakAuras.IsAuraLoaded(ADDON_NAME) then
return true
end
elseif WeakAuras.loaded then
if WeakAuras.loaded[ADDON_NAME] then
return true
end
end
end
return false
end
function Hatter:CreateHooks()
self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED", "OnEquipmentChanged")
self.ShowHelm = ShowHelm
self.ShowCloak = ShowCloak
hooksecurefunc("ShowHelm" , function(isShown) return Hatter:OnShown(Data.HEAD, isShown) end)
hooksecurefunc("ShowCloak", function(isShown) return Hatter:OnShown(Data.BACK, isShown) end)
end
function Hatter:CreateOptions()
AceConfig:RegisterOptionsTable(ADDON_NAME, Data:MakeOptionsTable(self, L))
local Panel = AceConfigDialog:AddToBlizOptions(ADDON_NAME)
Panel.default = function()
local db = self:GetDB()
for k, v in pairs(Data:GetDefaultOptions().profile.DefaultVisibility) do
db.DefaultVisibility[k] = v
end
AceConfigRegistry:NotifyChange(ADDON_NAME)
end
local profiles = AceDBOptions:GetOptionsTable(self.db)
AceConfig:RegisterOptionsTable(ADDON_NAME .. ".Profiles", profiles)
AceConfigDialog:AddToBlizOptions(ADDON_NAME .. ".Profiles", "Profiles", ADDON_NAME)
self:RegisterChatCommand(Data.CHAT_COMMAND, "OnChatCommand", true)
end
function Hatter:OnInitialize()
self.db = AceDB:New(("%sDB"):format(ADDON_NAME), Data:GetDefaultOptions())
end
function Hatter:OnEnable()
Data:Init(self, L)
self:CreateHooks()
self:CreateOptions()
C_Timer.After(1, function()
ShowHelm(ShowingHelm())
ShowCloak(ShowingCloak())
end)
C_Timer.After(1, function()
if self:IsWeakAuraFound() then
AceConfig:RegisterOptionsTable(ADDON_NAME .. " WeakAura", Data:MakeWeakAuraOptionsTable(self, L))
local Panel = AceConfigDialog:AddToBlizOptions(ADDON_NAME .. " WeakAura", "WeakAura", ADDON_NAME)
StaticPopup_Show(("%s_WEAKAURA_WARN"):format(ADDON_NAME:upper()), ADDON_NAME, nil, {Addon = self, Panel = Panel})
end
end)
end
function Hatter:OnDisable()
end