From 93dc709a3b1a862c15945d700f442bac3e0d5fe2 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Sat, 24 Jun 2023 12:19:56 +0200 Subject: [PATCH] metadata support --- readme.md | 6 ++++-- serialize.lua | 30 +++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index abcd02d..bf9aa04 100644 --- a/readme.md +++ b/readme.md @@ -32,12 +32,14 @@ Create a template for frequent reuse * Right-click one of corners to create a placement-tool for the template * Place as needed +# Portability + +The placement tool can be shared across worlds if the nodes are available there. + # Limitations The schematic data is stored in the tool and may not scale well beyond a certain size -Does not handle metadata currently - # Licenses * Code: `MIT` diff --git a/serialize.lua b/serialize.lua index 209c882..f3266d5 100644 --- a/serialize.lua +++ b/serialize.lua @@ -54,16 +54,32 @@ function pick_and_place.serialize(pos1, pos2) nodeid_mapping[nodeid] = name end - -- TODO: metadata + -- store metadata + local nodes_with_meta = minetest.find_nodes_with_meta(pos1, pos2) + for _, pos in ipairs(nodes_with_meta) do + local rel_pos = vector.subtract(pos, pos1) + local meta = minetest.get_meta(pos) + local meta_table = meta:to_table() - local size = vector.add(vector.subtract(pos2, pos1), 1) + -- Convert metadata item stacks to item strings + for _, invlist in pairs(meta_table.inventory) do + for index = 1, #invlist do + local itemstack = invlist[index] + if itemstack.to_string then + invlist[index] = itemstack:to_string() + end + end + end + + metadata[minetest.pos_to_string(rel_pos)] = meta_table + end local data = { version = 1, mapdata = table.concat(mapdata), metadata = metadata, nodeid_mapping = nodeid_mapping, - size = size + size = vector.add(vector.subtract(pos2, pos1), 1) } local serialized_data = minetest.serialize(data) @@ -125,6 +141,14 @@ function pick_and_place.deserialize(pos1, encoded_data) end end + -- metadata + for pos_str, meta_table in pairs(data.metadata) do + local pos = minetest.string_to_pos(pos_str) + local abs_pos = vector.add(pos1, pos) + local meta = minetest.get_meta(abs_pos) + meta:from_table(meta_table) + end + manip:set_data(node_data) manip:set_param2_data(param2) manip:write_to_map()