No-op if state is already correct

This commit is contained in:
Lars Mueller 2021-02-10 08:22:14 +01:00
parent 8cd25b2467
commit cbf778c1bf

View file

@ -60,16 +60,16 @@ local function lay_down(player, pos, bed_pos, state, skip)
-- stand up -- stand up
if state ~= nil and not state then if state ~= nil and not state then
assert(beds.player[name], "player " .. name .. " not in bed") if not beds.player[name] then
local p = beds.pos[name] or nil -- player not in bed, do nothing
return false
end
beds.bed_position[name] = nil beds.bed_position[name] = nil
-- skip here to prevent sending player specific changes (used for leaving players) -- skip here to prevent sending player specific changes (used for leaving players)
if skip then if skip then
return return
end end
if p then player:set_pos(beds.pos[name])
player:set_pos(p)
end
-- physics, eye_offset, etc -- physics, eye_offset, etc
local physics_override = beds.player[name].physics_override local physics_override = beds.player[name].physics_override
@ -102,6 +102,11 @@ local function lay_down(player, pos, bed_pos, state, skip)
return false return false
end end
if beds.player[name] then
-- player already in bed, do nothing
return false
end
beds.pos[name] = pos beds.pos[name] = pos
beds.bed_position[name] = bed_pos beds.bed_position[name] = bed_pos
beds.player[name] = {physics_override = player:get_physics_override()} beds.player[name] = {physics_override = player:get_physics_override()}
@ -275,10 +280,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local last_player_in_bed = get_player_in_bed_count() local last_player_in_bed = get_player_in_bed_count()
if fields.quit or fields.leave then if fields.quit or fields.leave then
if beds.player[player:get_player_name()] then lay_down(player, nil, nil, false)
-- make player stand up only if they were in bed
lay_down(player, nil, nil, false)
end
update_formspecs(false) update_formspecs(false)
end end