Repository navigation
Expand file tree
/
Copy pathcode.lua
More file actions
executable file
·736 lines (663 loc) · 25.8 KB
/
Copy pathcode.lua
File metadata and controls
executable file
·736 lines (663 loc) · 25.8 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
local L = LibStub("AceLocale-3.0"):GetLocale("AutoPotion")
local addonName, ham = ...
local macroName = L["AutoPotion"]
local bandageMacroName = L["AutoBandage"] or "AutoBandage"
local foodMacroName = L["AutoFood"] or "AutoFood"
local drinkMacroName = L["AutoDrink"] or "AutoDrink"
local manaPotionMacroName = L["AutoManaPotion"] or "AutoManaPotion"
local function isInInstancedPvP()
if not ham.isRetail then return false end
local inInstance, instanceType = IsInInstance()
return inInstance and (instanceType == "pvp" or instanceType == "arena")
end
ham.isInInstancedPvP = isInInstancedPvP
ham.debug = false
ham.tinkerSlot = nil
ham.myPlayer = ham.Player.new()
local spellsMacroString = ''
local itemsMacroString = ''
local macroStr = ''
local megaMacroRetryTime = 3 -- seconds between MegaMacro checks
-- MegaMacro addon compatibility
local megaMacro = {
name = "MegaMacro", -- the addon name
retries = 0, -- number of loaded checks to prevent infinite loop
checked = false, -- did we check for the addon?
installed = false, -- is the addon installed?
loaded = false, -- is the addon loaded?
}
-- Slots to check for tinkers
-- As of 2024-10-27, only Head, Wrist and Guns can have tinkers.
local TINKER_SLOTS = {
1, -- Head
9, -- Wrist
18, -- Gun
}
-- in debug mode Trinket 2 counts too, so a trinket with a use effect can stand in for a tinker
local DEBUG_TINKER_SLOTS = { 1, 9, 18, 14 }
local function tinkerSlots()
return ham.debug and DEBUG_TINKER_SLOTS or TINKER_SLOTS
end
local function isTinkerSlot(slot)
for _, tinkerSlot in ipairs(tinkerSlots()) do
if slot == tinkerSlot then return true end
end
return false
end
-- Takes a finished message; a caller that builds one from parts checks ham.debug first, so
-- logging builds no string while debug mode is off.
local function log(message)
if ham.debug then
print("|cffb48ef9AutoPotion:|r " .. message)
end
end
local function addPlayerHealingItemIfAvailable()
if ham.isRetail and ham.options.heartseekingInjector and ham.tinkerSlot then
table.insert(ham.itemIdList, "slot:" .. ham.tinkerSlot)
end
for i, value in ipairs(ham.myPlayer.getHealingItems()) do
if value.getCount() > 0 then
table.insert(ham.itemIdList, value.getId())
break;
end
end
end
local function addHealthstoneIfAvailable()
if ham.isClassic == true or ham.isTBC == true or ham.isWrath == true or ham.isCata == true or ham.isMop == true or ham.isForever == true then
for i, value in ipairs(ham.getHealthstonesClassic()) do
if value.getCount() > 0 then
table.insert(ham.itemIdList, value.getId())
--we break because all Healthstones share a cd so we only want the highest healing one
break;
end
end
else
if ham.healthstone.getCount() > 0 then
table.insert(ham.itemIdList, ham.healthstone.getId())
end
if ham.demonicHealthstone.getCount() > 0 then
table.insert(ham.itemIdList, ham.demonicHealthstone.getId())
end
end
end
local function addPotIfAvailable(useDelightPots)
log("Updating pot counts...")
useDelightPots = useDelightPots or false
local pots = useDelightPots and ham.getDelightPots() or ham.getPots()
for i, value in ipairs(pots) do
if ham.debug then log("Item: " .. tostring(value.getId()) .. " Count: " .. tostring(value.getCount())) end
if value.getCount() > 0 then
table.insert(ham.itemIdList, value.getId())
--we break because all Pots share a cd so we only want the highest healing one
break;
end
end
end
function ham.updateHeals()
ham.itemIdList = {}
--ham.spellIDs = ham.myPlayer.getHealingSpells()
ham.mySpells = ham.myPlayer.getHealingSpells()
-- Priority 1: Add player items, including tinkers
addPlayerHealingItemIfAvailable()
-- Priority 2: Add Healthstone if NOT set to lower priority
if not ham.options.raidStone then
addHealthstoneIfAvailable()
end
-- Priority 3: Add Health Pots (skip in instanced PvP - not allowed)
if not isInInstancedPvP() then
if not ham.options.heartseekingInjector or not ham.tinkerSlot then
addPotIfAvailable()
end
-- Priority 4: Add Cavedweller's Delight if enabled
if ham.options.cavedwellerDelight then
addPotIfAvailable(true)
end
end
-- Priority 5: Add Healthstone if set to lower priority
if ham.options.raidStone then
addHealthstoneIfAvailable()
end
end
-- Spells that are only usable out of combat (Recuperate, the Earthen racial Quiet
-- Contemplation). They can't go in the castsequence, so they get their own [nocombat] line.
local function isOutOfCombatSpell(id)
return id == ham.recuperate.getId() or id == ham.quietContemplation.getId()
end
-- The out-of-combat spell to put on the [nocombat] line, if any. Quiet Contemplation is
-- preferred since it also restores mana. Recuperate isn't allowed in instanced PvP.
local function getOutOfCombatSpell()
if not ham.isRetail then return nil end
if ham.dbContains(ham.quietContemplation.getId()) and ham.quietContemplation.isKnown() then
return ham.quietContemplation
end
if not isInInstancedPvP() and ham.dbContains(ham.recuperate.getId()) and ham.recuperate.isKnown() then
return ham.recuperate
end
return nil
end
-- The spell's base cooldown in seconds, nil when the client reports none or 0 (spells with
-- charges like Healing Elixir). GetSpellBaseCooldown exists on every flavor (confirmed in-game on
-- Forever), but can return nil for a spell id absent from the client's spell data.
local function baseCooldown(spellId)
local cooldown = spellId and GetSpellBaseCooldown(spellId)
if cooldown and cooldown > 0 then return cooldown / 1000 end
return nil
end
local function isHealthstone(itemId)
if itemId == ham.healthstone.getId() or itemId == ham.demonicHealthstone.getId() then return true end
for _, healthstone in ipairs(ham.getHealthstonesClassic()) do
if itemId == healthstone.getId() then return true end
end
return false
end
-- Whether the player knows this talent. C_SpellBook.IsSpellKnown is the current "is spell known"
-- query (IsPlayerSpell is a deprecated wrapper around it); Cata and Wrath have no C_SpellBook
-- version and keep the native IsSpellKnown global.
local function isTalentKnown(talentId)
if C_SpellBook and C_SpellBook.IsSpellKnown then
return C_SpellBook.IsSpellKnown(talentId, Enum.SpellBookSpellBank.Player)
end
return IsSpellKnown(talentId)
end
-- The spell's cooldown in seconds with the cooldown talents the player knows, nil when it has no
-- base cooldown. Of a talent with ranks (entries sharing `rankOf`) only the highest known counts.
local function talentedCooldown(spellId)
local cooldown = baseCooldown(spellId)
if not cooldown then return nil end
local taken = {}
for talentId, talent in pairs(ham.cooldownTalents) do
if talent.spell.getId() == spellId and isTalentKnown(talentId) then
local key = talent.rankOf or talentId
taken[key] = math.max(taken[key] or 0, talent.seconds)
end
end
for _, seconds in pairs(taken) do
cooldown = cooldown - seconds
end
return cooldown
end
-- The cooldown in seconds of the item's on-use spell, nil when the client reports none.
local function itemCooldown(itemId)
local getItemSpell = C_Item and C_Item.GetItemSpell or GetItemSpell
local _, spellId = getItemSpell(itemId)
return baseCooldown(spellId)
end
-- The cooldown in seconds of the castsequence's first entry, nil when unknown: the first spell's
-- cooldown with its cooldown talents, else a Healthstone's on-use spell's. Other items (potions,
-- the tinker slot) are unknown.
local function firstEntryCooldown()
for _, spell in ipairs(ham.mySpells) do
if not isOutOfCombatSpell(spell.getId()) then
return talentedCooldown(spell.getId())
end
end
local itemId = ham.itemIdList[1]
if type(itemId) ~= "number" or not isHealthstone(itemId) then return nil end
return itemCooldown(itemId)
end
-- What follows reset= in a castsequence: combat, plus the reset timer when there is one
local function resetArgument(seconds)
if seconds then return "combat/" .. seconds end
return "combat"
end
-- The AutoPotion castsequence's reset= argument: the first entry's cooldown is the reset timer
-- with the option on
local function sequenceResetArgument()
return resetArgument(ham.options.cdReset and firstEntryCooldown())
end
local function buildSpellMacroString()
spellsMacroString = ''
if next(ham.mySpells) ~= nil then
local spellCounter = 1
for i, spell in ipairs(ham.mySpells) do
local name = ''
if isOutOfCombatSpell(spell.getId()) then
--we don't want to add these because even though they are spells they are only usable out of combat
else
name = spell.getName();
--TODO HEALING Elixir Twice because it has two charges ?! kinda janky but will work for now
if spell.getId() == ham.healingElixir.getId() then
name = spell.getName() .. ", " .. spell.getName()
end
if spellCounter == 1 then
spellsMacroString = name;
else
spellsMacroString = spellsMacroString .. ", " .. name;
end
spellCounter = spellCounter + 1
end
end
end
end
local function buildItemMacroString()
if next(ham.itemIdList) ~= nil then
for i, name in ipairs(ham.itemIdList) do
local entry
-- Check if the entry starts with "slot:" and extract the slot number
if type(name) == "string" and name:match("^slot:") then
entry = name:sub(6) -- Extract everything after "slot:"
else
entry = "item:" .. tostring(name) -- Default to item ID formatting
end
-- Add the entry to the macro string
if i == 1 then
itemsMacroString = entry
else
itemsMacroString = itemsMacroString .. ", " .. entry
end
end
end
end
-- Hands the text to the MegaMacro macro with this display name. Returns false when the player
-- hasn't created it.
local function updateMegaMacro(name, text)
for _, macro in pairs(MegaMacroGlobalData.Macros) do
if macro.DisplayName == name then
MegaMacro.UpdateCode(macro, text)
if ham.debug then log("MegaMacro updated (" .. name .. ") with: " .. text) end
return true
end
end
print("|cffff0000AutoPotion Error:|r Missing global '" ..
tostring(name) .. "' macro in MegaMacro. Please create it then reload your game.")
return false
end
local writtenText = {} -- [macro name] = the text last written to it this session
local reportedFull = {} -- [macro name] = true once the player was told it didn't fit
-- Retail and Forever list the limit in Constants.MacroConsts, the older clients in a global.
local function maxGeneralMacros()
local consts = Constants and Constants.MacroConsts
return consts and consts.MAX_ACCOUNT_MACROS or MAX_ACCOUNT_MACROS
end
-- Creates the macro unless the general macro slots are full; then it tells the player, once per
-- session for each macro, and returns false.
local function createMacro(name, text)
if GetNumMacros() >= maxGeneralMacros() then
if not reportedFull[name] then
reportedFull[name] = true
local message =
L["Couldn't create the %s macro: your general macros are full. Delete one of them and it will be created."]
print("|cffb48ef9AutoPotion:|r " .. format(message, name))
end
return false
end
CreateMacro(name, "INV_Misc_QuestionMark", text)
return true
end
-- The one writer for all five macros, standard or MegaMacro-managed. Writing a macro makes the
-- action bars refresh, so it only writes text that differs from what it wrote last.
local function writeMacro(name, text)
if megaMacro.installed and megaMacro.loaded then
if writtenText[name] ~= text and updateMegaMacro(name, text) then writtenText[name] = text end
return
end
if GetMacroInfo(name) == nil then
if not createMacro(name, text) then return end
elseif writtenText[name] ~= text then
EditMacro(name, name, nil, text)
else
return
end
writtenText[name] = text
if ham.debug then log(name .. " macro updated.") end
end
local function checkMegaMacroAddon()
-- MegaMacro is only available for retail
if not ham.isRetail then
megaMacro.checked = true
return
end
-- is MegaMacro installed?
local name = C_AddOns.GetAddOnInfo(megaMacro.name)
if not name then
megaMacro.installed = false
megaMacro.checked = true
return
end
megaMacro.installed = true
-- is the addon loaded?
if C_AddOns.IsAddOnLoaded(megaMacro.name) then
megaMacro.loaded = true
megaMacro.checked = true
return
end
-- Retry loading if not yet loaded
if megaMacro.retries < 3 then
megaMacro.retries = megaMacro.retries + 1
C_Timer.After(megaMacroRetryTime, function()
checkMegaMacroAddon()
-- the macros wait for this check
if megaMacro.checked then ham.requestMacroUpdate() end
end)
else
megaMacro.checked = true
end
end
-- Rapid Regeneration's cooldown in seconds: the bandage spell's castsequence starts over after it
local BANDAGE_SPELL_COOLDOWN = 180
-- Build bandage macro string (highest available bandage first)
local function buildBandageMacroString()
local sequence = {}
local bandages = ham.getBandages()
for _, item in ipairs(bandages) do
if item.getCount() > 0 then
table.insert(sequence, "item:" .. tostring(item.getId()))
break
end
end
-- Neither the spell nor the bandage triggers the global cooldown, so two lines would fire both
-- on one press: a castsequence takes the bandage while the spell is on its cooldown
local spell = ham.getBandageSpell()
if spell then
if #sequence == 0 then
return "#showtooltip\n/cast " .. spell.getName()
end
return "#showtooltip\n/castsequence [@player] reset=combat/" .. BANDAGE_SPELL_COOLDOWN .. " " .. spell.getName() .. ", " .. sequence[1]
end
if #sequence == 0 then
return "#showtooltip"
end
return "#showtooltip\n/use [@player] " .. sequence[1]
end
-- Build food macro string (highest available food first)
local function buildFoodMacroString()
local sequence = {}
local food = ham.getFood()
for _, item in ipairs(food) do
if item.getCount() > 0 then
table.insert(sequence, "item:" .. tostring(item.getId()))
break
end
end
if #sequence == 0 then
return "#showtooltip"
end
return "#showtooltip\n/use [@player] " .. sequence[1]
end
-- Build drink macro string (highest available drink first)
local function buildDrinkMacroString()
local sequence = {}
local drink = ham.getDrink()
for _, item in ipairs(drink) do
if item.getCount() > 0 then
table.insert(sequence, "item:" .. tostring(item.getId()))
break
end
end
if #sequence == 0 then
return "#showtooltip"
end
return "#showtooltip\n/use [@player] " .. sequence[1]
end
-- Build mana potion macro string: the ticked mana spells the character knows (best restore
-- first), then the best mana gem, then the best mana potion, then (with "Include Demonic Runes")
-- a demonic rune, the last resort as it costs health. None of them share a cooldown, so with two
-- or more a castsequence uses one per press and starts over with the first when combat starts or
-- ends. The first entry's cooldown (a spell's with its cooldown talents) is the reset timer. With
-- a mana spell the line is cast on the player, so a friendly target never gets Innervate.
local function buildManaPotionMacroString()
local spells = ham.myPlayer.getManaSpells()
local items = {}
local gem = ham.getManaGem()
if gem then table.insert(items, gem) end
for _, item in ipairs(ham.getManaPotions()) do
if item.getCount() > 0 then
table.insert(items, item)
break
end
end
local demonicRune = ham.getDemonicRune()
if demonicRune then table.insert(items, demonicRune) end
local sequence = {}
for _, spell in ipairs(spells) do table.insert(sequence, spell.getName()) end
for _, item in ipairs(items) do table.insert(sequence, "item:" .. tostring(item.getId())) end
if #sequence == 0 then
return "#showtooltip"
end
local command, target, firstCooldown
if #spells > 0 then
command, target = "/cast ", "[@player] "
firstCooldown = talentedCooldown(spells[1].getId())
else
command, target = "/use ", ""
firstCooldown = itemCooldown(items[1].getId())
end
if #sequence == 1 then
return "#showtooltip\n" .. command .. target .. sequence[1]
end
return "#showtooltip\n/castsequence " .. target .. "reset=" .. resetArgument(firstCooldown) .. " "
.. table.concat(sequence, ", ")
end
-- Soulburn (Warlock talent) empowers the next Healthstone. It's off the GCD, so a leading
-- "/cast [combat] Soulburn" line fires in the same keypress as the castsequence step. It
-- can't live inside the castsequence itself: with no Soul Shard the sequence would get stuck.
local function shouldSoulburn()
if not ham.isRetail or not ham.options.soulburn then return false end
if ham.myPlayer.englishClass ~= "WARLOCK" then return false end
-- talents are not always in the spellbook, so also check whether the player knows it
-- (C_SpellBook.IsSpellKnown replaces the deprecated IsPlayerSpell; Retail only, see above)
if not (ham.soulburn.isKnown()
or C_SpellBook.IsSpellKnown(ham.soulburn.getId(), Enum.SpellBookSpellBank.Player)) then
return false
end
-- only when a Healthstone is actually in the sequence, so no shards are burned without one
for _, id in ipairs(ham.itemIdList) do
if id == ham.healthstone.getId() or id == ham.demonicHealthstone.getId() then
return true
end
end
return false
end
-- check if player has the engineering tinker: Heartseeking Health Injector
function ham.checkTinker()
if not ham.isRetail then return end
ham.tinkerSlot = nil -- always reset
for _, slot in ipairs(tinkerSlots()) do
local itemID = GetInventoryItemID("player", slot)
if itemID then
local spellName, spellID = C_Item.GetItemSpell(itemID)
if spellName then
-- note: i'm not an engineer, so i use a trinket with a use effect for debugging.
-- this is why the "Phylactery" reference exists if debugging is enabled --- phuze.
-- note: Using "spellName" to find "Heartseeking" can only support English region.
-- So I add spellID to support other region, e.g. Taiwan (TW), China (CN) and Korea (KR) --- Nephits
if ham.debug then
if spellName:find("Phylactery") or spellName:find("Heartseeking") or spellID == 452767 then
ham.tinkerSlot = slot
end
else
if spellName:find("Heartseeking") or spellID == 452767 then
ham.tinkerSlot = slot
end
end
end
end
end
end
function ham.updateMacro()
--if next(ham.itemIdList) == nil and next(ham.spellIDs) == nil then
if next(ham.itemIdList) == nil and next(ham.mySpells) == nil then
macroStr = "#showtooltip"
if ham.options.stopCast then
macroStr = macroStr .. "\n/stopcasting \n"
end
else
buildItemMacroString()
buildSpellMacroString()
macroStr = "#showtooltip \n"
if ham.options.stopCast then
macroStr = macroStr .. "/stopcasting \n"
end
-- Recuperate / Quiet Contemplation: out-of-combat only
-- this condition is needed because if not used the castsequence will use off gcd heals direclty after them
local combatCondition = ''
local outOfCombatSpell = getOutOfCombatSpell()
if outOfCombatSpell then
combatCondition = ',combat'
macroStr = macroStr .. "/cast [nocombat] " .. outOfCombatSpell.getName() .. "\n"
end
local sequenceStr = "/castsequence [@player" .. combatCondition .. "] reset=" .. sequenceResetArgument() .. " "
if spellsMacroString ~= "" then
sequenceStr = sequenceStr .. spellsMacroString
end
if spellsMacroString ~= "" and itemsMacroString ~= "" then
sequenceStr = sequenceStr .. ", "
end
if itemsMacroString ~= "" then
sequenceStr = sequenceStr .. itemsMacroString
end
-- Soulburn has to come before the castsequence so it is active when the Healthstone is used
if shouldSoulburn() then
local soulburnLine = "/cast [combat] " .. ham.soulburn.getName() .. "\n"
-- standard macros are limited to 255 characters (MegaMacro allows more);
-- rather drop Soulburn than break the whole macro
if not (megaMacro.installed and megaMacro.loaded) and #(macroStr .. soulburnLine .. sequenceStr) > 255 then
log("Macro too long, skipping Soulburn.")
else
macroStr = macroStr .. soulburnLine
end
end
macroStr = macroStr .. sequenceStr
end
if not megaMacro.checked then
log("MegaMacro not checked. Retrying.")
checkMegaMacroAddon()
return
end
writeMacro(macroName, macroStr)
end
function ham.updateBandageMacro()
writeMacro(bandageMacroName, buildBandageMacroString())
end
function ham.updateFoodMacro()
writeMacro(foodMacroName, buildFoodMacroString())
end
function ham.updateDrinkMacro()
writeMacro(drinkMacroName, buildDrinkMacroString())
end
function ham.updateManaPotionMacro()
writeMacro(manaPotionMacroName, buildManaPotionMacroString())
end
-- A hidden page's preview is left alone: its OnShow refreshes it when the player opens it.
local function isShown(page)
return page.panel ~= nil and page.panel:IsVisible()
end
-- Works out the current priorities from the bags and shows them on the settings pages that are
-- open. It makes no protected calls, so it also runs in combat.
function ham.refreshPreviews()
ham.checkTinker()
ham.updateHeals()
if isShown(ham.settingsFrame) then ham.settingsFrame:updatePrio() end
if isShown(ham.bandageSettingsFrame) then ham.bandageSettingsFrame:updateBandagePrio() end
if isShown(ham.foodSettingsFrame) then ham.foodSettingsFrame:updateFoodPrio() end
if isShown(ham.drinkSettingsFrame) then ham.drinkSettingsFrame:updateDrinkPrio() end
if isShown(ham.manaPotionSettingsFrame) then ham.manaPotionSettingsFrame:updateManaPotionPrio() end
end
local function rebuildMacros()
ham.refreshPreviews()
ham.updateMacro()
ham.updateBandageMacro()
ham.updateFoodMacro()
ham.updateDrinkMacro()
ham.updateManaPotionMacro()
end
local updatePending = false -- an update was requested and hasn't run yet
local flushArmed = false -- a next-frame flush is waiting
-- Runs a pending update unless combat lockdown blocks it; PLAYER_REGEN_ENABLED runs it then.
local function flushMacroUpdate()
if not updatePending or InCombatLockdown() then return end
-- dont attempt to create macro until MegaMacro addon is checked; the check requests an update
if not megaMacro.checked then
log("MegaMacro not checked or loaded. Retrying.")
checkMegaMacroAddon()
if not megaMacro.checked then return end
end
updatePending = false
rebuildMacros()
end
local function onNextFrame()
flushArmed = false
flushMacroUpdate()
end
-- The one way to get the macros rebuilt. Requests made in the same frame lead to one rebuild on
-- the next frame; a request made in combat waits until combat ends.
function ham.requestMacroUpdate()
updatePending = true
if flushArmed or InCombatLockdown() then return end
flushArmed = true
C_Timer.After(0, onNextFrame)
end
-- `message` is a constant, so it costs nothing while debug mode is off
local function requestOn(message)
return function()
log(message)
ham.requestMacroUpdate()
end
end
local eventHandlers = {
ADDON_LOADED = function(self, name)
if name ~= addonName then return end
self:UnregisterEvent("ADDON_LOADED")
log("event: ADDON_LOADED")
ham.requestMacroUpdate()
end,
-- fires once a burst of BAG_UPDATEs has settled
BAG_UPDATE_DELAYED = requestOn("event: BAG_UPDATE_DELAYED"),
UNIT_PET = requestOn("event: UNIT_PET"),
-- also on zoning: the instanced-PvP rules depend on the zone
PLAYER_ENTERING_WORLD = requestOn("event: PLAYER_ENTERING_WORLD"),
-- crossing into another zone has no loading screen; Forever has zone-locked potions
ZONE_CHANGED_NEW_AREA = requestOn("event: ZONE_CHANGED_NEW_AREA"),
-- only a tinker slot can change what the macros use; a gear set fires this once per slot
PLAYER_EQUIPMENT_CHANGED = function(_, slot)
if isTinkerSlot(slot) then
log("event: PLAYER_EQUIPMENT_CHANGED")
ham.requestMacroUpdate()
end
end,
TRAIT_CONFIG_UPDATED = requestOn("event: TRAIT_CONFIG_UPDATED"),
-- a trainer, a talent or a level teaches a spell a ticked heal or mana spell may be waiting for
LEARNED_SPELL_IN_SKILL_LINE = requestOn("event: LEARNED_SPELL_IN_SKILL_LINE"),
-- the food buff, for "only when the buff is missing"
UNIT_AURA = function(_, _, updateInfo)
ham.onPlayerAura(updateInfo)
end,
-- encounters, M+ and PvP hide auras from addons; after one, they can be read again
ADDON_RESTRICTION_STATE_CHANGED = function()
ham.onRestrictionChanged()
end,
-- run what was requested during combat
PLAYER_REGEN_ENABLED = function()
log("event: PLAYER_REGEN_ENABLED")
flushMacroUpdate()
end,
}
local updateFrame = CreateFrame("Frame")
updateFrame:RegisterEvent("ADDON_LOADED")
updateFrame:RegisterEvent("BAG_UPDATE_DELAYED")
updateFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
-- tinkers are Retail content
if ham.isRetail then
updateFrame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
end
if ham.isForever then
updateFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
end
-- talents (trait configs) can teach or remove healing spells; only clients that have them know
-- the event, and registering an unknown event is a Lua error
if C_EventUtils.IsEventValid("TRAIT_CONFIG_UPDATED") then
updateFrame:RegisterEvent("TRAIT_CONFIG_UPDATED")
end
if C_EventUtils.IsEventValid("LEARNED_SPELL_IN_SKILL_LINE") then
updateFrame:RegisterEvent("LEARNED_SPELL_IN_SKILL_LINE")
end
if C_EventUtils.IsEventValid("ADDON_RESTRICTION_STATE_CHANGED") then
updateFrame:RegisterEvent("ADDON_RESTRICTION_STATE_CHANGED")
end
updateFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
-- only the player's own pet, not every group member's
updateFrame:RegisterUnitEvent("UNIT_PET", "player")
updateFrame:RegisterUnitEvent("UNIT_AURA", "player")
updateFrame:SetScript("OnEvent", function(self, event, ...)
eventHandlers[event](self, ...)
end)