Skip to content

Commit

Permalink
api function to add conditions to canPlayerAddArea
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tour-ist committed Mar 14, 2024
1 parent c044d49 commit b8e7567
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
19 changes: 18 additions & 1 deletion internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b8e7567

Please sign in to comment.