moved chat command npc_talk_privs into own file

This commit is contained in:
Sokomine 2022-06-19 21:19:04 +02:00
parent 74b024645b
commit 6be2883b6a
3 changed files with 79 additions and 74 deletions

View File

@ -0,0 +1,75 @@
-- used by privs.lua
-- a chat command to grant or deny or disallow npc these privs;
-- it is not checked if the NPC exists
minetest.register_chatcommand( 'npc_talk_privs', {
description = "Grants or revokes the privilege <priv> to the "..
"yl_speak_up-NPC with the ID <n_id>.\n"..
"Call: [grant|revoke] <n_id> <priv>\n"..
"If called with parameter [list], all granted privs for all NPC are shown.",
privs = {privs = true},
func = function(pname, param)
if(not(param) or param == "") then
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, ", ")..".")
return
end
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>"
-- 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>"
end
end
minetest.chat_send_player(pname, text..".")
return
end
if((parts[1] ~= "grant" and parts[1] ~= "revoke") or #parts ~= 3) then
minetest.chat_send_player(pname, "Usage: [grant|revoke] <n_id> <priv>")
return
end
local command = parts[1]
local n_id = parts[2]
local priv = parts[3]
if(table.indexof(yl_speak_up.npc_priv_names, priv) == -1) then
minetest.chat_send_player(pname,
"Unknown priv \""..tostring(priv).."\".\n"..
"The following privilege exist:\n\t"..
table.concat(yl_speak_up.npc_priv_names, ", ")..".")
return
end
if(command == "grant" and not(yl_speak_up.npc_priv_table[n_id])) then
yl_speak_up.npc_priv_table[n_id] = {}
end
if(command == "grant") then
yl_speak_up.npc_priv_table[n_id][priv] = true
elseif(yl_speak_up.npc_priv_table[n_id]) then
yl_speak_up.npc_priv_table[n_id][priv] = nil
end
local text = "New privs of NPC "..tostring(n_id)..":"
local found = false
if(yl_speak_up.npc_priv_table[n_id]) then
for k, v in pairs(yl_speak_up.npc_priv_table[n_id]) do
text = text.." "..tostring(k)
found = true
end
end
if(not(found)) then
text = text.." <none>"
yl_speak_up.npc_priv_table[n_id] = nil
end
minetest.chat_send_player(pname, text..".")
yl_speak_up.npc_privs_store()
end
})

View File

@ -30,6 +30,8 @@ yl_speak_up.custom_server_functions = {}
dofile(modpath .. "config.lua")
-- players *and* npc need privs for certain things
dofile(modpath .. "privs.lua")
-- contains the command to hand out privs to NPC
dofile(modpath .. "command_npc_talk_privs.lua")
-- add generic dialogs
dofile(modpath .. "add_generic_dialogs.lua")
-- the actual command to add or remove generic npc dialogs

View File

@ -90,79 +90,7 @@ yl_speak_up.npc_privs_store = function()
end
-- a chat command to grant or deny or disallow npc these privs;
-- it is not checked if the NPC exists
minetest.register_chatcommand( 'npc_talk_privs', {
description = "Grants or revokes the privilege <priv> to the "..
"yl_speak_up-NPC with the ID <n_id>.\n"..
"Call: [grant|revoke] <n_id> <priv>\n"..
"If called with parameter [list], all granted privs for all NPC are shown.",
privs = {privs = true},
func = function(pname, param)
if(not(param) or param == "") then
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, ", ")..".")
return
end
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>"
-- 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>"
end
end
minetest.chat_send_player(pname, text..".")
return
end
if((parts[1] ~= "grant" and parts[1] ~= "revoke") or #parts ~= 3) then
minetest.chat_send_player(pname, "Usage: [grant|revoke] <n_id> <priv>")
return
end
local command = parts[1]
local n_id = parts[2]
local priv = parts[3]
if(table.indexof(yl_speak_up.npc_priv_names, priv) == -1) then
minetest.chat_send_player(pname,
"Unknown priv \""..tostring(priv).."\".\n"..
"The following privilege exist:\n\t"..
table.concat(yl_speak_up.npc_priv_names, ", ")..".")
return
end
if(command == "grant" and not(yl_speak_up.npc_priv_table[n_id])) then
yl_speak_up.npc_priv_table[n_id] = {}
end
if(command == "grant") then
yl_speak_up.npc_priv_table[n_id][priv] = true
elseif(yl_speak_up.npc_priv_table[n_id]) then
yl_speak_up.npc_priv_table[n_id][priv] = nil
end
local text = "New privs of NPC "..tostring(n_id)..":"
local found = false
if(yl_speak_up.npc_priv_table[n_id]) then
for k, v in pairs(yl_speak_up.npc_priv_table[n_id]) do
text = text.." "..tostring(k)
found = true
end
end
if(not(found)) then
text = text.." <none>"
yl_speak_up.npc_priv_table[n_id] = nil
end
minetest.chat_send_player(pname, text..".")
yl_speak_up.npc_privs_store()
end
})
-- when the game is started: load the npc privs
yl_speak_up.npc_privs_load()
-- the privs for NPC can be set via the chat command in command_npc_talk_priv.lua