From 45afbfe8a668106f3b9ebe7b73638e19bbed1bfa Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 07:20:23 +0000 Subject: [PATCH 01/17] add protector blocks to hud --- api.lua | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/api.lua b/api.lua index e8b9c7a..bce3713 100644 --- a/api.lua +++ b/api.lua @@ -1,7 +1,31 @@ +local get_protector_area = function(pos) + -- Add protector to the hud + local res = {} + local radius = (tonumber(minetest.setting_get("protector_radius")) or 5) + local protectors = minetest.find_nodes_in_area( + {x=pos.x -radius , y=pos.y -radius , z=pos.z -radius}, + {x=pos.x +radius , y=pos.y +radius , z=pos.z +radius}, + {"protector:protect","protector:protect2"} + ) + for _,npos in pairs(protectors) do + local node = minetest.get_node(npos) + local meta = minetest.get_meta(npos) + local nodeowner = meta:get_string("owner") + if minetest.is_protected(pos,nodeowner) then + res["protector"] = {name="("..node.name..")",owner=nodeowner} + break + end + end + return res +end +-- // + --- Returns a list of areas that include the provided position. function areas:getAreasAtPos(pos) local res = {} + --res = get_protector_area(pos) + if self.store then local a = self.store:get_areas_for_pos(pos, false, true) for store_id, store_area in pairs(a) do @@ -13,9 +37,10 @@ function areas:getAreasAtPos(pos) for id, area in pairs(self.areas) do local ap1, ap2 = area.pos1, area.pos2 if - (px >= ap1.x and px <= ap2.x) and - (py >= ap1.y and py <= ap2.y) and - (pz >= ap1.z and pz <= ap2.z) then + (px >= ap1.x and px <= ap2.x) and + (py >= ap1.y and py <= ap2.y) and + (pz >= ap1.z and pz <= ap2.z) + then res[id] = area end end From 2f802bf7809ec398f34ee984a77ab6494c55ba6f Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 07:20:26 +0000 Subject: [PATCH 02/17] change from unknown source --- internal.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal.lua b/internal.lua index bf7e92c..05a31cd 100644 --- a/internal.lua +++ b/internal.lua @@ -1,6 +1,6 @@ function areas:player_exists(name) - return minetest.get_auth_handler().get_auth(name) ~= nil + return minetest.auth_table[name] ~= nil end -- Save the areas table to a file From e1e719b1d66bbc09ec625d038c4dec5000c3bbda Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 12:13:33 +0000 Subject: [PATCH 03/17] add mod explicit name --- mod.conf | 1 + 1 file changed, 1 insertion(+) create mode 100644 mod.conf diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..0ca8ec1 --- /dev/null +++ b/mod.conf @@ -0,0 +1 @@ +name = areas From dedd0ebf6ec847b44787cada51a173f1af29e2ef Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 12:14:30 +0000 Subject: [PATCH 04/17] Add handler for other mods to advise protection * Allow other mods to register a handler to display protected areas in the hud * remove the protector mod specific code from this areas code --- api.lua | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/api.lua b/api.lua index bce3713..6442138 100644 --- a/api.lua +++ b/api.lua @@ -1,30 +1,25 @@ +local protection_detectors = {} -local get_protector_area = function(pos) - -- Add protector to the hud - local res = {} - local radius = (tonumber(minetest.setting_get("protector_radius")) or 5) - local protectors = minetest.find_nodes_in_area( - {x=pos.x -radius , y=pos.y -radius , z=pos.z -radius}, - {x=pos.x +radius , y=pos.y +radius , z=pos.z +radius}, - {"protector:protect","protector:protect2"} - ) - for _,npos in pairs(protectors) do - local node = minetest.get_node(npos) - local meta = minetest.get_meta(npos) - local nodeowner = meta:get_string("owner") - if minetest.is_protected(pos,nodeowner) then - res["protector"] = {name="("..node.name..")",owner=nodeowner} - break - end +areas.register_protector_detector = function(handler) + protection_detectors[#protection_detectors+1] = handler +end + +local detect_extra_protection = function(pos,res) + if #protection_detectors <= 0 then + return res + end + + for idx=1,#protection_detectors do + local func = protection_detectors[idx] + res = func(pos,res) end return res end --- // --- Returns a list of areas that include the provided position. function areas:getAreasAtPos(pos) local res = {} - --res = get_protector_area(pos) + res = detect_extra_protection(pos,res) if self.store then local a = self.store:get_areas_for_pos(pos, false, true) From e4ac30b7ff74432aada42bad48f82e75d8c906a4 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 12:15:59 +0000 Subject: [PATCH 05/17] support non-numeric IDs --- hud.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hud.lua b/hud.lua index 4908b92..764c2e3 100644 --- a/hud.lua +++ b/hud.lua @@ -8,7 +8,7 @@ minetest.register_globalstep(function(dtime) local pos = vector.round(player:getpos()) local areaStrings = {} for id, area in pairs(areas:getAreasAtPos(pos)) do - table.insert(areaStrings, ("%s [%u] (%s%s)") + table.insert(areaStrings, ("%s [%s] (%s%s)") :format(area.name, id, area.owner, area.open and ":open" or "")) end From 6708c966ca710f53dc2c9f071da4892745b50a97 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 12:23:46 +0000 Subject: [PATCH 06/17] adjusted handler registration name --- api.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.lua b/api.lua index 6442138..87073a5 100644 --- a/api.lua +++ b/api.lua @@ -1,6 +1,6 @@ local protection_detectors = {} -areas.register_protector_detector = function(handler) +areas.register_hud_handler = function(handler) protection_detectors[#protection_detectors+1] = handler end From 77a30f9e05332a022fdd90dd00b4dc8cb6d3cea7 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 12:32:11 +0000 Subject: [PATCH 07/17] commentary --- api.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api.lua b/api.lua index 87073a5..40ecdea 100644 --- a/api.lua +++ b/api.lua @@ -1,9 +1,11 @@ local protection_detectors = {} +-- Other protection mods should be able to display their protection in the hud areas.register_hud_handler = function(handler) protection_detectors[#protection_detectors+1] = handler end +-- Generalized call to registered handlers to add their proeciton labels to the areas list local detect_extra_protection = function(pos,res) if #protection_detectors <= 0 then return res From 70ed7e34a06d0c32fd24b245cca4795ec21349cd Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 12:33:27 +0000 Subject: [PATCH 08/17] add API documentation for modding extension --- api.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 api.md diff --git a/api.md b/api.md new file mode 100644 index 0000000..a0fcae7 --- /dev/null +++ b/api.md @@ -0,0 +1,21 @@ +# API Extension + +Adding your protections to the HUD +---------------------------------- + +If you are providing an extra protection mod to work in cunjunction with the +HUD feature of `areas`, you can register a callback to add your mod's code to +display your protection's existence. For example + + local myhandler = function(pos,arealist) + local areaowner = find_my_protections(pos) + + if areaowner then + arealist["mymodname"] = { + name = "Protection name", + owner = areaowner, + } + end + end + + areas.register_hud_handler(myhandler) From 4eda120fc1dac45e5867ccf26319d617f636cfc1 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 13:27:15 +0000 Subject: [PATCH 09/17] rename to explicit variables --- api.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api.lua b/api.lua index 40ecdea..e07785e 100644 --- a/api.lua +++ b/api.lua @@ -2,20 +2,20 @@ local protection_detectors = {} -- Other protection mods should be able to display their protection in the hud areas.register_hud_handler = function(handler) - protection_detectors[#protection_detectors+1] = handler + protection_detectors[#protection_detectors + 1] = handler end -- Generalized call to registered handlers to add their proeciton labels to the areas list -local detect_extra_protection = function(pos,res) +local detect_extra_protection = function(pos,area_list) if #protection_detectors <= 0 then - return res + return area_list end for idx=1,#protection_detectors do local func = protection_detectors[idx] - res = func(pos,res) + area_list = func(pos,area_list) end - return res + return area_list end --- Returns a list of areas that include the provided position. From 60d7a41c8cad27f052e179f5bfa7846a4b5e9f47 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Sat, 26 Nov 2016 13:48:48 +0000 Subject: [PATCH 10/17] De-couple from original code * `api.lua` create separate handler for listing registered nodes, leave the areas list clean * `hud.lua` call second handler separately * `api.md` extended and clarified documentaion --- api.lua | 7 ++++++- api.md | 31 ++++++++++++++++++++++++++++--- hud.lua | 15 ++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/api.lua b/api.lua index e07785e..fc9d41c 100644 --- a/api.lua +++ b/api.lua @@ -18,10 +18,15 @@ local detect_extra_protection = function(pos,area_list) return area_list end +function areas:getRegisteredProtections(pos) + local res = {} + res = detect_extra_protection(pos,res) + return res +end + --- Returns a list of areas that include the provided position. function areas:getAreasAtPos(pos) local res = {} - res = detect_extra_protection(pos,res) if self.store then local a = self.store:get_areas_for_pos(pos, false, true) diff --git a/api.md b/api.md index a0fcae7..dac876b 100644 --- a/api.md +++ b/api.md @@ -5,13 +5,38 @@ Adding your protections to the HUD If you are providing an extra protection mod to work in cunjunction with the HUD feature of `areas`, you can register a callback to add your mod's code to -display your protection's existence. For example +display your protection's existence. - local myhandler = function(pos,arealist) +Registering a handler: + +* `areas.register_hud_handler(handler_name) --> nil` + +Handler specification: + +* `handler_name(pos,area_list) --> new_area_list` + * `pos` - the position at which to check for protection coverage by your mod + * `area_list` - the current list of protected areas + * `new_area_list` - the list of protected areas, updated with your entries + +Area list items: + +The area list item is a map table identified with an ID, and properties + +The ID should be in the format `modname:` and appended with an identifier for the protection. + +Each area list item should be a table with the following properties + +* `owner` - (required) the name of the protection owner +* `name` - (optional) the name of the area + +Example +------- + + local myhandler = function(pos,area_list) local areaowner = find_my_protections(pos) if areaowner then - arealist["mymodname"] = { + arealist["mymodname:first"] = { name = "Protection name", owner = areaowner, } diff --git a/hud.lua b/hud.lua index 764c2e3..4ef96f4 100644 --- a/hud.lua +++ b/hud.lua @@ -7,11 +7,24 @@ minetest.register_globalstep(function(dtime) local name = player:get_player_name() local pos = vector.round(player:getpos()) local areaStrings = {} + for id, area in pairs(areas:getAreasAtPos(pos)) do - table.insert(areaStrings, ("%s [%s] (%s%s)") + table.insert(areaStrings, ("%s [%i] (%s%s)") :format(area.name, id, area.owner, area.open and ":open" or "")) end + + for id, area in pairs(areas:getRegisteredProtections(pos)) do + table.insert( + areaStrings, ("%s [%s] (%s)") + :format( + area.name or "", + id , + area.owner + ) + ) + end + local areaString = "Areas:" if #areaStrings > 0 then areaString = areaString.."\n".. From 73cb4c422dea9d1bee4040f84c5dc6690e383e48 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 18:57:49 +0800 Subject: [PATCH 11/17] fixed typo in documentation --- api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.md b/api.md index dac876b..a5a932c 100644 --- a/api.md +++ b/api.md @@ -3,7 +3,7 @@ Adding your protections to the HUD ---------------------------------- -If you are providing an extra protection mod to work in cunjunction with the +If you are providing an extra protection mod to work in conjunction with the HUD feature of `areas`, you can register a callback to add your mod's code to display your protection's existence. From 83dcc447944973925d5c6684e0d96fcc34386724 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 18:59:16 +0800 Subject: [PATCH 12/17] ignore vim cache files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5236e1e..70bb2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ +*.swp From 812917fca28bf1d49420439ee3cd82989d5017af Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 19:00:01 +0800 Subject: [PATCH 13/17] adjust handler registration function name --- api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api.md b/api.md index a5a932c..459e3bc 100644 --- a/api.md +++ b/api.md @@ -9,11 +9,11 @@ display your protection's existence. Registering a handler: -* `areas.register_hud_handler(handler_name) --> nil` +* `areas.registerHudHandler(handler) --> nil` Handler specification: -* `handler_name(pos,area_list) --> new_area_list` +* `handler(pos,area_list) --> new_area_list` * `pos` - the position at which to check for protection coverage by your mod * `area_list` - the current list of protected areas * `new_area_list` - the list of protected areas, updated with your entries From 64ec6ff68f38b32a3bf8f071febdb77bd7355d1b Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 22:37:01 +0800 Subject: [PATCH 14/17] reinstate the format string unsigned int] --- hud.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hud.lua b/hud.lua index 4ef96f4..ce83660 100644 --- a/hud.lua +++ b/hud.lua @@ -9,7 +9,7 @@ minetest.register_globalstep(function(dtime) local areaStrings = {} for id, area in pairs(areas:getAreasAtPos(pos)) do - table.insert(areaStrings, ("%s [%i] (%s%s)") + table.insert(areaStrings, ("%s [%u] (%s%s)") :format(area.name, id, area.owner, area.open and ":open" or "")) end From bb10d0c9f3a9cd5a9a0ced46c729e765cfe4e3d9 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 22:38:14 +0800 Subject: [PATCH 15/17] reverting auth change --- internal.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal.lua b/internal.lua index 05a31cd..bf7e92c 100644 --- a/internal.lua +++ b/internal.lua @@ -1,6 +1,6 @@ function areas:player_exists(name) - return minetest.auth_table[name] ~= nil + return minetest.get_auth_handler().get_auth(name) ~= nil end -- Save the areas table to a file From 9fc3583509079ff009163b1dbc5e7c72ee8ffbd5 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 22:40:00 +0800 Subject: [PATCH 16/17] restore code style, adjust further style --- api.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/api.lua b/api.lua index fc9d41c..49478e0 100644 --- a/api.lua +++ b/api.lua @@ -1,7 +1,7 @@ local protection_detectors = {} -- Other protection mods should be able to display their protection in the hud -areas.register_hud_handler = function(handler) +areas.registerHudHandler = function(handler) protection_detectors[#protection_detectors + 1] = handler end @@ -20,7 +20,7 @@ end function areas:getRegisteredProtections(pos) local res = {} - res = detect_extra_protection(pos,res) + res = detect_extra_protection(pos, res) return res end @@ -39,10 +39,9 @@ function areas:getAreasAtPos(pos) for id, area in pairs(self.areas) do local ap1, ap2 = area.pos1, area.pos2 if - (px >= ap1.x and px <= ap2.x) and - (py >= ap1.y and py <= ap2.y) and - (pz >= ap1.z and pz <= ap2.z) - then + (px >= ap1.x and px <= ap2.x) and + (py >= ap1.y and py <= ap2.y) and + (pz >= ap1.z and pz <= ap2.z) then res[id] = area end end From ea0d14d31297e5c22792807beed0be011747d640 Mon Sep 17 00:00:00 2001 From: "Tai @ Flex" Date: Mon, 28 Nov 2016 22:51:47 +0800 Subject: [PATCH 17/17] added space for stylistic imrpovement --- api.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.lua b/api.lua index 49478e0..198979a 100644 --- a/api.lua +++ b/api.lua @@ -6,7 +6,7 @@ areas.registerHudHandler = function(handler) end -- Generalized call to registered handlers to add their proeciton labels to the areas list -local detect_extra_protection = function(pos,area_list) +local detect_extra_protection = function(pos, area_list) if #protection_detectors <= 0 then return area_list end