From ce4909424d31cd00c0e07b17ecb25852c96f821c Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sat, 22 Jul 2023 11:13:29 +0200 Subject: [PATCH] allow to teleport to npc on the npc list --- fs_npc_list.lua | 54 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/fs_npc_list.lua b/fs_npc_list.lua index e99ec7d..48797e6 100644 --- a/fs_npc_list.lua +++ b/fs_npc_list.lua @@ -151,6 +151,39 @@ end -- allow to sort the npc list, display more info on one NPC etc. yl_speak_up.input_show_npc_list = function(player, formname, fields) local pname = player:get_player_name() + -- teleport to NPC + if(fields.teleport + and fields.selected_id + and yl_speak_up.cache_npc_list_per_player[pname] + and minetest.check_player_privs(pname, {teleport=true})) then + local id = tonumber(fields.selected_id) + if(not(id) or id < 0 + or not(yl_speak_up.npc_list[id]) + or table.indexof(yl_speak_up.cache_npc_list_per_player[pname], id) < 1) then + minetest.chat_send_player(pname, "Sorry. Cannot find that NPC.") + return + end + + -- try cached position + local pos = yl_speak_up.npc_list[id].pos + local obj = yl_speak_up.npc_list_objects[id] + if(obj) then + pos = obj:get_pos() + end + if(not(pos) or not(pos.x) or not(pos.y) or not(pos.z)) then + pos = yl_speak_up.npc_list[id].pos + end + if(not(pos) or not(pos.x) or not(pos.y) or not(pos.z)) then + minetest.chat_send_player(pname, "Sorry. Cannot find position of that NPC.") + return + end + player:set_pos(pos) + minetest.chat_send_player(pname, "Teleporting to NPC with ID ".. + tostring(fields.selected_id)..': '.. + tostring(yl_speak_up.npc_list[id].name)..'.') + return + end + -- sort by column or select an NPC if(fields.show_npc_list) then local selected = minetest.explode_table_event(fields.show_npc_list) -- sort by column @@ -277,13 +310,7 @@ yl_speak_up.get_fs_show_npc_list = function(pname, selected_row) end local formspec_start = 'size[18,14.7]'.. --- 'button[0.5,11.1;17,0.8;', --- back_link, --- ']'.. 'label[4.5,0.5;List of all NPC (that you can edit)]'.. --- 'button[0.5,0.1;3,0.8;', --- log_type_switch, --- ']', 'tablecolumns[' .. 'color;text,align=right;'.. -- the ID 'color;text,align=center;'.. -- is the NPC a generic one? @@ -336,7 +363,15 @@ yl_speak_up.get_fs_show_npc_list = function(pname, selected_row) if(data.created_at and data.created_at ~= "") then first_seen_at = minetest.formspec_escape(os.date("%m/%d/%y", data.created_at)) end --- TODO edit, visit button + -- allow those with teleport priv to easily visit their NPC + local teleport_button = '' + if(minetest.check_player_privs(pname, {teleport=true})) then + -- the ID of the NPC we want to visit is hidden in a field; this is unsafe, + -- but the actual check needs to happen when the teleport button is pressed + -- anyway + teleport_button = 'field[40,40;0,0;selected_id;;'..tostring(k)..']'.. + 'button_exit[12.1,1.8;5,0.6;teleport;Teleport to this NPC]' + end info_current_row = 'container[0.1,11.2]'.. 'label[0.1,0.0;Name, Desc:]'.. @@ -344,8 +379,8 @@ yl_speak_up.get_fs_show_npc_list = function(pname, selected_row) 'label[0.1,0.5;Typ:]'.. 'label[3.0,0.5;'.. minetest.formspec_escape(tostring(data.typ or '- ? -'))..']'.. - 'label[11.1,0.5;First seen at:]'.. - 'label[13.4,0.5;'.. + 'label[12.1,0.5;First seen at:]'.. + 'label[14.4,0.5;'.. first_seen_at..']'.. 'label[0.1,1.0;Can be edited by:]'.. 'label[3.0,1.0;'.. @@ -355,6 +390,7 @@ yl_speak_up.get_fs_show_npc_list = function(pname, selected_row) minetest.formspec_escape(table.concat(priv_list, ', '))..']'.. 'label[0.1,2.0;Properties:]'.. prop_text.. + teleport_button.. 'container_end[]' end else