From 71ae3c0a6ca80698f364142d80342bceb7a48214 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sun, 22 Sep 2024 11:29:51 +0200 Subject: [PATCH 1/3] de-duplicate code using table --- api.lua | 53 ++++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/api.lua b/api.lua index e698fea..17c9bcb 100644 --- a/api.lua +++ b/api.lua @@ -119,60 +119,43 @@ function areas:canInteract(pos, name) if minetest.check_player_privs(name, self.adminPrivs) then return true end + local areas_list if areas.config.use_smallest_area_precedence then - local area = self:getSmallestAreaAtPos(pos) - -- No area, player owns it or area is open - if not area - or area.owner == name - or area.open - then + local smallest_area = self:getSmallestAreaAtPos(pos) + -- No area + if not smallest_area then + return true + end + areas_list = { smallest_area } + else + areas_list = self:getAreasAtPos(pos) + end + local owned = false + for _, area in pairs(areas_list) do + -- Player owns the area or area is open + if area.owner == name or area.open then return true elseif areas.factions_available and area.faction_open then if (factions.version or 0) < 2 then local faction_name = factions.get_player_faction(name) if faction_name then - for _, fname in ipairs(area.faction_open or {}) do + for _, fname in ipairs(area.faction_open) do if faction_name == fname then return true end end end else - for _, fname in ipairs(area.faction_open or {}) do + for _, fname in ipairs(area.faction_open) do if factions.player_is_in_faction(fname, name) then return true end end end end - return false - else - local owned = false - for _, area in pairs(self:getAreasAtPos(pos)) do - if area.owner == name or area.open then - return true - elseif areas.factions_available and area.faction_open then - if (factions.version or 0) < 2 then - local faction_name = factions.get_player_faction(name) - if faction_name then - for _, fname in ipairs(area.faction_open or {}) do - if faction_name == fname then - return true - end - end - end - else - for _, fname in ipairs(area.faction_open or {}) do - if factions.player_is_in_faction(fname, name) then - return true - end - end - end - end - owned = true - end - return not owned + owned = true end + return not owned end -- Returns a table (list) of all players that own an area From a4a4ff34cc91e7727b1c967cab2de72346013608 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Tue, 24 Sep 2024 09:51:38 +0200 Subject: [PATCH 2/3] whitespace tweak --- api.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api.lua b/api.lua index 17c9bcb..3c34eaa 100644 --- a/api.lua +++ b/api.lua @@ -101,9 +101,9 @@ function areas:getSmallestAreaAtPos(pos) local smallest_area, smallest_id, volume local smallest_volume = math.huge for id, area in pairs(self:getAreasAtPos(pos)) do - volume = (area.pos2.x - area.pos1.x + 1) - * (area.pos2.y - area.pos1.y + 1) - * (area.pos2.z - area.pos1.z + 1) + volume = (area.pos2.x - area.pos1.x + 1) + * (area.pos2.y - area.pos1.y + 1) + * (area.pos2.z - area.pos1.z + 1) if smallest_volume >= volume then smallest_area = area smallest_id = id From 2b43015b330298599b0f871d57f8ae5018bbb592 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Tue, 24 Sep 2024 10:33:45 +0200 Subject: [PATCH 3/3] shorten code and indicate why var is needed can't do: areas_list = { self:getSmallestAreaAtPos(pos) } because that method returns two values. --- api.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/api.lua b/api.lua index 3c34eaa..8042785 100644 --- a/api.lua +++ b/api.lua @@ -121,11 +121,7 @@ function areas:canInteract(pos, name) end local areas_list if areas.config.use_smallest_area_precedence then - local smallest_area = self:getSmallestAreaAtPos(pos) - -- No area - if not smallest_area then - return true - end + local smallest_area, _ = self:getSmallestAreaAtPos(pos) areas_list = { smallest_area } else areas_list = self:getAreasAtPos(pos)