Skip to content

Commit

Permalink
Add a distance limit to the entity detector (#32)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fluxionary authored Aug 17, 2023
1 parent 464699e commit cbae2c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 11 additions & 14 deletions moremesecons_entity_detector/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbae2c7

Please sign in to comment.