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

This commit is contained in:
teknomunk 2024-04-11 13:09:24 +00:00
parent 09bd2647f5
commit 30b7e34b14
2 changed files with 20 additions and 4 deletions

View File

@ -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

View File

@ -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