Fix negative offsets not being supported by container[]
[oweals/minetest.git] / builtin / common / chatcommands.lua
index ef3a2441030d3538c18aed616c6a1aab9b9eb167..71204657c976e09535f1417caed0c0c7ebea8296 100644 (file)
@@ -27,4 +27,79 @@ function core.override_chatcommand(name, redefinition)
                rawset(chatcommand, k, v)
        end
        core.registered_chatcommands[name] = chatcommand
-end
\ No newline at end of file
+end
+
+local cmd_marker = "/"
+
+local function gettext(...)
+       return ...
+end
+
+local function gettext_replace(text, replace)
+       return text:gsub("$1", replace)
+end
+
+
+if INIT == "client" then
+       cmd_marker = "."
+       gettext = core.gettext
+       gettext_replace = fgettext_ne
+end
+
+local function do_help_cmd(name, param)
+       local function format_help_line(cmd, def)
+               local msg = core.colorize("#00ffff", cmd_marker .. cmd)
+               if def.params and def.params ~= "" then
+                       msg = msg .. " " .. def.params
+               end
+               if def.description and def.description ~= "" then
+                       msg = msg .. ": " .. def.description
+               end
+               return msg
+       end
+       if param == "" then
+               local cmds = {}
+               for cmd, def in pairs(core.registered_chatcommands) do
+                       if INIT == "client" or core.check_player_privs(name, def.privs) then
+                               cmds[#cmds + 1] = cmd
+                       end
+               end
+               table.sort(cmds)
+               return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
+                               .. gettext_replace("Use '$1help <cmd>' to get more information,"
+                               .. " or '$1help all' to list everything.", cmd_marker)
+       elseif param == "all" then
+               local cmds = {}
+               for cmd, def in pairs(core.registered_chatcommands) do
+                       if INIT == "client" or core.check_player_privs(name, def.privs) then
+                               cmds[#cmds + 1] = format_help_line(cmd, def)
+                       end
+               end
+               table.sort(cmds)
+               return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
+       else
+               local cmd = param
+               local def = core.registered_chatcommands[cmd]
+               if not def then
+                       return false, gettext("Command not available: ")..cmd
+               else
+                       return true, format_help_line(cmd, def)
+               end
+       end
+end
+
+if INIT == "client" then
+       core.register_chatcommand("help", {
+               params = gettext("[all | <cmd>]"),
+               description = gettext("Get help for commands"),
+               func = function(param)
+                       return do_help_cmd(nil, param)
+               end,
+       })
+else
+       core.register_chatcommand("help", {
+               params = "[all | privs | <cmd>]",
+               description = "Get help for commands or list privileges",
+               func = do_help_cmd,
+       })
+end