From 08ac0f9c05c4623a2783749b1dee2dafa234cee6 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Tue, 19 Jun 2018 19:49:24 +0200 Subject: Check for inventory existence before serializing, and do not create it again if it already exists. --- advtrains/init.lua | 5 ++++- advtrains/wagons.lua | 36 ++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 17 deletions(-) (limited to 'advtrains') diff --git a/advtrains/init.lua b/advtrains/init.lua index 482c33d..de72ea1 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -281,7 +281,10 @@ advtrains.avt_save = function(remove_players_from_wagons) local _,proto = advtrains.get_wagon_prototype(wdata) if proto.has_inventory then local inv=minetest.get_inventory({type="detached", name="advtrains_wgn_"..id}) - wdata.ser_inv=advtrains.serialize_inventory(inv) + if inv then -- inventory is not initialized when wagon was never loaded + -- TOOD: What happens with unloading rails when they don't find the inventory? + wdata.ser_inv=advtrains.serialize_inventory(inv) + end end -- TODO apply save-keys here too end diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index 6cc0072..cb139da 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -77,23 +77,27 @@ function wagon:set_id(wid) if self.has_inventory then --to be used later - local inv=minetest.create_detached_inventory("advtrains_wgn_"..self.id, { - allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) - return invcallback(wid, player:get_player_name(), count, 0) - end, - allow_put = function(inv, listname, index, stack, player) - return invcallback(wid, player:get_player_name(), stack:get_count(), 0) - end, - allow_take = function(inv, listname, index, stack, player) - return invcallback(wid, player:get_player_name(), stack:get_count(), 0) + local inv=minetest.get_inventory({type="detached", name="advtrains_wgn_"..self.id}) + -- create inventory, if not yet created + if not inv then + inv=minetest.create_detached_inventory("advtrains_wgn_"..self.id, { + allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) + return invcallback(wid, player:get_player_name(), count, 0) + end, + allow_put = function(inv, listname, index, stack, player) + return invcallback(wid, player:get_player_name(), stack:get_count(), 0) + end, + allow_take = function(inv, listname, index, stack, player) + return invcallback(wid, player:get_player_name(), stack:get_count(), 0) + end + }) + if data.ser_inv then + advtrains.deserialize_inventory(data.ser_inv, inv) end - }) - if data.ser_inv then - advtrains.deserialize_inventory(data.ser_inv, inv) - end - if self.inventory_list_sizes then - for lst, siz in pairs(self.inventory_list_sizes) do - inv:set_size(lst, siz) + if self.inventory_list_sizes then + for lst, siz in pairs(self.inventory_list_sizes) do + inv:set_size(lst, siz) + end end end end -- cgit v1.2.3