1 -- Minetest: builtin/common/chatcommands.lua
3 -- For server-side translations (if INIT == "game")
4 -- Otherwise, use core.gettext
5 local S = core.get_translator("__builtin")
7 core.registered_chatcommands = {}
9 function core.register_chatcommand(cmd, def)
11 def.params = def.params or ""
12 def.description = def.description or ""
13 def.privs = def.privs or {}
14 def.mod_origin = core.get_current_modname() or "??"
15 core.registered_chatcommands[cmd] = def
18 function core.unregister_chatcommand(name)
19 if core.registered_chatcommands[name] then
20 core.registered_chatcommands[name] = nil
22 core.log("warning", "Not unregistering chatcommand " ..name..
23 " because it doesn't exist.")
27 function core.override_chatcommand(name, redefinition)
28 local chatcommand = core.registered_chatcommands[name]
29 assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
30 for k, v in pairs(redefinition) do
31 rawset(chatcommand, k, v)
33 core.registered_chatcommands[name] = chatcommand
36 local function do_help_cmd(name, param)
37 local function format_help_line(cmd, def)
38 local cmd_marker = "/"
39 if INIT == "client" then
42 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
43 if def.params and def.params ~= "" then
44 msg = msg .. " " .. def.params
46 if def.description and def.description ~= "" then
47 msg = msg .. ": " .. def.description
53 for cmd, def in pairs(core.registered_chatcommands) do
54 if INIT == "client" or core.check_player_privs(name, def.privs) then
60 if INIT == "game" then
61 msg = S("Available commands: @1",
62 table.concat(cmds, " ")) .. "\n"
63 .. S("Use '/help <cmd>' to get more "
64 .. "information, or '/help all' to list "
67 msg = core.gettext("Available commands: ")
68 .. table.concat(cmds, " ") .. "\n"
69 .. core.gettext("Use '.help <cmd>' to get more "
70 .. "information, or '.help all' to list "
74 elseif param == "all" then
76 for cmd, def in pairs(core.registered_chatcommands) do
77 if INIT == "client" or core.check_player_privs(name, def.privs) then
78 cmds[#cmds + 1] = format_help_line(cmd, def)
83 if INIT == "game" then
84 msg = S("Available commands:")
86 msg = core.gettext("Available commands:")
88 return true, msg.."\n"..table.concat(cmds, "\n")
89 elseif INIT == "game" and param == "privs" then
91 for priv, def in pairs(core.registered_privileges) do
92 privs[#privs + 1] = priv .. ": " .. def.description
95 return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
98 local def = core.registered_chatcommands[cmd]
101 if INIT == "game" then
102 msg = S("Command not available: @1", cmd)
104 msg = core.gettext("Command not available: ") .. cmd
108 return true, format_help_line(cmd, def)
113 if INIT == "client" then
114 core.register_chatcommand("help", {
115 params = core.gettext("[all | <cmd>]"),
116 description = core.gettext("Get help for commands"),
117 func = function(param)
118 return do_help_cmd(nil, param)
122 core.register_chatcommand("help", {
123 params = S("[all | privs | <cmd>]"),
124 description = S("Get help for commands or list privileges"),