-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFishingHelper.lua
More file actions
370 lines (339 loc) · 12.7 KB
/
Copy pathFishingHelper.lua
File metadata and controls
370 lines (339 loc) · 12.7 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
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
local addonName, addonTable = ...
local FH = {}
addonTable[1] = FH
_G["FishingHelper"] = FH
_G["BINDING_NAME_CLICK FishingHelperMainButton:LeftButton"] = "Cast Fishing"
_G["BINDING_NAME_CLICK FishingHelperStopButton:LeftButton"] = "Stop Fishing"
-- Default database
local defaultDB = {
framePosition = { point = "CENTER", relativePoint = "CENTER", xOfs = 0, yOfs = 0 },
outfit = {
[1] = nil, -- Head (INVSLOT_HEAD)
[10] = nil, -- Hands (INVSLOT_HANDS)
[8] = nil, -- Feet (INVSLOT_FEET)
[16] = nil, -- Main Hand (INVSLOT_MAINHAND)
},
lureID = nil,
savedGear = {}, -- Used to restore previous gear
minimapPos = 45 -- Minimap button angle in degrees
}
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:RegisterEvent("BAG_UPDATE")
frame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START")
frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP")
frame:RegisterEvent("UNIT_SPELLCAST_START")
frame:RegisterEvent("UNIT_SPELLCAST_STOP")
frame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
frame:RegisterEvent("UNIT_SPELLCAST_FAILED")
frame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
-- Slash command
SLASH_FISHINGHELPER1 = "/fh"
function SlashCmdList.FISHINGHELPER(msg, editbox)
if FH.UI and FH.UI.frame then
if FH.UI.frame:IsShown() then
FH.UI.frame:Hide()
else
FH.UI.frame:Show()
end
end
end
function FH:Initialize()
if not FishingHelperDB then
FishingHelperDB = CopyTable(defaultDB)
end
-- Ensure all keys exist
for k, v in pairs(defaultDB) do
if FishingHelperDB[k] == nil then
FishingHelperDB[k] = type(v) == "table" and CopyTable(v) or v
end
end
self.db = FishingHelperDB
self.updateTimer = 0
frame:SetScript("OnUpdate", function(self, elapsed)
FH.updateTimer = FH.updateTimer + elapsed
if FH.updateTimer > 0.2 then
FH.updateTimer = 0
if FH.UpdateMacro then
FH:UpdateMacro()
end
end
end)
self.volumeSaved = false
self.autoLootSaved = false
print("|cFF00FF00Fishing Helper|r loaded! Type /fh to toggle the frame.")
end
function FH:EnhanceVolume()
if self.volumeSaved then return end
self.oldMasterVolume = GetCVar("Sound_MasterVolume")
self.oldSFXVolume = GetCVar("Sound_SFXVolume")
SetCVar("Sound_MasterVolume", "1.0")
SetCVar("Sound_SFXVolume", "1.0")
self.volumeSaved = true
end
function FH:RestoreVolume()
if not self.volumeSaved then return end
SetCVar("Sound_MasterVolume", self.oldMasterVolume or "1.0")
SetCVar("Sound_SFXVolume", self.oldSFXVolume or "1.0")
self.volumeSaved = false
end
function FH:EnableAutoLoot()
if self.autoLootSaved then return end
self.oldAutoLoot = GetCVar("autoLootDefault")
if self.oldAutoLoot ~= "1" then
SetCVar("autoLootDefault", "1")
self.autoLootSaved = true
end
end
function FH:RestoreAutoLoot()
if not self.autoLootSaved then return end
SetCVar("autoLootDefault", self.oldAutoLoot or "0")
self.autoLootSaved = false
end
function FH:SaveCurrentGear()
if InCombatLockdown() then return end
local needEquip = false
for slot, itemID in pairs(self.db.outfit) do
if itemID and GetInventoryItemID("player", slot) ~= itemID then
needEquip = true
break
end
end
if needEquip then
wipe(self.db.savedGear)
for slot, itemID in pairs(self.db.outfit) do
if itemID then
local link = GetInventoryItemLink("player", slot)
self.db.savedGear[slot] = link
end
end
if self.db.outfit[16] and not self.db.outfit[17] then
self.db.savedGear[17] = GetInventoryItemLink("player", 17)
end
self:UpdateStopMacro()
end
end
function FH:UpdateStopMacro()
if InCombatLockdown() or not self.UI or not self.UI.stopButton then return end
local lines = {}
-- Restore offhand AFTER mainhand, so sort it (16 comes before 17)
local sortedSlots = {}
for slot, link in pairs(self.db.savedGear) do
table.insert(sortedSlots, slot)
end
table.sort(sortedSlots)
for _, slot in ipairs(sortedSlots) do
local link = self.db.savedGear[slot]
if link then
local name = C_Item.GetItemInfo(link)
if name then
table.insert(lines, "/equipslot " .. slot .. " " .. name)
end
end
end
-- When stopping fishing, run a macro to equip old gear, AND a secure snippet or lua to restore AutoLoot.
-- We can just hook the "OnClick" of stopButton securely, no, insecurely to restore autoloot, since we aren't in combat.
self.UI.stopButton:SetAttribute("type", "macro")
if #lines > 0 then
self.UI.stopButton:SetAttribute("macrotext", table.concat(lines, "\n"))
else
self.UI.stopButton:SetAttribute("macrotext", "")
end
end
function FH:UpdateMacro()
if InCombatLockdown() then return end
if not self.UI or not self.UI.mainButton then return end
local needEquip = false
local itemsToEquip = {}
local anyOutfitConfigured = false
local tooltipItem = nil
-- Check outfit
for slot, itemID in pairs(self.db.outfit) do
if itemID then
anyOutfitConfigured = true
local equippedID = GetInventoryItemID("player", slot)
if equippedID ~= itemID then
needEquip = true
local name = C_Item.GetItemInfo(itemID)
if name then
table.insert(itemsToEquip, "/equipslot " .. slot .. " " .. name)
if not tooltipItem then tooltipItem = name end
end
end
end
end
local function ApplyMacro(macroStr, icon, tooltip)
self.UI.mainButton:SetAttribute("type", "macro")
self.UI.mainButton:SetAttribute("macrotext", macroStr)
if self.UI.mainButton.icon then
self.UI.mainButton.icon:SetTexture(icon)
end
local idx = GetMacroIndexByName("FishHelper")
if idx > 0 then
local mText = macroStr
if tooltip then
mText = "#showtooltip " .. tooltip .. "\n" .. macroStr
else
mText = "#showtooltip\n" .. macroStr
end
local name, icon, body = GetMacroInfo(idx)
if body ~= mText then
EditMacro(idx, "FishHelper", "INV_Misc_QuestionMark", mText)
end
end
self.currentMacroText = macroStr
self.currentMacroTooltip = tooltip
end
if needEquip then
-- Add the script call to save gear right before we equip
table.insert(itemsToEquip, 1, "/run FishingHelper:SaveCurrentGear()")
local macroStr = table.concat(itemsToEquip, "\n")
ApplyMacro(macroStr, "Interface\\Icons\\INV_Chest_Cloth_17", tooltipItem)
return
end
-- We are equipped, so we enable auto loot.
if anyOutfitConfigured then
self:EnableAutoLoot()
end
-- Outfit is equipped. Check Lure.
local lureID = self.db.lureID
local recentlySucceeded = self.lastLureSuccess and (GetTime() - self.lastLureSuccess < 2.0)
if lureID and anyOutfitConfigured and not self.isApplyingLure and not recentlySucceeded then
local name = C_Item.GetItemInfo(lureID)
if name and C_Item.GetItemCount(lureID) > 0 then
local hasMainHandEnchant, mainHandExpiration = GetWeaponEnchantInfo()
if not hasMainHandEnchant or (mainHandExpiration and mainHandExpiration < 20000) then
local macroStr = "/use " .. name .. "\n/use 16"
ApplyMacro(macroStr, C_Item.GetItemIconByID(lureID), name)
return
end
end
end
-- Ready to fish
---@diagnostic disable-next-line: undefined-global
local _, _, icon = GetSpellInfo("Fishing")
if not icon then icon = "Interface\\Icons\\Trade_Fishing" end
ApplyMacro("/cast Fishing", icon, "Fishing")
end
frame:SetScript("OnEvent", function(self, event, ...)
if event == "ADDON_LOADED" then
local loadedAddon = ...
if loadedAddon == addonName then
FH:Initialize()
if FH.UI and FH.UI.Initialize then
FH.UI:Initialize()
end
self:UnregisterEvent("ADDON_LOADED")
FH:UpdateMacro()
FH:UpdateStopMacro()
-- Hook Stop Button Click for AutoLoot Restore
if FH.UI and FH.UI.stopButton then
FH.UI.stopButton:HookScript("OnClick", function()
FH:RestoreAutoLoot()
end)
end
end
elseif event == "PLAYER_REGEN_ENABLED" or event == "PLAYER_EQUIPMENT_CHANGED" then
if event == "PLAYER_EQUIPMENT_CHANGED" then
local slotID = ...
if slotID and FH.db.savedGear[slotID] then
local currentItemID = GetInventoryItemID("player", slotID)
local fishingItemID = FH.db.outfit[slotID]
-- Check if the current item matches our configured fishing outfit
if fishingItemID then
if currentItemID ~= fishingItemID then
-- User manually equipped something else in an outfit slot
FH.db.savedGear[slotID] = nil
end
else
-- No fishing item configured for this slot, but we have something saved (e.g. slot 17)
-- If the slot is no longer empty, it's a manual change.
if currentItemID and currentItemID ~= 0 then
FH.db.savedGear[slotID] = nil
end
end
end
end
if FH.UpdateMacro then
FH:UpdateMacro()
FH:UpdateStopMacro()
end
elseif event == "BAG_UPDATE" then
if FH.UI and FH.UI.UpdateLureCount then
FH.UI:UpdateLureCount()
end
elseif event == "UNIT_SPELLCAST_CHANNEL_START" then
local unitTarget, _, spellID = ...
if unitTarget == "player" then
---@diagnostic disable-next-line: undefined-global
local name = GetSpellInfo(spellID)
if name == "Fishing" then
FH.isChannelingFishing = true
FH:EnhanceVolume()
end
end
elseif event == "UNIT_SPELLCAST_CHANNEL_STOP" then
local unitTarget, _, spellID = ...
if unitTarget == "player" then
---@diagnostic disable-next-line: undefined-global
local name = GetSpellInfo(spellID)
if name == "Fishing" then
FH.isChannelingFishing = false
C_Timer.After(1, function()
if not FH.isChannelingFishing then
FH:RestoreVolume()
end
end)
end
end
elseif event == "UNIT_SPELLCAST_START" then
local unitTarget, _, spellID = ...
if unitTarget == "player" then
---@diagnostic disable-next-line: undefined-global
local spellName = GetSpellInfo(spellID)
local lureID = FH.db.lureID
if lureID then
local lureName = C_Item.GetItemInfo(lureID)
if spellName == lureName then
FH.isApplyingLure = true
FH.lastLureSuccess = nil
FH:UpdateMacro()
end
end
end
elseif event == "UNIT_SPELLCAST_SUCCEEDED" then
local unitTarget, _, spellID = ...
if unitTarget == "player" then
---@diagnostic disable-next-line: undefined-global
local spellName = GetSpellInfo(spellID)
local lureID = FH.db.lureID
if lureID then
local lureName = C_Item.GetItemInfo(lureID)
if spellName == lureName then
FH.lastLureSuccess = GetTime()
end
end
end
elseif event == "UNIT_SPELLCAST_FAILED" or event == "UNIT_SPELLCAST_INTERRUPTED" then
local unitTarget, _, spellID = ...
if unitTarget == "player" then
---@diagnostic disable-next-line: undefined-global
local spellName = GetSpellInfo(spellID)
local lureID = FH.db.lureID
if lureID then
local lureName = C_Item.GetItemInfo(lureID)
if spellName == lureName then
FH.lastLureSuccess = nil
end
end
end
elseif event == "UNIT_SPELLCAST_STOP" then
local unitTarget = ...
if unitTarget == "player" then
FH.isApplyingLure = false
FH:UpdateMacro()
end
end
end)