From f90243f6e533ecd878ac6cd1f44b1794e42c0b50 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Tue, 9 Mar 2021 00:34:33 +0100 Subject: [PATCH] add public API to properly (take care of mcl_builtin_commands_overide) rename and alias chat command --- mods/CORE/mcl_commands/API.md | 14 ++++++++ mods/CORE/mcl_commands/init.lua | 25 ++++++++++++++ mods/MISC/mcl_basic_commands/alias.lua | 47 +++++++++++--------------- mods/MISC/mcl_basic_commands/list.lua | 25 ++++++++------ mods/MISC/mcl_basic_commands/seed.lua | 17 ++++++---- 5 files changed, 85 insertions(+), 43 deletions(-) diff --git a/mods/CORE/mcl_commands/API.md b/mods/CORE/mcl_commands/API.md index cbc634dfb8..95dcdd77d7 100644 --- a/mods/CORE/mcl_commands/API.md +++ b/mods/CORE/mcl_commands/API.md @@ -38,6 +38,20 @@ Register a complex chatcommand. If a chat command with the same name is already Same as above but will overide existing command. +### `mcl_commands.register_chatcommand_alias(alias, cmd, [bypass])` + +Register an alias called `alias` of the `cmd` command. +If the setting `mcl_builtin_commands_overide` is set to `false`, the function will silently fail. +If `bypass` is set to `true` the function will not take care of the above setting. +Will warn if trying to alias to already existing command. + +### `mcl_commands.rename_chatcommand(newname, cmd, [bypass])` + +Rename `cmd` command to `newname`. +If the setting `mcl_builtin_commands_overide` is set to `false`, the function will silently fail. +If `bypass` is set to `true` the function will not take care of the above setting. +Will warn if trying to rename to already existing command. + ### paterns mcl_commands adds many types for patterns diff --git a/mods/CORE/mcl_commands/init.lua b/mods/CORE/mcl_commands/init.lua index 6c146d5261..1c143731ec 100644 --- a/mods/CORE/mcl_commands/init.lua +++ b/mods/CORE/mcl_commands/init.lua @@ -238,4 +238,29 @@ function mcl_commands.build(name, chat_def) cmd.privs = chat_def.privs cmd.description = chat_def.description return cmd +end + +function mcl_commands.register_chatcommand_alias(alias, cmd, bypass) + if not bypass then bypass = false end + if minetest.registered_chatcommands[alias] then + minetest.log("warning", "[mcl_commands] trying to alias ["..cmd.."] to already existing ["..alias.."] command") + elseif minetest.settings:get_bool("mcl_builtin_commands_overide", true) or bypass then + minetest.register_chatcommand(alias, minetest.chatcommands[cmd]) + minetest.log("action", "[mcl_commands] ["..cmd.."] command aliased successfully to ["..alias.."]") + else + minetest.log("action", "[mcl_commands] ["..cmd.."] command not aliased to ["..alias.."]") + end +end + +function mcl_commands.rename_chatcommand(newname, cmd, bypass) + if not bypass then bypass = false end + if minetest.registered_chatcommands[alias] then + minetest.log("warning", "[mcl_commands] trying to rename ["..cmd.."] to already existing ["..alias.."] command") + elseif minetest.settings:get_bool("mcl_builtin_commands_overide", true) or bypass then + minetest.register_chatcommand(newname, minetest.chatcommands[cmd]) + minetest.unregister_chatcommand(cmd) + minetest.log("action", "[mcl_commands] ["..cmd.."] command renamed successfully to ["..newname.."]") + else + minetest.log("action", "[mcl_commands] ["..cmd.."] command not renamed to ["..newname.."]") + end end \ No newline at end of file diff --git a/mods/MISC/mcl_basic_commands/alias.lua b/mods/MISC/mcl_basic_commands/alias.lua index 2989b7b373..33210ed995 100644 --- a/mods/MISC/mcl_basic_commands/alias.lua +++ b/mods/MISC/mcl_basic_commands/alias.lua @@ -1,30 +1,23 @@ local S = minetest.get_translator("mcl_commands") -local function register_chatcommand_alias(alias, cmd) - local def = minetest.chatcommands[cmd] - minetest.register_chatcommand(alias, def) -end +mcl_commands.register_chatcommand_alias("?", "help", false) +mcl_commands.register_chatcommand_alias("pardon", "unban", false) +mcl_commands.rename_chatcommand("stop", "shutdown", false) +mcl_commands.register_chatcommand_alias("tell", "msg", false) +mcl_commands.register_chatcommand_alias("w", "msg", false) +mcl_commands.register_chatcommand_alias("tp", "teleport", false) +mcl_commands.rename_chatcommand("clear", "clearinv", false) -local function rename_chatcommand(newname, cmd) - local def = minetest.chatcommands[cmd] - minetest.register_chatcommand(newname, def) - minetest.unregister_chatcommand(cmd) -end - -if minetest.settings:get_bool("mcl_builtin_commands_overide", true) then - register_chatcommand_alias("?", "help") - register_chatcommand_alias("pardon", "unban") - rename_chatcommand("stop", "shutdown") - register_chatcommand_alias("tell", "msg") - register_chatcommand_alias("w", "msg") - register_chatcommand_alias("tp", "teleport") - rename_chatcommand("clear", "clearinv") - - minetest.register_chatcommand("banlist", { - description = S("List bans"), - privs = minetest.chatcommands["ban"].privs, - func = function(name) - return true, S("Ban list: @1", minetest.get_ban_list()) - end, - }) -end \ No newline at end of file +mcl_commands.register_command("banlist", { + func = function(cmd) + cmd:sub("", { + func = function(name) + return true, S("Ban list: @1", minetest.get_ban_list()) + end, + privs = {}, + }) + end, + description = S("List bans"), + params = "", + privs = minetest.chatcommands["ban"].privs, +}) diff --git a/mods/MISC/mcl_basic_commands/list.lua b/mods/MISC/mcl_basic_commands/list.lua index 0257e2837c..7dcc057552 100644 --- a/mods/MISC/mcl_basic_commands/list.lua +++ b/mods/MISC/mcl_basic_commands/list.lua @@ -1,14 +1,19 @@ local S = minetest.get_translator("mcl_commands") -minetest.register_chatcommand("list", { - description = S("Show who is logged on"), +mcl_commands.register_command("list", { + func = function(cmd) + cmd:sub("", { + func = function(name) + local players = "" + for _, player in ipairs(minetest.get_connected_players()) do + players = players..player:get_player_name().."\n" + end + return true, players + end, + privs = {}, + }) + end, + description = S("Show who is logged on"), params = "", - privs = {}, - func = function(name) - local players = "" - for _, player in ipairs(minetest.get_connected_players()) do - players = players..player:get_player_name().."\n" - end - minetest.chat_send_player(name, players) - end + privs = {}, }) \ No newline at end of file diff --git a/mods/MISC/mcl_basic_commands/seed.lua b/mods/MISC/mcl_basic_commands/seed.lua index da5f6a3039..9a7281463f 100644 --- a/mods/MISC/mcl_basic_commands/seed.lua +++ b/mods/MISC/mcl_basic_commands/seed.lua @@ -1,10 +1,15 @@ local S = minetest.get_translator("mcl_commands") -minetest.register_chatcommand("seed", { - description = S("Displays the world seed"), +mcl_commands.register_command("seed", { + func = function(cmd) + cmd:sub("", { + func = function(name) + return true, "Seed: "..minetest.get_mapgen_setting("seed") + end, + privs = {}, + }) + end, + description = S("Displays the world seed"), params = "", - privs = {}, - func = function(name) - minetest.chat_send_player(name, "Seed: "..minetest.get_mapgen_setting("seed")) - end + privs = {}, }) \ No newline at end of file