Add callbacks for area operations

This commit is contained in:
sofar 2019-04-01 20:28:50 -07:00 committed by GitHub
parent 09c030352f
commit 1bbb997c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 4 deletions

19
api.lua
View File

@ -1,5 +1,23 @@
local hudHandlers = {} local hudHandlers = {}
areas.registered_on_adds = {}
areas.registered_on_removes = {}
areas.registered_on_moves = {}
function areas:registerOnAdd(func)
table.insert(areas.registered_on_adds, func)
end
function areas:registerOnRemove(func)
table.insert(areas.registered_on_removes, func)
end
function areas:registerOnMove(func)
table.insert(areas.registered_on_moves, func)
end
--- Adds a function as a HUD handler, it will be able to add items to the Areas HUD element. --- Adds a function as a HUD handler, it will be able to add items to the Areas HUD element.
function areas:registerHudHandler(handler) function areas:registerHudHandler(handler)
table.insert(hudHandlers, handler) table.insert(hudHandlers, handler)
@ -141,4 +159,3 @@ function areas:canInteractInArea(pos1, pos2, name, allow_open)
-- intersecting areas and they are all owned by the player. -- intersecting areas and they are all owned by the player.
return true return true
end end

5
api.md
View File

@ -4,7 +4,10 @@ Areas mod API
API list 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:registerOnAdd(func(id, area))`
* `areas:registerOnRemove(func(id))`
* `areas:registerOnMove(func(id, area, pos1, pos2))`
HUD HUD

View File

@ -93,6 +93,11 @@ function areas:add(owner, name, pos1, pos2, parent)
owner = owner, owner = owner,
parent = parent parent = parent
} }
for i=1, #areas.registered_on_adds do
areas.registered_on_adds[i](id, self.areas[id])
end
-- Add to AreaStore -- Add to AreaStore
if self.store then if self.store then
local sid = self.store:insert_area(pos1, pos2, tostring(id)) local sid = self.store:insert_area(pos1, pos2, tostring(id))
@ -125,6 +130,10 @@ function areas:remove(id, recurse)
end end
end end
for i=1, #areas.registered_on_removes do
areas.registered_on_removes[i](id)
end
-- Remove main entry -- Remove main entry
self.areas[id] = nil self.areas[id] = nil
@ -140,6 +149,11 @@ function areas:move(id, area, pos1, pos2)
area.pos1 = pos1 area.pos1 = pos1
area.pos2 = pos2 area.pos2 = pos2
for i=1, #areas.registered_on_moves do
areas.registered_on_moves[i](id, area, pos1, pos2)
end
if self.store then if self.store then
self.store:remove_area(areas.store_ids[id]) self.store:remove_area(areas.store_ids[id])
local sid = self.store:insert_area(pos1, pos2, tostring(id)) local sid = self.store:insert_area(pos1, pos2, tostring(id))
@ -289,4 +303,3 @@ function areas:isAreaOwner(id, name)
end end
return false return false
end end

View File

@ -40,4 +40,3 @@ setting("number", "self_protection_max_areas_high", 32)
-- legacy_table (owner_defs) compatibility. Untested and has known issues. -- legacy_table (owner_defs) compatibility. Untested and has known issues.
setting("boolean", "legacy_table", false) setting("boolean", "legacy_table", false)