Thread Tools Display Modes
03-05-12, 08:53 PM   #1
Boradan
A Defias Bandit
AddOn Author - Click to view addons
Join Date: Mar 2012
Posts: 3
code to check player for buff

What is the code you need to check if a unit("player") has a certain buff?
Woulldnt mind fitting that into my addon to check if the player has any of the following buffs:
Battle Shout,
Horn of Winter,
Kings,
Mark of the Wild,
Well Fed.
Am new to this lua stuff, so any assistance would be appreciated ....

Last edited by Seerah : 03-06-12 at 10:07 AM. Reason: (moved from a thread in the tutorials section)
  Reply With Quote
03-06-12, 10:50 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
http://www.wowpedia.org/API_UnitAura

UnitAura allows you to check for buffs, debuffs
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
03-06-12, 11:02 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Something like this:

lua Code:
  1. --  Make a list of the buffs you want to check for:
  2. local buffs = {
  3.     ["Battle Shout"] = true,
  4.     ["Blessing of Kings"] = true,
  5.     ["Horn of Winter"] = true,
  6.     ["Mark of the Wild"] = true,
  7.     ["Well Fed"] = true,
  8. }
  9.  
  10. ------------------------------------------------------------------------
  11. --  Create a frame.
  12. --  Frames are the basic UI objects in WoW.
  13. --  You need them in order to respond to events (things happening in the
  14. --  game) or to show visible elements on the screen (graphics, text,
  15. --  buttons, etc.).
  16. ------------------------------------------------------------------------
  17. local frame = CreateFrame("Frame")
  18.  
  19. ------------------------------------------------------------------------
  20. --  Register for the UNIT_AURA event.
  21. --  Events are how WoW tells addons that something has happened.
  22. --  The UNIT_AURA event fires whenever any buff or debuff changes on any
  23. --  unit that currently has a unit token (such as player, target, boss1,
  24. --  focustarget, arena3, party2, or raid18pet).
  25. ------------------------------------------------------------------------
  26. frame:RegisterEvent("UNIT_AURA")
  27.  
  28. ------------------------------------------------------------------------
  29. --  Set an OnEvent handler for the frame.
  30. --  This is a function that will be run each time the frame receives
  31. --  an event for which it has registered.
  32. --  The function recieves a reference to the frame as its first
  33. --  argument (conventionally named "self"), the name of the event
  34. --  as the second argument (conventionally named "event"), and then
  35. --  event-specific data from the third argument onward.
  36. ------------------------------------------------------------------------
  37. frame:SetScript("OnEvent", function(self, event, ...)
  38.     --------------------------------------------------------------------
  39.     --  ... is called a vararg, and is a construct that contains any
  40.     --  number of variables. It is used mainly when receiving variables
  41.     --  without knowing how many of them there are. Since different
  42.     --  events pass different numbers of arguments, a vararg is very
  43.     --  common in event handling functions.
  44.     --  Technically you don't need it here, since your frame is only
  45.     --  registered for one event, but if you expand your addon later,
  46.     --  it may become useful.
  47.     --------------------------------------------------------------------
  48.  
  49.     --  Unpack the vararg into the variable(s) it contains:
  50.     local unit = ...
  51.  
  52.     --  If the unit is not "player" you don't care about it, so
  53.     --  you can quit out of the function early to save time:
  54.     if unit ~= "player" then
  55.         return
  56.     end
  57.  
  58.     --  Loop through all of the buffs you want to check for:
  59.     for buff in pairs(buffs) do
  60.         --  Check to see if you have the buff:
  61.         if UnitBuff("player", buff) then
  62.             --  You have the buff.
  63.             --  Do something here.
  64.         else
  65.             --  You don't have the buff.
  66.             --  Do something here.
  67.         end
  68.     end
  69. end)
  Reply With Quote
03-08-12, 07:49 PM   #4
Boradan
A Defias Bandit
AddOn Author - Click to view addons
Join Date: Mar 2012
Posts: 3
Hmmmm ... I think I wasnt specific enough in the original question ...

The addon I have done is up and running on wow interface ( http://www.wowinterface.com/download...enseStats.html )
It is working fine, apart from the fact that it assumes you have no buffs currently when it does all the calculations to output your CTC.
If you get a buff that you can select from the dropdowns, then effectively the buff is added twice to the calculations (these are the only buffs available that will affect your Combat Table Coverage (well, apart from different ranks and the myriad of food buffs that give lower stat rating boosts, but that would need a list faaaaaaar too long to factor into the equations) ....

Heres a pastebin of the lua as it stands:
http://pastebin.com/0prqsJwk

the issue I am running into is the UpdateBuffStats()
from line 336 to line 349

I want to check the player to see if he has any of the buffs you can select in the addon to see your raid buffed stats currently applied, and if so, to change the output of the UpdateBuffStats()

As it stands, the lines in question are :
Code:
    local food, strength, stat

    local function UpdateBuffStats()
    
        BuffStrengthAdjust = strength.StrengthValue + food.StrengthValue
        BuffParryAdjust =  food.ParryValue
        BuffDodgeAdjust =  food.DodgeValue
        BuffMasteryAdjust = food.MasteryValue
        BuffMultiplierAdjust = stat.BuffValue

    
        PAPERDOLL_STATINFO["TOTALAVOIDANCE"].updateFunc(Frame1, "player")
        PAPERDOLL_STATINFO["COMBATTABLE"].updateFunc(Frame2, "player")
    end
I need to check if the player has these buffs (it will need to be by spellId as the well fed buffs all return the name 'Well Fed' even though they give different stats .... (stupid way of blizzard doing this IMO)

Code:
"Mark of the Wild" = 79061
"Blessing of Kings" = 79063
"Horn of Winter" = 57330
"Battle Shout" = 6673
"Well Fed" = 87554  --Dodge food Buff
"Well Fed" = 87555   --Parry Food Buff
"Well Fed" = 87549   --Mastery Food Buff
"Well Fed" = 87545   --Strength Food Buff
I know WHAT I need to do, but cant, for the life of me incorporate it into the addon.....
Once getting the spellId of all buffs currently on the player (using spellId = UnitAura("player",i) ), the current values for BuffStrengthAdjust, BuffParryAdjust, BuffDodgeAdjust, BuffMasteryAdjust and BufMultiplierAdjust would need to be worked out in the following way:

Code:
if  (spellId ~= 79061 and spellId ~= 79063) then BuffMultiplierAdjust = stat.BuffValue else BuffMultiplierAdjust = 1

if  spellId ~= 87549 then BuffMasteryAdjust = food.MasteryValue else BuffMasteryAdjust = 0

if  spellId ~= 87554 then BuffDodgeAdjust = food.DodgeValue else BuffDodgeAdjust = 0

if spellId ~= 87555 then BuffParryAdjust = food.ParryValue else BuffParryAdjust = 0

--this next formula is a bit more complex as there are 3 combinations :(  

if  (spellId ~= 57330 and spellId ~= 6673 and spellId ~= 87545) then BuffStrengthAdjust = strength.StrengthValue + food.StrengthValue
else if (spellId = 57330 or spellId = 6673) and spellId ~= 87545 then BuffStrengthAdjust = food.StrengthValue
else if (spellId ~= 57330 and spellId ~= 6637) and spellId = 87545 then BuffStrengthAdjust = strength.StrengthValue
I have no feckin idea how to get this lot into the lua to factor in the current buffs when working out the new stat values ( I actually DID get it working once, but the values didnt update if the buff disappeared (like Battle shout with its 2 minute cooldown), but much to my chagrin, this poxy village suffered a power cut at that time and I lost the file )

I may be going about this in totally the wrong way, as it may be simpler to factor in the Current buffs when you are selecting the raid buffs from the dropdown menu (though that would be complicated when choosing from the list of food buffs )

Andy
  Reply With Quote
03-08-12, 08:42 PM   #5
kaels
A Cyclonian
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 46
Here's how you would modify Phanx's code to do what you want to do:

lua Code:
  1. local buffs = {
  2.     -- setting the defaults to false so that you can start out assuming the player doesn't have the buff
  3.     ["Battle Shout"] = false,
  4.     ["Blessing of Kings"] = false,
  5.     ["Horn of Winter"] = false,
  6.     ["Mark of the Wild"] = false,
  7.     -- add the spell IDs for food buffs here - you can feed them into UnitBuff with the same syntax as spell names
  8.     87554 = false,
  9.     87555 = false,
  10.     87549 = false,
  11.     87545 = false,
  12. }
  13.  
  14. local frame = CreateFrame("Frame")
  15.  
  16. frame:RegisterEvent("UNIT_AURA")
  17. -- probably want to check for buffs on login as well, I'm not sure if Unit_Aura fires
  18. frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  19.  
  20. frame:SetScript("OnEvent", function(self, event, ...)
  21.     if (unit and unit ~= "player") then
  22.         return
  23.     end
  24.  
  25.     for buff in pairs(buffs) do
  26.         if UnitBuff("player", buff) then
  27.             -- If you have the buff, check to see if it's already been factored in
  28.             if not buffs[buff] then
  29.                 -- If it hasn't been factored in, adjust accordingly
  30.                 buffs[buff] = true
  31.                 AddBuffAdjustment(buff)
  32.             end
  33.         else
  34.             -- If you don't have the buff, check to see if you're still adjusting for it
  35.             if buffs[buff] then
  36.                 -- If you're still adjusting for it, remove the adjustment
  37.                 buffs[buff] = false
  38.                 RemoveBuffAdjustment(buff)
  39.             end
  40.         end
  41.     end
  42. end)
  43.  
  44. local function AddBuffAdjustment(buff)
  45.     if buff == "Battle Shout" then
  46.         -- do whatever you need to do when Battle Shout is added
  47.     elseif buff == "Blessing of Kings" or buff = "Mark of the Wild" then
  48.         -- do whatever you need to do when BoK/Mark is added
  49.     elseif
  50.         -- and so forth
  51.     else
  52.    
  53.     end
  54. end
  55.  
  56. local function RemoveBuffAdjustment(buff)
  57.     if buff == "Battle Shout" then
  58.         -- do whatever you need to do when Battle Shout is removed
  59.     elseif buff == "Blessing of Kings" or buff == "Mark of the Wild" then
  60.         -- do whatever you need to do when BoK/Mark is removed
  61.     elseif
  62.         -- and so forth
  63.     else
  64.    
  65.     end
  66. end

You'll have to calculate a bit differently than you've been doing, but it should actually be simpler.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » code to check player for buff

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off