From 83c773cd33b5ce37ddaec522a8c503d0a6da5d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20R=C3=BChle?= Date: Sat, 2 Mar 2024 14:58:49 +0100 Subject: [PATCH] Fix invisible chests Pass chest entity initialization data to on_activate as staticdata so initialization is atomic, preventing premature deletion of chest entity by concurrent server steps. --- mods/ITEMS/mcl_chests/api.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/mods/ITEMS/mcl_chests/api.lua b/mods/ITEMS/mcl_chests/api.lua index 91069c743..c98050934 100644 --- a/mods/ITEMS/mcl_chests/api.lua +++ b/mods/ITEMS/mcl_chests/api.lua @@ -75,11 +75,14 @@ minetest.register_entity("mcl_chests:chest", { self.node_name = node_name self.sound_prefix = sound_prefix self.animation_type = animation_type - self.object:set_properties({ + local obj = self.object + obj:set_armor_groups({ immortal = 1 }) + obj:set_properties({ textures = textures, mesh = mesh_prefix .. (double and "_double" or "") .. ".b3d", }) self:set_yaw(dir) + self.players = {} end, reinitialize = function(self, node_name) @@ -102,9 +105,12 @@ minetest.register_entity("mcl_chests:chest", { return true end, - on_activate = function(self) - self.object:set_armor_groups({ immortal = 1 }) - self.players = {} + on_activate = function(self, initialization_data) + if initialization_data:find("^return") then + self:initialize(unpack(minetest.deserialize(initialization_data))) + else + minetest.log("warning", debug.traceback("[mcl_chests] on_activate called without initialization_data")) + end end, on_step = function(self, dtime) @@ -140,10 +146,14 @@ end local function create_entity(pos, node_name, textures, param2, double, sound_prefix, mesh_prefix, animation_type, dir, entity_pos) dir, entity_pos = get_entity_info(pos, param2, double, dir, entity_pos) - local obj = minetest.add_entity(entity_pos, "mcl_chests:chest") - local luaentity = obj:get_luaentity() - luaentity:initialize(pos, node_name, textures, dir, double, sound_prefix, mesh_prefix, animation_type) - return luaentity + local initialization_data = minetest.serialize({pos, node_name, textures, dir, double, sound_prefix, + mesh_prefix, animation_type}) + local obj = minetest.add_entity(entity_pos, "mcl_chests:chest", initialization_data) + if obj and obj:get_pos() then + return obj:get_luaentity() + else + minetest.log("warning", "[mcl_chests] Failed to create entity at " .. (entity_pos and minetest.pos_to_string(entity_pos, 1) or "nil")) + end end mcl_chests.create_entity = create_entity