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 if INIT == "client" then
37 function core.register_list_command(command, desc, setting)
39 def.description = desc
40 def.params = "del <item> | add <item> | list"
41 function def.func(param)
42 local list = (minetest.settings:get(setting) or ""):split(",")
43 if param == "list" then
44 return true, table.concat(list, ", ")
46 local sparam = param:split(" ")
48 local item = sparam[2]
51 return false, "Missing item."
53 local i = table.indexof(list, item)
55 return false, item .. " is not on the list."
58 core.settings:set(setting, table.concat(list, ","))
59 return true, "Removed " .. item .. " from the list."
61 elseif cmd == "add" then
63 return false, "Missing item."
65 local i = table.indexof(list, item)
67 return false, item .. " is already on the list."
69 table.insert(list, item)
70 core.settings:set(setting, table.concat(list, ","))
71 return true, "Added " .. item .. " to the list."
75 return false, "Invalid usage. (See .help " .. command .. ")"
77 core.register_chatcommand(command, def)
81 local function do_help_cmd(name, param)
82 local function format_help_line(cmd, def)
83 local cmd_marker = "/"
84 if INIT == "client" then
87 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
88 if def.params and def.params ~= "" then
89 msg = msg .. " " .. def.params
91 if def.description and def.description ~= "" then
92 msg = msg .. ": " .. def.description
98 for cmd, def in pairs(core.registered_chatcommands) do
99 if INIT == "client" or core.check_player_privs(name, def.privs) then
100 cmds[#cmds + 1] = cmd
105 if INIT == "game" then
106 msg = S("Available commands: @1",
107 table.concat(cmds, " ")) .. "\n"
108 .. S("Use '/help <cmd>' to get more "
109 .. "information, or '/help all' to list "
112 msg = core.gettext("Available commands: ")
113 .. table.concat(cmds, " ") .. "\n"
114 .. core.gettext("Use '.help <cmd>' to get more "
115 .. "information, or '.help all' to list "
119 elseif param == "all" then
121 for cmd, def in pairs(core.registered_chatcommands) do
122 if INIT == "client" or core.check_player_privs(name, def.privs) then
123 cmds[#cmds + 1] = format_help_line(cmd, def)
128 if INIT == "game" then
129 msg = S("Available commands:")
131 msg = core.gettext("Available commands:")
133 return true, msg.."\n"..table.concat(cmds, "\n")
134 elseif INIT == "game" and param == "privs" then
136 for priv, def in pairs(core.registered_privileges) do
137 privs[#privs + 1] = priv .. ": " .. def.description
140 return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
143 local def = core.registered_chatcommands[cmd]
146 if INIT == "game" then
147 msg = S("Command not available: @1", cmd)
149 msg = core.gettext("Command not available: ") .. cmd
153 return true, format_help_line(cmd, def)
158 if INIT == "client" then
159 core.register_chatcommand("help", {
160 params = core.gettext("[all | <cmd>]"),
161 description = core.gettext("Get help for commands"),
162 func = function(param)
163 return do_help_cmd(nil, param)
167 core.register_chatcommand("help", {
168 params = S("[all | privs | <cmd>]"),
169 description = S("Get help for commands or list privileges"),