Thread Tools Display Modes
08-18-13, 06:29 PM   #1
darksole
A Murloc Raider
Join Date: Aug 2013
Posts: 9
help implementing slash commands

hey guys i am trying to implement a slash command to toggle reporting on or off for this addon
at the moment it reports after every combat situation in a raid zone. i want to be able to type /PPC enable to allow the addon report after combat and /PPC disable would cause it to not report.

Code:
local buffNames = {
	["Potion of the Mountains"]=true,
	["Potion of Mogu Power"]=true,
	["Potion of the Jade Serpent"]=true,
	["Virmen's Bite"]=true,
}
local spellNames = {
	["Potion of the Mountains"]=true,
	["Potion of Mogu Power"]=true,
	["Master Mana Potion"]=true,
	["Potion of Focus"]=true,
	["Virmen's Bite"]=true,
	["Potion of the Jade Serpent"]=true,
	["Master Healing Potion"]=true,
}
local select, table, pairs, tinsert, print =
	  select, table, pairs, tinsert, print
local GetUnitName, GetInstanceInfo, UnitBuff, GetNumGroupMembers, UnitInRaid =
	  GetUnitName, GetInstanceInfo, UnitBuff, GetNumGroupMembers, UnitInRaid

local a = CreateFrame("Frame")
a:SetScript("OnEvent", function(self, event, ...)
    if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local subEvent, srcName, spellName = (select(2,...)), (select(5,...)), (select(13,...))
		local raidIndex = UnitInRaid(srcName)
        if subEvent == "SPELL_CAST_SUCCESS" and spellNames[spellName] and raidIndex then
            self.pots = self.pots or {}
            self.pots[GetUnitName(("raid%d"):format(raidIndex),true)] = spellName
        end
    elseif event == "PLAYER_REGEN_DISABLED" then
        local n = GetNumGroupMembers()
        local nP=(select(5, GetInstanceInfo()))
        if nP and (nP >= 10) and (n>0) then
            self.pots = self.pots and table.wipe(self.pots) or {}
            self.prepots = self.prepots and table.wipe(self.prepots) or {}
            for id=1, n do
				local uID = ("raid%d"):format(id)
                for buffName in pairs(buffNames) do
					if UnitBuff(uID, buffName) then
						self.prepots[GetUnitName(uID,true)] = true
						break
					end
				end
            end
        end
    elseif event == "PLAYER_REGEN_ENABLED" then
        local n  = GetNumGroupMembers()
        local nP=(select(5, GetInstanceInfo()))
        if nP and (nP >= 10) and (n>0) then
            local noprepot, noinpot = {}, {}
            for id=1, n do
                local name = GetUnitName(("raid%d"):format(id),true)
                if not self.prepots[name] then tinsert(noprepot, name) end
                if not self.pots[name] then tinsert(noinpot, name) end
            end
            if (#noprepot>0) then
                print("Players that did not have a potion buff when combat started: "..table.concat(noprepot, ", "))
            else
                print("Everyone had a potion buff when combat started!")
            end
            if (#noinpot>0) then
                print("Players that did not use a potion in combat: "..table.concat(noinpot, ", "))
            else
                print("Everyone used a potion during combat!")
            end
        end
    end
end)
a:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
a:RegisterEvent("PLAYER_REGEN_ENABLED")
a:RegisterEvent("PLAYER_REGEN_DISABLED")
  Reply With Quote
08-18-13, 07:24 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Since you don't have *anything* in your code yet where you attempted to add slash commands, I'm going to point you in this direction. If you need further help implementing this, then please come back with your specific questions.

http://wowpedia.org/Creating_a_slash_command
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
08-18-13, 08:20 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Code:
local subEvent, srcName, spellName = (select(2,...)), (select(5,...)), (select(13,...))
No, no, no, and absolutely not.

Do this instead:

Code:
local _, subEvent, _, _, srcName, _, _, _, _, _, _, _, spellName = ...
Function calls are the slowest thing you can do in Lua, and should be avoided when possible. In this case, it's absolutely insane to use three calls to select instead of just discarding the unwanted returns in "junk" variables. I don't know what example or reference you saw those select calls in, but you should get rid of it and never look at it again.
__________________
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
08-18-13, 08:34 PM   #4
darksole
A Murloc Raider
Join Date: Aug 2013
Posts: 9
so here is my attempt at implementing the slash command for enabling and disabling reporting. sorry if the code is bad, first attempt at lua, the bulk of the code i got from mmo-champ but it is out dated so i am trying to update for mop, figured i would throw the enable and disabling in to cause no raid leader wants prepot spam on trash.

Code:
local enabled = 'true';
SLASH_PPC1 = '/PPC';
local function SlashCmd(cmd,self)
	if (cmd == 'enable') then
		enabled = 'true';
		DEFAULT_CHAT_FRAME:AddMessage("Prepotcheck Enabled",1,0,0);
    elseif (cmd == 'disable') then
        enabled = 'false';
		DEFAULT_CHAT_FRAME:AddMessage("Prepotcheck Disabled",0,1,0);
    else
        DEFAULT_CHAT_FRAME:AddMessage("Unknown command. Enter either '/ppc enable' to activate Prepotcheck, or '/ppc disable' to deactivate it.",1,0,0);
    end
end
SlashCmdList["PPC"] = SlashCmd;

if (enabled == 'true') then
local buffNames = {
	["Potion of the Mountains"]=true,
	["Potion of Mogu Power"]=true,
	["Potion of the Jade Serpent"]=true,
	["Virmen's Bite"]=true,
}
local spellNames = {
	["Potion of the Mountains"]=true,
	["Potion of Mogu Power"]=true,
	["Master Mana Potion"]=true,
	["Potion of Focus"]=true,
	["Virmen's Bite"]=true,
	["Potion of the Jade Serpent"]=true,
	["Master Healing Potion"]=true,
}
local select, table, pairs, tinsert, print =
	  select, table, pairs, tinsert, print
local GetUnitName, GetInstanceInfo, UnitBuff, GetNumGroupMembers, UnitInRaid =
	  GetUnitName, GetInstanceInfo, UnitBuff, GetNumGroupMembers, UnitInRaid

local a = CreateFrame("Frame")
a:SetScript("OnEvent", function(self, event, ...)
    if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local subEvent, srcName, spellName = (select(2,...)), (select(5,...)), (select(13,...))
		local raidIndex = UnitInRaid(srcName)
        if subEvent == "SPELL_CAST_SUCCESS" and spellNames[spellName] and raidIndex then
            self.pots = self.pots or {}
            self.pots[GetUnitName(("raid%d"):format(raidIndex),true)] = spellName
        end
    elseif event == "PLAYER_REGEN_DISABLED" then
        local n = GetNumGroupMembers()
        local nP=(select(5, GetInstanceInfo()))
        if nP and (nP >= 10) and (n>0) then
            self.pots = self.pots and table.wipe(self.pots) or {}
            self.prepots = self.prepots and table.wipe(self.prepots) or {}
            for id=1, n do
				local uID = ("raid%d"):format(id)
                for buffName in pairs(buffNames) do
					if UnitBuff(uID, buffName) then
						self.prepots[GetUnitName(uID,true)] = true
						break
					end
				end
            end
        end
    elseif event == "PLAYER_REGEN_ENABLED" then
        local n  = GetNumGroupMembers()
        local nP=(select(5, GetInstanceInfo()))
        if nP and (nP >= 10) and (n>0) then
            local noprepot, noinpot = {}, {}
            for id=1, n do
                local name = GetUnitName(("raid%d"):format(id),true)
                if not self.prepots[name] then tinsert(noprepot, name) end
                if not self.pots[name] then tinsert(noinpot, name) end
            end
            if (#noprepot>0) then
                print("Players that did not have a potion buff when combat started: "..table.concat(noprepot, ", "))
            else
                print("Everyone had a potion buff when combat started!")
            end
            if (#noinpot>0) then
                print("Players that did not use a potion in combat: "..table.concat(noinpot, ", "))
            else
                print("Everyone used a potion during combat!")
            end
        end
    end
end)
a:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
a:RegisterEvent("PLAYER_REGEN_ENABLED")
a:RegisterEvent("PLAYER_REGEN_DISABLED")

Last edited by darksole : 08-18-13 at 08:51 PM.
  Reply With Quote
08-18-13, 09:09 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
(1) true and false should not have quotes around them, as those are boolean values in Lua. Adding quotes makes them string values, which are not the same thing.

(2) Since you gave your enabled variable an initial value of true, there's no need to check that it's true before loading the rest of your addon, since at that point it cannot possibly be anything else. Also, since that if statement has no corresponding end statement, you'll just get an error, and the whole thing will fail to load.

(3) There's nothing in your actual code that checks whether the addon is enabled or not (eg. if the user disabled it with the slash command) and acts accordingly.

(4) Finally, MMO Champion is almost as bad as Arena Junkies in terms of the quality of code posted on their forums. I'd strongly urge you to avoid using any code you find there as an example of anything other than how not to write an addon.

Here is a rewritten version of your code that doesn't make me want to gouge my eyes out with a plastic spoon. In addition to fixing tons of MMO Champion forum badness, it should also be a lot more efficient, since it will (a) shut itself off when not in a raid and (b) only listen for combat log events while in combat. There are some other changes that should be made if this is something you're planning to publish for others to use, but if you're just using it for yourself this will do, and should be recognizable based on your previous code.

Code:
local enabled = true

local buffNames = {
	["Potion of the Mountains"] = true,
	["Potion of Mogu Power"] = true,
	["Potion of the Jade Serpent"] = true,
	["Virmen's Bite"] = true,
}

local spellNames = {
	["Potion of the Mountains"] = true,
	["Potion of Mogu Power"] = true,
	["Master Mana Potion"] = true,
	["Potion of Focus"] = true,
	["Virmen's Bite"] = true,
	["Potion of the Jade Serpent"] = true,
	["Master Healing Potion"] = true,
}

local prepots, pots = {}, {}

local a = CreateFrame("Frame")
a:RegisterEvent("ZONE_CHANGED_NEW_AREA")

a:SetScript("OnEvent", function(self, event, ...)
	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local _, subEvent, _, _, srcName, _, _, _, _, _, _, _, spellName = ...
		if subEvent ~= "SPELL_CAST_SUCCESS" or not spellNames[spellName] then return end

		local raidIndex = UnitInRaid(srcName)
		if raidIndex then
			self.pots[GetUnitName("raid"..raidIndex), true] = spellName
		end

	elseif event == "PLAYER_REGEN_DISABLED" then
		wipe(prepots)
		wipe(pots)

		for i = 1, GetNumGroupMembers() do
			local unit = "raid"..i
			for buff in pairs(buffNames) do
				if UnitBuff(unit, buffName) then
					prepots[GetUnitName(unit, true)] = true
					break
				end
			end
		end

		self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "PLAYER_REGEN_ENABLED" then
		local noprepots, nopots = {}, {}
		for i = 1, GetNumGroupMembers() do
			local name = GetUnitName("raid"..i, true)
			if not prepots[name] then
				tinsert(noprepots, name)
			end
			if not pots[name] then
				tinsert(nopots, name)
			end
			if #noprepots > 0 then
				table.sort(noprepots)
				print("Players without a potion buff when combat started:", table.concat(noprepots))
			else
				print("Everyone had a potion buff when combat started.")
			end
			if #nopots > 0 then
				table.sort(nopots)
				print("Players who didn't use a potion in combat:", table.concat(nopots))
			else
				print("Everyone used a potion in combat.")
			end
		end			

		self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "ZONE_CHANGED_NEW_AREA" then
		local _, instanceType = GetInstanceInfo()
		if enabled and instanceType == "raid" and GetNumGroupMembers() > 1 then
			self:RegisterEvent("PLAYER_REGEN_DISABLED")
			self:RegisterEvent("PLAYER_REGEN_ENABLED")
			if UnitAffectingCombat("player") then
				self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
			end
		else
			self:UnregisterEvent("PLAYER_REGEN_DISABLED")
			self:UnregisterEvent("PLAYER_REGEN_ENABLED")
			self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
		end
	end
end)

SLASH_PREPOTCHECK1 = "/ppc"
SlashCmdList["PREPOTCHECK"] = function(cmd)
	enabled = not enabled
	print("PrePotCheck", enabled and "|cffff00enabled|r" or "|cff00ff00disabled|r")
	a:GetScript("OnEvent")(a, "ZONE_CHANGED_NEW_AREA")
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
08-18-13, 09:18 PM   #6
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
This will not work as you probably found out. The reason for it is that enable is always true (just use a boolean value and not a string for that please) the file gets loaded by the game so you always create your stuff and register your events and it runs all the time as your slash handler does not unregister them.

Set a table for the event you listen for:
Code:
local events = {
    "COMBAT_LOG_EVENT_UNFILTERED",
    "PLAYER_REGEN_ENABLED",
    "PLAYER_REGEN_DISABLED",
}
In your slash command func:
Code:
local function SlashCmd(cmd)
    if (cmd == 'enable') then
        for _, event in ipairs(events) do
            a:RegisterEvent(event)
        end
        DEFAULT_CHAT_FRAME:AddMessage("Prepotcheck Enabled",1,0,0);
    elseif (cmd == 'disable') then
        for _, event in ipairs(events) do
            a:UnregisterEvent(event)
        end
        DEFAULT_CHAT_FRAME:AddMessage("Prepotcheck Disabled",0,1,0);
    else
        DEFAULT_CHAT_FRAME:AddMessage("Unknown command. Enter either '/ppc enable' to activate Prepotcheck, or '/ppc disable' to deactivate it.",1,0,0);
    end
end
Edit: Just listen to Phanx, she put more effort in this than me

Last edited by Rainrider : 08-18-13 at 09:21 PM. Reason: Bah, Phanx, twice in a row :)
  Reply With Quote
08-18-13, 10:18 PM   #7
darksole
A Murloc Raider
Join Date: Aug 2013
Posts: 9
thank you for your help so far and sorry again for being terrible at this , ran into a issue when i replaced my code with yours the addon does not function no print outs in chat after combat. tested in naxx 10man. slash commands also return a type /help for help. all tested on a clean wow install no lua errors popped up
  Reply With Quote
08-19-13, 12:43 AM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Install BugSack. The game's built-in error display can't show errors that occur during the initial loading sequence, so you'll miss a lot if that's all you've got. There's probably a syntax error, but I don't have access to WoW at the moment to check for it myself.
__________________
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
08-19-13, 01:51 AM   #9
darksole
A Murloc Raider
Join Date: Aug 2013
Posts: 9
ok got the addon working, think this is the last hang up, for some reason it is double printing when combat ends for example me and my guild member koarinite tested it in naxx10 and it printed the following when combat ended and no potions were used

Players without a potion buff when combat started: Darksole
Players who did't use a potion in combat: Darksole
Players without a potion buff when combat started: DarksoleKoarinite
Players who did't use a potion in combat: DarksoleKoarinite

if we added a third person from guild we got the following

Players without a potion buff when combat started: Darksole
Players who did't use a potion in combat: Darksole
Players without a potion buff when combat started: DarksoleKoarinite
Players who did't use a potion in combat: DarksoleKoarinite
Players without a potion buff when combat started: DarksoleIronhideeKoarinite
Players who did't use a potion in combat: DarksoleIronhideeKoarinite


below is the updated code any idea how change to make it just print the two lines it is supposed to and not 2 lines per raid member

Code:
local enabled = true

local buffNames = {
	["Potion of the Mountains"] = true,
	["Potion of Mogu Power"] = true,
	["Potion of the Jade Serpent"] = true,
	["Virmen's Bite"] = true,
}

local spellNames = {
	["Potion of the Mountains"] = true,
	["Potion of Mogu Power"] = true,
	["Master Mana Potion"] = true,
	["Potion of Focus"] = true,
	["Virmen's Bite"] = true,
	["Potion of the Jade Serpent"] = true,
	["Master Healing Potion"] = true,
}

local prepots, pots = {}, {}

local a = CreateFrame("Frame")
a:RegisterEvent("ZONE_CHANGED_NEW_AREA")

a:SetScript("OnEvent", function(self, event, ...)
	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local _, subEvent, _, _, srcName, _, _, _, _, _, _, _, spellName = ...
		if subEvent ~= "SPELL_CAST_SUCCESS" or not spellNames[spellName] then return end

		local raidIndex = UnitInRaid(srcName)
		if raidIndex then
			self.pots[GetUnitName("raid"..raidIndex)] [true] = spellName
		end

	elseif event == "PLAYER_REGEN_DISABLED" then
		wipe(prepots)
		wipe(pots)

		for i = 1, GetNumGroupMembers() do
			local unit = "raid"..i
			for buff in pairs(buffNames) do
				if UnitBuff(unit, buffName) then
					prepots[GetUnitName(unit, true)] = true
					break
				end
			end
		end

		self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "PLAYER_REGEN_ENABLED" then
		local noprepots, nopots = {}, {}
		for i = 1, GetNumGroupMembers() do
			local name = GetUnitName("raid"..i, true)
			if not prepots[name] then
				tinsert(noprepots, name)
			end
			if not pots[name] then
				tinsert(nopots, name)
			end
			if #noprepots > 0 then
				table.sort(noprepots)
				print("Players without a potion buff when combat started:", table.concat(noprepots))
			else
				print("Everyone had a potion buff when combat started.")
			end
			if #nopots > 0 then
				table.sort(nopots)
				print("Players who didn't use a potion in combat:", table.concat(nopots))
			else
				print("Everyone used a potion in combat.")
			end
		end			

		self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "ZONE_CHANGED_NEW_AREA" then
		local _, instanceType = GetInstanceInfo()
		if enabled and instanceType == "raid" and GetNumGroupMembers() > 1 then
			self:RegisterEvent("PLAYER_REGEN_DISABLED")
			self:RegisterEvent("PLAYER_REGEN_ENABLED")
			if UnitAffectingCombat("player") then
				self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
			end
		else
			self:UnregisterEvent("PLAYER_REGEN_DISABLED")
			self:UnregisterEvent("PLAYER_REGEN_ENABLED")
			self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
		end
	end
end)

SLASH_PREPOTCHECK1 = "/ppc"
SlashCmdList["PREPOTCHECK"] = function(cmd)
	enabled = not enabled
	print("PrePotCheck", enabled and "|cffff00enabled|r" or "|cff00ff00disabled|r")
	a:GetScript("OnEvent")(a, "ZONE_CHANGED_NEW_AREA")
end

Last edited by darksole : 08-19-13 at 09:18 AM.
  Reply With Quote
08-19-13, 05:58 PM   #10
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Sounds like PLAYER_REGEN_ENABLED is somehow firing once for every player in your raid, instead of only for you, which isn't how it should work. Try adding some debug prints to see which events are firing, as they fire:

Code:
local enabled = true

local buffNames = {
	["Potion of Mogu Power"] = true,
	["Potion of the Jade Serpent"] = true,
	["Potion of the Mountains"] = true,
	["Virmen's Bite"] = true,
}

local spellNames = {
	["Master Healing Potion"] = true,
	["Master Mana Potion"] = true,
	["Potion of Focus"] = true,
	["Potion of Mogu Power"] = true,
	["Potion of the Jade Serpent"] = true,
	["Potion of the Mountains"] = true,
	["Virmen's Bite"] = true,
}

local prepots, pots = {}, {}

local a = CreateFrame("Frame")
a:RegisterEvent("ZONE_CHANGED_NEW_AREA")

a:SetScript("OnEvent", function(self, event, ...)
	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local _, subEvent, _, _, srcName, _, _, _, _, _, _, _, spellName = ...
		if subEvent == "SPELL_CAST_SUCCESS" and spellNames[spellName] then
			local i = UnitInRaid(srcName)
			if i then
				print("|cffff7f7fPPC:|r", srcName, "used", spellName)
				self.pots[GetUnitName("raid"..i, true)] = spellName
			end
		end

	elseif event == "PLAYER_REGEN_DISABLED" then
		print("|cffff7f7fPPC:|r Combat started")

		wipe(prepots)
		wipe(pots)

		for i = 1, GetNumGroupMembers() do
			local unit = "raid"..i
			for buff in pairs(buffNames) do
				if UnitBuff(unit, buffName) then
					prepots[GetUnitName(unit, true)] = true
					break
				end
			end
		end

		self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "PLAYER_REGEN_ENABLED" then
		print("|cffff7f7fPPC:|r Combat ended")

		local noprepots, nopots = {}, {}
		for i = 1, GetNumGroupMembers() do
			local name = GetUnitName("raid"..i, true)
			if not prepots[name] then
				tinsert(noprepots, name)
			end
			if not pots[name] then
				tinsert(nopots, name)
			end
			if #noprepots > 0 then
				table.sort(noprepots)
				print("|cffffd200PrePotCheck:|r Players without a potion buff when combat started:", table.concat(noprepots, ", "))
			else
				print("|cffffd200PrePotCheck:|r Everyone had a potion buff when combat started.")
			end
			if #nopots > 0 then
				table.sort(nopots)
				print("|cffffd200PrePotCheck:|r Players who didn't use a potion in combat:", table.concat(nopots, ", "))
			else
				print("|cffffd200PrePotCheck:|r Everyone used a potion in combat.")
			end
		end			

		self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "ZONE_CHANGED_NEW_AREA" then
		local _, instanceType = GetInstanceInfo()
		if enabled and instanceType == "raid" and GetNumGroupMembers() > 1 then
			print("|cffff7f7fPPC:|r Enabled")
			self:RegisterEvent("PLAYER_REGEN_DISABLED")
			self:RegisterEvent("PLAYER_REGEN_ENABLED")
		else
			print("|cffff7f7fPPC:|r Disabled")
			self:UnregisterEvent("PLAYER_REGEN_DISABLED")
			self:UnregisterEvent("PLAYER_REGEN_ENABLED")
			self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
		end
	end
end)

SLASH_PREPOTCHECK1 = "/ppc"
SlashCmdList["PREPOTCHECK"] = function(cmd)
	enabled = not enabled
	print("|cffffd200PrePotCheck|r is now", enabled and "enabled" or "disabled")
	a:GetScript("OnEvent")(a, "ZONE_CHANGED_NEW_AREA")
end
Also fixed the name concatenation, and a syntax error in your last-posted code that should have been throwing a Lua error in-game.
__________________
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
08-20-13, 10:22 AM   #11
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Isn't it better to initialize nopots and noprepots outside of the event handler and empty them using wipe in PLAYER_REGEN_ENABLED?

I'm thinking you overlooked those Phanx
  Reply With Quote
08-20-13, 02:00 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Strictly speaking, sure, but it's happening outside of combat, so I just didn't bother changing it.
__________________
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
08-20-13, 04:49 PM   #13
darksole
A Murloc Raider
Join Date: Aug 2013
Posts: 9
ok solved the printing loop issue had to remove the if statements for printing from the from the for statement in PLAYER_REGEN_ENABLED, but i am getting a lua error and the addon is not tracking potions any more just printing that no one used potions. any clue as to what i have to change to get it correctly reporting?
here is the error from bug sack
Code:
13x PrePotCheck-1.0\Prepotcheck-1.0.lua:45: Usage: UnitBuff("unit", [index] or ["name", "rank"][, "filter"])
<in C code>
PrePotCheck-1.0\Prepotcheck-1.0.lua:45: in function <PrePotCheck\Prepotcheck.lua:25>

Locals:
self = <unnamed> {
 0 = <userdata>
}
event = "PLAYER_REGEN_DISABLED"
(for index) = 1
(for limit) = 2
(for step) = 1
i = 1
unit = "raid1"
(for generator) = <func> =[C]:-1
(for state) = <table> {
 Virmen's Bite = true
 Potion of the Jade Serpent = true
 Potion of the Mountains = true
 Potion of Mogu Power = true
}
(for control) = "Virmen's Bite"
buff = "Virmen's Bite"
spellNames = <table> {
 Potion of Focus = true
 Potion of the Mountains = true
 Virmen's Bite = true
 Potion of the Jade Serpent = true
 Potion of Mogu Power = true
 Master Mana Potion = true
 Master Healing Potion = true
}
prepots = <table> {}
pots = <table> {}
buffNames = <table> {
 Virmen's Bite = true
 Potion of the Jade Serpent = true
 Potion of the Mountains = true
 Potion of Mogu Power = true
}
enabled = true
here is the updated code
Code:
local enabled = true

local buffNames = {
	["Potion of Mogu Power"] = true,
	["Potion of the Jade Serpent"] = true,
	["Potion of the Mountains"] = true,
	["Virmen's Bite"] = true,
}

local spellNames = {
	["Master Healing Potion"] = true,
	["Master Mana Potion"] = true,
	["Potion of Focus"] = true,
	["Potion of Mogu Power"] = true,
	["Potion of the Jade Serpent"] = true,
	["Potion of the Mountains"] = true,
	["Virmen's Bite"] = true,
}

local prepots, pots = {}, {}

local a = CreateFrame("Frame")
a:RegisterEvent("ZONE_CHANGED_NEW_AREA")

a:SetScript("OnEvent", function(self, event, ...)
	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local _, subEvent, _, _, srcName, _, _, _, _, _, _, _, spellName = ...
		if subEvent == "SPELL_CAST_SUCCESS" and spellNames[spellName] then
			local i = UnitInRaid(srcName)
			if i then
				print("|cffff7f7fPPC:|r", srcName, "used", spellName)
				self.pots[GetUnitName("raid"..i, true)] = spellName
			end
		end

	elseif event == "PLAYER_REGEN_DISABLED" then
		print("|cffff7f7fPPC:|r Combat started")

		wipe(prepots)
		wipe(pots)

		for i = 1, GetNumGroupMembers() do
			local unit = "raid"..i
			for buff in pairs(buffNames) do
				if UnitBuff(unit, buffName) then
					prepots[GetUnitName(unit, true)] = true
					break
				end
			end
		end

		self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "PLAYER_REGEN_ENABLED" then
		print("|cffff7f7fPPC:|r Combat ended")

		local noprepots, nopots = {}, {}
		for i = 1, GetNumGroupMembers() do
			local name = GetUnitName("raid"..i, true)
			if not prepots[name] then
				tinsert(noprepots, name)
			end
			if not pots[name] then
				tinsert(nopots, name)
			end
			end
			
			if #noprepots > 0 then
				table.sort(noprepots)
				print("|cffffff00PrePotCheck:|r Players without a potion buff when combat started:", table.concat(noprepots, ", "))
			else
				print("|cffffff00PrePotCheck:|r Everyone had a potion buff when combat started.")
			end
			if #nopots > 0 then
				table.sort(nopots)
				print("|cffffff00PrePotCheck:|r Players who didn't use a potion in combat:", table.concat(nopots, ", "))
			else
				print("|cffffff00PrePotCheck:|r Everyone used a potion in combat.")
			end
			

		self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

	elseif event == "ZONE_CHANGED_NEW_AREA" then
		local _, instanceType = GetInstanceInfo()
		if enabled and instanceType == "raid" and GetNumGroupMembers() > 1 then
			
			--print("|cffffff00PPC:|r |cff00ff00Enabled|r, version 1.0 Loaded. now checking for bads.")
			self:RegisterEvent("PLAYER_REGEN_DISABLED")
			self:RegisterEvent("PLAYER_REGEN_ENABLED")
		else
			--print("|cffff7f7fPPC:|r Disabled")
			self:UnregisterEvent("PLAYER_REGEN_DISABLED")
			self:UnregisterEvent("PLAYER_REGEN_ENABLED")
			self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
		end
	end
end)

SLASH_PREPOTCHECK1 = "/ppc"
SlashCmdList["PREPOTCHECK"] = function(cmd)
	enabled = not enabled
	print("|cffffff00PrePotCheck|r is now", enabled and "|cff00ff00enabled|r, |cff00ccffversion 1.0 loaded.|r" or "|cffff0000disabled|r")
	a:GetScript("OnEvent")(a, "ZONE_CHANGED_NEW_AREA")
end

Last edited by darksole : 08-20-13 at 04:51 PM.
  Reply With Quote
08-20-13, 05:24 PM   #14
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
Either change buffName to buff at line 45 or buff to buffName at line 44
  Reply With Quote
08-21-13, 10:04 AM   #15
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
It's a hash table, so I'm thinking pairs is returning the value(true) instead of the keys. Try this:
Code:
			for buff, _ in pairs(buffNames) do
				if UnitBuff(unit, buffName) then
					prepots[GetUnitName(unit, true)] = true
					break
				end
			end
  Reply With Quote
08-21-13, 04:59 PM   #16
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
No, the first thing returned by pairs is always the key. If your code does not care about the value, then the following lines are all functionally identical:

Code:
for k in pairs(t) do
for k, v in pairs(t) do
for k, _ in pairs(t) do
It's just like ignoring additional variables returned by a non-iterating function:

Code:
local a = GetValues()
local a, b = GetValues()
local a, _ = GetValues()
The only problem was that I typo'd the variable name, as Rainrider described. Change this part:

Code:
			local unit = "raid"..i
			for buff in pairs(buffNames) do
				if UnitBuff(unit, buff) then
					prepots[GetUnitName(unit, true)] = true
					break
				end
			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
08-21-13, 05:12 PM   #17
pelf
Sentient Plasmoid
 
pelf's Avatar
Premium Member
Join Date: May 2008
Posts: 133
Originally Posted by Phanx View Post
Here is a rewritten version of your code that doesn't make me want to gouge my eyes out with a plastic spoon.
Why a spoon, cousin?
  Reply With Quote
08-21-13, 06:19 PM   #18
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by pelf View Post
Why a spoon, cousin?
I'd think an olive fork would be more effective.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
08-21-13, 10:06 PM   #19
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Torhal View Post
I'd think an olive fork would be more effective.
An olive... fork? No, no no... you don't eat olives with a fork. That's way too slow.
__________________
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
08-21-13, 11:04 PM   #20
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by pelf View Post
Why a spoon, cousin?
http://youtu.be/MhfuuKiTcYQ?t=6s
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » help implementing slash commands


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