From 0cf4fb6bcd3030ad85c6445e264da4e28ded9c04 Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Sat, 30 Sep 2023 07:48:50 +0100 Subject: [PATCH] keep up to date with default beds fixes (thanks appgurueu) --- functions.lua | 54 +++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/functions.lua b/functions.lua index 321864a..bd05171 100644 --- a/functions.lua +++ b/functions.lua @@ -236,6 +236,35 @@ function beds.skip_night() end +local update_scheduled = false + +local function schedule_update() + + if update_scheduled then + -- there already is an update scheduled; don't schedule more to prevent races + return + end + + update_scheduled = true + + minetest.after(2, function() + + update_scheduled = false + + if not is_sp then + update_formspecs(is_night_skip_enabled()) + end + + if is_night_skip_enabled() then + + -- skip the night and let all players stand up + beds.skip_night() + beds.kick_players() + end + end) +end + + function beds.on_rightclick(pos, player) local name = player:get_player_name() @@ -267,20 +296,8 @@ function beds.on_rightclick(pos, player) update_formspecs(false) end - -- skip the night and let all players stand up if check_in_beds() then - - minetest.after(2, function() - - if not is_sp then - update_formspecs(is_night_skip_enabled()) - end - - if is_night_skip_enabled() then - beds.skip_night() - beds.kick_players() - end - end) + schedule_update() end end @@ -340,16 +357,7 @@ minetest.register_on_leaveplayer(function(player) beds.player[name] = nil if check_in_beds() then - - minetest.after(2, function() - - update_formspecs(is_night_skip_enabled()) - - if is_night_skip_enabled() then - beds.skip_night() - beds.kick_players() - end - end) + schedule_update() end end)