forked from Lexiebean/AutoDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoDB2.lua
122 lines (107 loc) · 3.31 KB
/
AutoDB2.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
-- luacheck: globals AutoDB AutoDB_AutoexecCommandsSet SLASH_AUTODB1
AutoDB_AutoexecCommandsSet = AutoDB_AutoexecCommandsSet or {["/db chests"] = true, ["/db rares"] = true}
AutoDB = CreateFrame("Frame")
AutoDB.version = "2.0"
local function ExecuteSlashCommand(command)
AutoDB.info("Executing [%s]", command)
DEFAULT_CHAT_FRAME.editBox:SetText(command)
ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox, 0)
end
local function GetEnabledSortedAutoexecCommands()
local result = {}
for _, command in ipairs(AutoDB.sortedkeys(AutoDB_AutoexecCommandsSet)) do
if AutoDB_AutoexecCommandsSet[command] then
table.insert(result, command)
end
end
return result
end
local commandsByName = {}
local function HandleHelpCommand()
AutoDB.info("AutoDB v%s", AutoDB.version)
for _, command in ipairs(AutoDB.sortedkeys(commandsByName)) do
local _, _, help = unpack(commandsByName[command])
local arguments, description = unpack(help)
AutoDB.info("%s %s - %s",
AutoDB.color("/autodb", "33ffcc"),
AutoDB.color(command .. (arguments ~= "" and (" " .. arguments) or ""), "ffffff"),
AutoDB.color(description, "cccccc"))
end
end
local function HandlePrintCommand()
local commands = GetEnabledSortedAutoexecCommands()
if getn(commands) == 0 then
AutoDB.info("AutoDB autoexec is empty")
return
end
AutoDB.info("AutoDB autoexec commands:")
for _, command in ipairs(commands) do
AutoDB.info(command)
end
end
local function HandleAddCommand(command)
AutoDB.info("Adding [%s] to autoexec", command)
AutoDB_AutoexecCommandsSet[command] = true
ExecuteSlashCommand(command)
end
local function HandleRemoveCommand(command)
if AutoDB_AutoexecCommandsSet[command] == nil then
AutoDB.error("Unable to find [%s] in autoexec", command)
return
end
AutoDB.info("Removing [%s] from autoexec", command)
AutoDB_AutoexecCommandsSet[command] = nil
end
commandsByName = {
["help"] = {
HandleHelpCommand,
false,
{"", "print this help"},
},
["list"] = {
HandlePrintCommand,
false,
{"", "print autoexec content"},
},
["add"] = {
HandleAddCommand,
true,
{"<command>", "add new command to autoexec (e.g. /autodb add mines auto)"},
},
["remove"] = {
HandleRemoveCommand,
true,
{"<command>", "removes command from autoexec (e.g. /autodb remove chests)"},
},
}
local function DispatchCommand(input)
local command, arguments = AutoDB.strsplit(AutoDB.strtrim(input), 2)
if arguments ~= nil then
arguments = format("/db %s", strlower(arguments))
end
local handle, requiresArguments = unpack(commandsByName[command] or {})
if handle == nil or requiresArguments ~= (arguments ~= nil) then
handle = HandleHelpCommand
end
handle(arguments)
end
AutoDB:RegisterEvent("SKILL_LINES_CHANGED")
AutoDB:SetScript("OnEvent", function ()
if event == "SKILL_LINES_CHANGED" then
this.skillsLoaded = true
end
end)
AutoDB:SetScript("OnUpdate", function()
if not this.done and this.skillsLoaded then
for _, command in ipairs(GetEnabledSortedAutoexecCommands()) do
ExecuteSlashCommand(command)
end
-- Hide world map that might have been opened by some pfQuest commands
if WorldMapFrame:IsShown() then
ToggleWorldMap()
end
this.done = true
end
end)
SLASH_AUTODB1 = "/autodb"
SlashCmdList["AUTODB"] = DispatchCommand