-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoKnowledgeMacro.lua
More file actions
331 lines (285 loc) · 12 KB
/
AutoKnowledgeMacro.lua
File metadata and controls
331 lines (285 loc) · 12 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
local MACRO_NAME, AutoKnowledgeMacro = ...
-- local MACRO_NAME = "AutoKnowledgeMacro"
if DLAPI then DLAPI.DebugLog(MACRO_NAME, "OK~"..MACRO_NAME.." loading...") end
SLASH_AUTOKNOWLEDGEMACRO1, SLASH_AUTOKNOWLEDGEMACRO2 = '/autokm', '/akm'
local BAGS = {
Enum.BagIndex.Backpack,
Enum.BagIndex.Bag_1,
Enum.BagIndex.Bag_2,
Enum.BagIndex.Bag_3,
Enum.BagIndex.Bag_4
}
-- this will collect the above arrays, keyed under their Enum.Profession value
AutoKnowledgeMacro.professionMap = {}
AutoKnowledgeMacro.ENUM_PROFESSION_ALL = 9999
local professionMap = AutoKnowledgeMacro.professionMap
-- this holds all the item ids that can be flagged as complete by quests, and their quest ids
AutoKnowledgeMacro.questFlaggedItems = {}
local questFlaggedItems = AutoKnowledgeMacro.questFlaggedItems
-- cache the Enum.Profession values I know
local myProfession1 = nil
local myProfession2 = nil
-- This will hold a list of every profession item. Key is item ID, value is {"profession" : Enum.Profession, "name" : name }
local ALL_PROFESSION_ITEMS = {}
-- This will hold a list of every profession item that can be disabled by a quest, like treatises. Key is item ID in string, val is quest ID to check
local ALL_QUEST_FLAGGED_ITEMS = {}
local debug = false
professionMap[Enum.Profession.Alchemy] = {}
professionMap[Enum.Profession.Blacksmithing] = {}
professionMap[Enum.Profession.Enchanting] = {}
professionMap[Enum.Profession.Engineering] = {}
professionMap[Enum.Profession.Herbalism] = {}
professionMap[Enum.Profession.Inscription] = {}
professionMap[Enum.Profession.Jewelcrafting] = {}
professionMap[Enum.Profession.Leatherworking] = {}
professionMap[Enum.Profession.Mining] = {}
professionMap[Enum.Profession.Skinning] = {}
professionMap[Enum.Profession.Tailoring] = {}
professionMap[AutoKnowledgeMacro.ENUM_PROFESSION_ALL] = {}
--################################################################################--
-- Only prints the message if debug == true
--################################################################################--
local function apkPrint(level, ...)
local status, res = pcall(format, ...)
if debug then
if DLAPI then
DLAPI.DebugLog(MACRO_NAME, level .."~".. res)
else
print(...)
end
end
end
--################################################################################--
-- "Simple" version of "C_Item.GetItemNameByID()"
--################################################################################--
local function GetItemNameByID(itemID)
local alreadyName = C_Item.GetItemNameByID(itemID)
if alreadyName then return alreadyName end
local item = Item:CreateFromItemID(itemID)
item:ContinueOnItemLoad(function()
local id = item:GetItemID()
local name = item:GetItemName()
if name then ALL_PROFESSION_ITEMS[id]["name"] = name end
AutoKnowledgeMacro:Update()
end)
return nil
end
--################################################################################--
-- return the macro index, create one if we need
--################################################################################--
local function GetMacroSlot()
local macroSlot = GetMacroIndexByName(MACRO_NAME)
if macroSlot == 0 then
macroSlot = CreateMacro(MACRO_NAME, "INV_Misc_QuestionMark", "", false)
end
return macroSlot
end
local f = CreateFrame("Frame")
--################################################################################--
-- Returns true if this is a treatise and I already learned it this week
--################################################################################--
local function IsItemUsed(itemID)
if itemID and ALL_QUEST_FLAGGED_ITEMS[tostring(itemID)] then
return C_QuestLog.IsQuestFlaggedCompleted(ALL_QUEST_FLAGGED_ITEMS[tostring(itemID)])
end
return false
end
--################################################################################--
-- Builds a list of items useable by the professions I have
--################################################################################--
local function UpdateProfessions()
apkPrint("WARN", "UpdateProfessions start")
local prof1, prof2 = GetProfessions()
if prof1 then
local profession_name, _, _, _, _, _, skillLine, _ = GetProfessionInfo(prof1)
local info = C_TradeSkillUI.GetProfessionInfoBySkillLineID(skillLine)
apkPrint("ERR", "Skill line " .. tostring(skillLine) .. " and profession " .. profession_name .. " found")
myProfession1 = info.profession
else
myProfession1 = nil
end
if prof2 then
local profession_name, _, _, _, _, _, skillLine, _ = GetProfessionInfo(prof2)
local info = C_TradeSkillUI.GetProfessionInfoBySkillLineID(skillLine)
apkPrint("ERR", "Skill line " .. tostring(skillLine) .. " and profession " .. profession_name .. " found")
myProfession2 = info.profession
else
myProfession2 = nil
end
apkPrint("WARN", "UpdateProfessions end")
end
local UpdateInProgress = false
--################################################################################--
-- This function updates the macro to the first item found for these professions
-- Clears the macro if there is nothing found
--################################################################################--
function AutoKnowledgeMacro:Update()
if UpdateInProgress then
apkPrint("WARN", "Update already in progress")
return
end
UpdateInProgress = true
apkPrint("WARN", "Update start...")
-- make sure we have a macro to update
local macroSlot = GetMacroSlot()
-- make sure our professions are found
if myProfession1 == nil and myProfession2 == nil then UpdateProfessions() end
-- if we still don't have any professions, don't bother
if myProfession1 or myProfession2 then
for _, tabID in ipairs(BAGS) do
for slot=1, C_Container.GetContainerNumSlots(tabID) do
local info = C_Container.GetContainerItemInfo(tabID, slot)
if info and ALL_PROFESSION_ITEMS[info.itemID] then
if IsItemUsed(info.itemID) then
apkPrint ("OK", "Already used " .. tostring(info.itemID) .. " this week");
else
local profID = ALL_PROFESSION_ITEMS[info.itemID]["profession"]
if profID == myProfession1 or profID == myProfession2 or profID == AutoKnowledgeMacro.ENUM_PROFESSION_ALL then
local displayText = GetItemNameByID(info.itemID) or tostring(info.itemID)
apkPrint ("OK", "Setting Auto PK to " .. displayText)
local body = "#showtooltip ".. displayText .. "\n/use item:" .. tostring(info.itemID)
EditMacro(macroSlot, MACRO_NAME, nil, body)
apkPrint("WARN", "Update end, found " .. displayText)
UpdateInProgress = false
return
end
end
end
end
end
end
-- no items found or no professions found, leave it alone
apkPrint("WARN", "Update end, nothing found")
EditMacro(macroSlot, MACRO_NAME, "INV_Misc_QuestionMark", "/akm update")
-- Give the addon some time for all the triggers to collect
C_Timer.After(0.25, function()
UpdateInProgress = false
end)
end
--################################################################################--
-- (Almost) everything the addon needs to do, excludes static data
--################################################################################--
local function Reload()
-- make sure we have a macro to update
GetMacroSlot()
ALL_PROFESSION_ITEMS = {}
-- Load all item names in now, save us the trouble later
for professionEnum, expansionList in pairs(professionMap) do
for _, itemList in pairs(expansionList) do
for _, itemID in pairs(itemList) do
ALL_PROFESSION_ITEMS[itemID] = { profession = professionEnum, name = tostring(itemID) }
local item = Item:CreateFromItemID(itemID)
item:ContinueOnItemLoad(function()
local id = item:GetItemID()
local name = item:GetItemName()
if name then ALL_PROFESSION_ITEMS[id]["name"] = name end
AutoKnowledgeMacro:Update()
end)
end
end
end
ALL_QUEST_FLAGGED_ITEMS = {}
for professionEnum, expansionList in pairs(questFlaggedItems) do
for _, itemQuestPair in ipairs(expansionList) do
local key = tostring(itemQuestPair.itemID)
apkPrint("OK", "item ID: " .. key .. ", questID: " .. tostring(itemQuestPair.questID))
ALL_QUEST_FLAGGED_ITEMS[key] = itemQuestPair.questID
end
end
UpdateProfessions()
AutoKnowledgeMacro:Update()
-- Weekly resets for Treatises
local seconds = C_DateAndTime.GetSecondsUntilWeeklyReset()
C_Timer.After(seconds, function()
Reload()
end)
end
--################################################################################--
-- EVENT HANDLING
--################################################################################--
function f:OnEvent(event, ...)
self[event](self, event, ...)
end
function f:ADDON_LOADED(event, addOnName)
if addOnName ~= MACRO_NAME then return end
apkPrint("WARN", event .. " " .. addOnName)
Reload()
end
function f:BAG_CONTAINER_UPDATE(event, ...)
apkPrint("WARN", event, ...)
AutoKnowledgeMacro:Update()
end
function f:BAG_NEW_ITEMS_UPDATED(event, ...)
apkPrint("WARN", event, ...)
AutoKnowledgeMacro:Update()
end
function f:BAG_UPDATE_DELAYED(event, ...)
apkPrint("WARN", event, ...)
AutoKnowledgeMacro:Update()
end
function f:PLAYER_LEAVE_COMBAT(event, ...)
apkPrint("WARN", event, ...)
AutoKnowledgeMacro:Update()
end
function f:SKILL_LINE_SPECS_UNLOCKED(event, ...)
apkPrint("WARN", event, ...)
UpdateProfessions()
AutoKnowledgeMacro:Update()
end
function f:SKILL_LINE_SPECS_RANKS_CHANGED(event, ...)
apkPrint("WARN", event, ...)
UpdateProfessions()
AutoKnowledgeMacro:Update()
end
function f:GET_ITEM_INFO_RECEIVED(event, itemID, success)
if ALL_PROFESSION_ITEMS[itemID] then
apkPrint("WARN", event .. " itemID: " .. tostring(itemID) .. " success: " .. tostring(success))
if (success) then
local name = C_Item.GetItemNameByID(itemID)
if name then ALL_PROFESSION_ITEMS[itemID]["name"] = name end
AutoKnowledgeMacro:Update()
end
end
end
--################################################################################--
-- REGISTER EVENT HANDLING
--################################################################################--
f:RegisterEvent("ADDON_LOADED") -- Init tables, get all item names
f:RegisterEvent("BAG_CONTAINER_UPDATE") -- a bag changed?
f:RegisterEvent("BAG_NEW_ITEMS_UPDATED") -- looted something?
f:RegisterEvent("BAG_UPDATE_DELAYED") -- bunch of stuff in the bags updated
f:RegisterEvent("PLAYER_LEAVE_COMBAT") -- if you looted something while fighting
f:RegisterEvent("SKILL_LINE_SPECS_UNLOCKED") -- learn\drop a profession? maybe?
f:RegisterEvent("SKILL_LINE_SPECS_RANKS_CHANGED") -- learn\drop a profession? maybe?
f:RegisterEvent("GET_ITEM_INFO_RECEIVED") -- Called C_Item.GetItemName(), name is ready
f:SetScript("OnEvent", f.OnEvent)
--################################################################################--
-- Slash commands
--################################################################################--
SlashCmdList["AUTOKNOWLEDGEMACRO"] = function(msg, editBox)
if msg == "update" then
apkPrint("OK", "AKM: Forcing update...")
AutoKnowledgeMacro:Update()
apkPrint("OK", "AKM: Complete")
print("AutoKnowledgeMacro: Update complete")
elseif msg == "debug" then
debug = not debug
apkPrint("OK", "AKM: Debug ".. (debug and "on" or "off"))
print("AutoKnowledgeMacro: Debug ".. (debug and "on" or "off"))
elseif msg == "pickup" then
apkPrint("OK", "AKM: Picking up macro")
PickupMacro(MACRO_NAME)
elseif msg == "reload" then
apkPrint("OK", "AKM: Updating professions")
Reload()
print("AutoKnowledgeMacro: Reload complete")
elseif msg == "help" or msg == nil or msg == "" then
print("AutoKnowledgeMacro commands: /autokm or /akm")
print("Options:")
print("/akm update Rescan bags for items")
print("/akm debug Toggle debug messages on/off")
print("/akm pickup Grabs macro from Macro Dialog, drag it to an action bar")
print("/akm reload Force reload of AKM")
print("/akm help Display this help info")
end
end