From 1bbb997c7a170b701159bc97bc6c6f16d896ec54 Mon Sep 17 00:00:00 2001 From: sofar Date: Mon, 1 Apr 2019 20:28:50 -0700 Subject: [PATCH] Add callbacks for area operations --- api.lua | 19 ++++++++++++++++++- api.md | 5 ++++- internal.lua | 15 ++++++++++++++- settings.lua | 1 - 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/api.lua b/api.lua index 730e96d..8103b84 100644 --- a/api.lua +++ b/api.lua @@ -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 - diff --git a/api.md b/api.md index c74b4c7..e243a8a 100644 --- a/api.md +++ b/api.md @@ -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 diff --git a/internal.lua b/internal.lua index e106398..27f85ed 100644 --- a/internal.lua +++ b/internal.lua @@ -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 - diff --git a/settings.lua b/settings.lua index ffb5355..22cba53 100644 --- a/settings.lua +++ b/settings.lua @@ -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) -