This commit is contained in:
BuckarooBanzay 2022-11-10 10:58:50 +01:00
parent e363dfe9f6
commit 16fb3129ae
6 changed files with 48 additions and 2 deletions

View File

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

18
events.lua Normal file
View File

@ -0,0 +1,18 @@
-- name -> list<fn>
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

16
events.spec.lua Normal file
View File

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

View File

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

View File

@ -84,10 +84,19 @@ building_lib.register_condition("on_flat_surface", {
})
```
## Conditions
Built-in conditions:
* `group=<groupname>` checks if there is already a building with the specified groupname
* `on_group=<groupname>` 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`

View File

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