From 19c9ca179b25bdd2f36d1490a6891589f5dbb5d9 Mon Sep 17 00:00:00 2001 From: Isidor Zeuner Date: Sat, 1 Jul 2017 10:23:51 +0200 Subject: [PATCH] move logic to method --- worldedit_commands/init.lua | 158 ++++++++++++++++-------------------- worldedit_commands/safe.lua | 47 +++++++---- 2 files changed, 103 insertions(+), 102 deletions(-) diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index ac6db3b..a2bb8fb 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -465,29 +465,23 @@ local check_sphere = function(name, param) worldedit.player_notify(name, "invalid usage: " .. param) return nil end - if nil ~= area_protection.areas then - local pos1 = worldedit.pos1[name] - local allowed, conflicting = area_protection.areas:canInteractInArea( - { - x = pos1.x - radius, - y = pos1.y - radius, - z = pos1.z - radius, - }, - { - x = pos1.x + radius, - y = pos1.y + radius, - z = pos1.z + radius, - }, - name, - false - ) - if false == allowed then - worldedit.player_notify( - name, - "sphere may conflict with non-owned region " .. conflicting - ) - return nil - end + local pos1 = worldedit.pos1[name] + local allowed = area_protection:interaction_allowed( + "sphere", + { + x = pos1.x - radius, + y = pos1.y - radius, + z = pos1.z - radius, + }, + { + x = pos1.x + radius, + y = pos1.y + radius, + z = pos1.z + radius, + }, + name + ) + if not allowed then + return nil end local node = get_node(name, nodename) if not node then return nil end @@ -528,29 +522,23 @@ local check_dome = function(name, param) worldedit.player_notify(name, "invalid usage: " .. param) return nil end - if nil ~= area_protection.areas then - local pos1 = worldedit.pos1[name] - local allowed, conflicting = area_protection.areas:canInteractInArea( - { - x = pos1.x - radius, - y = pos1.y, - z = pos1.z - radius, - }, - { - x = pos1.x + radius, - y = pos1.y + radius, - z = pos1.z + radius, - }, - name, - false - ) - if false == allowed then - worldedit.player_notify( - name, - "dome may conflict with non-owned region " .. conflicting - ) - return nil - end + local pos1 = worldedit.pos1[name] + local allowed = area_protection:interaction_allowed( + "dome", + { + x = pos1.x - radius, + y = pos1.y, + z = pos1.z - radius, + }, + { + x = pos1.x + radius, + y = pos1.y + radius, + z = pos1.z + radius, + }, + name + ) + if not allowed then + return nil end local node = get_node(name, nodename) if not node then return nil end @@ -591,48 +579,42 @@ local check_cylinder = function(name, param) worldedit.player_notify(name, "invalid usage: " .. param) return nil end - if nil ~= area_protection.areas then - length = tonumber(length) - if axis == "?" then - local sign - axis, sign = worldedit.player_axis(name) - length = length * sign - end - local pos1 = worldedit.pos1[name] - local current_pos = {x=pos1.x, y=pos1.y, z=pos1.z} - if length < 0 then - length = -length - current_pos[axis] = current_pos[axis] - length - end - local other1, other2 = worldedit.get_axis_others(axis) - local interact_pos1 = { - x = current_pos.x, - y = current_pos.y, - z = current_pos.z, - } - local interact_pos2 = { - x = current_pos.x, - y = current_pos.y, - z = current_pos.z, - } - interact_pos1[other1] = interact_pos1[other1] - radius - interact_pos1[other2] = interact_pos1[other2] - radius - interact_pos2[other1] = interact_pos2[other1] + radius - interact_pos2[other2] = interact_pos2[other2] + radius - interact_pos2[axis] = interact_pos2[axis] + length - local allowed, conflicting = area_protection.areas:canInteractInArea( - interact_pos1, - interact_pos2, - name, - false - ) - if false == allowed then - worldedit.player_notify( - name, - "cylinder may conflict with non-owned region " .. conflicting - ) - return nil - end + length = tonumber(length) + if axis == "?" then + local sign + axis, sign = worldedit.player_axis(name) + length = length * sign + end + local pos1 = worldedit.pos1[name] + local current_pos = {x=pos1.x, y=pos1.y, z=pos1.z} + if length < 0 then + length = -length + current_pos[axis] = current_pos[axis] - length + end + local other1, other2 = worldedit.get_axis_others(axis) + local interact_pos1 = { + x = current_pos.x, + y = current_pos.y, + z = current_pos.z, + } + local interact_pos2 = { + x = current_pos.x, + y = current_pos.y, + z = current_pos.z, + } + interact_pos1[other1] = interact_pos1[other1] - radius + interact_pos1[other2] = interact_pos1[other2] - radius + interact_pos2[other1] = interact_pos2[other1] + radius + interact_pos2[other2] = interact_pos2[other2] + radius + interact_pos2[axis] = interact_pos2[axis] + length + local allowed = area_protection:interaction_allowed( + "cylinder", + interact_pos1, + interact_pos2, + name + ) + if not allowed then + return nil end local node = get_node(name, nodename) if not node then return nil end diff --git a/worldedit_commands/safe.lua b/worldedit_commands/safe.lua index f6a359b..d4ac454 100644 --- a/worldedit_commands/safe.lua +++ b/worldedit_commands/safe.lua @@ -4,6 +4,31 @@ if minetest.get_modpath("areas") then area_protection.areas = areas end +local area_protection.interaction_allowed = function( + area_protection, + description, + pos1, + pos2, + player_name +) + if area_protection.areas then + local allowed, conflicting = area_protection.areas:canInteractInArea( + pos1, + pos2, + player_name, + false + ) + if not allowed then + worldedit.player_notify( + player_name, + description .. " conflicts with non-owned region " .. conflicting + ) + end + return allowed + end + return true +end + local safe_region_callback = {} local safe_region_param = {} @@ -13,20 +38,14 @@ local function check_region(name, param) worldedit.player_notify(name, "no region selected") return nil end - if nil ~= area_protection.areas then - local allowed, conflicting = area_protection.areas:canInteractInArea( - pos1, - pos2, - name, - false - ) - if false == allowed then - worldedit.player_notify( - name, - "region conflicts with non-owned region " .. conflicting - ) - return nil - end + local allowed = area_protection:interaction_allowed( + "region", + pos1, + pos2, + name + ) + if not allowed then + return nil end return worldedit.volume(pos1, pos2) end