There's a lot of places in this code that needs improvement. First of all, you shouldn't be looking for buffs by name. This will only work on one locale (client language) and not others. You should use SpellIDs instead.
Also, storing pointers by frame name will oftentimes get you into trouble as not all frames are named. This will make you run into "index by
nil" errors when this happens. You should reference by the frame pointer itself. This is completely unique and is never
nil.
Overall, the problem with an addon like this is the fact that buffs that modify the same stat type override each other. This may lead to two or more players fighting over whether or not their buff is up because they keep overriding each other. This is where having consolidated buffs on is helpful so you can tell what stat types are being buffed and remember what your buff does as part of knowing your class.
Anyway, here's a much better implementation of your code. Doing what you're describing, performing the inverse of showing/hiding the frame, is practically impossible in design. This is because a few of the classes have to choose which buff they activate which are mutually exclusive.
Lua Code:
local IndicatorLookup={};
local BuffList={
-- Death Knight
[57330]=true;-- Horn of Winter
-- Druid
[1126]=true;-- Mark of the Wild
-- Mage
[1459]=true;-- Arcane Brilliance
[61316]=true;-- Dalaran Brilliance
-- Monk
[115921]=true;-- Legacy of the Emperor
[116781]=true;-- Legacy of the White Tiger
-- Paladin
[19740]=true;-- Blessing of Might
[20217]=true;-- Blessing of Kings
-- Priest
[21562]=true;-- Power Word: Fortitude
-- Warlock
[109773]=true;-- Dark Intent
-- Warrior
[6673]=true;-- Battle Shout
[469]=true;-- Commanding Shout
};
hooksecurefunc("CompactUnitFrame_UpdateBuffs",function(self)
if not self:IsVisible() then return; end
local unit,index=self.displayedUnit,1;-- Prepare variables for loop
repeat
local _,_,_,_,_,_,_,caster,_,_,spellid=UnitBuff(unit,index);-- Grab buffs cast by us
if spellid then-- Buff exists?
if (caster=="player" or caster=="pet" or caster=="vehicle") and BuffList[spellid] then break; end-- If we found our buff, break out of loop
index=index+1;-- Increment index
else
index=nil;-- Else clear index
end
until not spellid-- Stop if no more buffs
-- At this point, Index will hold our found buff,
local frame=IndicatorLookup[self];
if not frame then
-- If we didn't find our buff, then don't bother creating a frame
if not index then return; end
frame=CreateFrame("Button",nil,self,"CompactAuraTemplate");
frame:ClearAllPoints();
frame:SetPoint("TOPRIGHT",-3,-2);
frame:SetSize(self.buffFrames[1]:GetSize());
frame:SetAlpha(0.6);
IndicatorLookup[self]=frame;
end
if index then CompactUnitFrame_UtilSetBuff(frame,unit,index,"PLAYER"); end-- Apply buff found to template
frame:SetShown(index and true or false);-- Typecast to boolean
end);