Fix #57 by using core.hash_node_position for reliable indexing

The previously used minetest.serialize function may provide different
results for the same table in certain circumstances, thus it is not
suitable for generating strings to index tables with.
This commit is contained in:
Gabriel Pérez-Cerezo 2021-03-26 23:02:37 +01:00 committed by Linus Jahn
parent 662be1a11f
commit b424bc3499
4 changed files with 10 additions and 10 deletions

@ -89,8 +89,8 @@ function drawers.drawer_on_destruct(pos)
drawers.remove_visuals(pos) drawers.remove_visuals(pos)
-- clean up visual cache -- clean up visual cache
if drawers.drawer_visuals[core.serialize(pos)] then if drawers.drawer_visuals[core.hash_node_position(pos)] then
drawers.drawer_visuals[core.serialize(pos)] = nil drawers.drawer_visuals[core.hash_node_position(pos)] = nil
end end
end end
@ -193,7 +193,7 @@ end
Inserts an incoming stack into a drawer and uses all slots. Inserts an incoming stack into a drawer and uses all slots.
]] ]]
function drawers.drawer_insert_object_from_tube(pos, node, stack, direction) function drawers.drawer_insert_object_from_tube(pos, node, stack, direction)
local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)] local drawer_visuals = drawers.drawer_visuals[core.hash_node_position(pos)]
if not drawer_visuals then if not drawer_visuals then
return stack return stack
end end
@ -231,7 +231,7 @@ end
Returns whether a stack can be (partially) inserted to any slot of a drawer. Returns whether a stack can be (partially) inserted to any slot of a drawer.
]] ]]
function drawers.drawer_can_insert_stack_from_tube(pos, node, stack, direction) function drawers.drawer_can_insert_stack_from_tube(pos, node, stack, direction)
local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)] local drawer_visuals = drawers.drawer_visuals[core.hash_node_position(pos)]
if not drawer_visuals then if not drawer_visuals then
return false return false
end end
@ -245,7 +245,7 @@ function drawers.drawer_can_insert_stack_from_tube(pos, node, stack, direction)
end end
function drawers.drawer_take_item(pos, itemstack) function drawers.drawer_take_item(pos, itemstack)
local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)] local drawer_visuals = drawers.drawer_visuals[core.hash_node_position(pos)]
if not drawer_visuals then if not drawer_visuals then
return ItemStack("") return ItemStack("")

@ -274,7 +274,7 @@ local function controller_insert_to_drawers(pos, stack)
-- will put the items in the drawer -- will put the items in the drawer
if content.name == stack:get_name() and if content.name == stack:get_name() and
content.count < content.maxCount and content.count < content.maxCount and
drawers.drawer_visuals[core.serialize(drawer_pos)] then drawers.drawer_visuals[core.hash_node_position(drawer_pos)] then
return drawers.drawer_insert_object(drawer_pos, stack, visualid) return drawers.drawer_insert_object(drawer_pos, stack, visualid)
end end
elseif drawer_net_index["empty"] then elseif drawer_net_index["empty"] then
@ -284,7 +284,7 @@ local function controller_insert_to_drawers(pos, stack)
-- If the drawer is still empty and the drawer entity is loaded, we will -- If the drawer is still empty and the drawer entity is loaded, we will
-- put the items in the drawer -- put the items in the drawer
if content.name == "" and drawers.drawer_visuals[core.serialize(drawer_pos)] then if content.name == "" and drawers.drawer_visuals[core.hash_node_position(drawer_pos)] then
local leftover = drawers.drawer_insert_object(drawer_pos, stack, visualid) local leftover = drawers.drawer_insert_object(drawer_pos, stack, visualid)
-- Add the item to the drawers table index and set the empty one to nil -- Add the item to the drawers table index and set the empty one to nil

@ -215,7 +215,7 @@ end
visualid can be: "", "1", "2", ... or 1, 2, ... visualid can be: "", "1", "2", ... or 1, 2, ...
]] ]]
function drawers.get_visual(pos, visualid) function drawers.get_visual(pos, visualid)
local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)] local drawer_visuals = drawers.drawer_visuals[core.hash_node_position(pos)]
if not drawer_visuals then if not drawer_visuals then
return nil return nil
end end
@ -256,7 +256,7 @@ function drawers.update_drawer_upgrades(pos)
stackMaxFactor = stackMaxFactor / drawerType stackMaxFactor = stackMaxFactor / drawerType
-- set the new stack max factor in all visuals -- set the new stack max factor in all visuals
local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)] local drawer_visuals = drawers.drawer_visuals[core.hash_node_position(pos)]
if not drawer_visuals then return end if not drawer_visuals then return end
for _,visual in pairs(drawer_visuals) do for _,visual in pairs(drawer_visuals) do

@ -89,7 +89,7 @@ core.register_entity("drawers:visual", {
-- PLEASE contact me, if this is wrong -- PLEASE contact me, if this is wrong
local vId = self.visualId local vId = self.visualId
if vId == "" then vId = 1 end if vId == "" then vId = 1 end
local posstr = core.serialize(self.drawer_pos) local posstr = core.hash_node_position(self.drawer_pos)
if not drawers.drawer_visuals[posstr] then if not drawers.drawer_visuals[posstr] then
drawers.drawer_visuals[posstr] = {[vId] = self} drawers.drawer_visuals[posstr] = {[vId] = self}
else else