From cbae2c7f8808bbfae0cded8ea411f84f87d99b17 Mon Sep 17 00:00:00 2001 From: fluxionary <25628292+fluxionary@users.noreply.github.com> Date: Thu, 17 Aug 2023 11:42:54 -0700 Subject: [PATCH] Add a distance limit to the entity detector (#32) The distance limit can be configured with the `moremesecons_entity_detector.max_radius` setting. An additional change prevents a crash if an object returned by `get_objects_inside_radius` is neither a valid player nor a luaentity. We don't know why this happens but it sometimes does. --- moremesecons_entity_detector/init.lua | 25 +++++++++++-------------- settingtypes.txt | 4 ++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/moremesecons_entity_detector/init.lua b/moremesecons_entity_detector/init.lua index d2badb1..165e399 100644 --- a/moremesecons_entity_detector/init.lua +++ b/moremesecons_entity_detector/init.lua @@ -26,7 +26,8 @@ local function object_detector_on_receive_fields(pos, _, fields, player) meta:set_string("digiline_channel", fields.digiline_channel) local r = tonumber(fields.radius) if r then - meta:set_int("radius", r) + local max_radius = moremesecons.setting("entity_detector", "max_radius", 16, 0) + meta:set_int("radius", math.min(r, max_radius)) end end @@ -36,23 +37,19 @@ local object_detector_scan = function (pos) local scanname = meta:get_string("scanname") local scan_all = scanname == "" local scan_names = scanname:split(',') - local radius = meta:get_int("radius") - if radius == 0 then - radius = 6 - end + local max_radius = moremesecons.setting("entity_detector", "max_radius", 16, 0) + local radius = math.min(tonumber(meta:get("radius")) or 6, max_radius) for _,obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do - if not obj:is_player() then - local luaentity = obj:get_luaentity() + local luaentity = obj:get_luaentity() + if luaentity then + if scan_all then + return true + end local isname = luaentity.name - if isname then - if scan_all then + for _, name in ipairs(scan_names) do + if isname == name or (isname == "__builtin:item" and luaentity.itemstring == name) then return true end - for _, name in ipairs(scan_names) do - if isname == name or (isname == "__builtin:item" and luaentity.itemstring == name) then - return true - end - end end end end diff --git a/settingtypes.txt b/settingtypes.txt index e424eed..c54c8c2 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -13,6 +13,10 @@ moremesecons_commandblock.authorized_commands (Authorized commands) string tell # Any value less than or equal to 0 will be changed to 1 and a NaN value will be changed to the default value moremesecons_commandblock.nearest_max_distance (Nearest player maximum distance) float 8 +[Entity Detector] + +moremesecons_entity_detector.max_radius (Maximum entity detector radius) float 16 0 + [Signal Jammer] # Jammer action range