From d2209675ed3e147051f3d9e9b1823892e93b9268 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Tue, 27 Jul 2021 19:22:06 -0700 Subject: [PATCH] Improve help messages --- chat.lua | 235 +++++++++++++++++++++++++++++++------------- locale/template.txt | 1 + 2 files changed, 169 insertions(+), 67 deletions(-) diff --git a/chat.lua b/chat.lua index c25e9f1..946a280 100644 --- a/chat.lua +++ b/chat.lua @@ -21,54 +21,44 @@ local function pos_list(ppos, radius) return plist end -local param_desc = { - ["radius"] = S("Search radius."), - ["entity"] = S("Entity technical name."), - ["node"] = S("Node technical name."), - ["old_node"] = S("Technical name of node to be replaced."), - ["new_node"] = S("Technical name of node to be used in place."), - ["old_item"] = S("Technical name of item to be replaced."), - ["new_item"] = S("Technical name of item to be used in place."), - ["ore"] = S("Ore technical name."), +local param_def = { + radius = {name=S("radius"), desc=S("Search radius.")}, + entity = {name=S("entity"), desc=S("Entity technical name.")}, + node = {name=S("node"), desc=S("Node technical name.")}, + old_node = {name=S("old_node"), desc=S("Technical name of node to be replaced.")}, + new_node = {name=S("new_node"), desc=S("Technical name of node to be used in place.")}, + old_item = {name=S("old_item"), desc=S("Technical name of item to be replaced.")}, + new_item = {name=S("new_item"), desc=S("Technical name of item to be used in place.")}, + ore = {name=S("ore"), desc=S("Ore technical name.")}, } -local function format_help(cmd, param_string, params) - local retval = S("Usage:") .. "\n /" .. cmd .. " " .. param_string - .. "\n" - - local p_count = 0 - for _, p in ipairs(params) do - if p_count == 0 then - retval = retval .. "\n" .. S("Params:") - end - - retval = retval .. "\n " .. S(p) .. ": " .. param_desc[p] - p_count = p_count + 1 - end - - return retval -end - local cmd_repo = { entity = { cmd = "remove_entity", - params = "<" .. S("entity") .. "> [" .. S("radius") .. "]", + params = {"entity"}, + oparams = {radius=100}, }, - node = { - cmd_rem = "remove_node", - cmd_rep = "replace_node", - cmd_find = "find_unknown_nodes", - params_rem = "<" .. S("node") .. "> [" .. S("radius") .. "]", - params_rep = "<" .. S("old_node") .. "> <" .. S("new_node") .. "> [" .. S("radius") .. "]", - params_find = "[" .. S("radius") .. "]", + rem_node = { + cmd = "remove_node", + params = {"node"}, + oparams = {radius=5}, + }, + rep_node = { + cmd = "replace_node", + params = {"old_node", "new_node"}, + oparams = {radius=5}, + }, + find_node = { + cmd = "find_unknown_nodes", + oparams = {radius=100}, }, item = { cmd = "replace_item", - params = "<" .. S("old_item") .. "> <" .. S("new_item") .. ">", + params = {"old_item", "new_item"}, }, ore = { cmd = "remove_ore", - params = "<" .. S("ore") .. ">", + params = {"ore"}, }, param = { missing = S("Missing parameter."), @@ -77,6 +67,117 @@ local cmd_repo = { }, } +for k, def in pairs(cmd_repo) do + if k ~= "param" then + local cmd_help = { + param_string = "", + usage_string = "/" .. def.cmd, + } + + if def.params or def.oparams then + if def.params then + local params = {} + for _, p in ipairs(def.params) do + -- translate + table.insert(params, S(p)) + end + + cmd_help.param_string = "<" .. table.concat(params, "> <") .. ">" + end + end + + if def.oparams then + for k, v in pairs(def.oparams) do + local op = k + if type(op) == "number" then + op = v + end + + cmd_help.param_string = cmd_help.param_string .. " [" .. S(op) .. "]" + end + end + + if cmd_help.param_string ~= "" then + cmd_help.usage_string = cmd_help.usage_string .. " " .. cmd_help.param_string + end + + cmd_repo[k].help = cmd_help + end +end + +local function get_cmd_def(cmd) + for k, v in pairs(cmd_repo) do + if v.cmd == cmd then return v end + end +end + +local function format_usage(cmd) + local def = get_cmd_def(cmd) + if def then + return S("Usage:") .. "\n " .. def.help.usage_string + end +end + +local function format_params(cmd) + local def = get_cmd_def(cmd) + + local param_count + local all_params = {} + if def.params then + for k, v in ipairs(def.params) do + table.insert(all_params, p) + end + end + if def.oparams then + for k, v in pairs(def.oparams) do + + end + end + + local retval = "" + local p_count = 0 + + if def.params then + for _, p in ipairs(def.params) do + if p_count == 0 then + retval = retval .. S("Params:") + end + + retval = retval .. "\n " .. S(p) .. ": " .. param_def[p].desc + + p_count = p_count + 1 + end + end + + if def.oparams then + for k, v in pairs(def.oparams) do + if p_count == 0 then + retval = retval .. S("Params:") + end + + local p = k + local dvalue = v + if type(p) == "number" then + p = v + dvalue = nil + end + + retval = retval .. "\n " .. S(p) .. ": " .. param_def[p].desc + if dvalue then + retval = retval .. " (" .. S("default: @1", dvalue) .. ")" + end + + p_count = p_count + 1 + end + end + + return retval +end + +local function format_help(cmd) + return format_usage(cmd) .. "\n\n" .. format_params(cmd) +end + --- Removes nearby entities. -- @@ -85,11 +186,12 @@ local cmd_repo = { -- @tparam[opt] int radius core.register_chatcommand(cmd_repo.entity.cmd, { privs = {server=true}, - description = S("Remove an entity from game."), - params = cmd_repo.entity.params, + description = S("Remove an entity from game.") .. "\n\n" + .. format_params(cmd_repo.entity.cmd), + params = cmd_repo.entity.help.param_string, func = function(name, param) local entity - local radius = 100 + local radius = cmd_repo.entity.oparams.radius if param:find(" ") then entity = param:split(" ") radius = tonumber(entity[2]) @@ -106,8 +208,7 @@ core.register_chatcommand(cmd_repo.entity.cmd, { end if err then - return false, err .. "\n\n" - .. format_help(cmd_repo.entity.cmd, cmd_repo.entity.params, {"entity", "radius"}) + return false, err .. "\n\n" .. format_help(cmd_repo.entity.cmd) end local player = core.get_player_by_name(name) @@ -138,13 +239,14 @@ core.register_chatcommand(cmd_repo.entity.cmd, { -- @chatcmd remove_node -- @param node Node technical name. -- @tparam[opt] int radius -core.register_chatcommand(cmd_repo.node.cmd_rem, { +core.register_chatcommand(cmd_repo.rem_node.cmd, { privs = {server=true}, - description = S("Remove a node from game."), - params = cmd_repo.node.params_rem, + description = S("Remove a node from game.") .. "\n\n" + .. format_params(cmd_repo.rem_node.cmd), + params = cmd_repo.rem_node.help.param_string, func = function(name, param) local nname - local radius = 100 + local radius = cmd_repo.rem_node.oparams.radius if param:find(" ") then nname = param:split(" ") radius = tonumber(nname[2]) @@ -161,8 +263,7 @@ core.register_chatcommand(cmd_repo.node.cmd_rem, { end if err then - return false, err .. "\n\n" - .. format_help(cmd_repo.node.cmd_rem, cmd_repo.node.params_rem, {"node", "radius"}) + return false, err .. "\n\n" .. format_help(cmd_repo.rem_node.cmd) end local ppos = core.get_player_by_name(name):get_pos() @@ -187,13 +288,12 @@ core.register_chatcommand(cmd_repo.node.cmd_rem, { -- @param new_item Technical name of item to be used in place. core.register_chatcommand(cmd_repo.item.cmd, { privs = {server=true}, - description = S("Replace an item in game."), - params = cmd_repo.item.params, + description = S("Replace an item in game.") .. "\n\n" + .. format_params(cmd_repo.item.cmd), + params = cmd_repo.item.help.param_string, func = function(name, param) - local help = format_help(cmd_repo.item.cmd, cmd_repo.item.params, {"old_item", "new_item"}) - if not param:find(" ") then - return false, cmd_repo.param.missing .. "\n\n" .. help + return false, cmd_repo.param.missing .. "\n\n" .. format_help(cmd_repo.item.cmd) end local src = param:split(" ") @@ -211,24 +311,23 @@ core.register_chatcommand(cmd_repo.item.cmd, { --- Replaces nearby nodes. -- --- FIXME: sometimes nodes on top disappear --- -- @chatcmd replace_node -- @param old_node Technical name of node to replace. -- @param new_node Technical name of node to be used in place. -- @tparam[opt] int radius -core.register_chatcommand(cmd_repo.node.cmd_rep, { +core.register_chatcommand(cmd_repo.rep_node.cmd, { privs = {server=true}, - description = S("Replace a node in game."), - params = cmd_repo.node.params_rep, + description = S("Replace a node in game.") .. "\n\n" + .. format_params(cmd_repo.rep_node.cmd), + params = cmd_repo.rep_node.help.param_string, func = function(name, param) - local help = format_help(cmd_repo.node.cmd_rep, cmd_repo.node.params_rep, {"old_node", "new_node", "radius"}) + local help = format_help(cmd_repo.rep_node.cmd) if not param:find(" ") then return false, cmd_repo.param.missing .. "\n\n" .. help end - local radius = 100 + local radius = cmd_repo.rep_node.oparams.radius local params = param:split(" ") local src = params[1] @@ -266,18 +365,19 @@ core.register_chatcommand(cmd_repo.node.cmd_rep, { -- -- @chatcmd find_unknown_nodes -- @tparam[opt] int radius Search radius. -core.register_chatcommand(cmd_repo.node.cmd_find, { +core.register_chatcommand(cmd_repo.find_node.cmd, { privs = {server=true}, - description = S("Find names of unknown nodes."), - params = cmd_repo.node.params_find, + description = S("Find names of unknown nodes.") .. "\n\n" + .. format_params(cmd_repo.find_node.cmd), + params = cmd_repo.find_node.help.param_string, func = function(name, param) - local help = format_help(cmd_repo.node.cmd_find, cmd_repo.node.params_find, {"radius"}) + local help = format_help(cmd_repo.find_node.cmd) if param:find(" ") then return false, cmd_repo.param.excess .. "\n\n" .. help end - local radius = 100 + local radius = cmd_repo.find_node.oparams.radius if param and param:trim() ~= "" then radius = tonumber(param) end @@ -327,8 +427,9 @@ if cleaner.unsafe then -- @param ore Ore technical name. core.register_chatcommand(cmd_repo.ore.cmd, { privs = {server=true}, - description = S("Remove an ore from game."), - params = cmd_repo.ore.params, + description = S("Remove an ore from game.") .. "\n\n" + .. format_params(cmd_repo.ore.cmd), + params = cmd_repo.ore.help.param_string, func = function(name, param) local err if not param or param:trim() == "" then @@ -338,7 +439,7 @@ if cleaner.unsafe then end if err then - return false, err .. "\n\n" .. format_help(cmd_repo.ore.cmd, cmd_repo.ore.params, {"ore"}) + return false, err .. "\n\n" .. format_help(cmd_repo.ore.cmd) end local success = false diff --git a/locale/template.txt b/locale/template.txt index 4e9f484..4f81862 100644 --- a/locale/template.txt +++ b/locale/template.txt @@ -14,6 +14,7 @@ new_node= ore= Usage:= Params:= +default: @1= Search radius.= Entity technical name.= Node technical name.=