View Single Post
10-16-20, 03:49 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
This one takes a slightly different approach and lets you add/remove players with slash commands.
Lua Code:
  1. local DeathCounter,DeathData,PlayerGUID=CreateFrame("Frame"),{},UnitGUID("player");
  2. DeathCounter:SetScript("OnEvent",function(self,event,unit)
  3.     if event=="GROUP_ROSTER_UPDATE" then
  4.         if IsInGroup() and not IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then-- Check for party/raid, but not battleground
  5.             for guid,data in pairs(DeathData) do--  Remove members when they leave
  6.                 if guid~=PlayerGUID and not IsGUIDInGroup(guid) then
  7.                     DeathData[guid]=nil;--  Cleanup if no longer in group
  8.                     print(("%s was removed from watch list."):format(data.Name));
  9.                 end
  10.             end
  11.  
  12.             if not next(DeathData) then self:UnregisterAllEvents(); end--   Disable if empty
  13.         else--  No longer in a valid group
  14.             table.wipe(DeathData);--    Clear all raid members
  15.             self:UnregisterAllEvents();--   Disable
  16.             print("Watch list cleared.");
  17.         end
  18.     elseif event=="UNIT_HEALTH" then
  19.         local guid,dead=UnitGUID(unit),UnitIsDead(unit);
  20.         local data=DeathData[guid]; if data then
  21.             if dead and not data.WasDead then-- TLDR: Did unit just die?
  22.                 data.Deaths=data.Deaths+1;--    Increment deaths
  23.                 if not IsInRaid() or UnitIsGroupLeader("player") or UnitIsGroupAssistant("player") then--   Can't send RAID_WARNING if not either of these
  24.                     SendChatMessage(("DeathCounter: %s has died %d time(s)."):format(UnitName(unit),data.Deaths),"RAID_WARNING");
  25.                 end
  26.             end
  27.             data.WasDead=dead;--    Update living state
  28.         end
  29.     end
  30. end);
  31.  
  32. function SlashCmdList.DEATHCOUNTER_ADDUNIT(unit)
  33. --  Test Group
  34.     if not IsInGroup() then print("You must be in a party/raid to use this command."); return; end
  35.     if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then print("You cannot use this command in a battleground."); return; end
  36.  
  37. --  Test Unit
  38.     if (unit or "")=="" then unit="target"; end
  39.     if not (UnitIsPlayer(unit) and (UnitInParty(unit) or UnitInRaid(unit))) then print("Unit must be a player in your party/raid."); return; end
  40.  
  41.     if IsInRaid() and not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then
  42.         print("Warning: Raid warnings can only be sent by a raid leader or assistant.\nDeaths will still be counted.");
  43.     end
  44.  
  45.     local name,guid=UnitName(unit),UnitGUID(unit);
  46.     if not DeathData[guid] then
  47.         DeathData[guid]={Name=name,WasDead=UnitIsDead(unit),Deaths=0};--    Add unit
  48.         DeathCounter:RegisterEvent("GROUP_ROSTER_UPDATE");--    Register group changes
  49.         DeathCounter:RegisterEvent("UNIT_HEALTH");--    Enable death watch
  50.         print(("Now watching %s."):format(name));
  51.     else print(("Already watching %s."):format(name)); end
  52. end
  53. SLASH_DEATHCOUNTER_ADDUNIT1="/dcadd";
  54.  
  55. function SlashCmdList.DEATHCOUNTER_REMUNIT(unit)
  56.     if (unit or "")=="" then unit="target"; end
  57.     if not next(DeathData) then print("Watch list is empty."); return; end
  58.     if not UnitIsPlayer(unit) then print("Unit must be a player."); return; end
  59.  
  60.     local name,guid=UnitName(unit),UnitGUID(unit);
  61.     if DeathData[guid] then
  62.         DeathData[guid]=nil;--  Remove entry
  63.         if not next(DeathData) then DeathCounter:UnregisterAllEvents(); end--   Disable if empty
  64.         print(("%s was removed from watch list."):format(name));
  65.     else print(("%s not found in watch list."):format(name)); end
  66. end
  67. SLASH_DEATHCOUNTER_REMUNIT1="/dcrem";
  68.  
  69. function SlashCmdList.DEATHCOUNTER_CLEAR()
  70.     if next(DeathData) then
  71.         table.wipe(DeathData);--    Clear
  72.         DeathCounter:UnregisterAllEvents();--   Disable
  73.         print("Watch list cleared.");
  74.     else print("Watch list is empty."); end
  75. end
  76. SLASH_DEATHCOUNTER_CLEAR1="/dcclr";

Usage:
/dcadd unit - Adds unit to list of players being watched.
/dcrem unit - Removes unit from list of players being watched.
/dcclr - Clears watch list.

Notes: Only players in your party/raid can be added to the watch list. They will be automatically removed if they leave the group. The list is also cleared if you leave the group. Even though these commands are written to take a UnitID, keep in mind the names of players in your party/raid are also valid UnitIDs.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 10-19-20 at 12:01 AM.
  Reply With Quote