From 1f5076cfd02cf11c43726f17e0726d391a5f51c7 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Mon, 8 Mar 2021 16:48:59 +0100 Subject: [PATCH] Add ability for sub commands to have special privs --- mods/MISC/mcl_commands/init.lua | 23 ++++++++++++++--------- mods/MISC/mcl_commands/kill.lua | 21 ++++++++++++++------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/mods/MISC/mcl_commands/init.lua b/mods/MISC/mcl_commands/init.lua index b707ab598..fa4e3a37b 100644 --- a/mods/MISC/mcl_commands/init.lua +++ b/mods/MISC/mcl_commands/init.lua @@ -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") \ No newline at end of file diff --git a/mods/MISC/mcl_commands/kill.lua b/mods/MISC/mcl_commands/kill.lua index a212bcb45..996c767e2 100644 --- a/mods/MISC/mcl_commands/kill.lua +++ b/mods/MISC/mcl_commands/kill.lua @@ -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) \ No newline at end of file + 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}, +}) \ No newline at end of file