From e486a1ba6421c62bf3bf1a4c54cad9451d44d051 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Fri, 15 Mar 2024 08:04:00 +0100 Subject: [PATCH] add `pick_and_place.register_on_deserialize` --- readme.md | 15 +++++++++++++++ serialize.lua | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/readme.md b/readme.md index cdd3b24..8c16b8e 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/serialize.lua b/serialize.lua index 6f27dd6..5eec759 100644 --- a/serialize.lua +++ b/serialize.lua @@ -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 \ No newline at end of file