9720910 - Bump TOCs for 9.1
[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     [342675] = true, -- Bone Spear (Theater of Pain)
11     [320788] = true, -- Frozen Binds (Necrotic Wake)
12 };
13
14 local frame = CreateFrame("Frame", "kehysIncoming");
15
16 local function event(_, event, target, cast, id)
17     if event == "PLAYER_REGEN_ENABLED" then
18         for _, f in pairs(guids) do
19             f.incoming = {};
20         end
21         return
22     end
23     if abilities[id] and UnitIsEnemy("player", target) then
24         local f = guids[UnitGUID(target .. "target")];
25         if f then
26             if event == "UNIT_SPELLCAST_START" then
27                 if f.incoming[id] then
28                     f.incoming[id] = f.incoming[id] + 1;
29                 else
30                     f.incoming[id] = 1;
31                 end
32             elseif f.incoming[id] then
33                 f.incoming[id] = f.incoming[id] - 1;
34                 if f.incoming[id] == 0 then
35                     f.incoming[id] = nil;
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_CHANNEL_STOP");
48     frame:RegisterEvent("UNIT_SPELLCAST_STOP");
49     frame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED");
50     frame:RegisterEvent("UNIT_SPELLCAST_FAILED");
51     frame:RegisterEvent("UNIT_SPELLCAST_FAILED_QUIET");
52     frame:RegisterEvent("PLAYER_REGEN_ENABLED");
53 end
54
55 frame:SetScript("OnEvent", setup);
56 frame:RegisterEvent("PLAYER_LOGIN");