From 89c0c1f5a14b32fd1463fa99c6fcb960af1db4de Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 19 Sep 2024 09:13:19 +0200 Subject: [PATCH] Clean up seat state after node destruct --- handlers/animations.lua | 18 +++++++++++++++++- src/nodes.lua | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/handlers/animations.lua b/handlers/animations.lua index a56be2e..01c4e43 100644 --- a/handlers/animations.lua +++ b/handlers/animations.lua @@ -69,7 +69,7 @@ function xdecor.sit(pos, node, clicker, pointed_thing) player_api.player_attached[player_name] = true player_api.set_animation(clicker, "sit") sitting[player_name] = table.copy(pos) - seats_occupied[hash] = true + seats_occupied[hash] = player_name clicker:set_pos(pos) if node.param2 == 0 then @@ -103,6 +103,22 @@ function xdecor.sit_dig(pos, digger) return true 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 -- or moved away from the seat minetest.register_globalstep(function(dtime) diff --git a/src/nodes.lua b/src/nodes.lua index 384f105..1daacb1 100644 --- a/src/nodes.lua +++ b/src/nodes.lua @@ -212,6 +212,7 @@ xdecor.register("chair", { {3, 6, 3, 10, 2, 8} }), can_dig = xdecor.sit_dig, + after_destruct = xdecor.sit_destruct, on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) xdecor.sit(pos, node, clicker, pointed_thing) return itemstack @@ -324,6 +325,7 @@ xdecor.register("cushion", { on_place = minetest.rotate_node, node_box = xdecor.nodebox.slab_y(0.5), can_dig = xdecor.sit_dig, + after_destruct = xdecor.sit_destruct, on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) xdecor.sit(pos, node, clicker, pointed_thing) return itemstack