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
missing_privs_str = missing_privs_str..missing_privs[i].." " if not i == #missing_privs then
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 true
return false, S("Invalid usage, see /help say.") end})
end
minetest.chat_send_all(("["..name.."] "..param))
return true
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,41 +103,40 @@ function mcl_title.reset(playername)
end end
end end
mcl_commands.register_command("title", function(cmd) mcl_commands.register_command("title", {
cmd:sub(":name:username title :params:json", { func = function(cmd)
func = function(name, target, json) cmd:sub(":name:username title :params:json", {
return mcl_title.set(target, "title", json) func = function(name, target, json)
end, return mcl_title.set(target, "title", json)
}) end,
cmd:sub(":name:username subtitle :params:json", { })
func = function(name, target, json) cmd:sub(":name:username subtitle :params:json", {
return mcl_title.set(target, "subtitle", json) func = function(name, target, json)
end, return mcl_title.set(target, "subtitle", json)
}) end,
cmd:sub(":name:username actionbar :params:json", { })
func = function(name, target, json) cmd:sub(":name:username actionbar :params:json", {
return mcl_title.set(target, "actionbar", json) func = function(name, target, json)
end, return mcl_title.set(target, "actionbar", json)
}) end,
cmd:sub(":name:username times :fadeIn:int :stay:int :fadeOut:int", { })
func = function(name, target, fadeIn, stay, fadeOut) --WIP cmd:sub(":name:username times :fadeIn:int :stay:int :fadeOut:int", {
return mcl_title.times(target, fadeIn, stay, fadeOut) func = function(name, target, fadeIn, stay, fadeOut) --WIP
end, return mcl_title.times(target, fadeIn, stay, fadeOut)
}) end,
cmd:sub(":name:username clear", { })
func = function(name, target) cmd:sub(":name:username clear", {
return mcl_title.clear(target) func = function(name, target)
end, return mcl_title.clear(target)
}) end,
cmd:sub(":name:username reset", { })
func = function(name, target) cmd:sub(":name:username reset", {
return mcl_title.reset(target) func = function(name, target)
end, return mcl_title.reset(target)
}) 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,
}
}) })