From 492077bd48e94b5a8c92bd4264929514a6ab53d2 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Sun, 30 Dec 2018 21:10:02 +1300 Subject: [PATCH] =?UTF-8?q?Add=20teleportation=20and=20renaming=20=20?= =?UTF-8?q?=E2=80=A2=20Adds=20marker=20renaming=20=20=E2=80=A2=20Adds=20te?= =?UTF-8?q?leportation=20=20=20=20=C2=B7=20You=20can=20teleport=20with=20/?= =?UTF-8?q?tpj,=20/tpc=20or=20/teleport.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.lua | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index d2de803..d287fe3 100644 --- a/init.lua +++ b/init.lua @@ -57,6 +57,17 @@ function advmarkers.get_marker(name) return string_to_pos(storage:get_string('marker-' .. tostring(name))) end +-- Rename a marker and re-interpret the position. +function advmarkers.rename_marker(oldname, newname) + oldname, newname = tostring(oldname), tostring(newname) + local pos = advmarkers.get_marker(oldname) + if not pos or not advmarkers.set_marker(pos, newname) then return end + if oldname ~= newname then + advmarkers.delete_marker(oldname) + end + return true +end + -- Display a marker function advmarkers.display_marker(name) return advmarkers.set_hud_pos(advmarkers.get_marker(name)) @@ -111,8 +122,10 @@ local selected_name = false function advmarkers.display_formspec() local formspec = 'size[5.25,8]' .. 'label[0,0;Marker list]' .. - 'button_exit[0,7.5;2.625,0.5;display;Display]' .. - 'button[2.625,7.5;2.625,0.5;delete;Delete]' .. + 'button_exit[0,7.5;1.3125,0.5;display;Display]' .. + 'button[1.3125,7.5;1.3125,0.5;teleport;Teleport]' .. + 'button[2.625,7.5;1.3125,0.5;rename;Rename]' .. + 'button[3.9375,7.5;1.3125,0.5;delete;Delete]' .. 'textlist[0,0.75;5,6;marker;' -- Iterate over all the markers @@ -223,6 +236,63 @@ minetest.register_on_formspec_input(function(formname, fields) if not advmarkers.display_marker(name) then minetest.display_chat_message('Error displaying marker!') end + elseif fields.rename then + minetest.show_formspec('advmarkers-csm', 'size[6,3]' .. + 'label[0.35,0.25;Rename marker]' .. + 'field[0.25,1.5;6,1;new_name;New name;' .. + minetest.formspec_escape(name) .. ']' .. + 'button[0,2;3,1;cancel;Cancel]' .. + 'button[3,2;3,1;rename_confirm;Rename]') + elseif fields.rename_confirm then + if fields.new_name and #fields.new_name > 0 then + if advmarkers.rename_marker(name, fields.new_name) then + selected_name = fields.new_name + else + minetest.display_chat_message('Error renaming marker!') + end + advmarkers.display_formspec() + else + minetest.display_chat_message( + 'Please enter a new name for the marker.' + ) + end + elseif fields.teleport then + minetest.show_formspec('advmarkers-csm', 'size[6,3]' .. + 'label[0.35,0.25;' .. minetest.formspec_escape( + 'Teleport to a marker\n - ' .. name + ) .. ']' .. + 'button[0,2;2,1;cancel;Cancel]' .. + 'button_exit[2,2;1,1;teleport_tpj;/tpj]' .. + 'button_exit[3,2;1,1;teleport_tpc;/tpc]' .. + 'button_exit[4,2;2,1;teleport_confirm;/teleport]') + elseif fields.teleport_tpj then + -- Teleport with /tpj + local pos = advmarkers.get_marker(name) + if pos and minetest.localplayer then + local cpos = minetest.localplayer:get_pos() + for _, dir in ipairs({'x', 'y', 'z'}) do + local distance = pos[dir] - cpos[dir] + minetest.run_server_chatcommand('tpj', dir .. ' ' .. + tostring(distance)) + end + else + minetest.display_chat_message('Error teleporting to marker!') + end + elseif fields.teleport_confirm or fields.teleport_tpc then + -- Teleport with /teleport + local pos = advmarkers.get_marker(name) + local cmd + if fields.teleport_confirm then + cmd = 'teleport' + else + cmd = 'tpc' + end + if pos and minetest.localplayer then + minetest.run_server_chatcommand(cmd, + pos.x .. ',' .. pos.y .. ',' .. pos.z) + else + minetest.display_chat_message('Error teleporting to marker!') + end elseif fields.delete then minetest.show_formspec('advmarkers-csm', 'size[6,2]' .. 'label[0.35,0.25;Are you sure you want to delete this marker?]' ..