diff --git a/command_npc_talk_privs.lua b/command_npc_talk_privs.lua new file mode 100644 index 0000000..d184da5 --- /dev/null +++ b/command_npc_talk_privs.lua @@ -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 to the ".. + "yl_speak_up-NPC with the ID .\n".. + "Call: [grant|revoke] \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".. + "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 ".. + ": " + -- 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.." " + 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] ") + 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.." " + yl_speak_up.npc_priv_table[n_id] = nil + end + minetest.chat_send_player(pname, text..".") + yl_speak_up.npc_privs_store() + end +}) diff --git a/init.lua b/init.lua index 9b269a2..044e269 100644 --- a/init.lua +++ b/init.lua @@ -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 diff --git a/privs.lua b/privs.lua index a494a3f..11b7958 100644 --- a/privs.lua +++ b/privs.lua @@ -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 to the ".. - "yl_speak_up-NPC with the ID .\n".. - "Call: [grant|revoke] \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".. - "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 ".. - ": " - -- 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.." " - 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] ") - 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.." " - 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