Clean up seat state after node destruct

This commit is contained in:
Wuzzy 2024-09-19 09:13:19 +02:00
parent 1ad835bd53
commit 89c0c1f5a1
2 changed files with 19 additions and 1 deletions

View File

@ -69,7 +69,7 @@ function xdecor.sit(pos, node, clicker, pointed_thing)
player_api.player_attached[player_name] = true player_api.player_attached[player_name] = true
player_api.set_animation(clicker, "sit") player_api.set_animation(clicker, "sit")
sitting[player_name] = table.copy(pos) sitting[player_name] = table.copy(pos)
seats_occupied[hash] = true seats_occupied[hash] = player_name
clicker:set_pos(pos) clicker:set_pos(pos)
if node.param2 == 0 then if node.param2 == 0 then
@ -103,6 +103,22 @@ function xdecor.sit_dig(pos, digger)
return true return true
end end
-- To be called when a seat (sittable node) got destroyed
-- to clean up state. Precisely, this should be used
-- as the `after_destruct` handler.
function xdecor.sit_destruct(pos)
local hash = minetest.hash_node_position(pos)
local occupier = seats_occupied[hash]
if occupier then
local player = minetest.get_player_by_name(occupier)
if player then
stand_up(player)
end
seats_occupied[hash] = nil
sitting[occupied] = nil
end
end
-- Automatically cause players to stand up if they pressed a control -- Automatically cause players to stand up if they pressed a control
-- or moved away from the seat -- or moved away from the seat
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)

View File

@ -212,6 +212,7 @@ xdecor.register("chair", {
{3, 6, 3, 10, 2, 8} {3, 6, 3, 10, 2, 8}
}), }),
can_dig = xdecor.sit_dig, can_dig = xdecor.sit_dig,
after_destruct = xdecor.sit_destruct,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
xdecor.sit(pos, node, clicker, pointed_thing) xdecor.sit(pos, node, clicker, pointed_thing)
return itemstack return itemstack
@ -324,6 +325,7 @@ xdecor.register("cushion", {
on_place = minetest.rotate_node, on_place = minetest.rotate_node,
node_box = xdecor.nodebox.slab_y(0.5), node_box = xdecor.nodebox.slab_y(0.5),
can_dig = xdecor.sit_dig, can_dig = xdecor.sit_dig,
after_destruct = xdecor.sit_destruct,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
xdecor.sit(pos, node, clicker, pointed_thing) xdecor.sit(pos, node, clicker, pointed_thing)
return itemstack return itemstack