]> git.lizzy.rs Git - worldedit.git/commitdiff
serialize: Fix detecting empty metadata (#176)
authorHybridDog <ovvv@web.de>
Wed, 24 Apr 2019 20:51:18 +0000 (22:51 +0200)
committersfan5 <sfan5@live.de>
Wed, 24 Apr 2019 20:51:18 +0000 (22:51 +0200)
worldedit/serialization.lua

index a0848e221c7c80227cd556511ff98a5b9dcae0f6..4aef556297c72ee2eb8c08976020cf4eb1385ebe 100644 (file)
@@ -56,10 +56,19 @@ function worldedit.serialize(pos1, pos2)
 \r
        worldedit.keep_loaded(pos1, pos2)\r
 \r
+       local get_node, get_meta, hash_node_position =\r
+               minetest.get_node, minetest.get_meta, minetest.hash_node_position\r
+\r
+       -- Find the positions which have metadata\r
+       local has_meta = {}\r
+       local meta_positions = minetest.find_nodes_with_meta(pos1, pos2)\r
+       for i = 1, #meta_positions do\r
+               has_meta[hash_node_position(meta_positions[i])] = true\r
+       end\r
+\r
        local pos = {x=pos1.x, y=0, z=0}\r
        local count = 0\r
        local result = {}\r
-       local get_node, get_meta = minetest.get_node, minetest.get_meta\r
        while pos.x <= pos2.x do\r
                pos.y = pos1.y\r
                while pos.y <= pos2.y do\r
@@ -68,20 +77,19 @@ function worldedit.serialize(pos1, pos2)
                                local node = get_node(pos)\r
                                if node.name ~= "air" and node.name ~= "ignore" then\r
                                        count = count + 1\r
-                                       local meta = get_meta(pos):to_table()\r
-\r
-                                       local meta_empty = true\r
-                                       -- Convert metadata item stacks to item strings\r
-                                       for name, inventory in pairs(meta.inventory) do\r
-                                               for index, stack in ipairs(inventory) do\r
-                                                       meta_empty = false\r
-                                                       inventory[index] = stack.to_string and stack:to_string() or stack\r
-                                               end\r
-                                       end\r
-                                       for k in pairs(meta) do\r
-                                               if k ~= "inventory" then\r
-                                                       meta_empty = false\r
-                                                       break\r
+\r
+                                       local meta\r
+                                       if has_meta[hash_node_position(pos)] then\r
+                                               meta = get_meta(pos):to_table()\r
+\r
+                                               -- Convert metadata item stacks to item strings\r
+                                               for _, invlist in pairs(meta.inventory) do\r
+                                                       for index = 1, #invlist do\r
+                                                               local itemstack = invlist[index]\r
+                                                               if itemstack.to_string then\r
+                                                                       invlist[index] = itemstack:to_string()\r
+                                                               end\r
+                                                       end\r
                                                end\r
                                        end\r
 \r
@@ -92,7 +100,7 @@ function worldedit.serialize(pos1, pos2)
                                                name = node.name,\r
                                                param1 = node.param1 ~= 0 and node.param1 or nil,\r
                                                param2 = node.param2 ~= 0 and node.param2 or nil,\r
-                                               meta = not meta_empty and meta or nil,\r
+                                               meta = meta,\r
                                        }\r
                                end\r
                                pos.z = pos.z + 1\r