PageRenderTime 91ms CodeModel.GetById 40ms app.highlight 29ms RepoModel.GetById 11ms app.codeStats 1ms

/Tukui/modules/unitframes/plugins/oUF_Trinkets/oUF_Trinkets.lua

http://github.com/Asphyxia/Tukui
Lua | 122 lines | 107 code | 13 blank | 2 comment | 32 complexity | fecca5aeeddc097e816a4b68001d68b3 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2if C.unitframes.enable ~= true or C.arena.unitframes ~= true then return end
  3
  4local _, ns = ...
  5local oUF = ns.oUF
  6assert(oUF, 'oUF not loaded')
  7
  8local arenaFrame = {}
  9local arenaGUID  = {}
 10local usedTrinkets = {}
 11local trinketFrame = {}
 12 
 13local TrinketUpdate = function(self, elapsed)
 14	if self.endTime < GetTime() then
 15		usedTrinkets[self.guid] = false
 16		local unit = arenaGUID[self.guid]
 17		if unit and arenaFrame[unit] then
 18			if arenaFrame[unit].Trinket.trinketUpAnnounce then
 19				SendChatMessage("Trinket ready: "..UnitName(unit).." "..UnitClass(unit), "PARTY")
 20			end
 21		end
 22		self:SetScript("OnUpdate", nil)
 23	end
 24end
 25
 26local GetTrinketIcon = function(unit)
 27	if UnitFactionGroup(unit) == "Horde" then
 28		return UnitLevel(unit) == 80 and "Interface\\Icons\\INV_Jewelry_Necklace_38" or "Interface\\Icons\\INV_Jewelry_TrinketPVP_02"
 29	else
 30		return UnitLevel(unit) == 80 and "Interface\\Icons\\INV_Jewelry_Necklace_37" or "Interface\\Icons\\INV_Jewelry_TrinketPVP_01"
 31	end
 32end
 33
 34local TrinketUsed = function(guid, time)
 35	local message
 36	local unit = arenaGUID[guid]
 37	if unit and arenaFrame[unit] then
 38		CooldownFrame_SetTimer(arenaFrame[unit].Trinket.cooldownFrame, GetTime(), time, 1)
 39		if arenaFrame[unit].Trinket.trinketUseAnnounce then
 40			message = time == 120 and "Trinket used: " or "WotF used: "
 41			SendChatMessage(message..UnitName(unit).." "..UnitClass(unit), "PARTY")
 42		end
 43	end
 44	usedTrinkets[guid] = true
 45	if not trinketFrame[guid] then 
 46		trinketFrame[guid] = CreateFrame("Frame")
 47	end
 48	trinketFrame[guid].endTime = GetTime() + time
 49	trinketFrame[guid].guid = guid
 50	trinketFrame[guid]:SetScript("OnUpdate", TrinketUpdate)
 51end
 52
 53local Update = function(self, event, ...)
 54	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
 55		local timestamp, eventType, _, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, spellID, spellName
 56		
 57		if T.toc < 40200 then
 58			timestamp, eventType, _, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellID, spellName = ...
 59		else
 60			timestamp, eventType, _, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, spellID, spellName = ...
 61		end
 62		
 63		if eventType == "SPELL_CAST_SUCCESS" then
 64			-- enemy trinket usage
 65			if spellID == 59752 or spellID == 42292 then
 66				TrinketUsed(sourceGUID, 120)
 67			end
 68			-- WotF
 69			if spellID == 7744 then
 70				TrinketUsed(sourceGUID, 30)
 71			end
 72		end
 73	elseif event == "ARENA_OPPONENT_UPDATE" then
 74		local unit, type = ...
 75		if type == "seen" then
 76			if UnitExists(unit) and UnitIsPlayer(unit) and arenaFrame[unit] then
 77				arenaGUID[UnitGUID(unit)] = unit
 78				arenaFrame[unit].Trinket.Icon:SetTexture(GetTrinketIcon(unit))
 79			end
 80		end
 81	elseif event == "PLAYER_ENTERING_WORLD" then
 82		for k, v in pairs(trinketFrame) do
 83			v:SetScript("OnUpdate", nil)
 84		end
 85		for k, v in pairs(arenaFrame) do
 86			CooldownFrame_SetTimer(v.Trinket.cooldownFrame, 1, 1, 1)
 87		end
 88		arenaGUID  = {}
 89		usedTrinkets = {}
 90		trinketFrame = {}
 91	end
 92end
 93
 94local frame = CreateFrame("Frame")
 95frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
 96frame:RegisterEvent("ARENA_OPPONENT_UPDATE")
 97frame:RegisterEvent("PLAYER_ENTERING_WORLD")
 98frame:SetScript("OnEvent", Update)
 99
100oUF.Tags['[trinket]'] = function(unit)
101	if usedTrinkets[UnitGUID(unit)] or not UnitIsPlayer(unit) then return end
102	return string.format("|T%s:20:20:0:0|t", GetTrinketIcon(unit))
103end
104
105local Enable = function(self)
106	if self.Trinket then
107		self.Trinket.cooldownFrame = CreateFrame("Cooldown", nil, self.Trinket)
108		self.Trinket.cooldownFrame:SetAllPoints(self.Trinket)
109		self.Trinket.Icon = self.Trinket:CreateTexture(nil, "BORDER")
110		self.Trinket.Icon:SetAllPoints(self.Trinket)
111		self.Trinket.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
112		arenaFrame[self.unit] = self
113	end
114end
115 
116local Disable = function(self)
117	if self.Trinket then
118		arenaFrame[self.unit] = nil
119	end
120end
121 
122oUF:AddElement('Trinket', function() return end, Enable, Disable)