1 local COLOR_BLUE = "#7AF"
2 local COLOR_GREEN = "#7F7"
3 local COLOR_GRAY = "#BBB"
5 local LIST_FORMSPEC = [[
8 tablecolumns[color;tree;text;text]
9 table[0,0.5;12.8,5.5;list;%s;0]
10 button_exit[5,6;3,1;quit;%s]
13 local LIST_FORMSPEC_DESCRIPTION = [[
16 tablecolumns[color;tree;text;text]
17 table[0,0.5;12.8,4.8;list;%s;%i]
18 box[0,5.5;12.8,1.5;#000]
19 textarea[0.3,5.5;13.05,1.9;;;%s]
20 button_exit[5,7;3,1;quit;%s]
23 local formspec_escape = core.formspec_escape
24 local check_player_privs = core.check_player_privs
27 -- CHAT COMMANDS FORMSPEC
31 local function load_mod_command_tree()
34 local check_player_privs = core.check_player_privs
35 for name, def in pairs(core.registered_chatcommands) do
36 mod_cmds[def.mod_origin] = mod_cmds[def.mod_origin] or {}
37 local cmds = mod_cmds[def.mod_origin]
39 -- Could be simplified, but avoid the priv checks whenever possible
40 cmds[#cmds + 1] = { name, def }
42 local sorted_mod_cmds = {}
43 for modname, cmds in pairs(mod_cmds) do
44 table.sort(cmds, function(a, b) return a[1] < b[1] end)
45 sorted_mod_cmds[#sorted_mod_cmds + 1] = { modname, cmds }
47 table.sort(sorted_mod_cmds, function(a, b) return a[1] < b[1] end)
48 mod_cmds = sorted_mod_cmds
51 core.after(0, load_mod_command_tree)
53 local function build_chatcommands_formspec(name, sel, copy)
55 rows[1] = "#FFF,0,Command,Parameters"
57 local description = "For more information, click on any entry in the list.\n" ..
58 "Double-click to copy the entry to the chat history."
60 for i, data in ipairs(mod_cmds) do
61 rows[#rows + 1] = COLOR_BLUE .. ",0," .. formspec_escape(data[1]) .. ","
62 for j, cmds in ipairs(data[2]) do
63 local has_priv = check_player_privs(name, cmds[2].privs)
64 rows[#rows + 1] = ("%s,1,%s,%s"):format(
65 has_priv and COLOR_GREEN or COLOR_GRAY,
66 cmds[1], formspec_escape(cmds[2].params))
68 description = cmds[2].description
70 core.chat_send_player(name, ("Command: %s %s"):format(
71 core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params))
77 return LIST_FORMSPEC_DESCRIPTION:format(
78 "Available commands: (see also: /help <cmd>)",
79 table.concat(rows, ","), sel or 0,
85 -- PRIVILEGES FORMSPEC
87 local function build_privs_formspec(name)
89 for name, def in pairs(core.registered_privileges) do
90 privs[#privs + 1] = { name, def }
92 table.sort(privs, function(a, b) return a[1] < b[1] end)
95 rows[1] = "#FFF,0,Privilege,Description"
97 local player_privs = core.get_player_privs(name)
98 for i, data in ipairs(privs) do
99 rows[#rows + 1] = ("%s,0,%s,%s"):format(
100 player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
101 data[1], formspec_escape(data[2].description))
104 return LIST_FORMSPEC:format(
105 "Available privileges:",
106 table.concat(rows, ","),
112 -- DETAILED CHAT COMMAND INFORMATION
114 core.register_on_player_receive_fields(function(player, formname, fields)
115 if formname ~= "__builtin:help_cmds" or fields.quit then
119 local event = minetest.explode_table_event(fields.list)
120 if event.type ~= "INV" then
121 local name = player:get_player_name()
122 core.show_formspec(name, "__builtin:help_cmds",
123 build_chatcommands_formspec(name, event.row, event.type == "DCL"))
128 local help_command = core.registered_chatcommands["help"]
129 local old_help_func = help_command.func
131 help_command.func = function(name, param)
132 if param == "privs" then
133 core.show_formspec(name, "__builtin:help_privs",
134 build_privs_formspec(name))
137 if param == "" or param == "all" then
138 core.show_formspec(name, "__builtin:help_cmds",
139 build_chatcommands_formspec(name))
143 return old_help_func(name, param)