View Single Post
01-21-22, 01:10 AM   #6
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Lua Code:
  1. local whitelist = {
  2.  
  3.     [116841] = "player", --Tiger's Lust
  4.     [228287] = "player", --Mark of the Crane
  5.     [115078] = "all", --Paralysis
  6.    
  7. }
  8.  
  9. local function replacefilter(unit)
  10.     C_NamePlate.GetNamePlateForUnit(unit).UnitFrame.BuffFrame.ShouldShowBuff=function(self,name,caster,nameplateShowPersonal,nameplateShowAll,duration)
  11.         if name then
  12.             local isPlayer=UnitIsUnit(self.unit,"player")
  13.             local isEnemy=UnitCanAttack(self.unit,"player")
  14.             local filter=isPlayer and "HELPFUL" or "HARMFUL"
  15.             if isPlayer or isEnemy then
  16.                 for i=1,BUFF_MAX_DISPLAY do
  17.                     local spellName,_,_,_,spellDuration,_,spellCaster,_,_,spellId=UnitAura(self.unit,i,filter)
  18.                     if not spellName then break end
  19.                     if name==spellName and caster==spellCaster and duration==spellDuration then
  20.                         if whitelist[spellId]==caster or whitelist[spellId]=="all" then
  21.                             return true
  22.                         end
  23.                     end
  24.                 end
  25.             else
  26.                 return nameplateShowAll or (nameplateShowPersonal and (caster=="player" or caster=="pet" or caster=="vehicle"))
  27.             end
  28.         end
  29.         return false
  30.     end
  31. end
  32. local f=CreateFrame("frame")
  33. f:RegisterEvent("NAME_PLATE_UNIT_ADDED")
  34. f:SetScript("OnEvent",function(_,_,unit) replacefilter(unit) end)

I had a go at reorganizing the code. Some notes:
  • Filtering friendly at function replacement won't work, since all 40 nameplate frames will eventually get the replacement due to frame pooling, so you have to filter within the replacement function.
  • Additionally, you don't have to worry about touching instanced friendly nameplates, separate events deal with separate secure frames while still maintaining a max of 40 nameplates active at once (e.g. FORBIDDEN_NAME_PLATE_CREATED)
  • The original ShouldShowBuff has a name==nil escape, which I think should still be included.
  • Since this is a usermade whitelist, INCLUDE_NAME_PLATE_ONLY should be dropped, or you may end up having an aura you want not show up if Blizzard decided it shouldn't show on nameplates.
  • From my experience with a personal addon dealing with attached nameplate frames, UnitIsFriend is not consistent with neutral units, so UnitCanAttack is used instead.
  • Finally, the long return line is from the original ShouldShowBuff, allowing friendly nameplates affected by this replaced function to behave as Blizzard's code intended.

NAME_PLATE_CREATED happens before UnitFrame is added, so stick with the original UNIT_ADDED event

Last edited by Kanegasi : 01-21-22 at 05:46 PM. Reason: Edited to account for error posted below
  Reply With Quote