Browse Source

Add callbacks for area operations

pull/36/head
sofar 1 year ago
parent
commit
1bbb997c7a
No account linked to committer's email address
4 changed files with 36 additions and 4 deletions
  1. +18
    -1
      api.lua
  2. +4
    -1
      api.md
  3. +14
    -1
      internal.lua
  4. +0
    -1
      settings.lua

+ 18
- 1
api.lua View File

@@ -1,5 +1,23 @@
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.
function areas:registerHudHandler(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.
return true
end


+ 4
- 1
api.md View File

@@ -4,7 +4,10 @@ Areas mod API
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

+ 14
- 1
internal.lua View File

@@ -93,6 +93,11 @@ function areas:add(owner, name, pos1, pos2, parent)
owner = owner,
parent = parent
}

for i=1, #areas.registered_on_adds do
areas.registered_on_adds[i](id, self.areas[id])
end

-- Add to AreaStore
if self.store then
local sid = self.store:insert_area(pos1, pos2, tostring(id))
@@ -125,6 +130,10 @@ function areas:remove(id, recurse)
end
end

for i=1, #areas.registered_on_removes do
areas.registered_on_removes[i](id)
end

-- Remove main entry
self.areas[id] = nil

@@ -140,6 +149,11 @@ function areas:move(id, area, pos1, pos2)
area.pos1 = pos1
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
self.store:remove_area(areas.store_ids[id])
local sid = self.store:insert_area(pos1, pos2, tostring(id))
@@ -289,4 +303,3 @@ function areas:isAreaOwner(id, name)
end
return false
end


+ 0
- 1
settings.lua View File

@@ -40,4 +40,3 @@ setting("number", "self_protection_max_areas_high", 32)

-- legacy_table (owner_defs) compatibility. Untested and has known issues.
setting("boolean", "legacy_table", false)


Loading…
Cancel
Save