From 30b7e34b1477100a40703046d4c5c48661e380b0 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Thu, 11 Apr 2024 13:09:24 +0000 Subject: [PATCH] Modify mcl_entity_invs to add support for save/load items hooks in entities, add save/load hooks to minecarts to store item list in the minecart data and not in the entity data so that respawn doesn't destroy items --- mods/ENTITIES/mcl_entity_invs/init.lua | 13 ++++++++++--- mods/ENTITIES/mcl_minecarts/carts.lua | 11 ++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/mods/ENTITIES/mcl_entity_invs/init.lua b/mods/ENTITIES/mcl_entity_invs/init.lua index 35af491e1..601ec2019 100644 --- a/mods/ENTITIES/mcl_entity_invs/init.lua +++ b/mods/ENTITIES/mcl_entity_invs/init.lua @@ -35,7 +35,9 @@ function mcl_entity_invs.load_inv(ent,size) mcl_log("load_inv 3") inv = minetest.create_detached_inventory(ent._inv_id, inv_callbacks) inv:set_size("main", size) - if ent._items then + if ent._mcl_entity_invs_load_items then + inv:set_list("main",ent:_mcl_entity_invs_load_items()) + elseif ent._items then inv:set_list("main",ent._items) end else @@ -46,9 +48,14 @@ end function mcl_entity_invs.save_inv(ent) if ent._inv then - ent._items = {} + local items = {} for i,it in ipairs(ent._inv:get_list("main")) do - ent._items[i] = it:to_string() + items[i] = it:to_string() + end + if ent._mcl_entity_invs_save_items then + ent:_mcl_entity_invs_save_items(items) + else + ent._items = items end minetest.remove_detached_inventory(ent._inv_id) ent._inv = nil diff --git a/mods/ENTITIES/mcl_minecarts/carts.lua b/mods/ENTITIES/mcl_minecarts/carts.lua index 9d7be5b74..bd86676f8 100644 --- a/mods/ENTITIES/mcl_minecarts/carts.lua +++ b/mods/ENTITIES/mcl_minecarts/carts.lua @@ -143,6 +143,16 @@ function DEFAULT_CART_DEF:get_staticdata() return minetest.serialize({uuid = self._staticdata.uuid, seq=self._seq}) end +function DEFAULT_CART_DEF:_mcl_entity_invs_load_items() + local staticdata = self._staticdata + return staticdata.inventory or {} +end +function DEFAULT_CART_DEF:_mcl_entity_invs_save_items(items) + local staticdata = self._staticdata + print("Saving entity inventory items="..dump(items)) + staticdata.inventory = table.copy(items) +end + function DEFAULT_CART_DEF:add_node_watch(pos) local staticdata = self._staticdata local watches = staticdata.node_watches or {} @@ -246,7 +256,6 @@ function DEFAULT_CART_DEF:on_step(dtime) end mod.update_cart_orientation(self) - end function mod.kill_cart(staticdata) local pos