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 = "(.+)",
}
function mcl_commands.register_command(name, func, def)
function mcl_commands.register_command(name, def)
def = def or {}
local cmd = mcl_commands.build(func, def)
local cmd = mcl_commands.build(name, def)
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")
end
@ -36,9 +36,9 @@ function mcl_commands.register_command(name, func, def)
return cmd
end
function mcl_commands.overide_command(name, func, def)
function mcl_commands.overide_command(name, def)
def = def or {}
local cmd = mcl_commands.build(func, def)
local cmd = mcl_commands.build(name, def)
if minetest.registered_chatcommands[name] then
minetest.unregister_chatcommand(name)
end
@ -61,7 +61,7 @@ end
local dprint = function() end
function mcl_commands.build(func, def)
function mcl_commands.build(def)
local cmd = {
_subs = {}
}
@ -174,8 +174,8 @@ function mcl_commands.build(func, def)
table.insert(self._subs, sub)
end
if func then
func(cmd)
if def.func then
def.func(cmd)
end
cmd.func = function(name, param)
@ -218,13 +218,17 @@ function mcl_commands.build(func, def)
else
local missing_privs_str = ""
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
return false, "You don't have permission to run this command (missing privilege: "..missing_privs_str..")" --TODO:replace message
end
end
end
return false, "Invalid command"
return false, "Invalid parameters: "
end
if not def.params then
def.params = ""

View File

@ -4,15 +4,31 @@ minetest.register_privilege("announce", {
description = S("Can use /say"),
give_to_singleplayer = false,
})
minetest.register_chatcommand("say", {
params = S("<message>"),
description = S("Send a message to every player"),
privs = {announce=true},
func = function(name, param)
if not param then
return false, S("Invalid usage, see /help say.")
end
minetest.chat_send_all(("["..name.."] "..param))
return true
mcl_commands.register_command("say", 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,
}
})
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
mcl_commands.register_command("title", function(cmd)
cmd:sub(":name:username title :params:json", {
func = function(name, target, json)
return mcl_title.set(target, "title", json)
end,
})
cmd:sub(":name:username subtitle :params:json", {
func = function(name, target, json)
return mcl_title.set(target, "subtitle", json)
end,
})
cmd:sub(":name:username actionbar :params:json", {
func = function(name, target, json)
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
return mcl_title.times(target, fadeIn, stay, fadeOut)
end,
})
cmd:sub(":name:username clear", {
func = function(name, target)
return mcl_title.clear(target)
end,
})
cmd:sub(":name:username reset", {
func = function(name, target)
return mcl_title.reset(target)
end,
})
end, {
mcl_commands.register_command("title", {
func = function(cmd)
cmd:sub(":name:username title :params:json", {
func = function(name, target, json)
return mcl_title.set(target, "title", json)
end,
})
cmd:sub(":name:username subtitle :params:json", {
func = function(name, target, json)
return mcl_title.set(target, "subtitle", json)
end,
})
cmd:sub(":name:username actionbar :params:json", {
func = function(name, target, json)
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
return mcl_title.times(target, fadeIn, stay, fadeOut)
end,
})
cmd:sub(":name:username clear", {
func = function(name, target)
return mcl_title.clear(target)
end,
})
cmd:sub(":name:username reset", {
func = function(name, target)
return mcl_title.reset(target)
end,
})
end,
description = "Controls text displayed on the screen.",
params = "<target> command <params>",
privs = {
server = true,
}
privs = {server = true},
})