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

View File

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