This commit is contained in:
AFCMS 2021-03-08 18:53:01 +01:00
parent 9e7ec24c0e
commit f7b832508f
2 changed files with 21 additions and 14 deletions

View File

@ -27,26 +27,22 @@ mcl_commands.types = {
function mcl_commands.register_command(name, func, def)
def = def or {}
local cmd = mcl_commands.build(func)
cmd.def = def
def.func = cmd.run
local cmd = mcl_commands.build(func, 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
minetest.register_chatcommand(name, def)
minetest.register_chatcommand(name, cmd)
minetest.log("action", "[mcl_commands] ["..name.."] command registered successfully")
return cmd
end
function mcl_commands.overide_command(name, func, def)
def = def or {}
local cmd = mcl_commands.build(func)
cmd.def = def
def.func = cmd.run
local cmd = mcl_commands.build(func, def)
if minetest.registered_chatcommands[name] then
minetest.unregister_chatcommand(name)
end
minetest.register_chatcommand(name, def)
minetest.register_chatcommand(name, cmd)
minetest.log("action", "[mcl_commands] ["..name.."] command overridden successfully")
return cmd
end
@ -65,7 +61,7 @@ end
local dprint = function() end
function mcl_commands.build(func)
function mcl_commands.build(func, def)
local cmd = {
_subs = {}
}
@ -80,8 +76,9 @@ function mcl_commands.build(func)
pattern = "^",
params = {},
func = def.func,
privs = def.privs or {}
desc = def.desc
privs = def.privs or {},
desc = def.desc,
params_desc = def.params or "",
}
-- End of param reached: add it to the pattern
@ -181,7 +178,7 @@ function mcl_commands.build(func)
func(cmd)
end
cmd.run = function(name, param)
cmd.func = function(name, param)
for i = 1, #cmd._subs do
local sub = cmd._subs[i]
local res = { string.match(param, sub.pattern) }
@ -219,13 +216,22 @@ function mcl_commands.build(func)
return sub.func(unpack(params))
end
else
return false, "You don't have permission to run this command (missing privilege: "..minetest.serialize(missing_privs)..")"
return false, "You don't have permission to run this command (missing privilege: "..minetest.serialize(missing_privs)..")" --TODO:replace message
end
end
end
return false, "Invalid command"
end
if not def.params then
def.params = ""
end
local params_help = ""
for i = 1, #cmd._subs do
params_help = params_help..def.params.." "..cmd._subs[i].params_desc.." "
end
cmd.params = params_help
cmd.privs = def.privs
cmd.description = def.description
return cmd
end

View File

@ -108,6 +108,7 @@ mcl_commands.register_command("title", function(cmd)
func = function(name, target, json)
return mcl_title.set(target, "title", json)
end,
params = "<json>",
})
cmd:sub(":name:username subtitle :params:json", {
func = function(name, target, json)