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 if INIT == "client" then
33 function core.register_list_command(command, desc, setting)
35 def.description = desc
36 def.params = "del <item> | add <item> | list"
37 function def.func(param)
38 local list = (minetest.settings:get(setting) or ""):split(",")
39 if param == "list" then
40 return true, table.concat(list, ", ")
42 local sparam = param:split(" ")
44 local item = sparam[2]
47 return false, "Missing item."
49 local i = table.indexof(list, item)
51 return false, item .. " is not on the list."
54 core.settings:set(setting, table.concat(list, ","))
55 return true, "Removed " .. item .. " from the list."
57 elseif cmd == "add" then
59 return false, "Missing item."
61 local i = table.indexof(list, item)
63 return false, item .. " is already on the list."
65 table.insert(list, item)
66 core.settings:set(setting, table.concat(list, ","))
67 return true, "Added " .. item .. " to the list."
71 return false, "Invalid usage. (See /help " .. command .. ")"
73 core.register_chatcommand(command, def)
77 local cmd_marker = "/"
79 local function gettext(...)
83 local function gettext_replace(text, replace)
84 return text:gsub("$1", replace)
88 if INIT == "client" then
90 gettext = core.gettext
91 gettext_replace = fgettext_ne
94 local function do_help_cmd(name, param)
95 local function format_help_line(cmd, def)
96 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
97 if def.params and def.params ~= "" then
98 msg = msg .. " " .. def.params
100 if def.description and def.description ~= "" then
101 msg = msg .. ": " .. def.description
107 for cmd, def in pairs(core.registered_chatcommands) do
108 if INIT == "client" or core.check_player_privs(name, def.privs) then
109 cmds[#cmds + 1] = cmd
113 return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
114 .. gettext_replace("Use '$1help <cmd>' to get more information,"
115 .. " or '$1help all' to list everything.", cmd_marker)
116 elseif param == "all" then
118 for cmd, def in pairs(core.registered_chatcommands) do
119 if INIT == "client" or core.check_player_privs(name, def.privs) then
120 cmds[#cmds + 1] = format_help_line(cmd, def)
124 return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
125 elseif INIT == "game" and param == "privs" then
127 for priv, def in pairs(core.registered_privileges) do
128 privs[#privs + 1] = priv .. ": " .. def.description
131 return true, "Available privileges:\n"..table.concat(privs, "\n")
134 local def = core.registered_chatcommands[cmd]
136 return false, gettext("Command not available: ")..cmd
138 return true, format_help_line(cmd, def)
143 if INIT == "client" then
144 core.register_chatcommand("help", {
145 params = gettext("[all | <cmd>]"),
146 description = gettext("Get help for commands"),
147 func = function(param)
148 return do_help_cmd(nil, param)
152 core.register_chatcommand("help", {
153 params = "[all | privs | <cmd>]",
154 description = "Get help for commands or list privileges",