-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternalCooldowns.lua
181 lines (158 loc) · 5.8 KB
/
InternalCooldowns.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local _, ICD = ...
ICD.InternalCooldowns = {}
local lib = ICD.InternalCooldowns
local substr = _G.string.sub
local playerGUID = UnitGUID("player")
local GetTime, unpack, tonumber = _G.GetTime, _G.unpack, _G.tonumber
local type, ipairs = _G.type, _G.ipairs
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
local bt4 = IsAddOnLoaded("Bartender4")
lib.spellToItem = lib.spellToItem or {}
lib.cooldownStartTimes = lib.cooldownStartTimes or {}
lib.cooldownDurations = lib.cooldownDurations or {}
lib.cooldowns = lib.cooldowns or {}
if not lib.eventFrame then
lib.eventFrame = CreateFrame("Frame")
lib.eventFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
lib.eventFrame:RegisterEvent("PLAYER_LOGIN")
lib.eventFrame:SetScript("OnEvent", function(frame, event, ...)
frame.lib[event](frame.lib, event, ...)
end)
end
lib.eventFrame.lib = lib
local INVALID_EVENTS = {
SPELL_DISPEL = true,
SPELL_DISPEL_FAILED = true,
SPELL_STOLEN = true,
SPELL_AURA_REMOVED = true,
SPELL_AURA_REMOVED_DOSE = true,
SPELL_AURA_BROKEN = true,
SPELL_AURA_BROKEN_SPELL = true,
SPELL_CAST_FAILED = true,
}
local function isEquipped(itemID)
for slot = 0, 19 do
if GetInventoryItemID("player", slot) == itemID then
return true
end
end
return false
end
function lib:COMBAT_LOG_EVENT_UNFILTERED()
local _, event, _, sourceGUID, _, _, _, destGUID, _, _, _, spellID = CombatLogGetCurrentEventInfo()
if ((destGUID == playerGUID and (sourceGUID == nil or sourceGUID == destGUID)) or sourceGUID == playerGUID) and not INVALID_EVENTS[event] and substr(event, 0, 6) == "SPELL_" then
local itemID = lib.spellToItem[spellID]
if itemID then
if type(itemID) == "table" then
for _, v in ipairs(itemID) do
if isEquipped(v) then
self:SetCooldownFor(v, spellID)
end
end
return
else
if isEquipped(itemID) then
self:SetCooldownFor(itemID, spellID)
end
return
end
end
end
end
function lib:SetCooldownFor(itemID, spellID)
local duration = lib.cooldowns[spellID] or 45
lib.cooldownStartTimes[itemID] = GetTime()
lib.cooldownDurations[itemID] = duration
end
local function cooldownReturn(itemID)
if not itemID or not lib.cooldownStartTimes[itemID] or not lib.cooldownDurations[itemID] then
return nil
end
local startTime = lib.cooldownStartTimes[itemID]
local duration = lib.cooldownDurations[itemID]
if GetTime() > startTime + duration then
return 0, 0, 0
else
return startTime, duration, 1
end
end
local function TMGetInventoryItemCooldown(maybeGlobal)
local itemID1 = GetInventoryItemID("player", 13)
local itemID2 = GetInventoryItemID("player", 14)
if itemID1 or itemID2 then
local start1, duration1, enable1 = GetInventoryItemCooldown("player", 13)
local start2, duration2, enable2 = GetInventoryItemCooldown("player", 14)
local start3, duration3, running3 = cooldownReturn(itemID1)
local start4, duration4, running4 = cooldownReturn(itemID2)
if start3 then
start1 = start3
duration1 = duration3
enable1 = running3
end
if start4 then
start2 = start4
duration2 = duration4
enable2 = running4
end
CooldownFrame_Set(TrinketMenu_Trinket0Cooldown, start1, duration1, enable1)
CooldownFrame_Set(TrinketMenu_Trinket1Cooldown, start2, duration2, enable2)
end
if not maybeGlobal then
TrinketMenu.WriteWornCooldowns()
end
end
local something -- overflow prevention flag
local function GetActionCooldown(self, start, duration, enable)
local parent, slot = self:GetParent()
if bt4 then
slot = parent._state_action
else
slot = parent.action
end
if not slot or something then return end
something = true
local actionType, actionID, actionSubType = GetActionInfo(slot)
if actionType == "item" then
local start, duration, running = cooldownReturn(actionID)
if start then
CooldownFrame_Set(self, start, duration, running, false)
end
elseif actionType == "macro" then
local _, tex = GetMacroInfo(actionID)
local itemID1 = GetInventoryItemID("player", 13)
local itemID2 = GetInventoryItemID("player", 14)
if itemID1 and GetItemIcon(itemID1) == tex then
local start, duration, running = cooldownReturn(itemID1)
if start then
CooldownFrame_Set(self, start, duration, running, false)
end
elseif itemID2 and GetItemIcon(itemID2) == tex then
local start, duration, running = cooldownReturn(itemID2)
if start then
CooldownFrame_Set(self, start, duration, running, false)
end
end
end
something = false
end
local function PaperDollCD(self)
local start, duration, enable = GetInventoryItemCooldown("player", self:GetID())
if not enable or enable == 0 then
local itemID = GetInventoryItemID("player", self:GetID())
if itemID then
local start, duration, running = cooldownReturn(itemID)
local cooldown = _G[self:GetName() .. "Cooldown"]
if start then
CooldownFrame_Set(cooldown, start, duration, running)
end
end
end
end
function lib:PLAYER_LOGIN()
playerGUID = UnitGUID("player")
hooksecurefunc("CooldownFrame_Set", GetActionCooldown)
hooksecurefunc("PaperDollItemSlotButton_Update", PaperDollCD)
if IsAddOnLoaded("TrinketMenu") then
TrinketMenu.UpdateWornCooldowns = TMGetInventoryItemCooldown
end
end