Skip to content

Commit c31e354

Browse files
red-001paramat
authored andcommittedOct 16, 2016
Builtin/../chatcommands: Add /grantme command
1 parent 4b17105 commit c31e354

File tree

1 file changed

+56
-38
lines changed

1 file changed

+56
-38
lines changed
 

‎builtin/game/chatcommands.lua

+56-38
Original file line numberDiff line numberDiff line change
@@ -154,53 +154,71 @@ core.register_chatcommand("privs", {
154154
core.get_player_privs(param), ' ')
155155
end,
156156
})
157+
158+
local function handle_grant_command(caller, grantname, grantprivstr)
159+
local caller_privs = minetest.get_player_privs(caller)
160+
if not (caller_privs.privs or caller_privs.basic_privs) then
161+
return false, "Your privileges are insufficient."
162+
end
163+
164+
if not core.auth_table[grantname] then
165+
return false, "Player " .. grantname .. " does not exist."
166+
end
167+
local grantprivs = core.string_to_privs(grantprivstr)
168+
if grantprivstr == "all" then
169+
grantprivs = core.registered_privileges
170+
end
171+
local privs = core.get_player_privs(grantname)
172+
local privs_unknown = ""
173+
local basic_privs =
174+
core.string_to_privs(core.setting_get("basic_privs") or "interact,shout")
175+
for priv, _ in pairs(grantprivs) do
176+
if not basic_privs[priv] and not caller_privs.privs then
177+
return false, "Your privileges are insufficient."
178+
end
179+
if not core.registered_privileges[priv] then
180+
privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
181+
end
182+
privs[priv] = true
183+
end
184+
if privs_unknown ~= "" then
185+
return false, privs_unknown
186+
end
187+
core.set_player_privs(grantname, privs)
188+
core.log("action", caller..' granted ('..core.privs_to_string(grantprivs, ', ')..') privileges to '..grantname)
189+
if grantname ~= caller then
190+
core.chat_send_player(grantname, caller
191+
.. " granted you privileges: "
192+
.. core.privs_to_string(grantprivs, ' '))
193+
end
194+
return true, "Privileges of " .. grantname .. ": "
195+
.. core.privs_to_string(
196+
core.get_player_privs(grantname), ' ')
197+
end
198+
157199
core.register_chatcommand("grant", {
158200
params = "<name> <privilege>|all",
159201
description = "Give privilege to player",
160202
func = function(name, param)
161-
if not core.check_player_privs(name, {privs=true}) and
162-
not core.check_player_privs(name, {basic_privs=true}) then
163-
return false, "Your privileges are insufficient."
164-
end
165203
local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
166204
if not grantname or not grantprivstr then
167205
return false, "Invalid parameters (see /help grant)"
168-
elseif not core.auth_table[grantname] then
169-
return false, "Player " .. grantname .. " does not exist."
170-
end
171-
local grantprivs = core.string_to_privs(grantprivstr)
172-
if grantprivstr == "all" then
173-
grantprivs = core.registered_privileges
174-
end
175-
local privs = core.get_player_privs(grantname)
176-
local privs_unknown = ""
177-
local basic_privs =
178-
core.string_to_privs(core.setting_get("basic_privs") or "interact,shout")
179-
for priv, _ in pairs(grantprivs) do
180-
if not basic_privs[priv] and
181-
not core.check_player_privs(name, {privs=true}) then
182-
return false, "Your privileges are insufficient."
183-
end
184-
if not core.registered_privileges[priv] then
185-
privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
186-
end
187-
privs[priv] = true
188-
end
189-
if privs_unknown ~= "" then
190-
return false, privs_unknown
191-
end
192-
core.set_player_privs(grantname, privs)
193-
core.log("action", name..' granted ('..core.privs_to_string(grantprivs, ', ')..') privileges to '..grantname)
194-
if grantname ~= name then
195-
core.chat_send_player(grantname, name
196-
.. " granted you privileges: "
197-
.. core.privs_to_string(grantprivs, ' '))
198-
end
199-
return true, "Privileges of " .. grantname .. ": "
200-
.. core.privs_to_string(
201-
core.get_player_privs(grantname), ' ')
206+
end
207+
return handle_grant_command(name, grantname, grantprivstr)
208+
end,
209+
})
210+
211+
core.register_chatcommand("grantme", {
212+
params = "<privilege>|all",
213+
description = "Grant privileges to yourself",
214+
func = function(name, param)
215+
if param == "" then
216+
return false, "Invalid parameters (see /help grantme)"
217+
end
218+
return handle_grant_command(name, name, param)
202219
end,
203220
})
221+
204222
core.register_chatcommand("revoke", {
205223
params = "<name> <privilege>|all",
206224
description = "Remove privilege from player",

0 commit comments

Comments
 (0)
Please sign in to comment.