Validate user inputs (#39)

This commit is contained in:
tour 2024-12-19 19:47:36 +01:00 committed by GitHub
parent 79b72e8b76
commit 3e93b939f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 8 deletions

@ -2,6 +2,8 @@
-- Detects players in a certain radius -- Detects players in a certain radius
-- The radius can be changes by right-click (by default 6) -- The radius can be changes by right-click (by default 6)
local MAX_RADIUS = moremesecons.setting("adjustable_player_detector", "max_radius", 16, 0)
local function make_formspec(meta) local function make_formspec(meta)
meta:set_string("formspec", "size[9,5]" .. meta:set_string("formspec", "size[9,5]" ..
"field[0.3, 0;9,2;scanname;Comma-separated list of the names of players to scan for (empty for any):;${scanname}]".. "field[0.3, 0;9,2;scanname;Comma-separated list of the names of players to scan for (empty for any):;${scanname}]"..
@ -36,7 +38,7 @@ local object_detector_scan = function (pos)
local scanname = meta:get_string("scanname") local scanname = meta:get_string("scanname")
local scan_all = scanname == "" local scan_all = scanname == ""
local scan_names = scanname:split(',') local scan_names = scanname:split(',')
local radius = meta:get_int("radius") local radius = math.min(meta:get_int("radius"), MAX_RADIUS)
if radius <= 0 then if radius <= 0 then
radius = 6 radius = 6
end end
@ -76,11 +78,11 @@ local object_detector_digiline = {
make_formspec(meta) make_formspec(meta)
end end
end end
if msg.scanname then if type(msg.scanname) == "string" then
meta:set_string("scanname", msg.scanname) meta:set_string("scanname", msg.scanname)
make_formspec(meta) make_formspec(meta)
end end
if msg.command and msg.command == "get" then if msg.command == "get" then
local found, name = object_detector_scan(pos) local found, name = object_detector_scan(pos)
if not found then if not found then
name = "" name = ""

@ -2,6 +2,8 @@
-- Detects entitys in a certain radius -- Detects entitys in a certain radius
-- The radius can be changes by right-click (by default 6) -- The radius can be changes by right-click (by default 6)
local MAX_RADIUS = moremesecons.setting("entity_detector", "max_radius", 16, 0)
local function make_formspec(meta) local function make_formspec(meta)
meta:set_string("formspec", "size[9,5]" .. meta:set_string("formspec", "size[9,5]" ..
"field[0.3, 0;9,2;scanname;Comma-separated list of the names (itemstring) of entities to scan for (empty for any):;${scanname}]".. "field[0.3, 0;9,2;scanname;Comma-separated list of the names (itemstring) of entities to scan for (empty for any):;${scanname}]"..
@ -26,8 +28,7 @@ local function object_detector_on_receive_fields(pos, _, fields, player)
meta:set_string("digiline_channel", fields.digiline_channel) meta:set_string("digiline_channel", fields.digiline_channel)
local r = tonumber(fields.radius) local r = tonumber(fields.radius)
if r then if r then
local max_radius = moremesecons.setting("entity_detector", "max_radius", 16, 0) meta:set_int("radius", math.min(r, MAX_RADIUS))
meta:set_int("radius", math.min(r, max_radius))
end end
end end
@ -37,8 +38,7 @@ local object_detector_scan = function (pos)
local scanname = meta:get_string("scanname") local scanname = meta:get_string("scanname")
local scan_all = scanname == "" local scan_all = scanname == ""
local scan_names = scanname:split(',') local scan_names = scanname:split(',')
local max_radius = moremesecons.setting("entity_detector", "max_radius", 16, 0) local radius = math.min(tonumber(meta:get("radius")) or 6, MAX_RADIUS)
local radius = math.min(tonumber(meta:get("radius")) or 6, max_radius)
for _,obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do for _,obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do
local luaentity = obj:get_luaentity() local luaentity = obj:get_luaentity()
if luaentity then if luaentity then
@ -62,7 +62,7 @@ local object_detector_digiline = {
action = function (pos, node, channel, msg) action = function (pos, node, channel, msg)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local active_channel = meta:get_string("digiline_channel") local active_channel = meta:get_string("digiline_channel")
if channel ~= active_channel then if channel ~= active_channel or type(msg) ~= "string" then
return return
end end
meta:set_string("scanname", msg) meta:set_string("scanname", msg)

@ -3,6 +3,10 @@
# Minimal interval authorized. Any lower will be set to it. # Minimal interval authorized. Any lower will be set to it.
moremesecons_adjustable_blinky_plant.min_interval (Minimum Interval) float 0.5 moremesecons_adjustable_blinky_plant.min_interval (Minimum Interval) float 0.5
[Adjustable Player Detector]
moremesecons_adjustable_player_detector.max_radius (Maximum adjustable player detector radius) float 16 0
[Craftable Commandblock] [Craftable Commandblock]
# Space-separated list of authorized commands # Space-separated list of authorized commands