improve chat command definition

This commit is contained in:
AFCMS 2021-03-08 22:48:44 +01:00
parent 155548f384
commit 43c641f84f
3 changed files with 73 additions and 54 deletions

View File

@ -25,9 +25,9 @@ mcl_commands.types = {
json = "(.+)", json = "(.+)",
} }
function mcl_commands.register_command(name, func, def) function mcl_commands.register_command(name, def)
def = def or {} def = def or {}
local cmd = mcl_commands.build(func, def) local cmd = mcl_commands.build(name, def)
if minetest.registered_chatcommands[name] then if minetest.registered_chatcommands[name] then
error("[mcl_commands] Failed to register command: ["..name.."] command already existing! Use mcl_commands.overide_command() if you want to overide existing command") error("[mcl_commands] Failed to register command: ["..name.."] command already existing! Use mcl_commands.overide_command() if you want to overide existing command")
end end
@ -36,9 +36,9 @@ function mcl_commands.register_command(name, func, def)
return cmd return cmd
end end
function mcl_commands.overide_command(name, func, def) function mcl_commands.overide_command(name, def)
def = def or {} def = def or {}
local cmd = mcl_commands.build(func, def) local cmd = mcl_commands.build(name, def)
if minetest.registered_chatcommands[name] then if minetest.registered_chatcommands[name] then
minetest.unregister_chatcommand(name) minetest.unregister_chatcommand(name)
end end
@ -61,7 +61,7 @@ end
local dprint = function() end local dprint = function() end
function mcl_commands.build(func, def) function mcl_commands.build(def)
local cmd = { local cmd = {
_subs = {} _subs = {}
} }
@ -174,8 +174,8 @@ function mcl_commands.build(func, def)
table.insert(self._subs, sub) table.insert(self._subs, sub)
end end
if func then if def.func then
func(cmd) def.func(cmd)
end end
cmd.func = function(name, param) cmd.func = function(name, param)
@ -218,13 +218,17 @@ function mcl_commands.build(func, def)
else else
local missing_privs_str = "" local missing_privs_str = ""
for i = 1, #missing_privs do for i = 1, #missing_privs do
if not i == #missing_privs then
missing_privs_str = missing_privs_str..missing_privs[i].." " missing_privs_str = missing_privs_str..missing_privs[i].." "
else
missing_privs_str = missing_privs_str..missing_privs[i]
end
end end
return false, "You don't have permission to run this command (missing privilege: "..missing_privs_str..")" --TODO:replace message return false, "You don't have permission to run this command (missing privilege: "..missing_privs_str..")" --TODO:replace message
end end
end end
end end
return false, "Invalid command" return false, "Invalid parameters: "
end end
if not def.params then if not def.params then
def.params = "" def.params = ""

View File

@ -4,15 +4,31 @@ minetest.register_privilege("announce", {
description = S("Can use /say"), description = S("Can use /say"),
give_to_singleplayer = false, give_to_singleplayer = false,
}) })
minetest.register_chatcommand("say", {
params = S("<message>"), mcl_commands.register_command("say", function(cmd)
description = S("Send a message to every player"), cmd:sub(":message:text", {
privs = {announce=true}, func = function(name, message)
func = function(name, param) minetest.chat_send_all(("["..name.."] "..message))
if not param then
return false, S("Invalid usage, see /help say.")
end
minetest.chat_send_all(("["..name.."] "..param))
return true return true
end})
end, end,
{
description = S("Send a message to every player"),
params = S("<message>"),
privs = {
announce = true,
}
})
mcl_commands.register_command("say", {
func = function(cmd)
cmd:sub(":message:text", {
func = function(name, message)
minetest.chat_send_all(("["..name.."] "..message))
return true
end})
end,
description = S("Send a message to every player"),
params = S("<message>"),
privs = {announce = true},
}) })

View File

@ -103,7 +103,8 @@ function mcl_title.reset(playername)
end end
end end
mcl_commands.register_command("title", function(cmd) mcl_commands.register_command("title", {
func = function(cmd)
cmd:sub(":name:username title :params:json", { cmd:sub(":name:username title :params:json", {
func = function(name, target, json) func = function(name, target, json)
return mcl_title.set(target, "title", json) return mcl_title.set(target, "title", json)
@ -134,10 +135,8 @@ mcl_commands.register_command("title", function(cmd)
return mcl_title.reset(target) return mcl_title.reset(target)
end, end,
}) })
end, { end,
description = "Controls text displayed on the screen.", description = "Controls text displayed on the screen.",
params = "<target> command <params>", params = "<target> command <params>",
privs = { privs = {server = true},
server = true,
}
}) })