controller: Try to fix bugs with corrupted drawer index

This commit is contained in:
Linus Jahn 2020-06-10 19:24:39 +02:00
parent 62b5fa51ae
commit b97e905bc2
No known key found for this signature in database
GPG Key ID: 4663231A91A1E27B

@ -70,6 +70,16 @@ local function controller_formspec(pos)
return formspec return formspec
end end
local function is_valid_drawer_index_slot(net_index, item_name)
return net_index and
net_index[item_name] and
net_index[item_name].drawer_pos and
net_index[item_name].drawer_pos.x and
net_index[item_name].drawer_pos.y and
net_index[item_name].drawer_pos.z and
net_index[item_name].visualid
end
local function controller_index_slot(pos, visualid) local function controller_index_slot(pos, visualid)
return { return {
drawer_pos = pos, drawer_pos = pos,
@ -225,18 +235,13 @@ local function controller_get_drawer_index(pos, itemstring)
local drawer_net_index = core.deserialize(meta:get_string("drawers_table_index")) local drawer_net_index = core.deserialize(meta:get_string("drawers_table_index"))
-- If the index has not been created -- If the index has not been created
if not drawer_net_index then -- If the item isn't in the index (or the index is corrupted)
if not is_valid_drawer_index_slot(drawer_net_index, itemstring) then
drawer_net_index = index_drawers(pos) drawer_net_index = index_drawers(pos)
meta:set_string("drawers_table_index", core.serialize(drawer_net_index)) meta:set_string("drawers_table_index", core.serialize(drawer_net_index))
-- If the item isn't in the index -- There is a valid entry in the index: check that the entry is still up-to-date
elseif not drawer_net_index[itemstring] then else
drawer_net_index = index_drawers(pos)
meta:set_string("drawers_table_index", core.serialize(drawer_net_index))
-- If the item is in the index but either the name that was indexed is not
-- the same as what is currently in the drawer or the drawer is full
elseif drawer_net_index[itemstring] then
local content = drawers.drawer_get_content(drawer_net_index[itemstring].drawer_pos, drawer_net_index[itemstring].visualid) local content = drawers.drawer_get_content(drawer_net_index[itemstring].drawer_pos, drawer_net_index[itemstring].visualid)
if content.name ~= itemstring or content.count >= content.maxCount then if content.name ~= itemstring or content.count >= content.maxCount then