1 -- Minetest: builtin/common/chatcommands.lua
3 core.registered_chatcommands = {}
5 function core.register_chatcommand(cmd, def)
7 def.params = def.params or ""
8 def.description = def.description or ""
9 def.privs = def.privs or {}
10 def.mod_origin = core.get_current_modname() or "??"
11 core.registered_chatcommands[cmd] = def
14 function core.unregister_chatcommand(name)
15 if core.registered_chatcommands[name] then
16 core.registered_chatcommands[name] = nil
18 core.log("warning", "Not unregistering chatcommand " ..name..
19 " because it doesn't exist.")
23 function core.override_chatcommand(name, redefinition)
24 local chatcommand = core.registered_chatcommands[name]
25 assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
26 for k, v in pairs(redefinition) do
27 rawset(chatcommand, k, v)
29 core.registered_chatcommands[name] = chatcommand
32 local cmd_marker = "/"
34 if INIT == "client" then
38 local function do_help_cmd(name, param)
39 local function format_help_line(cmd, def)
40 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
41 if def.params and def.params ~= "" then
42 msg = msg .. " " .. def.params
44 if def.description and def.description ~= "" then
45 msg = msg .. ": " .. def.description
51 for cmd, def in pairs(core.registered_chatcommands) do
52 if INIT == "client" or core.check_player_privs(name, def.privs) then
57 return true, "Available commands: " .. table.concat(cmds, " ") .. "\n"
58 .. "Use '"..cmd_marker.."help <cmd>' to get more information,"
59 .. " or '"..cmd_marker.."help all' to list everything."
60 elseif param == "all" then
62 for cmd, def in pairs(core.registered_chatcommands) do
63 if INIT == "client" or core.check_player_privs(name, def.privs) then
64 cmds[#cmds + 1] = format_help_line(cmd, def)
68 return true, "Available commands:\n"..table.concat(cmds, "\n")
69 elseif INIT == "game" and param == "privs" then
71 for priv, def in pairs(core.registered_privileges) do
72 privs[#privs + 1] = priv .. ": " .. def.description
75 return true, "Available privileges:\n"..table.concat(privs, "\n")
78 local def = core.registered_chatcommands[cmd]
80 return false, "Command not available: "..cmd
82 return true, format_help_line(cmd, def)
87 if INIT == "client" then
88 core.register_chatcommand("help", {
89 params = "[all/<cmd>]",
90 description = "Get help for commands",
91 func = function(param)
92 return do_help_cmd(nil, param)
96 core.register_chatcommand("help", {
97 params = "[all/privs/<cmd>]",
98 description = "Get help for commands or list privileges",