From 949a2b787ef42ec834d15aa9f2c50bf05231efad Mon Sep 17 00:00:00 2001 From: CyberMango Date: Fri, 6 Jan 2023 22:07:07 +0200 Subject: [PATCH 1/4] Added protection violation checker functions. 1 completley generic and 3 more that use it for more specific cases - placing a node, modifying a node and planting over a node. --- mods/CORE/mcl_util/init.lua | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 4684234e5..5ade7bdd0 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -728,3 +728,70 @@ function mcl_util.set_bone_position(obj, bone, pos, rot) obj:set_bone_position(bone, pos or current_pos, rot or current_rot) end end + +--[[Check for a protection violation on given nodes. +-- +-- @param affected_nodes Node to check for protection violation. +-- @param player The player performing the action. +-- @param create_log Default: False. Should a log message be created on violation detection. +-- +-- @return true on protection violation detection. false otherwise. +]] +function mcl_util.check_nodes_protection(affected_nodes, player, create_log) + create_log = create_log or false + local name = player and player:get_player_name() or "" + + for _, position in ipairs(affected_nodes) do + if minetest.is_protected(position, name) then + if create_log then + minetest.log("action", name .. " tried violating protection at position " + .. minetest.pos_to_string(position)) + end + + minetest.record_protection_violation(position, name) + return true + end + end + + return false +end + +--[[Check protection violation for a planting action. +-- +-- @param pointed_thing The pointed_thing table for the object the action was pointed at. +-- @param player See mcl_util.check_nodes_protection. +-- @param create_log See mcl_util.check_nodes_protection. +-- +-- @return See mcl_util.check_nodes_protection. +]] +function mcl_util.check_planting_protection(pointed_thing, player, create_log) + -- We dont want to allow planting on top of protected nodes, even if the air is unprotected. + return mcl_util.check_nodes_protection({pointed_thing.above, pointed_thing.under}, player, + create_log) +end + +--[[Check protection violation for a node placement action. +-- +-- @param pointed_thing The pointed_thing table for the object the action was pointed at. +-- @param player See mcl_util.check_nodes_protection. +-- @param create_log See mcl_util.check_nodes_protection. +-- +-- @return See mcl_util.check_nodes_protection. +]] +function mcl_util.check_placement_protection(pointed_thing, player, create_log) + return mcl_util.check_nodes_protection({pointed_thing.above}, player, create_log) +end + +--[[Check protection violation for a node modification action. +-- +-- Such as using a shovel or a hoe on dirt, destroying a block or using a chest. +-- +-- @param pointed_thing The pointed_thing table for the object the action was pointed at. +-- @param player See mcl_util.check_nodes_protection. +-- @param create_log See mcl_util.check_nodes_protection. +-- +-- @return See mcl_util.check_nodes_protection. +]] +function mcl_util.check_node_modification_protection(pointed_thing, player, create_log) + return mcl_util.check_nodes_protection({pointed_thing.under}, player, create_log) +end From 685a7ff25678ab94de7aa43d10b17a5666f11b18 Mon Sep 17 00:00:00 2001 From: CyberMango Date: Fri, 6 Jan 2023 22:58:56 +0200 Subject: [PATCH 2/4] Added a function for a single position and better naming. Now the functions are named with "position" instead of "node" to better reflect what they do and what args they expect. Also added a function for cehcking just a single position since thats the most common use case, so it saves a small performance overhead. --- mods/CORE/mcl_util/init.lua | 75 ++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 5ade7bdd0..c8f38f659 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -729,26 +729,26 @@ function mcl_util.set_bone_position(obj, bone, pos, rot) end end ---[[Check for a protection violation on given nodes. +--[[Check for a protection violation on any of the given positions. -- --- @param affected_nodes Node to check for protection violation. --- @param player The player performing the action. --- @param create_log Default: False. Should a log message be created on violation detection. +-- @param positions A table of position tables to check for protection violation. +-- @param player The player performing the action. +-- @param create_log Default: False. Should a log message be created on violation detection. -- -- @return true on protection violation detection. false otherwise. ]] -function mcl_util.check_nodes_protection(affected_nodes, player, create_log) +function mcl_util.check_positions_protection(positions, player, create_log) create_log = create_log or false local name = player and player:get_player_name() or "" - for _, position in ipairs(affected_nodes) do - if minetest.is_protected(position, name) then + for i = 1, #positions do + if minetest.is_protected(positions[i], name) then if create_log then minetest.log("action", name .. " tried violating protection at position " - .. minetest.pos_to_string(position)) + .. minetest.pos_to_string(positions[i])) end - minetest.record_protection_violation(position, name) + minetest.record_protection_violation(positions[i], name) return true end end @@ -756,42 +756,67 @@ function mcl_util.check_nodes_protection(affected_nodes, player, create_log) return false end +--[[Check for a protection violation on a single position. +-- +-- @param position A position table to check for protection violation. +-- @param player See mcl_util.check_positions_protection. +-- @param create_log See mcl_util.check_positions_protection. +-- +-- @return See check_positions_protection. +]] +function mcl_util.check_position_protection(position, player, create_log) + create_log = create_log or false + local name = player and player:get_player_name() or "" + + if minetest.is_protected(position, name) then + if create_log then + minetest.log("action", name .. " tried violating protection at position " + .. minetest.pos_to_string(position)) + end + + minetest.record_protection_violation(position, name) + return true + end + + return false +end + --[[Check protection violation for a planting action. -- --- @param pointed_thing The pointed_thing table for the object the action was pointed at. --- @param player See mcl_util.check_nodes_protection. --- @param create_log See mcl_util.check_nodes_protection. +-- @param pointed_thing The pointed_thing table for the object the action was pointed at. +-- @param player See mcl_util.check_positions_protection. +-- @param create_log See mcl_util.check_positions_protection. -- --- @return See mcl_util.check_nodes_protection. +-- @return See mcl_util.check_positions_protection. ]] function mcl_util.check_planting_protection(pointed_thing, player, create_log) - -- We dont want to allow planting on top of protected nodes, even if the air is unprotected. - return mcl_util.check_nodes_protection({pointed_thing.above, pointed_thing.under}, player, + -- We dont want to allow planting on top of protected positions, even if the air is unprotected. + return mcl_util.check_positions_protection({pointed_thing.above, pointed_thing.under}, player, create_log) end --[[Check protection violation for a node placement action. -- --- @param pointed_thing The pointed_thing table for the object the action was pointed at. --- @param player See mcl_util.check_nodes_protection. --- @param create_log See mcl_util.check_nodes_protection. +-- @param pointed_thing The pointed_thing table for the object the action was pointed at. +-- @param player See mcl_util.check_positions_protection. +-- @param create_log See mcl_util.check_positions_protection. -- --- @return See mcl_util.check_nodes_protection. +-- @return See mcl_util.check_positions_protection. ]] function mcl_util.check_placement_protection(pointed_thing, player, create_log) - return mcl_util.check_nodes_protection({pointed_thing.above}, player, create_log) + return mcl_util.check_position_protection(pointed_thing.above, player, create_log) end --[[Check protection violation for a node modification action. -- -- Such as using a shovel or a hoe on dirt, destroying a block or using a chest. -- --- @param pointed_thing The pointed_thing table for the object the action was pointed at. --- @param player See mcl_util.check_nodes_protection. --- @param create_log See mcl_util.check_nodes_protection. +-- @param pointed_thing The pointed_thing table for the object the action was pointed at. +-- @param player See mcl_util.check_positions_protection. +-- @param create_log See mcl_util.check_positions_protection. -- --- @return See mcl_util.check_nodes_protection. +-- @return See mcl_util.check_positions_protection. ]] function mcl_util.check_node_modification_protection(pointed_thing, player, create_log) - return mcl_util.check_nodes_protection({pointed_thing.under}, player, create_log) + return mcl_util.check_position_protection(pointed_thing.under, player, create_log) end From b0d9eed3e1cd567c471a105f12b4e56c903696cf Mon Sep 17 00:00:00 2001 From: CyberMango Date: Sat, 7 Jan 2023 00:59:05 +0200 Subject: [PATCH 3/4] Removed specific functions and added usage examples. The specific functions didnt end up adding much simplicity, but did add some degree of confusion. --- mods/CORE/mcl_util/init.lua | 40 ------------------------------- mods/ITEMS/mcl_ocean/mod.conf | 2 +- mods/ITEMS/mcl_ocean/seagrass.lua | 8 +------ mods/ITEMS/mcl_smoker/init.lua | 5 ++-- mods/ITEMS/mcl_smoker/mod.conf | 2 +- 5 files changed, 5 insertions(+), 52 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index c8f38f659..acda9e7a3 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -780,43 +780,3 @@ function mcl_util.check_position_protection(position, player, create_log) return false end - ---[[Check protection violation for a planting action. --- --- @param pointed_thing The pointed_thing table for the object the action was pointed at. --- @param player See mcl_util.check_positions_protection. --- @param create_log See mcl_util.check_positions_protection. --- --- @return See mcl_util.check_positions_protection. -]] -function mcl_util.check_planting_protection(pointed_thing, player, create_log) - -- We dont want to allow planting on top of protected positions, even if the air is unprotected. - return mcl_util.check_positions_protection({pointed_thing.above, pointed_thing.under}, player, - create_log) -end - ---[[Check protection violation for a node placement action. --- --- @param pointed_thing The pointed_thing table for the object the action was pointed at. --- @param player See mcl_util.check_positions_protection. --- @param create_log See mcl_util.check_positions_protection. --- --- @return See mcl_util.check_positions_protection. -]] -function mcl_util.check_placement_protection(pointed_thing, player, create_log) - return mcl_util.check_position_protection(pointed_thing.above, player, create_log) -end - ---[[Check protection violation for a node modification action. --- --- Such as using a shovel or a hoe on dirt, destroying a block or using a chest. --- --- @param pointed_thing The pointed_thing table for the object the action was pointed at. --- @param player See mcl_util.check_positions_protection. --- @param create_log See mcl_util.check_positions_protection. --- --- @return See mcl_util.check_positions_protection. -]] -function mcl_util.check_node_modification_protection(pointed_thing, player, create_log) - return mcl_util.check_position_protection(pointed_thing.under, player, create_log) -end diff --git a/mods/ITEMS/mcl_ocean/mod.conf b/mods/ITEMS/mcl_ocean/mod.conf index 9b639a7b0..a50609f52 100644 --- a/mods/ITEMS/mcl_ocean/mod.conf +++ b/mods/ITEMS/mcl_ocean/mod.conf @@ -1,4 +1,4 @@ name = mcl_ocean description = Includes various ocean nodes -depends = mcl_core, mcl_sounds, mcl_dye +depends = mcl_core, mcl_sounds, mcl_dye, mcl_util optional_depends = doc, doc_items, screwdriver diff --git a/mods/ITEMS/mcl_ocean/seagrass.lua b/mods/ITEMS/mcl_ocean/seagrass.lua index 8213fec10..48d42870e 100644 --- a/mods/ITEMS/mcl_ocean/seagrass.lua +++ b/mods/ITEMS/mcl_ocean/seagrass.lua @@ -39,13 +39,7 @@ local function seagrass_on_place(itemstack, placer, pointed_thing) return itemstack end - if minetest.is_protected(pos_under, player_name) or - minetest.is_protected(pos_above, player_name) then - minetest.log("action", player_name - .. " tried to place " .. itemstack:get_name() - .. " at protected position " - .. minetest.pos_to_string(pos_under)) - minetest.record_protection_violation(pos_under, player_name) + if mcl_util.check_positions_protection({pos_under, pos_above}, placer, true) then return itemstack end diff --git a/mods/ITEMS/mcl_smoker/init.lua b/mods/ITEMS/mcl_smoker/init.lua index 696df4917..81f3c366f 100644 --- a/mods/ITEMS/mcl_smoker/init.lua +++ b/mods/ITEMS/mcl_smoker/init.lua @@ -88,11 +88,10 @@ end -- local function allow_metadata_inventory_put(pos, listname, index, stack, player) - local name = player:get_player_name() - if minetest.is_protected(pos, name) then - minetest.record_protection_violation(pos, name) + if mcl_util.check_position_protection(pos, player) then return 0 end + local meta = minetest.get_meta(pos) local inv = meta:get_inventory() if listname == "fuel" then diff --git a/mods/ITEMS/mcl_smoker/mod.conf b/mods/ITEMS/mcl_smoker/mod.conf index c6bda0fc1..c2c49db62 100644 --- a/mods/ITEMS/mcl_smoker/mod.conf +++ b/mods/ITEMS/mcl_smoker/mod.conf @@ -1,3 +1,3 @@ name = mcl_smoker -depends = mcl_init, mcl_formspec, mcl_core, mcl_furnaces, mcl_sounds, mcl_craftguide, mcl_achievements, mcl_particles +depends = mcl_init, mcl_formspec, mcl_core, mcl_furnaces, mcl_sounds, mcl_craftguide, mcl_achievements, mcl_particles, mcl_util optional_depends = doc, screwdriver From 64a7f76d5b10f4724c91ce65b02735e3d83c09fa Mon Sep 17 00:00:00 2001 From: CyberMango Date: Thu, 12 Jan 2023 21:22:36 +0200 Subject: [PATCH 4/4] Replaced positions check with an area check. This one is using the minetest.is_area_protected so it should work faster. It also doesnt require the user to manually add all the points that should be checked so its nicer to use. --- mods/CORE/mcl_util/init.lua | 43 +++++++++++++------------------ mods/ITEMS/mcl_ocean/seagrass.lua | 2 +- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index acda9e7a3..a79c970cd 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -729,28 +729,28 @@ function mcl_util.set_bone_position(obj, bone, pos, rot) end end ---[[Check for a protection violation on any of the given positions. +--[[Check for a protection violation in a given area. -- --- @param positions A table of position tables to check for protection violation. +-- Applies is_protected() to a 3D lattice of points in the defined volume. The points are spaced +-- evenly throughout the volume and have a spacing similar to, but no larger than, "interval". +-- +-- @param pos1 A position table of the area volume's first edge. +-- @param pos2 A position table of the area volume's second edge. -- @param player The player performing the action. --- @param create_log Default: False. Should a log message be created on violation detection. +-- @param interval Optional. Max spacing between checked points at the volume. +-- Default: Same as minetest.is_area_protected. -- -- @return true on protection violation detection. false otherwise. +-- +-- @notes *All corners and edges of the defined volume are checked. ]] -function mcl_util.check_positions_protection(positions, player, create_log) - create_log = create_log or false +function mcl_util.check_area_protection(pos1, pos2, player, interval) local name = player and player:get_player_name() or "" - for i = 1, #positions do - if minetest.is_protected(positions[i], name) then - if create_log then - minetest.log("action", name .. " tried violating protection at position " - .. minetest.pos_to_string(positions[i])) - end - - minetest.record_protection_violation(positions[i], name) - return true - end + local protected_pos = minetest.is_area_protected(pos1, pos2, name, interval) + if protected_pos then + minetest.record_protection_violation(protected_pos, name) + return true end return false @@ -759,21 +759,14 @@ end --[[Check for a protection violation on a single position. -- -- @param position A position table to check for protection violation. --- @param player See mcl_util.check_positions_protection. --- @param create_log See mcl_util.check_positions_protection. +-- @param player The player performing the action. -- --- @return See check_positions_protection. +-- @return true on protection violation detection. false otherwise. ]] -function mcl_util.check_position_protection(position, player, create_log) - create_log = create_log or false +function mcl_util.check_position_protection(position, player) local name = player and player:get_player_name() or "" if minetest.is_protected(position, name) then - if create_log then - minetest.log("action", name .. " tried violating protection at position " - .. minetest.pos_to_string(position)) - end - minetest.record_protection_violation(position, name) return true end diff --git a/mods/ITEMS/mcl_ocean/seagrass.lua b/mods/ITEMS/mcl_ocean/seagrass.lua index 48d42870e..8f319b29f 100644 --- a/mods/ITEMS/mcl_ocean/seagrass.lua +++ b/mods/ITEMS/mcl_ocean/seagrass.lua @@ -39,7 +39,7 @@ local function seagrass_on_place(itemstack, placer, pointed_thing) return itemstack end - if mcl_util.check_positions_protection({pos_under, pos_above}, placer, true) then + if mcl_util.check_area_protection(pos_under, pos_above, placer) then return itemstack end