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

View File

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