vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | -------------------------------------------------------------------------- |
2 | -- IsMounted.lua |
||
3 | -------------------------------------------------------------------------- |
||
4 | --[[ |
||
5 | IsMounted |
||
6 | |||
7 | author: AnduinLothar KarlKFI@cosmosui.org |
||
8 | |||
9 | -Mini-Library that maintains a list of who's mounted. |
||
10 | |||
11 | Note: Information gathered from this addon is only correct for friendly units or units that you can see the buffs of. |
||
12 | |||
13 | Change Log: |
||
14 | v1.61 |
||
15 | -Fixed nil find string error |
||
16 | v1.6 |
||
17 | -Player mounted check now uses the PlayerBuff functions rather than UnitBuff. |
||
18 | -Player Buff ID returned now coresponds to the buff index returned from GetPlayerBuff(i, "HELPFUL|PASSIVE") and can be used directly by CancelPlayerBuff. |
||
19 | -Dismount now works correctly with debuffs. |
||
20 | -Localed a few internal functions for quicker indexing |
||
21 | v1.5 |
||
22 | - Mounted status now accurate when called from PLAYER_AURAS_CHANGED and not cleared on UNIT_AURA |
||
23 | (UNIT_AURA w/ arg1=="player" is always called after PLAYER_AURAS_CHANGED.) |
||
24 | v1.4 |
||
25 | - Changed tooltip scan to use IsShown to work with 1.9 |
||
26 | v1.3 |
||
27 | - Added French and German Localizations |
||
28 | v1.2 |
||
29 | - Added Function: Dismount() |
||
30 | - Added Function: UnitMountSpeed(unit) |
||
31 | - Added Function: UnitMountBuffID( string unit ) |
||
32 | - Changed all refrences of 'buff slot' to 'id' |
||
33 | Note: id is the id of the buff in terms of Tooltip/Texture index that starts at 1 while the Buff/Frame index starts at 0. So you might have to subtract 1 till blizz fixes it next patch, depending on your usage. |
||
34 | v1.1 |
||
35 | - Fixed but that would throw error when UNIT_AURA triggered for 'mouseover' unit. (I Didn't know it caught mouseover O.o) |
||
36 | v1.0 |
||
37 | - Initial Release |
||
38 | ]]-- |
||
39 | |||
40 | -------------------------------------------------- |
||
41 | -- Globals |
||
42 | -------------------------------------------------- |
||
43 | |||
44 | IsMounted = {}; |
||
45 | |||
46 | -- Predictibly consistant units |
||
47 | IsMounted.UnitTable = { |
||
48 | player = {}; |
||
49 | target = {}; |
||
50 | } |
||
51 | for i=1, 4 do IsMounted.UnitTable["party"..i] = {} end |
||
52 | for i=1, 40 do IsMounted.UnitTable["raid"..i] = {} end |
||
53 | |||
54 | -------------------------------------------------- |
||
55 | -- Internal Functions |
||
56 | -------------------------------------------------- |
||
57 | |||
58 | IsMounted.OnLoad = function() |
||
59 | IsMountedFrame:RegisterEvent("PLAYER_AURAS_CHANGED"); |
||
60 | IsMountedFrame:RegisterEvent("UNIT_AURA"); |
||
61 | IsMountedFrame:RegisterEvent("PLAYER_TARGET_CHANGED"); |
||
62 | IsMountedFrame:RegisterEvent("PARTY_MEMBERS_CHANGED"); |
||
63 | IsMountedFrame:RegisterEvent("RAID_ROSTER_UPDATE"); |
||
64 | IsMountedFrame:RegisterEvent("PLAYER_ENTERING_WORLD"); |
||
65 | IsMountedTooltip:SetOwner(UIParent, "ANCHOR_NONE"); |
||
66 | end |
||
67 | |||
68 | -- Nils predictibly consistant stored values when the unit or its buffs change |
||
69 | IsMounted.OnEvent = function(event) |
||
70 | if (event == "PLAYER_ENTERING_WORLD") then |
||
71 | IsMounted.UnitTable["player"].speed = nil; |
||
72 | IsMounted.UnitTable["player"].id = nil; |
||
73 | elseif (event == "PLAYER_AURAS_CHANGED") then |
||
74 | --Sea.io.print("player IsMounted mount state nilled."); |
||
75 | IsMounted.UnitTable["player"].speed = nil; |
||
76 | IsMounted.UnitTable["player"].id = nil; |
||
77 | elseif (event == "UNIT_AURA") then |
||
78 | if (not IsMounted.UnitTable[arg1]) then |
||
79 | --Triggered by 'mouseover' buff change |
||
80 | --Sea.io.print("UNIT_AURA: ", arg1); |
||
81 | elseif (arg1 ~= "player") then |
||
82 | --Sea.io.print(arg1, " IsMounted mount state nilled."); |
||
83 | IsMounted.UnitTable[arg1].speed = nil; |
||
84 | IsMounted.UnitTable[arg1].id = nil; |
||
85 | end |
||
86 | elseif (event == "PLAYER_TARGET_CHANGED") then |
||
87 | IsMounted.UnitTable["target"].speed = nil; |
||
88 | IsMounted.UnitTable["target"].id = nil; |
||
89 | elseif (event == "PARTY_MEMBERS_CHANGED") then |
||
90 | for i=1, 4 do |
||
91 | IsMounted.UnitTable["party"..i].speed = nil; |
||
92 | IsMounted.UnitTable["party"..i].id = nil; |
||
93 | end |
||
94 | elseif (event == "RAID_ROSTER_UPDATE") then |
||
95 | for i=1, 40 do |
||
96 | IsMounted.UnitTable["raid"..i].speed = nil; |
||
97 | IsMounted.UnitTable["raid"..i].id = nil; |
||
98 | end |
||
99 | end |
||
100 | end |
||
101 | |||
102 | -- Sets stored values of predictibly consistant units and returns the speed and id of the mount buff. |
||
103 | local GetUpdatedUnitTable = function(unit) |
||
104 | local speed, id = IsMounted.GetUpdatedMountBuffInfo(unit); |
||
105 | if (IsMounted.UnitTable[unit]) then |
||
106 | --Sea.io.print("Updating UnitTable for ".. unit..": ", speed, ", ", id); -- debug msg |
||
107 | IsMounted.UnitTable[unit].speed = speed; |
||
108 | IsMounted.UnitTable[unit].id = id; |
||
109 | end |
||
110 | return speed, id; |
||
111 | end |
||
112 | |||
113 | |||
114 | -- Gets speed and id of player mount buff according to GetPlayerBuff |
||
115 | local GetPlayerMountBuffInfo = function() |
||
116 | -- Check the tooltips of the player's active buffs for the key string. |
||
117 | local text, buffIndex, untilCancelled, _, speed; |
||
118 | for i = 0, 23 do |
||
119 | buffIndex, untilCancelled = GetPlayerBuff(i, "HELPFUL|PASSIVE"); |
||
120 | if ( buffIndex < 0 ) then |
||
121 | break; |
||
122 | elseif ( untilCancelled ) then |
||
123 | IsMountedTooltip:SetPlayerBuff(buffIndex); |
||
124 | if (IsMountedTooltipTextLeft2:IsShown()) then |
||
125 | text = IsMountedTooltipTextLeft2:GetText(); |
||
126 | if (text) then |
||
127 | _, _, speed = string.find(text, ISMOUNTED_SPEED_INCREASED_BY); |
||
128 | if (speed) then |
||
129 | return tonumber(speed), buffIndex; |
||
130 | end |
||
131 | end |
||
132 | end |
||
133 | end |
||
134 | end |
||
135 | return false; |
||
136 | end |
||
137 | |||
138 | -- Gets speed and id of non-player mount buff according to UnitBuff |
||
139 | local GetNonPlayerMountBuffInfo = function(unit) |
||
140 | -- Check the tooltips of the unit's active buffs for the key string. |
||
141 | local text, _, speed; |
||
142 | for i = 1, 24 do |
||
143 | if (not UnitBuff(unit, i)) then |
||
144 | break; |
||
145 | end |
||
146 | IsMountedTooltip:SetUnitBuff(unit, i); |
||
147 | if (IsMountedTooltipTextLeft2:IsShown()) then |
||
148 | text = IsMountedTooltipTextLeft2:GetText(); |
||
149 | if (text) then |
||
150 | --Sea.io.print("Checking text: ".. text); -- debug msg |
||
151 | _, _, speed = string.find(text, ISMOUNTED_SPEED_INCREASED_BY); |
||
152 | if (speed) then |
||
153 | return tonumber(speed), i; |
||
154 | end |
||
155 | end |
||
156 | end |
||
157 | end |
||
158 | return false; |
||
159 | end |
||
160 | |||
161 | -------------------------------------------------- |
||
162 | -- User Functions |
||
163 | -------------------------------------------------- |
||
164 | |||
165 | -- |
||
166 | -- UnitIsMounted( string unit ) |
||
167 | -- |
||
168 | -- EX: if (UnitIsMounted("player")) then doSomething(); end |
||
169 | -- |
||
170 | -- Returns: |
||
171 | -- (Boolean isMounted) |
||
172 | -- isMounted - 1 if mounted, else nil |
||
173 | -- |
||
174 | function UnitIsMounted(unit) |
||
175 | if (IsMounted.GetMountBuffInfo(unit)) then |
||
176 | return 1; |
||
177 | end |
||
178 | end |
||
179 | |||
180 | -- |
||
181 | -- Dismount() |
||
182 | -- |
||
183 | -- EX: if (Dismount()) then print("I'm Dismounting"); end |
||
184 | -- Macro EX: /dismount |
||
185 | -- |
||
186 | -- Returns: |
||
187 | -- (Boolean wasMounted) |
||
188 | -- wasMounted - 1 if was mounted, else nil |
||
189 | -- |
||
190 | function Dismount() |
||
191 | local speed, id = IsMounted.GetMountBuffInfo("player"); |
||
192 | if (speed) then |
||
193 | CancelPlayerBuff(id); |
||
194 | return 1; |
||
195 | end |
||
196 | end |
||
197 | |||
198 | SlashCmdList["DISMOUNT"] = Dismount; |
||
199 | SLASH_DISMOUNT1 = "/dismount"; |
||
200 | |||
201 | --[[ |
||
202 | SlashCmdList["LAG"] = function(msg) |
||
203 | local n, _, t, spell = strfind(msg, "(%d-[%.%d]%d-) (.*)", 1); |
||
204 | if (not n) then |
||
205 | n, _, t = strfind(msg, "(%d-[%.%d]%d-)", 1); |
||
206 | end |
||
207 | local endTime = GetTime() + t; |
||
208 | while (GetTime() < endTime) do |
||
209 | end |
||
210 | if (spell) then |
||
211 | CastSpellByName(spell); |
||
212 | end |
||
213 | end |
||
214 | SLASH_LAG1 = "/lag"; |
||
215 | ]]-- |
||
216 | |||
217 | -- |
||
218 | -- UnitMountSpeed( string unit ) |
||
219 | -- |
||
220 | -- EX: local speed = UnitMountSpeed("target") |
||
221 | -- |
||
222 | -- Returns: |
||
223 | -- (Number speed) |
||
224 | -- speed - percent speed increase of the mount (60 or 100) if unit is mounted, |
||
225 | -- false if mount is not found |
||
226 | -- |
||
227 | function UnitMountSpeed(unit) |
||
228 | local speed, id = IsMounted.GetMountBuffInfo(unit); |
||
229 | return speed; |
||
230 | end |
||
231 | |||
232 | -- |
||
233 | -- UnitMountBuffID( string unit ) |
||
234 | -- |
||
235 | -- EX: local id = UnitMountBuffID("target") |
||
236 | -- |
||
237 | -- Returns: |
||
238 | -- (Number id) |
||
239 | -- id - the buff id id of the mount, nil if mount is not found or unit doesn't exist |
||
240 | -- |
||
241 | function UnitMountBuffID(unit) |
||
242 | local speed, id = IsMounted.GetMountBuffInfo(unit); |
||
243 | return id; |
||
244 | end |
||
245 | |||
246 | -- |
||
247 | -- IsMounted.GetMountBuffInfo( string unit ) |
||
248 | -- |
||
249 | -- EX: local speed, id = IsMounted.GetMountBuffInfo("player") |
||
250 | -- |
||
251 | -- Returns: |
||
252 | -- (Number speed, Number id) |
||
253 | -- speed - percent speed increase of the mount (60 or 100) if unit is mounted, |
||
254 | -- false if mount is not found |
||
255 | -- id - the buff id id of the mount, nil if mount is not found or unit doesn't exist |
||
256 | -- |
||
257 | IsMounted.GetMountBuffInfo = function(unit) |
||
258 | if (IsMounted.UnitTable[unit]) and (IsMounted.UnitTable[unit].speed ~= nil) then |
||
259 | return IsMounted.UnitTable[unit].speed, IsMounted.UnitTable[unit].id; |
||
260 | end |
||
261 | return GetUpdatedUnitTable(unit); |
||
262 | end |
||
263 | |||
264 | -- |
||
265 | -- IsMounted.GetUpdatedMountBuffInfo( string unit ) |
||
266 | -- |
||
267 | -- Gets Updated Mount Buff Info (duh!) |
||
268 | -- |
||
269 | -- Returns: |
||
270 | -- (Number speed, Number id) |
||
271 | -- speed - percent speed increase of the mount (60 or 100) if unit is mounted, |
||
272 | -- false if mount is not found (so that we know it's been checked since last changed) |
||
273 | -- id - the buff id id of the mount, nil if mount is not found or unit doesn't exist |
||
274 | -- |
||
275 | IsMounted.GetUpdatedMountBuffInfo = function(unit) |
||
276 | if (UnitIsPlayer(unit)) then |
||
277 | if ( (not IsMounted.UnitTable[unit]) or (unit == "target") ) and (UnitIsFriend("player", unit)) then |
||
278 | -- This unit check will always check 46 stored tables for saved speed, but it should hopefully still be faster than setting and checking the tooltips of the unit's active buffs. |
||
279 | for u, i in IsMounted.UnitTable do |
||
280 | if (i.speed ~= nil) and (UnitIsUnit(unit, u)) then |
||
281 | --Sea.io.print(unit, " is ", u); -- debug msg |
||
282 | if (unit == "target") then |
||
283 | --only store the info for the unit if it is predictibly consistant |
||
284 | IsMounted.UnitTable.target.speed = i.speed; |
||
285 | IsMounted.UnitTable.target.id = i.id; |
||
286 | end |
||
287 | return i.speed, i.id; |
||
288 | end |
||
289 | end |
||
290 | end |
||
291 | |||
292 | if (unit == "player") then |
||
293 | return GetPlayerMountBuffInfo(); |
||
294 | else |
||
295 | return GetNonPlayerMountBuffInfo(unit); |
||
296 | end |
||
297 | end |
||
298 | return false; |
||
299 | end |