From 300d557a802d7630d3707011a44b4b2378162afe Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 19 Jun 2022 20:34:09 +0200 Subject: [PATCH] moved chat command npc_talk_generic into own file --- add_generic_dialogs.lua | 61 ++---------------------------------- command_npc_talk_generic.lua | 59 ++++++++++++++++++++++++++++++++++ init.lua | 2 ++ 3 files changed, 63 insertions(+), 59 deletions(-) create mode 100644 command_npc_talk_generic.lua diff --git a/add_generic_dialogs.lua b/add_generic_dialogs.lua index 59cd1d2..03949d4 100644 --- a/add_generic_dialogs.lua +++ b/add_generic_dialogs.lua @@ -1,3 +1,5 @@ +-- the command to add new generic dialogs can be found in command_npc_talk_generic.lua + -- this table holds all generic dialogs with indices -- generic_dialog[n_id][d_id_n_id] yl_speak_up.generic_dialogs = {} @@ -432,65 +434,6 @@ yl_speak_up.add_generic_dialogs = function(dialog, current_n_id, player) 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_generic', { - description = "Lists, add or removes the dialogs of NPC as generic dialogs.\n".. - "Call: [list|add|remove|reload] []", - privs = {privs = true}, - func = function(pname, param) - if(not(param) or param == "") then - minetest.chat_send_player(pname, "Usage: [list|add|remove|reload] ") - return - end - local parts = string.split(param, " ") - local s = yl_speak_up.modstorage:get_string("generic_npc_list") or "" - local generic_npc_list = string.split(s, " ") - if((parts[1] == "add" or parts[1] == "remove") and #parts == 2) then - local do_reload = false - local i = table.indexof(generic_npc_list, parts[2]) - if(parts[1] == "add" and i == -1) then - local n_id = parts[2] - table.insert(generic_npc_list, n_id) - local dialog = yl_speak_up.load_dialog(n_id, false) - local res = yl_speak_up.check_and_add_as_generic_dialog(dialog, n_id) - minetest.chat_send_player(pname, "Adding NPC "..tostring(n_id).."..: "..tostring(res)) - do_reload = true - elseif(parts[1] == "remove" and i ~= -1) then - table.remove(generic_npc_list, i) - minetest.chat_send_player(pname, "Removing NPC "..tostring(parts[2]).." ..") - do_reload = true - yl_speak_up.generic_dialogs[parts[2]] = nil - yl_speak_up.generic_dialog_conditions[parts[2]] = nil - elseif(parts[1] == "reload") then - do_reload = true - end - -- actually reload the NPC list - if(do_reload) then - -- store the updated version - yl_speak_up.modstorage:set_string("generic_npc_list", - table.concat(generic_npc_list, " ")) - yl_speak_up.load_generic_dialogs() - end - elseif(parts[1] ~= "list" and parts[1] ~= "reload") then - minetest.chat_send_player(pname, "Usage: [list|add|remove|reload] ") - return - end - local liste = {} - for n_id, v in pairs(yl_speak_up.generic_dialogs) do - table.insert(liste, n_id) - end - if(#liste < 1) then - minetest.chat_send_player(pname, "No NPC provides generic dialogs.") - else - minetest.chat_send_player(pname, "These NPC provide generic dialogs: ".. - table.concat(liste, ", ")..".") - end - return - end -}) - - yl_speak_up.load_generic_dialogs = function() yl_speak_up.generic_dialogs = {} yl_speak_up.generic_dialog_conditions = {} diff --git a/command_npc_talk_generic.lua b/command_npc_talk_generic.lua new file mode 100644 index 0000000..eed17ae --- /dev/null +++ b/command_npc_talk_generic.lua @@ -0,0 +1,59 @@ +-- this is used by add_generic_dialogs.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_generic', { + description = "Lists, add or removes the dialogs of NPC as generic dialogs.\n".. + "Call: [list|add|remove|reload] []", + privs = {privs = true}, + func = function(pname, param) + if(not(param) or param == "") then + minetest.chat_send_player(pname, "Usage: [list|add|remove|reload] ") + return + end + local parts = string.split(param, " ") + local s = yl_speak_up.modstorage:get_string("generic_npc_list") or "" + local generic_npc_list = string.split(s, " ") + if((parts[1] == "add" or parts[1] == "remove") and #parts == 2) then + local do_reload = false + local i = table.indexof(generic_npc_list, parts[2]) + if(parts[1] == "add" and i == -1) then + local n_id = parts[2] + table.insert(generic_npc_list, n_id) + local dialog = yl_speak_up.load_dialog(n_id, false) + local res = yl_speak_up.check_and_add_as_generic_dialog(dialog, n_id) + minetest.chat_send_player(pname, "Adding NPC "..tostring(n_id).."..: "..tostring(res)) + do_reload = true + elseif(parts[1] == "remove" and i ~= -1) then + table.remove(generic_npc_list, i) + minetest.chat_send_player(pname, "Removing NPC "..tostring(parts[2]).." ..") + do_reload = true + yl_speak_up.generic_dialogs[parts[2]] = nil + yl_speak_up.generic_dialog_conditions[parts[2]] = nil + elseif(parts[1] == "reload") then + do_reload = true + end + -- actually reload the NPC list + if(do_reload) then + -- store the updated version + yl_speak_up.modstorage:set_string("generic_npc_list", + table.concat(generic_npc_list, " ")) + yl_speak_up.load_generic_dialogs() + end + elseif(parts[1] ~= "list" and parts[1] ~= "reload") then + minetest.chat_send_player(pname, "Usage: [list|add|remove|reload] ") + return + end + local liste = {} + for n_id, v in pairs(yl_speak_up.generic_dialogs) do + table.insert(liste, n_id) + end + if(#liste < 1) then + minetest.chat_send_player(pname, "No NPC provides generic dialogs.") + else + minetest.chat_send_player(pname, "These NPC provide generic dialogs: ".. + table.concat(liste, ", ")..".") + end + return + end +}) diff --git a/init.lua b/init.lua index 368d508..939732c 100644 --- a/init.lua +++ b/init.lua @@ -32,6 +32,8 @@ dofile(modpath .. "config.lua") dofile(modpath .. "privs.lua") -- add generic dialogs dofile(modpath .. "add_generic_dialogs.lua") +-- the actual command to add or remove generic npc dialogs +dofile(modpath .. "command_npc_talk_generic.lua") -- react to right-click etc. dofile(modpath .. "interface_mobs_api.lua") -- handle on_player_receive_fields and showing of formspecs