From cf6a55037873eae2717c9583edf6d64e538922d3 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 24 Oct 2023 12:13:44 -0600 Subject: [PATCH] Update the timer in the formspec in real-time (#55) --- commands.lua | 7 ++++++- functions.lua | 26 ++++++++++++++++++++++++-- init.lua | 9 +++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/commands.lua b/commands.lua index 5e3949f..4852599 100644 --- a/commands.lua +++ b/commands.lua @@ -69,7 +69,12 @@ minetest.register_chatcommand("tpn", { minetest.register_chatcommand("tpf", { description = S("Show all teleport requests, made by you or to you, that are still active"), privs = {interact = true, tp = true}, - func = tp.list_requests + func = function(player) + local playername = minetest.get_player_by_name(player) + + tp.tpf_update_time[playername] = true + tp.list_requests(playername) + end }) minetest.register_chatcommand("tpr_mute", { diff --git a/functions.lua b/functions.lua index 0bfc2c3..8f54149 100644 --- a/functions.lua +++ b/functions.lua @@ -200,7 +200,9 @@ function tp.deny_request(id, own) end end -function tp.list_requests(playername) +function tp.list_requests(player) + local playername = player:get_player_name() + local sent_requests = tp.get_requests(playername, "sender") local received_requests = tp.get_requests(playername, "receiver") local area_requests = tp.get_requests(playername, "area") @@ -277,7 +279,23 @@ function tp.list_requests(playername) formspec = ("size[8,%f]label[1,0.3;%s:]"):format(math.min(y,10),S("Teleport Requests")) ..request_list_formspec end + minetest.show_formspec(playername, "teleport_request_list", formspec) + + local function update_time() + if formspec == "" or string.find(formspec, S("You have no requests.")) then + tp.tpf_update_time[player] = false + return + end + + if tp.tpf_update_time[player] then + -- TODO: find a way to edit the text only and update + -- the formspec without re-calling the function. + tp.list_requests(player) + end + end + + minetest.after(1, update_time) end minetest.register_on_player_receive_fields(function(player, formname, fields) @@ -307,8 +325,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) changes = true end end + if changes and not fields.quit then - tp.list_requests(playername) + tp.tpf_update_time[player] = true + tp.list_requests(player) + elseif fields.quit then + tp.tpf_update_time[player] = false end end) diff --git a/init.lua b/init.lua index c31aa95..b42a0ac 100644 --- a/init.lua +++ b/init.lua @@ -31,10 +31,11 @@ local S = minetest.get_translator(minetest.get_current_modname()) tp = { S = S, - tpr_list = {}, - tphr_list = {}, - tpc_list = {}, - tpn_list = {} + tpr_list = { }, + tphr_list = { }, + tpc_list = { }, + tpn_list = { }, + tpf_update_time = { } } -- Clear requests when the player leaves