View Single Post
02-19-14, 07:01 PM   #19
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
First and foremost, stop doing this:
Code:
for _, event in next, {
	"UNIT_COMBO_POINTS",
	"UNIT_POWER",
} do
	caelUI.powersound:RegisterUnitEvent(event, "player")
end
There is absolutely no benefit to registering events this way. You're just wasting memory on a table and CPU on a loop. If you were dynamically altering the list of events to register this might make sense, but for a fixed list of 2 events, just do it normally:
Code:
caelUI.powersound:RegisterUnitEvent("UNIT_COMBO_POINTS", "player")
caelUI.powersound:RegisterUnitEvent("UNIT_POWER", "player")
Other than that, my only complaints are stylistic:
(1) Rather than POWER_TYPE and SPELL_POWER_TYPE, I'd use variable names that more clearly identified what they are, such as POWER_TYPE_NAME and POWER_TYPE_ID.
(2) Mixing upvalues and local variables like this seems sloppy:
Code:
local floor, numPrevious = math.floor, 0
Plus you missed a few upvalues:
Code:
local floor, UnitPower, UnitPowerMax = math.floor, UnitPower, UnitPowerMax
local numPrevious = 0
Actually there is an actual problem; you forgot to define MAX_POWER as a local variable at the top there, so you're leaking a global.

Lua Code:
  1. local _, caelUI = ...
  2.  
  3. -- Upvalues
  4. local floor, UnitPower, UnitPowerMax = math.floor, UnitPower, UnitPowerMax
  5.  
  6. -- Variables
  7. local numPrevious = 0
  8. local POWER_TYPE_NAME, POWER_TYPE_ID, MAX_POWER
  9.  
  10. -- Create frame and register global events
  11. caelUI.powersound = caelUI.createModule("PowerSound")
  12. caelUI.powersound:RegisterUnitEvent("UNIT_COMBO_POINTS", "player")
  13. caelUI.powersound:RegisterUnitEvent("UNIT_POWER", "player")
  14.  
  15. -- Define class variables and register class events
  16. if caelUI.playerClass == "MONK" then
  17.     POWER_TYPE_NAME, POWER_TYPE_ID = "CHI", SPELL_POWER_CHI
  18. elseif caelUI.playerClass == "PALADIN" then
  19.     POWER_TYPE_NAME, POWER_TYPE_ID = "HOLY_POWER", SPELL_POWER_HOLY_POWER
  20. elseif caelUI.playerClass == "PRIEST" then
  21.     POWER_TYPE_NAME, POWER_TYPE_ID, MAX_POWER = "SHADOW_ORBS", SPELL_POWER_SHADOW_ORBS, PRIEST_BAR_NUM_ORBS
  22. elseif caelUI.playerClass == "WARLOCK" then
  23.     caelUI.powersound:RegisterEvent("PLAYER_LOGIN")
  24.     caelUI.powersound:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
  25. end
  26.  
  27. -- Many events, all sides, handle it!
  28. caelUI.powersound:SetScript("OnEvent", function(self, event, unit, powerType)
  29.     if event == "UNIT_POWER" and powerType ~= POWER_TYPE_NAME then return end
  30.  
  31.     if event == "PLAYER_LOGIN" or event == "PLAYER_SPECIALIZATION_CHANGED" then
  32.         local spec = GetSpecialization()
  33.         if spec == SPEC_WARLOCK_AFFLICTION then
  34.             POWER_TYPE_NAME, POWER_TYPE_ID = "SOUL_SHARDS", SPELL_POWER_SOUL_SHARDS
  35. --      elseif spec == SPEC_WARLOCK_DEMONOLOGY then
  36. --          POWER_TYPE_NAME, POWER_TYPE_ID = "DEMONIC_FURY", SPELL_POWER_DEMONIC_FURY
  37.         elseif spec == SPEC_WARLOCK_DESTRUCTION then
  38.             POWER_TYPE_NAME, POWER_TYPE_ID = "BURNING_EMBERS", SPELL_POWER_BURNING_EMBERS
  39.         end
  40.         return
  41.     end
  42.  
  43.     local num, numMax
  44.     if event == "UNIT_COMBO_POINTS" then
  45.         num = GetComboPoints("player", "target")
  46.         numMax = MAX_COMBO_POINTS
  47.     else
  48.         num = UnitPower(unit, POWER_TYPE_ID)
  49.         numMax = MAX_POWER or UnitPowerMax(unit, POWER_TYPE_ID)
  50.     end
  51.  
  52.     if num > numPrevious then
  53.         if num == numMax then
  54.             PlaySoundFile(caelMedia.files.soundComboMax, "Master")
  55.         else
  56.             PlaySoundFile(caelMedia.files.soundCombo, "Master")
  57.         end
  58.     end
  59.  
  60.     numPrevious = num
  61. end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote