]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit/serialization.lua
Fix //load with 0 nodes (#177)
[worldedit.git] / worldedit / serialization.lua
index 00d984d099d6e9b07eccc5b38c12180aea0d29e0..e796fc1477653a8afa0d64047c0734747e6970a5 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
@@ -144,9 +152,9 @@ local function load_schematic(value)
                                "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do\r
                        param1, param2 = tonumber(param1), tonumber(param2)\r
                        table.insert(nodes, {\r
-                               x = originx + tonumber(x),\r
-                               y = originy + tonumber(y),\r
-                               z = originz + tonumber(z),\r
+                               x = tonumber(x),\r
+                               y = tonumber(y),\r
+                               z = tonumber(z),\r
                                name = name,\r
                                param1 = param1 ~= 0 and param1 or nil,\r
                                param2 = param2 ~= 0 and param2 or nil,\r
@@ -219,6 +227,7 @@ end
 function worldedit.deserialize(origin_pos, value)\r
        local nodes = load_schematic(value)\r
        if not nodes then return nil end\r
+       if #nodes == 0 then return #nodes end\r
 \r
        local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)\r
        worldedit.keep_loaded(pos1, pos2)\r