From 16fb3129aeed88fa22fdc0cf7161b1a7294f1359 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Thu, 10 Nov 2022 10:58:50 +0100 Subject: [PATCH] events --- build.lua | 2 +- events.lua | 18 ++++++++++++++++++ events.spec.lua | 16 ++++++++++++++++ init.lua | 2 ++ readme.md | 9 +++++++++ remove.lua | 3 ++- 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 events.lua create mode 100644 events.spec.lua diff --git a/build.lua b/build.lua index 31c4d4f..078f72c 100644 --- a/build.lua +++ b/build.lua @@ -151,7 +151,7 @@ function building_lib.build(mapblock_pos, playername, building_name, rotation, c end) placement.place(placement, mapblock_pos, building_def, rotation, callback) - + building_lib.fire_event("placed", mapblock_pos, playername, building_def, rotation) return true end diff --git a/events.lua b/events.lua new file mode 100644 index 0000000..34b4f7c --- /dev/null +++ b/events.lua @@ -0,0 +1,18 @@ + +-- name -> list +local events = {} + +function building_lib.fire_event(name, ...) + for _, fn in ipairs(events[name] or {}) do + fn(...) + end +end + +function building_lib.register_on(name, fn) + local list = events[name] + if not list then + list = {} + events[name] = list + end + table.insert(list, fn) +end \ No newline at end of file diff --git a/events.spec.lua b/events.spec.lua new file mode 100644 index 0000000..0042ed6 --- /dev/null +++ b/events.spec.lua @@ -0,0 +1,16 @@ + +mtt.register("events", function(callback) + + local success = false + + building_lib.register_on("my_event", function(x,y) + if x == 1 and y == 2 then + success = true + end + end) + + building_lib.fire_event("my_event", 1, 2) + + assert(success) + callback() +end) \ No newline at end of file diff --git a/init.lua b/init.lua index b87fd92..a51ed64 100644 --- a/init.lua +++ b/init.lua @@ -19,9 +19,11 @@ dofile(MP .. "/build.lua") dofile(MP .. "/remove.lua") dofile(MP .. "/chat.lua") dofile(MP .. "/tools.lua") +dofile(MP .. "/events.lua") dofile(MP .. "/mapgen.lua") if minetest.get_modpath("mtt") and mtt.enabled then + dofile(MP .. "/events.spec.lua") dofile(MP .. "/conditions.spec.lua") dofile(MP .. "/build.spec.lua") end diff --git a/readme.md b/readme.md index eeccc81..e8153f3 100644 --- a/readme.md +++ b/readme.md @@ -84,10 +84,19 @@ building_lib.register_condition("on_flat_surface", { }) ``` +## Conditions + Built-in conditions: * `group=` checks if there is already a building with the specified groupname * `on_group=` checks if there is a building with the specified groupname below +## Events + +```lua +building_lib.register_on("placed", function(mapblock_pos, playername, building_def, rotation) end) +building_lib.register_on("removed", function(mapblock_pos, playername, building_info) end) +``` + ## Chat commands * `/building_info` diff --git a/remove.lua b/remove.lua index 23db67b..46b6fc8 100644 --- a/remove.lua +++ b/remove.lua @@ -13,7 +13,7 @@ function building_lib.can_remove(mapblock_pos) return true end -function building_lib.remove(mapblock_pos) +function building_lib.remove(mapblock_pos, playername) local success, err = building_lib.can_remove(mapblock_pos) if not success then return success ,err @@ -34,5 +34,6 @@ function building_lib.remove(mapblock_pos) end end + building_lib.fire_event("removed", mapblock_pos, playername, building_info) return true end \ No newline at end of file