From 637b5b4be4337c41dbbf4b454be43bf70bb96bd1 Mon Sep 17 00:00:00 2001 From: number Zero Date: Thu, 29 Nov 2018 15:24:08 +0300 Subject: [PATCH] Unify protection handling --- filter.lua | 3 +-- multiblock.lua | 15 ++++++--------- util.lua | 13 +++++++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/filter.lua b/filter.lua index 832657a..eee81fb 100644 --- a/filter.lua +++ b/filter.lua @@ -26,8 +26,7 @@ local function filter_init(pos) end local function filter_receive_fields(pos, formname, fields, sender) - if minetest.is_protected(pos, sender) and not minetest.check_player_privs(sender, "protection_bypass") then - minetest.record_protection_violation(pos, sender) + if digiline_routing.is_protected(pos, sender) then return end if not fields.channel then diff --git a/multiblock.lua b/multiblock.lua index dc2d98f..0f79e85 100644 --- a/multiblock.lua +++ b/multiblock.lua @@ -12,16 +12,14 @@ digiline_routing.multiblock.build2 = function(node1, node2, itemstack, placer, p pos = pointed_thing.above end - if minetest.is_protected(pos, placer:get_player_name()) and not minetest.check_player_privs(placer, "protection_bypass") then - minetest.record_protection_violation(pos, placer:get_player_name()) + if digiline_routing.is_protected(pos, placer) then return itemstack, false end local dir = minetest.dir_to_facedir(placer:get_look_dir()) local botpos = vector.add(pos, minetest.facedir_to_dir(dir)) - if minetest.is_protected(botpos, placer:get_player_name()) and not minetest.check_player_privs(placer, "protection_bypass") then - minetest.record_protection_violation(botpos, placer:get_player_name()) + if digiline_routing.is_protected(botpos, placer) then return itemstack, false end @@ -45,11 +43,11 @@ digiline_routing.multiblock.rotate2 = function(pos, node, user, mode, new_param2 local dir = minetest.facedir_to_dir(node.param2) local p = vector.add(pos, dir) local node2 = minetest.get_node_or_nil(p) - if not node2 or not node.param2 == node2.param2 then + if not node2 or node.param2 ~= node2.param2 then return false end - if minetest.is_protected(p, user:get_player_name()) then - minetest.record_protection_violation(p, user:get_player_name()) + -- protection at `pos` is checked by the screwdriver + if digiline_routing.is_protected(p, user) then return false end if mode ~= screwdriver.ROTATE_FACE then @@ -61,8 +59,7 @@ digiline_routing.multiblock.rotate2 = function(pos, node, user, mode, new_param2 if not node_def or not node_def.buildable_to then return false end - if minetest.is_protected(newp, user:get_player_name()) then - minetest.record_protection_violation(newp, user:get_player_name()) + if digiline_routing.is_protected(newp, user) then return false end node.param2 = new_param2 diff --git a/util.lua b/util.lua index dae8581..7b5db65 100644 --- a/util.lua +++ b/util.lua @@ -14,3 +14,16 @@ digiline_routing.get_base_rule = function(rule, param2) end return BASE_RULES[(rule + param2) % 4] end + +digiline_routing.is_protected = function(pos, player) + local name = player:get_player_name() + if not minetest.is_protected(pos, name) then + return false + end + -- FIXME: is this necessary? + if minetest.check_player_privs(name, "protection_bypass") then + return false + end + minetest.record_protection_violation(pos, name) + return true +end