+-- incoming.lua
+-- 2020 Aleksi Blinnikka
+local _;
+local UnitGUID, UnitIsEnemy = UnitGUID, UnitIsEnemy;
+
+local _, addon = ...;
+local guids = addon.FrameGuids;
+local abilities = {
+ [260741] = true, -- Jagged Nettles (Waycrest Manor)
+};
+
+local frame = CreateFrame("Frame", "kehysIncoming");
+
+local function event(_, event, target, cast, id)
+ if event == "PLAYER_REGEN_ENABLED" then
+ for _, f in pairs(guids) do
+ f.incoming = {};
+ end
+ return
+ end
+ if abilities[id] and UnitIsEnemy("player", target) then
+ local f = guids[UnitGUID(target .. "target")];
+ if f then
+ if event == "UNIT_SPELLCAST_START" then
+ if f.incoming[id] then
+ f.incoming[id] = f.incoming[id] + 1;
+ else
+ f.incoming[id] = 1;
+ end
+ elseif event == "UNIT_SPELLCAST_STOP" or event == "UNIT_SPELLCAST_CHANNEL_START" then
+ if f.incoming[id] then
+ f.incoming[id] = f.incoming[id] - 1;
+ if f.incoming[id] == 0 then
+ f.incoming[id] = nil;
+ end
+ end
+ end
+ end
+ end
+end
+
+local function setup()
+ frame:UnregisterAllEvents();
+ frame:SetScript("OnEvent", event);
+ frame:RegisterEvent("UNIT_SPELLCAST_START");
+ frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
+ frame:RegisterEvent("UNIT_SPELLCAST_STOP");
+ frame:RegisterEvent("PLAYER_REGEN_ENABLED");
+end
+
+frame:SetScript("OnEvent", setup);
+frame:RegisterEvent("PLAYER_LOGIN");