public
Description: WoW Addon - Handles all your mount & travel-form needs
Homepage: http://www.tekkub.net/
Clone URL: git://github.com/tekkub/mountme.git
Click here to lend your support to: mountme and make a donation at www.pledgie.com !
mountme / MountMe.lua
100644 174 lines (131 sloc) 4.892 kb
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

----------------------------
-- Localization --
----------------------------
 
local L = {
  ["Flight Form"] = "Flight Form",
  ["Swift Flight Form"] = "Swift Flight Form",
}
 
 
------------------------------
-- Are you local? --
------------------------------
 
local _, myclass = UnitClass("player")
local items, equipCheck, delayed, incombat, dbpc, mounted, isflight = {}, {}
local forms = myclass == "SHAMAN" and {"Ghost Wolf"} or myclass == "DRUID" and {"Cat Form", "Bear Form", "Travel Form", "Dire Bear Form", "Flight Form", "Swift Flight Form"}
local itemstrs = {carrot = "item:11122:%d+:%d+:%d+", crop = "item:25653:%d+:%d+:%d+", charm = "item:32481:%d+:%d+:%d+", whip = "item:32863:%d+:%d+:%d+", spurs = "item:%d+:464:%d+:%d+", gloves = "item:%d+:930:%d+:%d+"}
local itemslots = {carrot = 13, crop = 13, whip = 13, charm = 13, spurs = 8, gloves = 10}
local unknowns = {carrot = 13, crop = 13, whip = 13, charm = 13, spurs = 8, gloves = 10}
 
 
-------------------------------------
-- Namespace Declaration --
-------------------------------------
 
MountMe = DongleStub("Dongle-1.0"):New("MountMe", CreateFrame("Frame"))
if tekDebug then MountMe:EnableDebug(1, tekDebug:GetFrame("MountMe")) end
 
 
------------------------------
-- Dongle Methods --
------------------------------
 
function MountMe:Initialize()
  MountMeDB = MountMeDB or {BGsuspend = true, PvPsuspend = false}
  self.db = MountMeDB
 
  MountMeItemSwapDB = MountMeItemSwapDB or {}
  dbpc = MountMeItemSwapDB
end
 
 
function MountMe:Enable()
  self:ScanInventory()
 
  self:RegisterEvent("PLAYER_REGEN_DISABLED")
  self:RegisterEvent("PLAYER_REGEN_ENABLED")
  self:RegisterEvent("PLAYER_AURAS_CHANGED")
  self:RegisterEvent("UNIT_INVENTORY_CHANGED")
  if forms then self:RegisterEvent("TAXIMAP_OPENED") end
end
 
 
-------------------------------
-- Mount Detection --
-------------------------------
 
MountMe:SetScript("OnUpdate", function(self)
  local m = IsMounted() or isflight
  if m == mounted then return end
 
  if m then
    self:Debug(1, "Mounted")
    self:Swap()
  else
    self:Debug(1, "Dismounted")
    if incombat then delayed = true
    else self:SwapReset() end
  end
 
  mounted = m
end)
 
 
------------------------------
-- Event Handlers --
------------------------------
 
function MountMe:TAXIMAP_OPENED()
  for _,form in pairs(forms) do CancelPlayerBuff(form) end
end
 
 
function MountMe:PLAYER_REGEN_DISABLED()
  incombat = true
end
 
 
function MountMe:PLAYER_REGEN_ENABLED()
  if delayed then self:SwapReset()
  else for i, link in pairs(equipCheck) do EquipItemByName(link) end end
  incombat, delayed = nil, nil
end
 
 
function MountMe:PLAYER_AURAS_CHANGED()
  isflight = nil
 
  for i=1, GetNumShapeshiftForms() do
    local _, name, active = GetShapeshiftFormInfo(i)
    if active and (name == L["Flight Form"] or name == L["Swift Flight Form"]) then isflight = true end
  end
end
 
 
function MountMe:UNIT_INVENTORY_CHANGED()
  for itemtype,link in pairs(equipCheck) do
    if link == GetInventoryItemLink("player", itemslots[itemtype]) then equipCheck[itemtype] = nil end
  end
end
 
 
-----------------------------------
-- Speed item swapping --
-----------------------------------
 
function MountMe:IsSuspended()
  -- While you can switch trinkets while inside an arena, you cannot once the match starts so we're disabled when inside arenas no matter what
  local _, instanceType = IsInInstance()
  if self.db.PvPsuspend and UnitIsPVP("player")
    or self.db.BGsuspend and instanceType == "pvp"
    or instanceType == "arena" then return true end
end
 
 
function MountMe:Swap(reset)
  if self:IsSuspended() or not HasFullControl() then return end
  if next(unknowns) then self:ScanInventory() end
 
  for i, matchstr in pairs(itemstrs) do
    local link = GetInventoryItemLink("player", itemslots[i])
    if items[i] and not string.match(link, matchstr) and ((i ~= "charm" and not isflight) or (i == "charm" and isflight)) then
      -- Makes sure we don't change our originally equipped item if it's our charm mainly this is for Flight Form since spamming it can mess up our original item
      if link ~= items[i] and equipCheck[i] ~= link then dbpc[i] = link end
      EquipItemByName(items[i])
    end
  end
end
 
 
-- Reset our gear to the unmounted version
function MountMe:SwapReset()
  for i, link in pairs(dbpc) do
    dbpc[i] = nil
    equipCheck[i] = link
    EquipItemByName(link)
  end
end
 
 
----------------------------------
-- Speed item tracking --
----------------------------------
 
function MountMe:ScanInventory()
  for bag=0,4 do
    for slot=1,GetContainerNumSlots(bag) do
      local link = GetContainerItemLink(bag, slot)
      if link then
        -- Check if we need to add it to our list of mounted items
        for i, matchstr in pairs(itemstrs) do
          if string.match(link, matchstr) then
            unknowns[i] = nil
            items[i] = link
            break
          end
        end
      end
    end
  end
end