156861c - Non-raid unit frame fixes
[wowui.git] / kehys / incoming.lua
1 -- incoming.lua
2 -- 2020 Aleksi Blinnikka
3 local _;
4 local UnitGUID, UnitIsEnemy = UnitGUID, UnitIsEnemy;
5
6 local _, addon = ...;
7 local guids = addon.FrameGuids;
8 local abilities = {
9     [260741] = true, -- Jagged Nettles (Waycrest Manor)
10 };
11
12 local frame = CreateFrame("Frame", "kehysIncoming");
13
14 local function event(_, event, target, cast, id)
15     if event == "PLAYER_REGEN_ENABLED" then
16         for _, f in pairs(guids) do
17             f.incoming = {};
18         end
19         return
20     end
21     if abilities[id] and UnitIsEnemy("player", target) then
22         local f = guids[UnitGUID(target .. "target")];
23         if f then
24             if event == "UNIT_SPELLCAST_START" then
25                 if f.incoming[id] then
26                     f.incoming[id] = f.incoming[id] + 1;
27                 else
28                     f.incoming[id] = 1;
29                 end
30             elseif event == "UNIT_SPELLCAST_STOP" or event == "UNIT_SPELLCAST_CHANNEL_START" then
31                 if f.incoming[id] then
32                     f.incoming[id] = f.incoming[id] - 1;
33                     if f.incoming[id] == 0 then
34                         f.incoming[id] = nil;
35                     end
36                 end
37             end
38         end
39     end
40 end
41
42 local function setup()
43     frame:UnregisterAllEvents();
44     frame:SetScript("OnEvent", event);
45     frame:RegisterEvent("UNIT_SPELLCAST_START");
46     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
47     frame:RegisterEvent("UNIT_SPELLCAST_STOP");
48     frame:RegisterEvent("PLAYER_REGEN_ENABLED");
49 end
50
51 frame:SetScript("OnEvent", setup);
52 frame:RegisterEvent("PLAYER_LOGIN");