-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCore.lua
207 lines (189 loc) · 7.09 KB
/
Core.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
local Private = select(2, ...)
local TeleportCloak = CreateFrame("Button", "TeleportCloak", UIParent, "SecureActionButtonTemplate")
TeleportCloak:RegisterEvent("ADDON_LOADED")
TeleportCloak:SetMouseClickEnabled(true)
TeleportCloak:SetAttribute("pressAndHoldAction", true)
TeleportCloak:SetAttribute("typerelease", "item")
local TeleportItems = Private.Items
local EquipItemByName, GetInventoryItemID, GetItemCooldown, GetItemCount, GetItemInfo,
GetItemInfoInstant, GetTime, InCombatLockdown, pairs, print, tinsert, wipe =
C_Item.EquipItemByName, GetInventoryItemID, C_Item.GetItemCooldown, C_Item.GetItemCount, C_Item.GetItemInfo,
C_Item.GetItemInfoInstant, GetTime, InCombatLockdown, pairs, print, tinsert, wipe
function TeleportCloak:Print(...)
print("|cff33ff99TeleportCloak:|r ", ...)
end
local InventorySlots = {
INVSLOT_NECK,
INVSLOT_FEET,
INVSLOT_FINGER1,
INVSLOT_FINGER2,
INVSLOT_TRINKET1,
INVSLOT_TRINKET2,
INVSLOT_BACK,
INVSLOT_TABARD,
}
local InventoryTypes = {
cloaks = INVTYPE_CLOAK,
feet = INVTYPE_FEET,
necks = INVTYPE_NECK,
rings = INVTYPE_FINGER,
tabards = INVTYPE_TABARD,
trinkets = INVTYPE_TRINKET,
}
function TeleportCloak:Command(msg)
local cmd, arg = (msg or ""):lower():match("^(%S*)%s*(.-)$")
if cmd == "add" then
if arg == "" then
self:Print("Usage: |cff80ffc0/tc add <item or type>|r")
else
if InventoryTypes[arg] then
for _, item in pairs(TeleportItems[InventoryTypes[arg]]) do
tinsert(self.items, item)
end
else
local itemId = GetItemInfoInstant(arg)
if (not itemId) then
self:Print("Item not found:", arg)
else
tinsert(self.items, itemId)
end
end
end
return
elseif cmd == "save" then
local count = 0
for _, slot in pairs(InventorySlots) do
local itemId = GetInventoryItemID("player", slot)
if itemId and self:IsTeleportItem(itemId) then
self.db.saved[slot] = itemId
self:Print(select(2, GetItemInfo(itemId)), "saved.")
count = count + 1
end
end
if count == 0 then
self:Print("No teleport items equipped.")
end
return
elseif cmd == "warnings" then
self.db.warnings = not self.db.warnings
else
print("|cff33ff99TeleportCloak", self.Version, "|r")
self:Print("Add |cff80ffc0/click TeleportCloak|r",
"to a macro, and TeleportCloak will use your equipped teleport item,",
"or attempt to equip one if none are equipped.")
self:Print("To limit to specific items, add",
"|cff80ffc0/tc add <item or type>|r",
"for each item or type to the beginning of the macro.")
self:Print("|cff80ffc0<item>|r can be an item ID or item name")
local types = "|cff80ffc0<type>|r can be"
for inventoryType, _ in pairs(InventoryTypes) do
types = types.." |cff80ffc0"..inventoryType.."|r,"
end
self:Print(types, "and will add all items of that type.")
self:Print("To prevent TeleportCloak from restoring currently equipped teleport items after they are used,",
"type |cff80ffc0/tc save|r.")
end
self:Print("Warnings are",
self.db.warnings and "|cff19ff19Enabled|r." or "|cffff2020Disabled|r.",
"To turn them",
self.db.warnings and "off," or "on,",
"type |cff80ffc0/tc warnings|r")
end
function TeleportCloak:IsTeleportItem(item)
for _, items in pairs(TeleportItems) do
for _, teleportItem in pairs(items) do
if item == teleportItem then
return true
end
end
end
return false
end
TeleportCloak:SetScript("OnEvent", function(self, event, ...)
if event == "ADDON_LOADED" then
local addon = ...
if addon == "TeleportCloak" then
self:UnregisterEvent("ADDON_LOADED")
self.items = {}
-- Set version
self.Version = C_AddOns.GetAddOnMetadata("TeleportCloak", "Version") or ""
if self.Version:sub(1, 1) == "@" then
self.Version = "Development Version"
end
-- Initialize saved variables
TeleportCloakDB = TeleportCloakDB or { warnings = true }
self.db = TeleportCloakDB
self.db.saved = self.db.saved or {}
-- Register events
self:RegisterEvent("PLAYER_ENTERING_WORLD")
self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
self:RegisterEvent("ZONE_CHANGED_INDOORS")
self:RegisterEvent("ZONE_CHANGED_NEW_AREA")
self:RegisterEvent("ZONE_CHANGED")
-- Register slash commands
_G["SLASH_TeleportCloak1"] = "/teleportcloak"
_G["SLASH_TeleportCloak2"] = "/tc"
SlashCmdList.TeleportCloak = function(msg) TeleportCloak:Command(msg) end
end
return
end
if event == "PLAYER_ENTERING_WORLD" or event == "PLAYER_EQUIPMENT_CHANGED" then
-- Save equipped items
for _, slot in pairs(InventorySlots) do
local item = GetInventoryItemID("player", slot)
-- Save the item if it's not a teleport item
if item and (not self:IsTeleportItem(item)) then
self.db.saved[slot] = item
end
end
return
end
-- Wait to restore until out of combat
if InCombatLockdown() then
-- A restore was requested while in combat
self:RegisterEvent("PLAYER_REGEN_ENABLED")
return
end
if event == "PLAYER_REGEN_ENABLED" then
self:UnregisterEvent("PLAYER_REGEN_ENABLED")
end
-- Restore equipped items
for _, slot in pairs(InventorySlots) do
local item = GetInventoryItemID("player", slot)
if item and self:IsTeleportItem(item) then
if (not self.db.saved[slot]) then
if self.db.warnings then
TeleportCloak:Print(select(2, GetItemInfo(item)), "is equipped.")
end
else
EquipItemByName(self.db.saved[slot])
end
end
end
end)
TeleportCloak:SetScript("PreClick", function(self)
if InCombatLockdown() then return end
if (not self.items[1]) then
-- no items are set, default to cloaks
for _, item in pairs(TeleportItems[INVTYPE_CLOAK]) do
tinsert(self.items, item)
end
end
for _, item in pairs(self.items) do
local count = GetItemCount(item)
if count > 0 then
local startTime, duration = GetItemCooldown(item)
if (startTime == 0 or duration - (GetTime() - startTime) <= 30) then
self:SetAttribute("item", "item:"..item)
return
end
end
end
self:Print("All items are on cooldown.")
end)
TeleportCloak:SetScript("PostClick", function(self)
wipe(self.items)
if (not InCombatLockdown()) then
self:SetAttribute("item", nil)
end
end)