add pick_and_place.register_on_deserialize

This commit is contained in:
BuckarooBanzay 2024-03-15 08:04:00 +01:00
parent 2826392927
commit e486a1ba64
2 changed files with 27 additions and 0 deletions

View File

@ -57,6 +57,21 @@ If you pick-and-place a replacement node the default node-placement is replaced
Grid-snapping can be enabled with `/pnp_snap on` while holding a place-tool.
Disabling it can be done with `/pnp_snap off`
# Modding api
## pick_and_place.register_on_deserialize(fn)
register a function callback for after deserialization
```lua
local mynodeid = minetest.get_content_id("my:node")
pick_and_place.register_on_deserialize(function(pos1, pos2, nodeids)
if nodeids[mynodeid] then
-- nodeid matches, do something after the schematic has been placed into the world
end
end)
```
# Portability
The placement tool can be shared across worlds if the nodes are available there.

View File

@ -51,6 +51,8 @@ function pick_and_place.serialize(pos1, pos2)
}
end
-- table: fn(pos1, pos2, node_ids)
local deserialization_callbacks = {}
function pick_and_place.deserialize(pos1, schematic, disable_replacements)
local pos2 = vector.add(pos1, vector.subtract(schematic.size, 1))
@ -61,6 +63,7 @@ function pick_and_place.deserialize(pos1, schematic, disable_replacements)
local node_data = manip:get_data()
local param2 = manip:get_param2_data()
local node_ids = {}
local ctx = {}
local j = 1
@ -69,6 +72,7 @@ function pick_and_place.deserialize(pos1, schematic, disable_replacements)
for x=pos1.x,pos2.x do
local i = area:index(x,y,z)
local nodeid = schematic.node_id_data[j]
node_ids[nodeid] = true
if nodeid == replacement_cid and not disable_replacements then
-- replacement placement
@ -107,5 +111,13 @@ function pick_and_place.deserialize(pos1, schematic, disable_replacements)
manip:set_param2_data(param2)
manip:write_to_map()
for _, fn in ipairs(deserialization_callbacks) do
fn(pos1, pos2, node_ids)
end
return true
end
function pick_and_place.register_on_deserialize(fn)
table.insert(deserialization_callbacks, fn)
end