-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashlight.lua
More file actions
317 lines (264 loc) · 9.75 KB
/
Copy pathFlashlight.lua
File metadata and controls
317 lines (264 loc) · 9.75 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
local folderName, addon = ...
local L = LibStub("AceLocale-3.0"):GetLocale("LudiusPlus")
local C_UnitAuras_GetPlayerAuraBySpellID = _G.C_UnitAuras.GetPlayerAuraBySpellID
local SetOverrideBindingMacro = _G.SetOverrideBindingMacro
local UnitAffectingCombat = _G.UnitAffectingCombat
local InCombatLockdown = _G.InCombatLockdown
-- Cave Spelunker's Torch
-- https://www.wowhead.com/spell=453163/cave-spelunkers-torch
-- https://www.wowhead.com/item=224552/cave-spelunkers-torch
-- https://www.wowhead.com/object=437211/illuminated-footlocker
-- https://warcraft.wiki.gg/wiki/API_C_UnitAuras.GetPlayerAuraBySpellID
-- https://warcraft.wiki.gg/wiki/FileDataID
-- https://github.com/wowdev/wow-listfile
local itemID = 224552
local spellID = 453163
-- Check if player owns the Cave Spelunker's Torch toy
addon.HasFlashlightToy = function()
return PlayerHasToy(itemID)
end
local macroTorchOnName = "Torch On"
local macroTorchOnIcon = 135433
local macroTorchOffName = "Torch Off"
local macroTorchOffIcon = 136122
local flashlightBindingName = "MACRO Torch Toggle"
local torchToggleButton = CreateFrame("button", "TorchToggleButton", nil, "SecureActionButtonTemplate")
local torchBuffInstanceId = nil
local deferredSetupNeeded = false -- Deferred operation flag for combat lockdown protection
local deferredTeardownNeeded = false -- Deferred macro deletion when disabled during combat
local eventFrame = CreateFrame("Frame")
-- Track if we've reached PLAYER_LOGIN yet (when macro API becomes effective)
local playerLoginFired = false
local function RegisterMainEvents()
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
eventFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
eventFrame:RegisterEvent("UNIT_AURA")
end
local function UpdateMacros()
-- Macro API is only effective from PLAYER_LOGIN onwards
if not playerLoginFired then
return
end
-- Check combat lockdown - defer if in combat
if InCombatLockdown() then
deferredSetupNeeded = true
return
end
-- Check if player has the toy
if not addon.HasFlashlightToy() then
if LP_config and LP_config.flashlight_enabled then
LP_config.flashlight_enabled = false
print("|cffff0000Ludius Plus:|r " .. L["Flashlight module disabled because you don't own the toy."])
-- Refresh the options panel to update the warning message and enable button
if LibStub then
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0", true)
if AceConfigRegistry then
AceConfigRegistry:NotifyChange("Ludius Plus")
end
end
end
return
end
-- Ensure macros are created
local item = Item:CreateFromItemID(itemID)
item:ContinueOnItemLoad(function()
local itemName = C_Item.GetItemInfo(itemID)
local spellName = C_Spell.GetSpellInfo(spellID).name
local macroTorchOnBody = "/usetoy " .. itemName
local macroTorchOffBody = "/cancelaura " .. spellName
if not GetMacroInfo(macroTorchOnName) then
CreateMacro(macroTorchOnName, macroTorchOnIcon, macroTorchOnBody)
else
EditMacro(macroTorchOnName, macroTorchOnName, macroTorchOnIcon, macroTorchOnBody)
end
if not GetMacroInfo(macroTorchOffName) then
CreateMacro(macroTorchOffName, macroTorchOffIcon, macroTorchOffBody)
else
EditMacro(macroTorchOffName, macroTorchOffName, macroTorchOffIcon, macroTorchOffBody)
end
-- After macros are created, set up bindings
local hotkey = GetBindingKey(flashlightBindingName)
if hotkey then
ClearOverrideBindings(torchToggleButton)
local aura = C_UnitAuras_GetPlayerAuraBySpellID(spellID)
if UnitAffectingCombat("player") or not aura then
SetOverrideBindingMacro(torchToggleButton, true, hotkey, macroTorchOnName)
else
SetOverrideBindingMacro(torchToggleButton, true, hotkey, macroTorchOffName)
torchBuffInstanceId = aura.auraInstanceID
end
end
end)
end
local function SetupDisabledNotification()
local hotkey = GetBindingKey(flashlightBindingName)
if hotkey then
ClearOverrideBindings(torchToggleButton)
SetOverrideBinding(torchToggleButton, true, hotkey, "CLICK TorchToggleButton:LeftButton")
torchToggleButton:SetScript("OnClick", function()
print("|cffff0000Ludius Plus:|r " .. L["Flashlight module is disabled. Enable it in the addon options."])
end)
end
end
local function EventFrameScript(self, event, ...)
-- Handle toy acquisition events
if event == "NEW_TOY_ADDED" or event == "TOYS_UPDATED" then
local eventItemID = ...
if eventItemID == itemID and addon.HasFlashlightToy() then
-- Toy was just obtained! Transition to operational mode
if LP_config and LP_config.flashlight_enabled then
-- Unregister toy tracking events first
self:UnregisterEvent("NEW_TOY_ADDED")
self:UnregisterEvent("TOYS_UPDATED")
-- Now register main operational events
RegisterMainEvents()
UpdateMacros()
end
-- Refresh the options panel to update the warning message and enable button
if LibStub then
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0", true)
if AceConfigRegistry then
AceConfigRegistry:NotifyChange("Ludius Plus")
end
end
end
return
end
if event == "PLAYER_ENTERING_WORLD" or event == "PLAYER_REGEN_DISABLED" then
if LP_config and LP_config.flashlight_enabled then
UpdateMacros()
end
elseif event == "UPDATE_BINDINGS" then
ClearOverrideBindings(torchToggleButton)
if LP_config and LP_config.flashlight_enabled then
UpdateMacros()
else
SetupDisabledNotification()
end
elseif event == "UNIT_AURA" then
local unitTarget, updateInfo = ...
if unitTarget ~= "player" then return end
-- Note: In WoW Midnight, addons will no longer be able to access aura information
-- during combat. Checking combat lockdown early and deferring anticipates this change.
if InCombatLockdown() then
deferredSetupNeeded = true
return
end
if updateInfo.addedAuras or updateInfo.removedAuraInstanceIDs then
-- Check if torch aura was added or removed
local torchChanged = false
if updateInfo.addedAuras then
for _, k in pairs(updateInfo.addedAuras) do
if issecretvalue(k.spellId) then
deferredSetupNeeded = true
return
end
if k.spellId == spellID then
torchChanged = true
break
end
end
end
if not torchChanged and updateInfo.removedAuraInstanceIDs then
for _, k in pairs(updateInfo.removedAuraInstanceIDs) do
if k == torchBuffInstanceId then
torchChanged = true
break
end
end
end
if torchChanged then
UpdateMacros()
end
end
elseif event == "PLAYER_REGEN_ENABLED" then
-- Execute deferred macro deletion after combat ends
if deferredTeardownNeeded then
deferredTeardownNeeded = false
if GetMacroInfo(macroTorchOnName) then
DeleteMacro(macroTorchOnName)
end
if GetMacroInfo(macroTorchOffName) then
DeleteMacro(macroTorchOffName)
end
-- Now fully tear down since macros are deleted
self:UnregisterEvent("PLAYER_REGEN_ENABLED")
self:SetScript("OnEvent", nil)
return
end
-- Execute deferred setup after combat ends
if deferredSetupNeeded then
deferredSetupNeeded = false
UpdateMacros()
end
elseif event == "PLAYER_LOGIN" then
playerLoginFired = true
if LP_config and LP_config.flashlight_enabled then
UpdateMacros()
else
SetupDisabledNotification()
end
elseif event == "ADDON_LOADED" then
local addonName = ...
if addonName == folderName then
self:UnregisterEvent("ADDON_LOADED")
-- Check if module requires dangerous scripts on startup
addon.CheckDangerousScriptsOnStartup()
addon.SetupOrTeardownFlashlight()
end
end
end
local function SetupFlashlight()
-- print("SetupFlashlight")
eventFrame:SetScript("OnEvent", EventFrameScript)
-- Only register main events if player has the toy
if addon.HasFlashlightToy() then
RegisterMainEvents()
-- Only update macros if PLAYER_LOGIN has fired
if playerLoginFired then
UpdateMacros()
end
else
-- Register toy tracking events if player doesn't have the toy yet
eventFrame:RegisterEvent("NEW_TOY_ADDED")
eventFrame:RegisterEvent("TOYS_UPDATED")
end
end
local function TeardownFlashlight()
-- print("TeardownFlashlight")
eventFrame:UnregisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:UnregisterEvent("PLAYER_REGEN_DISABLED")
eventFrame:UnregisterEvent("UNIT_AURA")
eventFrame:UnregisterEvent("NEW_TOY_ADDED")
eventFrame:UnregisterEvent("TOYS_UPDATED")
-- Delete macros when module is disabled
if not InCombatLockdown() then
if GetMacroInfo(macroTorchOnName) then
DeleteMacro(macroTorchOnName)
end
if GetMacroInfo(macroTorchOffName) then
DeleteMacro(macroTorchOffName)
end
eventFrame:UnregisterEvent("PLAYER_REGEN_ENABLED")
-- Don't remove OnEvent handler - we need it for UPDATE_BINDINGS and PLAYER_LOGIN
else
-- Defer macro deletion until combat ends
deferredTeardownNeeded = true
-- Keep PLAYER_REGEN_ENABLED registered to handle deferred deletion
end
-- Disabled notification is set up via UPDATE_BINDINGS/PLAYER_LOGIN handlers
SetupDisabledNotification()
end
function addon.SetupOrTeardownFlashlight()
if LP_config and LP_config.flashlight_enabled then
SetupFlashlight()
else
TeardownFlashlight()
end
end
-- Initialize when addon loads
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:RegisterEvent("UPDATE_BINDINGS")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:SetScript("OnEvent", EventFrameScript)