diff --git a/api.lua b/api.lua index e6fe90e..1ad2bde 100644 --- a/api.lua +++ b/api.lua @@ -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 diff --git a/api.md b/api.md index e243a8a..7d7542f 100644 --- a/api.md +++ b/api.md @@ -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 --- diff --git a/internal.lua b/internal.lua index 65cfbe6..8bf7c09 100644 --- a/internal.lua +++ b/internal.lua @@ -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: