#5501 allow to configure setting of npc_privs

This commit is contained in:
Sokomine 2024-12-19 22:12:08 +01:00
parent 95e721b8f9
commit 1b45fc7792
3 changed files with 85 additions and 29 deletions

View File

@ -48,14 +48,7 @@ yl_speak_up.command_npc_talk = function(pname, param)
-- implemented in fs_npc_list.lua:
return yl_speak_up.command_npc_force_restore_npc(pname, rest)
elseif(cmd and cmd == "privs") then
-- TODO: make this available for npc_talk_admin?
if(not(minetest.check_player_privs(pname, {privs = true}))) then
minetest.chat_send_player(pname, "This command is used for managing "..
"privs (like execute lua, teleportation, giving items...) for NPC. "..
"You lack the \"privs\" priv required to "..
"run this command.")
return
end
-- the command now checks for player privs
-- implemented in npc_privs.lua:
return yl_speak_up.command_npc_talk_privs(pname, rest)
end
@ -68,9 +61,9 @@ yl_speak_up.command_npc_talk = function(pname, param)
" version show human-readable version information\n"..
" list shows a list of NPC that you can edit\n"..
" debug debug a particular NPC\n"..
" generic [requores npc_talk_admin priv] list, add or remove NPC as generic NPC\n"..
" privs list, grant or revoke privs for your NPC\n"..
" generic [requires npc_talk_admin priv] list, add or remove NPC as generic NPC\n"..
" force_restore_npc [requires npc_talk_admin priv] restore NPC that got lost\n"..
" privs [requires privs priv] list, grant or revoke privs for an NPC\n"..
-- reload is fully handled in register_once
"Note: /npc_talk_reload [requires privs priv] reloads the code of the mod without server "..
"restart."..

View File

@ -129,15 +129,32 @@ yl_speak_up.player_vars_min_save_time = 60
------------------------------------------------------------------------------
-- Privs - usually no need to change
------------------------------------------------------------------------------
-- * set the name of the priv that allows to add, edit and change preconditions, actions and
-- effects listed in yl_speak_up.npc_priv_names in npc_privs.lua
-- * this also allows the player to use the "/npc_talk privs" command to assign these privs
-- to NPC
-- * it does *NOT* include the "precon_exec_lua" and "effect_exec_lua" priv - just
-- "effect_give_item", "effect_take_item" and "effect_move_player"
-- NPC need npc privs in order to use some preconditions, actions and effects.
--
-- Plaers need privs in order to add, edit and change preconditions, actions and
-- effects listed in yl_speak_up.npc_priv_names in npc_privs.lua.
--
-- The following player priv allows the player to use the "/npc_talk privs" command to
-- grant/revoke/see these npc privs for *all NPC - not only for those the player can edit!
-- * default: "npc_talk_admin" (but can also be set to "npc_master" or "privs" if you want)
yl_speak_up.npc_privs_priv = "npc_talk_admin"
-- depending on your server, you might want to allow /npc_talk privs to be used by players
-- who *don't* have the privs priv;
-- WANRING: "precon_exec_lua" and "effect_exec_lua" are dangerous npc privs. Only players
-- with the privs priv ought to be able to use those!
-- The privs priv is the fallback if nothing is specified here.
yl_speak_up.npc_priv_needs_player_priv = {}
-- these privs allow to create items out of thin air - similar to the "give" priv
yl_speak_up.npc_priv_needs_player_priv["effect_give_item"] = "give"
yl_speak_up.npc_priv_needs_player_priv["effect_take_item"] = "give"
-- on servers with travelnets and/or teleporters, you'd most likely want to allow every
-- player to let NPC teleport players around; the "interact" priv covers that
--yl_speak_up.npc_priv_needs_player_priv["effect_move_player"] = "interact"
-- on YourLand, travel is very restricted; only those who can teleport players around can
-- do it with NPC as well; for backward compatibility, this is set for all servers
yl_speak_up.npc_priv_needs_player_priv["effect_move_player"] = "bring"
------------------------------------------------------------------------------
-- Blacklists - not all blocks may be suitable for all effects NPC can do
------------------------------------------------------------------------------

View File

@ -13,6 +13,17 @@ yl_speak_up.npc_priv_names = {
"effect_exec_lua", "effect_give_item", "effect_take_item", "effect_move_player",
}
-- make sure this table exists
if(not(yl_speak_up.npc_priv_needs_player_priv)) then
yl_speak_up.npc_priv_needs_player_priv = {}
end
-- and set it to privs if nothing is specified (because the *_lua are extremly dangerous npc_privs!)
for i, p in ipairs(yl_speak_up.npc_priv_names) do
if(not(yl_speak_up.npc_priv_needs_player_priv[p])) then
yl_speak_up.npc_priv_needs_player_priv[p] = "privs"
end
end
-- either the npc with n_id *or* if generic_npc_id is set the generic npc with the
-- id generic_npc_id needs to have been granted priv_name
yl_speak_up.npc_has_priv = function(n_id, priv_name, generic_npc_id)
@ -74,27 +85,43 @@ end
-- "If called with parameter [list], all granted privs for all NPC are shown.",
-- privs = {privs = true},
yl_speak_up.command_npc_talk_privs = function(pname, param)
-- can the player see the privs for all NPC? or just for those he can edit?
local list_all = false
local ptmp = {}
ptmp[yl_speak_up.npc_privs_priv] = true
if(minetest.check_player_privs(pname, ptmp)) then
list_all = true
end
if(not(param) or param == "") then
-- if the npc priv has a player priv as requirement, then list that
local tmp = {}
for i, p in ipairs(yl_speak_up.npc_priv_names) do
table.insert(tmp, tostring(p)..
" ["..tostring(yl_speak_up.npc_priv_needs_player_priv[p]).."]")
end
minetest.chat_send_player(pname,
"Usage: [grant|revoke|list] <n_id> <priv>\n"..
"The following privilege exist:\n\t"..
table.concat(yl_speak_up.npc_priv_names, ", ")..".")
"The following privilege exist [and require you to have this priv to set]:\n\t"..
table.concat(tmp, ", ")..".")
return
end
local player = minetest.get_player_by_name(pname)
local parts = string.split(param, " ")
if(parts[1] == "list") then
local text = "This list contains the privs of each NPC in the form of "..
"<npc_name>: <list of privs>"
local text = "This list contains the privs of each NPC you can edit "..
"in the form of <npc_name>: <list of privs>"
-- create list of all existing extra privs for npc
for n_id, v in pairs(yl_speak_up.npc_priv_table) do
text = text..".\n"..tostring(n_id)..":"
local found = false
for priv, w in pairs(v) do
text = text.." "..tostring(priv)
found = true
end
if(not(found)) then
text = text.." <none>"
if(list_all or yl_speak_up.may_edit_npc(player, n_id)) then
text = text..".\n"..tostring(n_id)..":"
local found = false
for priv, w in pairs(v) do
text = text.." "..tostring(priv)
found = true
end
if(not(found)) then
text = text.." <none>"
end
end
end
minetest.chat_send_player(pname, text..".")
@ -114,8 +141,27 @@ yl_speak_up.command_npc_talk_privs = function(pname, param)
table.concat(yl_speak_up.npc_priv_names, ", ")..".")
return
end
-- does the player have the necessary player priv to grant or revoke this npc priv?
local ptmp = {}
ptmp[yl_speak_up.npc_priv_needs_player_priv[priv]] = true
if(not(minetest.check_player_privs(pname, ptmp))) then
minetest.chat_send_player(pname, "You lack the \""..
tostring(yl_speak_up.npc_priv_needs_player_priv[priv])..
"\" priv required to grant or revoke this NPC priv!")
return
end
-- does the player have the right to edit/change this npc?
if(not(list_all) and not(yl_speak_up.may_edit_npc(player, n_id))) then
minetest.chat_send_player(pname, "You can only set privs for NPC which you can edit. \""..
tostring(n_id).." cannot be edited by you.")
return
end
-- revoking privs of nonexistant NPC is allowed - but not granting them privs
if(command == "grant" and not(yl_speak_up.npc_list[n_id])) then
local id = tonumber(string.sub(n_id, 3)) or 0
if(command == "grant" and not(yl_speak_up.npc_list[id])) then
minetest.chat_send_player(pname,
"Unknown NPC \""..tostring(n_id).."\".\n")
return