Asa@0: TankSpotter = select(2, ...) Asa@0: TankSpotter = LibStub("AceAddon-3.0"):NewAddon(TankSpotter, "TankSpotter", "AceEvent-3.0") Asa@0: Asa@0: local tankGUID = nil Asa@0: Asa@0: function TankSpotter:Slash(msg, editBox) Asa@0: msg = strlower(msg) Asa@0: if msg == 'set' then Asa@0: if UnitName("target") and UnitIsFriend("player", "target") then Asa@0: tankGUID = UnitGUID("target") Asa@0: if UnitGUID("pet") == tankGUID then Asa@0: -- pets change GUIDs when spawned, so this GUID can't be used Asa@0: tankGUID = 'pet' Asa@0: end Asa@0: else Asa@0: print('You must select a friendly target to set it as your tank.') Asa@0: end Asa@0: elseif msg == 'clear' then Asa@0: tankGUID = nil Asa@0: elseif msg == 'update' then Asa@0: -- UpdateAllMacros is called at the end of the function. Asa@0: else Asa@0: print('Usage: TODO: Implement instructions') Asa@0: end Asa@0: TankSpotter:UpdateAllMacros(msg) Asa@0: end Asa@0: Asa@0: function TankSpotter:findTank() Asa@0: -- the guid is simply to make sure the pet still exists. Asa@0: if tankGUID == 'pet' and UnitGUID("pet") then Asa@0: return 'pet' Asa@0: end Asa@0: Asa@0: local tank = nil Asa@0: for i = 1, GetNumRaidMembers()-1 do Asa@0: local unit = 'raid'..i Asa@1: if not UnitIsDead(unit) then Asa@1: if tankGUID and UnitGUID(unit) == tankGUID then Asa@1: return unit Asa@1: end Asa@1: if UnitGroupRolesAssigned(unit) == 'TANK' then Asa@1: if tank == nil then Asa@1: tank = unit Asa@1: else Asa@1: -- Too many tanks. Asa@1: tank = '' Asa@1: end Asa@0: end Asa@0: end Asa@0: end Asa@0: if tank and tank ~= '' then Asa@0: return tank Asa@0: end Asa@0: Asa@0: tank = nil Asa@0: for i = 1, GetNumPartyMembers() do Asa@0: local unit = 'party'..i Asa@1: if not UnitIsDead(unit) then Asa@1: if tankGUID and UnitGUID(unit) == tankGUID then Asa@1: return unit Asa@1: end Asa@1: Asa@1: if UnitGroupRolesAssigned(unit) == 'TANK' then Asa@1: tank = unit Asa@1: end Asa@0: end Asa@0: end Asa@0: Asa@0: return tank or 'pet' Asa@0: end Asa@0: Asa@0: TankSpotter.former_tank = '' Asa@0: function TankSpotter:updateMacro(index) Asa@0: if index > 0 then Asa@0: name, iconTexture, body, isLocal = GetMacroInfo(index); Asa@0: local former_tank = body:match('#tank=(%w+)') Asa@0: local new_tank = self:findTank() Asa@0: if former_tank and new_tank ~= former_tank then Asa@0: if self.former_tank ~= new_tank then Asa@0: print(format('Updating tank macros from %s to %s', former_tank, new_tank)) Asa@0: Asa@0: end Asa@0: self.former_tank = new_tank Asa@0: local new_body = body:gsub(former_tank, new_tank) Asa@0: name = name:gsub(former_tank, new_tank) Asa@0: -- print('y', index, name, iconTexture, body, isLocal) Asa@0: if new_body ~= body then Asa@0: EditMacro(index, name, 0, new_body, nil) Asa@0: end Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: local updateAfterCombat = false Asa@0: function TankSpotter:UpdateAllMacros(event) Asa@0: if InCombatLockdown() then Asa@0: updateAfterCombat = true Asa@0: TankSpotter:RegisterEvent("PLAYER_LEAVE_COMBAT", 'UpdateAllMacros') Asa@0: return Asa@0: end Asa@0: Asa@0: if updateAfterCombat then Asa@0: TankSpotter:UnregisterEvent("PLAYER_LEAVE_COMBAT") Asa@0: end Asa@0: Asa@0: local endGlobal, endChar = GetNumMacros() Asa@0: endChar = endChar + 36 -- character specific macros start at 37 Asa@0: local i = 1 Asa@0: Asa@0: while i <= endChar do Asa@0: self:updateMacro(i) Asa@0: if i == endGlobal then Asa@0: i = 36 Asa@0: end Asa@0: i = i + 1 Asa@0: end Asa@0: end Asa@0: Asa@0: Asa@0: TankSpotter:RegisterEvent("PLAYER_ROLES_ASSIGNED", 'UpdateAllMacros') Asa@0: TankSpotter:RegisterEvent("PLAYER_ENTERING_WORLD", 'UpdateAllMacros') Asa@0: TankSpotter:RegisterEvent("PARTY_MEMBERS_CHANGED", 'UpdateAllMacros') Asa@0: -- This is used to determine when a pet is summoned or dismissed Asa@0: TankSpotter:RegisterEvent("UNIT_PET", 'UpdateAllMacros') Asa@0: Asa@1: -- If your tank dies, this will trigger a new tank to be found as soon as combat is over. Asa@1: TankSpotter:RegisterEvent("PLAYER_DEAD", 'UpdateAllMacros') Asa@1: TankSpotter:RegisterEvent("PLAYER_UNGHOST", 'UpdateAllMacros') Asa@1: Asa@1: Asa@1: Asa@0: SLASH_TANKSPOTTER1, SLASH_TANKSPOTTER2 = '/tankspotter', '/ts' Asa@0: SlashCmdList["TANKSPOTTER"] = function(msg, editBox) Asa@0: TankSpotter:Slash(msg, editBox) Asa@0: end