-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistentUnsheath.lua
More file actions
249 lines (200 loc) · 8.48 KB
/
Copy pathPersistentUnsheath.lua
File metadata and controls
249 lines (200 loc) · 8.48 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
local folderName, addon = ...
-- Make functions local for performance!
local C_Timer_After = _G.C_Timer.After
local C_Timer_NewTimer = _G.C_Timer.NewTimer
local C_UnitAuras_GetPlayerAuraBySpellID = _G.C_UnitAuras.GetPlayerAuraBySpellID
local GetSheathState = _G.GetSheathState
local GetTime = _G.GetTime
local IsMounted = _G.IsMounted
local IsSwimming = _G.IsSwimming
local MapUtil_GetDisplayableMapForPlayer = _G.MapUtil.GetDisplayableMapForPlayer
local MuteSoundFile = _G.MuteSoundFile
local ToggleSheath = _G.ToggleSheath
local UnitAffectingCombat = _G.UnitAffectingCombat
local UnitCastingInfo = _G.UnitCastingInfo
local UnitInVehicle = _G.UnitInVehicle
local UnitOnTaxi = _G.UnitOnTaxi
local UnitPosition = _G.UnitPosition
local UnmuteSoundFile = _G.UnmuteSoundFile
local realmName = GetRealmName()
local playerName = UnitName("player")
-- To be mapped to saved variable. Must be a table to work!!
local desiredUnsheath = nil
local function MuteUnsheathSounds(mute)
-- https://wago.tools/files?search=sheath
-- https://wago.tools/files?search=unsheath
-- Muting sheath sounds as well. Because sometimes after closing the world map, ToggleSheath() leads to an unsheath.
local unsheathSounds = {567430, 567473, 567506, 567395, 567456, 567498}
if mute then
-- print("Muting")
for _, k in pairs(unsheathSounds) do
MuteSoundFile(k)
end
else
-- print("Unmuting")
for _, k in pairs(unsheathSounds) do
UnmuteSoundFile(k)
end
end
end
local unmuteTimer = nil
local restoreUnsheathTicker = nil
local noRestoreBefore = GetTime()
local currentPosition = UnitPosition("player")
local function RestoreUnsheath()
-- Return early if module is disabled
if not LP_config or (not LP_config.persistentUnsheath_autoSheath and not LP_config.persistentUnsheath_autoUnsheath) then return end
local currentTime = GetTime()
local lastPosition = currentPosition
currentPosition = UnitPosition("player")
if noRestoreBefore > currentTime then return end
-- print("RestoreUnsheath", GetSheathState(), "should be", desiredUnsheath[playerName])
local currentlyUnsheated = GetSheathState() ~= 1
-- If only one of the two options is enabled, we might have to change desiredUnsheath.
if (LP_config.persistentUnsheath_autoSheath ~= LP_config.persistentUnsheath_autoUnsheath) then
-- If auto-sheath is disabled, and the game has unsheathed, the new desired state should become unsheathed.
if not LP_config.persistentUnsheath_autoSheath and currentlyUnsheated then
desiredUnsheath[playerName] = true
-- If auto-unsheath is disabled, and the game has sheathed, the new desired state should become sheathed.
elseif not LP_config.persistentUnsheath_autoUnsheath and not currentlyUnsheated then
desiredUnsheath[playerName] = false
end
end
local shouldUnsheath = desiredUnsheath[playerName]
-- print("RestoreUnsheath", currentlyUnsheated, "should be", shouldUnsheath)
-- Check if we should auto-unsheath
if shouldUnsheath and not currentlyUnsheated and LP_config.persistentUnsheath_autoUnsheath then
if not UnitAffectingCombat("player")
and not IsMounted()
and not UnitOnTaxi("player")
and not UnitInVehicle("player")
and not UnitCastingInfo("player")
and not (IsSwimming("player") and (currentPosition ~= lastPosition)) -- Not while swimming and moving.
and not (MapUtil_GetDisplayableMapForPlayer() == 2301 and (currentPosition ~= lastPosition)) -- Not while underwater running in "The Sinkhole".
and not (MapUtil_GetDisplayableMapForPlayer() == 2259 and (currentPosition ~= lastPosition)) -- Not while underwater running in "Tak-Rethan Abyss".
and not C_UnitAuras_GetPlayerAuraBySpellID(221883) -- Not while on Divine Steed.
then
-- print("Got to auto-toggle unsheath!")
if unmuteTimer and not unmuteTimer:IsCancelled() then
unmuteTimer:Cancel()
end
if LP_config.persistentUnsheath_muteToggleSounds then
MuteUnsheathSounds(true)
end
ToggleSheath(folderName)
noRestoreBefore = currentTime + 1.5
if LP_config.persistentUnsheath_muteToggleSounds then
unmuteTimer = C_Timer_NewTimer(1, function()
MuteUnsheathSounds(false)
end)
end
end
-- Check if we should auto-sheath
elseif not shouldUnsheath and currentlyUnsheated and LP_config.persistentUnsheath_autoSheath then
if not C_UnitAuras_GetPlayerAuraBySpellID(453163) then -- Not while Cave Spelunker's Torch is out
-- print("Got to auto-toggle sheath!")
if unmuteTimer and not unmuteTimer:IsCancelled() then
unmuteTimer:Cancel()
end
if LP_config.persistentUnsheath_muteToggleSounds then
MuteUnsheathSounds(true)
end
ToggleSheath(folderName)
noRestoreBefore = currentTime + 1.5
if LP_config.persistentUnsheath_muteToggleSounds then
unmuteTimer = C_Timer_NewTimer(1, function()
MuteUnsheathSounds(false)
end)
end
end
end
end
local eventFrame = CreateFrame("Frame")
local isHooked = false
local function StartTicker()
-- Stop existing ticker if running
if restoreUnsheathTicker and not restoreUnsheathTicker:IsCancelled() then
-- print("Stopping ticker", GetTime())
restoreUnsheathTicker:Cancel()
end
-- Start ticker after delay to ensure GetSheathState() returns reliable values
C_Timer_After(1.5, function()
if not restoreUnsheathTicker or restoreUnsheathTicker:IsCancelled() then
-- print("Starting ticker", GetTime())
restoreUnsheathTicker = C_Timer.NewTicker(0.25, RestoreUnsheath)
end
end)
end
local function EventFrameScript(self, event, ...)
if event == "PLAYER_ENTERING_WORLD" then
-- print("PLAYER_ENTERING_WORLD")
StartTicker()
elseif event == "ADDON_LOADED" then
-- print("ADDON_LOADED")
local addonName = ...
if addonName == "LudiusPlus" then
-- Initialize variables if needed.
LP_desiredUnsheath = LP_desiredUnsheath or {}
LP_desiredUnsheath[realmName] = LP_desiredUnsheath[realmName] or {}
desiredUnsheath = desiredUnsheath or LP_desiredUnsheath[realmName]
-- print("ADDON_LOADED LudiusPlus", LP_config.persistentUnsheath_autoSheath, LP_config.persistentUnsheath_autoUnsheath)
self:UnregisterEvent("ADDON_LOADED")
addon.SetupOrTeardownPersistentUnsheath()
end
end
end
local function SetupPersistentUnsheath()
-- print("SetupPersistentUnsheath")
-- Register event handler for zone changes and reloads
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:SetScript("OnEvent", EventFrameScript)
-- Start immediately if we're already in the world.
StartTicker()
-- Only setup hooks once.
if not isHooked then
-- If player manually calls ToggleSheath(), we check the result.
hooksecurefunc("ToggleSheath", function(caller)
-- print("ToggleSheath", caller)
if caller ~= folderName then
-- Value is only reliable after some time.
-- While checking we want no restoring.
noRestoreBefore = GetTime() + 0.75
C_Timer_After(0.5, function()
local newDesiredUnsheath = (GetSheathState() ~= 1)
if desiredUnsheath and desiredUnsheath[playerName] ~= newDesiredUnsheath then
-- print("-----------> NOW CHANGING TO", newDesiredUnsheath)
desiredUnsheath[playerName] = newDesiredUnsheath
end
end)
end
end)
-- No restoring while an emote is in action! (roughly 3 seconds)
hooksecurefunc(C_ChatInfo, "PerformEmote", function(...)
-- print("PerformEmote", ...)
noRestoreBefore = GetTime() + 3
end)
isHooked = true
end
end
local function TeardownPersistentUnsheath()
-- print("TeardownPersistentUnsheath")
eventFrame:UnregisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:SetScript("OnEvent", nil)
if restoreUnsheathTicker and not restoreUnsheathTicker:IsCancelled() then
restoreUnsheathTicker:Cancel()
end
if unmuteTimer and not unmuteTimer:IsCancelled() then
unmuteTimer:Cancel()
end
end
function addon.SetupOrTeardownPersistentUnsheath()
-- print("SetupOrTeardownPersistentUnsheath", LP_config.persistentUnsheath_autoSheath, LP_config.persistentUnsheath_autoUnsheath)
if LP_config and (LP_config.persistentUnsheath_autoSheath or LP_config.persistentUnsheath_autoUnsheath) then
SetupPersistentUnsheath()
else
TeardownPersistentUnsheath()
end
end
-- Initialize when addon loads
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:SetScript("OnEvent", EventFrameScript)