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

http://github.com/Asphyxia/Tukui · Lua · 122 lines · 107 code · 13 blank · 2 comment · 49 complexity · fecca5aeeddc097e816a4b68001d68b3 MD5 · raw file

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