Skip to content

Commit

Permalink
Update the timer in the formspec in real-time (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Panquesito7 authored Oct 24, 2023
1 parent c692fa2 commit cf6a550
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
7 changes: 6 additions & 1 deletion commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
26 changes: 24 additions & 2 deletions functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 5 additions & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf6a550

Please sign in to comment.