Skip to content

Commit d1cbd42

Browse files
HybridDogsfan5
authored andcommittedApr 24, 2019
serialize: Fix detecting empty metadata (#176)
1 parent 0aeee79 commit d1cbd42

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed
 

Diff for: ‎worldedit/serialization.lua

+24-16
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,19 @@ function worldedit.serialize(pos1, pos2)
5656

5757
worldedit.keep_loaded(pos1, pos2)
5858

59+
local get_node, get_meta, hash_node_position =
60+
minetest.get_node, minetest.get_meta, minetest.hash_node_position
61+
62+
-- Find the positions which have metadata
63+
local has_meta = {}
64+
local meta_positions = minetest.find_nodes_with_meta(pos1, pos2)
65+
for i = 1, #meta_positions do
66+
has_meta[hash_node_position(meta_positions[i])] = true
67+
end
68+
5969
local pos = {x=pos1.x, y=0, z=0}
6070
local count = 0
6171
local result = {}
62-
local get_node, get_meta = minetest.get_node, minetest.get_meta
6372
while pos.x <= pos2.x do
6473
pos.y = pos1.y
6574
while pos.y <= pos2.y do
@@ -68,20 +77,19 @@ function worldedit.serialize(pos1, pos2)
6877
local node = get_node(pos)
6978
if node.name ~= "air" and node.name ~= "ignore" then
7079
count = count + 1
71-
local meta = get_meta(pos):to_table()
72-
73-
local meta_empty = true
74-
-- Convert metadata item stacks to item strings
75-
for name, inventory in pairs(meta.inventory) do
76-
for index, stack in ipairs(inventory) do
77-
meta_empty = false
78-
inventory[index] = stack.to_string and stack:to_string() or stack
79-
end
80-
end
81-
for k in pairs(meta) do
82-
if k ~= "inventory" then
83-
meta_empty = false
84-
break
80+
81+
local meta
82+
if has_meta[hash_node_position(pos)] then
83+
meta = get_meta(pos):to_table()
84+
85+
-- Convert metadata item stacks to item strings
86+
for _, invlist in pairs(meta.inventory) do
87+
for index = 1, #invlist do
88+
local itemstack = invlist[index]
89+
if itemstack.to_string then
90+
invlist[index] = itemstack:to_string()
91+
end
92+
end
8593
end
8694
end
8795

@@ -92,7 +100,7 @@ function worldedit.serialize(pos1, pos2)
92100
name = node.name,
93101
param1 = node.param1 ~= 0 and node.param1 or nil,
94102
param2 = node.param2 ~= 0 and node.param2 or nil,
95-
meta = not meta_empty and meta or nil,
103+
meta = meta,
96104
}
97105
end
98106
pos.z = pos.z + 1

0 commit comments

Comments
 (0)
Please sign in to comment.