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 local function gettext(...)
38 local function gettext_replace(text, replace)
39 return text:gsub("$1", replace)
43 if INIT == "client" then
45 gettext = core.gettext
46 gettext_replace = fgettext_ne
49 local function do_help_cmd(name, param)
50 local function format_help_line(cmd, def)
51 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
52 if def.params and def.params ~= "" then
53 msg = msg .. " " .. def.params
55 if def.description and def.description ~= "" then
56 msg = msg .. ": " .. def.description
62 for cmd, def in pairs(core.registered_chatcommands) do
63 if INIT == "client" or core.check_player_privs(name, def.privs) then
68 return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
69 .. gettext_replace("Use '$1help <cmd>' to get more information,"
70 .. " or '$1help all' to list everything.", cmd_marker)
71 elseif param == "all" then
73 for cmd, def in pairs(core.registered_chatcommands) do
74 if INIT == "client" or core.check_player_privs(name, def.privs) then
75 cmds[#cmds + 1] = format_help_line(cmd, def)
79 return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
80 elseif INIT == "game" and param == "privs" then
82 for priv, def in pairs(core.registered_privileges) do
83 privs[#privs + 1] = priv .. ": " .. def.description
86 return true, "Available privileges:\n"..table.concat(privs, "\n")
89 local def = core.registered_chatcommands[cmd]
91 return false, gettext("Command not available: ")..cmd
93 return true, format_help_line(cmd, def)
98 if INIT == "client" then
99 core.register_chatcommand("help", {
100 params = gettext("[all/<cmd>]"),
101 description = gettext("Get help for commands"),
102 func = function(param)
103 return do_help_cmd(nil, param)
107 core.register_chatcommand("help", {
108 params = "[all/privs/<cmd>]",
109 description = "Get help for commands or list privileges",