X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=builtin%2Fcommon%2Fchatcommands.lua;h=817f1f526186d2657ba9084731d12adf844ec8d5;hb=21df26984da91143c15587f5a03c98d68c3adc4e;hp=bf989db7d1f8fd53349a18f20a421a974fea2758;hpb=eb6aca8b4a67ef55108231e71ff29a18a29bf5ae;p=dragonfireclient.git diff --git a/builtin/common/chatcommands.lua b/builtin/common/chatcommands.lua index bf989db7d..817f1f526 100644 --- a/builtin/common/chatcommands.lua +++ b/builtin/common/chatcommands.lua @@ -1,7 +1,47 @@ -- Minetest: builtin/common/chatcommands.lua +-- For server-side translations (if INIT == "game") +-- Otherwise, use core.gettext +local S = core.get_translator("__builtin") + core.registered_chatcommands = {} +-- Interpret the parameters of a command, separating options and arguments. +-- Input: command, param +-- command: name of command +-- param: parameters of command +-- Returns: opts, args +-- opts is a string of option letters, or false on error +-- args is an array with the non-option arguments in order, or an error message +-- Example: for this command line: +-- /command a b -cd e f -g +-- the function would receive: +-- a b -cd e f -g +-- and it would return: +-- "cdg", {"a", "b", "e", "f"} +-- Negative numbers are taken as arguments. Long options (--option) are +-- currently rejected as reserved. +local function getopts(command, param) + local opts = "" + local args = {} + for match in param:gmatch("%S+") do + if match:byte(1) == 45 then -- 45 = '-' + local second = match:byte(2) + if second == 45 then + return false, S("Invalid parameters (see /help @1).", command) + elseif second and (second < 48 or second > 57) then -- 48 = '0', 57 = '9' + opts = opts .. match:sub(2) + else + -- numeric, add it to args + args[#args + 1] = match + end + else + args[#args + 1] = match + end + end + return opts, args +end + function core.register_chatcommand(cmd, def) def = def or {} def.params = def.params or "" @@ -49,7 +89,7 @@ if INIT == "client" then local i = table.indexof(list, item) if i == -1 then return false, item .. " is not on the list." - else + else table.remove(list, i) core.settings:set(setting, table.concat(list, ",")) return true, "Removed " .. item .. " from the list." @@ -74,35 +114,30 @@ if INIT == "client" then end 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 +local function format_help_line(cmd, def) + local cmd_marker = INIT == "client" and "." or "/" + 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 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 + local opts, args = getopts("help", param) + if not opts then + return false, args + end + if #args > 1 then + return false, S("Too many arguments, try using just /help ") end - if param == "" then + local use_gui = INIT ~= "client" and core.get_player_by_name(name) + use_gui = use_gui and not opts:find("t") + + if #args == 0 and not use_gui then local cmds = {} for cmd, def in pairs(core.registered_chatcommands) do if INIT == "client" or core.check_player_privs(name, def.privs) then @@ -110,10 +145,25 @@ local function do_help_cmd(name, param) end end table.sort(cmds) - return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n" - .. gettext_replace("Use '$1help ' to get more information," - .. " or '$1help all' to list everything.", cmd_marker) - elseif param == "all" then + local msg + if INIT == "game" then + msg = S("Available commands: @1", + table.concat(cmds, " ")) .. "\n" + .. S("Use '/help ' to get more " + .. "information, or '/help all' to list " + .. "everything.") + else + msg = core.gettext("Available commands: ") + .. table.concat(cmds, " ") .. "\n" + .. core.gettext("Use '.help ' to get more " + .. "information, or '.help all' to list " + .. "everything.") + end + return true, msg + elseif #args == 0 or (args[1] == "all" and use_gui) then + core.show_general_help_formspec(name) + return true + elseif args[1] == "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 @@ -121,19 +171,35 @@ local function do_help_cmd(name, param) end end table.sort(cmds) - return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n") - elseif INIT == "game" and param == "privs" then + local msg + if INIT == "game" then + msg = S("Available commands:") + else + msg = core.gettext("Available commands:") + end + return true, msg.."\n"..table.concat(cmds, "\n") + elseif INIT == "game" and args[1] == "privs" then + if use_gui then + core.show_privs_help_formspec(name) + return true + end local privs = {} for priv, def in pairs(core.registered_privileges) do privs[#privs + 1] = priv .. ": " .. def.description end table.sort(privs) - return true, "Available privileges:\n"..table.concat(privs, "\n") + return true, S("Available privileges:").."\n"..table.concat(privs, "\n") else - local cmd = param + local cmd = args[1] local def = core.registered_chatcommands[cmd] if not def then - return false, gettext("Command not available: ")..cmd + local msg + if INIT == "game" then + msg = S("Command not available: @1", cmd) + else + msg = core.gettext("Command not available: ") .. cmd + end + return false, msg else return true, format_help_line(cmd, def) end @@ -142,16 +208,16 @@ end if INIT == "client" then core.register_chatcommand("help", { - params = gettext("[all | ]"), - description = gettext("Get help for commands"), + params = core.gettext("[all | ]"), + description = core.gettext("Get help for commands"), func = function(param) return do_help_cmd(nil, param) end, }) else core.register_chatcommand("help", { - params = "[all | privs | ]", - description = "Get help for commands or list privileges", + params = S("[all | privs | ] [-t]"), + description = S("Get help for commands or list privileges (-t: output in chat)"), func = do_help_cmd, }) end