api function to add conditions to canPlayerAddArea

This commit is contained in:
tour 2024-03-14 20:06:15 +01:00
parent c044d49d21
commit b8e7567a9c
3 changed files with 38 additions and 1 deletions

View File

@ -1,9 +1,14 @@
local hudHandlers = {}
areas.registered_protection_conditions = {}
areas.registered_on_adds = {}
areas.registered_on_removes = {}
areas.registered_on_moves = {}
function areas:registerProtectionCondition(func)
table.insert(areas.registered_protection_conditions, func)
end
function areas:registerOnAdd(func)
table.insert(areas.registered_on_adds, func)
end

15
api.md
View File

@ -5,11 +5,26 @@ API list
---
* `areas:registerHudHandler(handler)` - Registers a handler to add items to the Areas HUD. See [HUD](#hud).
* `areas:registerProtectionCondition(func(pos1, pos2, name))` -
See [Protection Conditions](#Protection-Conditions)
* `areas:registerOnAdd(func(id, area))`
* `areas:registerOnRemove(func(id))`
* `areas:registerOnMove(func(id, area, pos1, pos2))`
Protection Conditions
---
With `areas:registerProtectionCondition(func(pos1, pos2, name))`
you can register rules to control whether to allow or prohibit the creation of an area.
Return values:
* `true` Always create the area, no matter of other conditions.
Note that this includes the conditions set by this mod.
* `false, errMsg` Disable the creation of the area and return an error message.
* `nil` Enable the creation of the area, if all other callbacks return `nil` too.
HUD
---

View File

@ -211,6 +211,8 @@ end
-- if the area intersects other areas that they do not own.
-- Also checks the size of the area and if the user already
-- has more than max_areas.
-- checks all possible restrictions registered with
-- areas:registerProtectionCondition
function areas:canPlayerAddArea(pos1, pos2, name)
local privs = minetest.get_player_privs(name)
if privs.areas then
@ -258,7 +260,22 @@ function areas:canPlayerAddArea(pos1, pos2, name)
area.name, id, area.owner)
end
return true
local allowed = true
local errMsg
for i=1, #areas.registered_protection_conditions do
local res, msg = areas.registered_protection_conditions[i](pos1, pos2, name)
if res == true then
-- always allow to protect, no matter of other conditions
return true
elseif res == false then
-- there might be another callback that returns true, so we can't break here
allowed = false
-- save the first error that occurred
errMsg = errMsg or msg
end
end
return allowed, errMsg
end
-- Given a id returns a string in the format: