add public API to properly (take care of mcl_builtin_commands_overide) rename and alias chat command

This commit is contained in:
AFCMS 2021-03-09 00:34:33 +01:00
parent 884097a8e5
commit f90243f6e5
5 changed files with 85 additions and 43 deletions

View File

@ -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. 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 ### paterns
mcl_commands adds many types for patterns mcl_commands adds many types for patterns

View File

@ -239,3 +239,28 @@ function mcl_commands.build(name, chat_def)
cmd.description = chat_def.description cmd.description = chat_def.description
return cmd return cmd
end 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

View File

@ -1,30 +1,23 @@
local S = minetest.get_translator("mcl_commands") local S = minetest.get_translator("mcl_commands")
local function register_chatcommand_alias(alias, cmd) mcl_commands.register_chatcommand_alias("?", "help", false)
local def = minetest.chatcommands[cmd] mcl_commands.register_chatcommand_alias("pardon", "unban", false)
minetest.register_chatcommand(alias, def) mcl_commands.rename_chatcommand("stop", "shutdown", false)
end 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) mcl_commands.register_command("banlist", {
local def = minetest.chatcommands[cmd] func = function(cmd)
minetest.register_chatcommand(newname, def) cmd:sub("", {
minetest.unregister_chatcommand(cmd) func = function(name)
end return true, S("Ban list: @1", minetest.get_ban_list())
end,
if minetest.settings:get_bool("mcl_builtin_commands_overide", true) then privs = {},
register_chatcommand_alias("?", "help") })
register_chatcommand_alias("pardon", "unban") end,
rename_chatcommand("stop", "shutdown") description = S("List bans"),
register_chatcommand_alias("tell", "msg") params = "",
register_chatcommand_alias("w", "msg") privs = minetest.chatcommands["ban"].privs,
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

View File

@ -1,14 +1,19 @@
local S = minetest.get_translator("mcl_commands") local S = minetest.get_translator("mcl_commands")
minetest.register_chatcommand("list", { mcl_commands.register_command("list", {
description = S("Show who is logged on"), 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 = "", params = "",
privs = {}, 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
}) })

View File

@ -1,10 +1,15 @@
local S = minetest.get_translator("mcl_commands") local S = minetest.get_translator("mcl_commands")
minetest.register_chatcommand("seed", { mcl_commands.register_command("seed", {
description = S("Displays the world 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 = "", params = "",
privs = {}, privs = {},
func = function(name)
minetest.chat_send_player(name, "Seed: "..minetest.get_mapgen_setting("seed"))
end
}) })