Add ability for sub commands to have special privs

This commit is contained in:
AFCMS 2021-03-08 16:48:59 +01:00
parent 7139ca1395
commit 1f5076cfd0
2 changed files with 28 additions and 16 deletions

View File

@ -67,10 +67,9 @@ function mcl_commands.build(func)
local cmd = { local cmd = {
_subs = {} _subs = {}
} }
function cmd:sub(route, func, def) function cmd:sub(route, def)
dprint("Parsing " .. route) dprint("Parsing " .. route)
def = def or {}
if string.trim then if string.trim then
route = string.trim(route) route = string.trim(route)
end end
@ -78,7 +77,8 @@ function mcl_commands.build(func)
local sub = { local sub = {
pattern = "^", pattern = "^",
params = {}, params = {},
func = func func = def.func,
privs = def.privs or {}
} }
-- End of param reached: add it to the pattern -- End of param reached: add it to the pattern
@ -206,12 +206,17 @@ function mcl_commands.build(func)
pointer = pointer + 1 pointer = pointer + 1
end end
end end
if table.unpack then local can_execute, missing_privs = minetest.check_player_privs(name, sub.privs)
-- lua 5.2 or later if minetest.check_player_privs(name, sub.privs) then
return sub.func(table.unpack(params)) if table.unpack then
-- lua 5.2 or later
return sub.func(table.unpack(params))
else
-- lua 5.1 or earlier
return sub.func(unpack(params))
end
else else
-- lua 5.1 or earlier return false, "You haven't the required privilege. Missing privs: "..minetest.serialize(missing_privs)
return sub.func(unpack(params))
end end
end end
end end
@ -230,6 +235,6 @@ dofile(modpath.."/summon.lua")
dofile(modpath.."/say.lua") dofile(modpath.."/say.lua")
dofile(modpath.."/list.lua") dofile(modpath.."/list.lua")
dofile(modpath.."/sound.lua") dofile(modpath.."/sound.lua")
dofile(modpath.."/title.lua") --dofile(modpath.."/title.lua")
dofile(modpath.."/alias.lua") dofile(modpath.."/alias.lua")

View File

@ -42,10 +42,17 @@ local function handle_kill_command(suspect, victim)
end end
mcl_commands.overide_command("kill", function(cmd) mcl_commands.overide_command("kill", function(cmd)
cmd:sub("", function(name) cmd:sub("", {
return handle_kill_command(name, name) func = function(name)
end) return handle_kill_command(name, name)
cmd:sub(":target:username", function(name, target) end,
return handle_kill_command(name, target) })
end) cmd:sub(":target:username", {
end) func = function(name, target)
return handle_kill_command(name, target)
end,
privs = {settime = true}
})
end, {
privs = {server = true},
})