moved chat command npc_talk_generic into own file

This commit is contained in:
Sokomine 2022-06-19 20:34:09 +02:00
parent 187416396b
commit 300d557a80
3 changed files with 63 additions and 59 deletions

View File

@ -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 <n_id> as generic dialogs.\n"..
"Call: [list|add|remove|reload] [<n_id>]",
privs = {privs = true},
func = function(pname, param)
if(not(param) or param == "") then
minetest.chat_send_player(pname, "Usage: [list|add|remove|reload] <n_id>")
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] <n_id>")
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 = {}

View File

@ -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 <n_id> as generic dialogs.\n"..
"Call: [list|add|remove|reload] [<n_id>]",
privs = {privs = true},
func = function(pname, param)
if(not(param) or param == "") then
minetest.chat_send_player(pname, "Usage: [list|add|remove|reload] <n_id>")
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] <n_id>")
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
})

View File

@ -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