api function to add conditions to canPlayerAddArea
This commit is contained in:
parent
c044d49d21
commit
b8e7567a9c
5
api.lua
5
api.lua
|
@ -1,9 +1,14 @@
|
||||||
local hudHandlers = {}
|
local hudHandlers = {}
|
||||||
|
|
||||||
|
areas.registered_protection_conditions = {}
|
||||||
areas.registered_on_adds = {}
|
areas.registered_on_adds = {}
|
||||||
areas.registered_on_removes = {}
|
areas.registered_on_removes = {}
|
||||||
areas.registered_on_moves = {}
|
areas.registered_on_moves = {}
|
||||||
|
|
||||||
|
function areas:registerProtectionCondition(func)
|
||||||
|
table.insert(areas.registered_protection_conditions, func)
|
||||||
|
end
|
||||||
|
|
||||||
function areas:registerOnAdd(func)
|
function areas:registerOnAdd(func)
|
||||||
table.insert(areas.registered_on_adds, func)
|
table.insert(areas.registered_on_adds, func)
|
||||||
end
|
end
|
||||||
|
|
15
api.md
15
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: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:registerOnAdd(func(id, area))`
|
||||||
* `areas:registerOnRemove(func(id))`
|
* `areas:registerOnRemove(func(id))`
|
||||||
* `areas:registerOnMove(func(id, area, pos1, pos2))`
|
* `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
|
HUD
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
19
internal.lua
19
internal.lua
|
@ -211,6 +211,8 @@ end
|
||||||
-- if the area intersects other areas that they do not own.
|
-- if the area intersects other areas that they do not own.
|
||||||
-- Also checks the size of the area and if the user already
|
-- Also checks the size of the area and if the user already
|
||||||
-- has more than max_areas.
|
-- has more than max_areas.
|
||||||
|
-- checks all possible restrictions registered with
|
||||||
|
-- areas:registerProtectionCondition
|
||||||
function areas:canPlayerAddArea(pos1, pos2, name)
|
function areas:canPlayerAddArea(pos1, pos2, name)
|
||||||
local privs = minetest.get_player_privs(name)
|
local privs = minetest.get_player_privs(name)
|
||||||
if privs.areas then
|
if privs.areas then
|
||||||
|
@ -258,7 +260,22 @@ function areas:canPlayerAddArea(pos1, pos2, name)
|
||||||
area.name, id, area.owner)
|
area.name, id, area.owner)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-- Given a id returns a string in the format:
|
-- Given a id returns a string in the format:
|
||||||
|
|
Loading…
Reference in New Issue