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.
This commit is contained in:
fluxionary 2023-08-17 11:42:54 -07:00 committed by GitHub
parent 464699e78b
commit cbae2c7f88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 14 deletions

View File

@ -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

View File

@ -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