metadata support

This commit is contained in:
BuckarooBanzay 2023-06-24 12:19:56 +02:00
parent df73dbf761
commit 93dc709a3b
2 changed files with 31 additions and 5 deletions

View File

@ -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`

View File

@ -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()