In the interests of reducing code duplication, and reducing the number of function calls and global lookups (checking the class and spec on every power update is quite wasteful) I would rewrite that like so:
Lua Code:
caelUI.powersound = caelUI.createModule("PowerSound")
-- Use RegisterUnitEvent, not RegisterEvent, to filter out irrelevant events.
caelUI.powersound:RegisterUnitEvent("UNIT_COMBO_POINTS", "player")
caelUI.powersound:RegisterUnitEvent("UNIT_POWER", "player")
-- Look these things up once at load; they're not going to change mid-session.
local POWER_TYPE, POWER_DIVISOR
if caelUI.playerClass == "MONK" then
POWER_TYPE = "CHI"
elseif caelUI.playerClass == "PALADIN" then
POWER_TYPE = "HOLY_POWER"
elseif caelUI.playerClass == "PRIEST" then
POWER_TYPE = "SHADOW_ORBS"
MAX_POWER = PRIEST_BAR_NUM_ORBS
elseif caelUI.playerClass == "WARLOCK" then
-- Only register these events on a class that needs it.
caelUI.powersound:RegisterEvent("PLAYER_LOGIN")
caelUI.powersound:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
end
local lastCount = 0
caelUI.powersound:SetScript("OnEvent", function(self, event, unit, powerType)
-- This will quickly get out of irrelevant power updates, which are the most frequent case.
if event == "UNIT_POWER" and powerType ~= POWER_TYPE then return end
-- This is the least frequent case, but the other events share code, so check for this one first.
if event == "PLAYER_SPECIALIZATION_CHANGED" then
local spec = GetSpecialization()
if spec == SPEC_WARLOCK_AFFLICTION then
POWER_TYPE = "SOUL_SHARDS"
POWER_DIVISOR = 1
elseif spec == SPEC_WARLOCK_DEMONOLOGY then
POWER_TYPE = "DEMONIC_FURY"
POWER_DIVISOR = 250
elseif spec == SPEC_WARLOCK_DESTRUCTION then
POWER_TYPE = "BURNING_EMBERS"
POWER_DIVISOR = 10
end
return
end
local count, maxCount
if event == "UNIT_COMBO_POINTS" then
count = UnitComboPoints("player", "target")
maxCount = MAX_COMBO_POINTS
elseif POWER_DIVISOR then
-- Only use this code path for warlocks to avoid the 2 function calls if they're not needed.
count = floor(UnitPower(unit, powerType) / POWER_DIVISOR)
maxCount = MAX_POWER or floor(UnitPowerMax(unit, powerType) / POWER_DIVISOR)
else
count = UnitPower(unit, powerType)
maxCount = MAX_POWER or UnitPowerMax(unit, powerType)
end
if count > lastCount then
-- count can't be < 0, so neither can lastCount, and 0 is not > 0, so no need to check count > 0
if count == maxCount then
-- Check this first so you can just "else" straight into the other thing.
PlaySoundFile(caelMedia.files.soundComboMax, "Master")
else
PlaySoundFile(caelMedia.files.soundCombo, "Master")
end
end
lastCount = count
end)
Also you may want to upvalue UnitPower, UnitPowerMax, and math.floor to speed things up.