Merge ShadowNinja's version
This commit is contained in:
Tai @ Flex 2016-12-19 16:42:46 +00:00
commit 767d8ff348
3 changed files with 61 additions and 23 deletions

23
api.lua
View File

@ -1,22 +1,17 @@
local protection_detectors = {} local hudHandlers = {}
-- Other protection mods should be able to display their protection in the hud --- Adds a function as a HUD handler, it will be able to add items to the Areas HUD element.
areas.registerHudHandler = function(handler) function areas:registerHudHandler(handler)
protection_detectors[#protection_detectors + 1] = handler table.insert(hudHandlers, handler)
end end
-- Generalized call to registered handlers to add their proeciton labels to the areas list
function areas:getRegisteredProtections(pos)
local area_list = {}
if #protection_detectors <= 0 then
return area_list
end
for idx=1, #protection_detectors do function areas:getExternalHudEntries(pos)
local func = protection_detectors[idx] local areas = {}
area_list = func(pos, area_list) for _, func in pairs(hudHandlers) do
func(pos, areas)
end end
return area_list return areas
end end
--- Returns a list of areas that include the provided position. --- Returns a list of areas that include the provided position.

46
api.md
View File

@ -44,3 +44,49 @@ Example
end end
areas.register_hud_handler(myhandler) areas.register_hud_handler(myhandler)
=======
Areas mod API
===
API list
---
* `areas.registerHudHandler(handler)` - Registers a handler to add items to the Areas HUD. See [HUD](#hud).
HUD
---
If you are making a protection mod or a similar mod that adds invisible regions
to the world, and you would like then to show up in the areas HUD element, you
can register a callback to show your areas.
HUD handler specification:
* `handler(pos, list)`
* `pos` - The position to check.
* `list` - The list of area HUD elements, this should be modified in-place.
The area list item is a table containing a list of tables with the following fields:
* `id` - An identifier for the area. This should be a unique string in the format `mod:id`.
* `name` - The name of the area.
* `owner` - The player name of the region owner, if any.
All of the fields are optional but at least one of them must be set.
### Example
local function areas_hud_handler(pos, areas)
local val = find_my_protection(pos)
if val then
table.insert(areas, {
id = "mod:"..val.id,
name = val.name,
owner = val.owner,
})
end
end
areas:registerHudHandler(areas_hud_handler)

15
hud.lua
View File

@ -14,15 +14,12 @@ minetest.register_globalstep(function(dtime)
area.open and ":open" or "")) area.open and ":open" or ""))
end end
for id, area in pairs(areas:getRegisteredProtections(pos)) do for i, area in pairs(areas:getExternalHudEntries(pos)) do
table.insert( local str = ""
areaStrings, ("%s [%s] (%s)") if area.name then str = area.name .. " " end
:format( if area.id then str = str.."["..area.id.."] " end
area.name or "", if area.owner then str = str.."("..area.owner..")" end
id , table.insert(areaStrings, str)
area.owner
)
)
end end
local areaString = "Areas:" local areaString = "Areas:"