diff --git a/moremesecons_commandblock/depends.txt b/moremesecons_commandblock/depends.txt index acaa924..a259a42 100644 --- a/moremesecons_commandblock/depends.txt +++ b/moremesecons_commandblock/depends.txt @@ -1 +1,2 @@ mesecons +moremesecons_utils diff --git a/moremesecons_commandblock/init.lua b/moremesecons_commandblock/init.lua index 0c33517..9ecc4d1 100644 --- a/moremesecons_commandblock/init.lua +++ b/moremesecons_commandblock/init.lua @@ -1,10 +1,5 @@ local function initialize_data(meta) - local NEAREST_MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_commandblock.nearest_max_distance")) or 8 - if NEAREST_MAX_DISTANCE <= 0 then - NEAREST_MAX_DISTANCE = 1 - elseif NEAREST_MAX_DISTANCE ~= NEAREST_MAX_DISTANCE then -- NaN - NEAREST_MAX_DISTANCE = 8 - end + local NEAREST_MAX_DISTANCE = moremesecons.setting("commandblock", "nearest_max_distance", 8, 1) local commands = meta:get_string("commands") meta:set_string("formspec", @@ -72,20 +67,13 @@ local function resolve_commands(commands, pos) end local function commandblock_action_on(pos, node) - local NEAREST_MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_commandblock.nearest_max_distance")) or 8 - if NEAREST_MAX_DISTANCE <= 0 then - NEAREST_MAX_DISTANCE = 1 - end + local NEAREST_MAX_DISTANCE = moremesecons.setting("commandblock", "nearest_max_distance", 8, 1) local accepted_commands = {} do - local commands_str = minetest.setting_get("moremesecons_commandblock.authorized_commands") - if commands_str then - for command in string.gmatch(commands_str, "([^ ]+)") do - accepted_commands[command] = true - end - else - accepted_commands = {tell = true} + local commands_str = moremesecons.setting("commandblock", "authorized_commands", "tell") + for command in string.gmatch(commands_str, "([^ ]+)") do + accepted_commands[command] = true end end diff --git a/moremesecons_jammer/depends.txt b/moremesecons_jammer/depends.txt index 9408116..b6d53b6 100644 --- a/moremesecons_jammer/depends.txt +++ b/moremesecons_jammer/depends.txt @@ -1,2 +1,3 @@ mesecons vector_extras +moremesecons_utils diff --git a/moremesecons_jammer/init.lua b/moremesecons_jammer/init.lua index d224e1a..95902f7 100644 --- a/moremesecons_jammer/init.lua +++ b/moremesecons_jammer/init.lua @@ -16,12 +16,7 @@ local function remove_jammer(pos) end local function is_jammed(pos) - local JAMMER_MAX_DISTANCE = tonumber(minetest.setting_get("moresecons_jammer.max_distance")) or 10 - if JAMMER_MAX_DISTANCE <= 0 then - JAMMER_MAX_DISTANCE = 1 - elseif JAMMER_MAX_DISTANCE ~= JAMMER_MAX_DISTANCE then -- NaN - JAMMER_MAX_DISTANCE = 10 - end + local JAMMER_MAX_DISTANCE = moremesecons.setting("jammer", "max_distance", 10, 1) local pz,py,px = vector.unpack(pos) for z,yxs in pairs(jammers) do diff --git a/moremesecons_playerkiller/depends.txt b/moremesecons_playerkiller/depends.txt index bc7b062..886b035 100644 --- a/moremesecons_playerkiller/depends.txt +++ b/moremesecons_playerkiller/depends.txt @@ -1,2 +1,3 @@ mesecons mesecons_materials +moremesecons_utils diff --git a/moremesecons_playerkiller/init.lua b/moremesecons_playerkiller/init.lua index 54ae332..04607a4 100644 --- a/moremesecons_playerkiller/init.lua +++ b/moremesecons_playerkiller/init.lua @@ -1,10 +1,5 @@ local kill_nearest_player = function(pos) - local MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_playerkiller.max_distance")) or 8 -- Use this number to set maximal distance to kill - if MAX_DISTANCE <= 0 then - MAX_DISTANCE = 8 - elseif MAX_DISTANCE ~= MAX_DISTANCE then -- NaN - MAX_DISTANCE = 8 - end + local MAX_DISTANCE = moremesecons.setting("playerkiller", "max_distance", 8, 1) -- Search the nearest player local nearest diff --git a/moremesecons_sayer/depends.txt b/moremesecons_sayer/depends.txt index 8bbc625..4c6f872 100644 --- a/moremesecons_sayer/depends.txt +++ b/moremesecons_sayer/depends.txt @@ -1,3 +1,4 @@ mesecons mesecons_noteblock +moremesecons_utils default diff --git a/moremesecons_sayer/init.lua b/moremesecons_sayer/init.lua index 71c023c..85b82ce 100644 --- a/moremesecons_sayer/init.lua +++ b/moremesecons_sayer/init.lua @@ -1,7 +1,4 @@ -local use_speech_dispatcher = minetest.setting_getbool("moremesecons_sayer.use_speech_dispatcher") -if use_speech_dispatcher == nil then - use_speech_dispatcher = true -end +local use_speech_dispatcher = moremesecons.setting("sayer", "use_speech_dispatcher", true) local popen, execute = io.popen, os.execute if use_speech_dispatcher then @@ -44,13 +41,7 @@ if use_speech_dispatcher then end function sayer_activate(pos) - local MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_sayer.max_distance")) or 8 - if MAX_DISTANCE <= 0 then - MAX_DISTANCE = 1 - elseif MAX_DISTANCE ~= MAX_DISTANCE then -- NaN - MAX_DISTANCE = 8 - end - MAX_DISTANCE = MAX_DISTANCE^2 + local MAX_DISTANCE = moremesecons.setting("sayer", "max_distance", 8, 1) ^ 2 local text = minetest.get_meta(pos):get_string("text") if text == "" then @@ -85,12 +76,7 @@ if use_speech_dispatcher then end else function sayer_activate(pos) - local MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_sayer.max_distance")) or 8 - if MAX_DISTANCE <= 0 then - MAX_DISTANCE = 1 - elseif MAX_DISTANCE ~= MAX_DISTANCE then -- NaN - MAX_DISTANCE = 8 - end + local MAX_DISTANCE = moremesecons.setting("sayer", "max_distance", 8, 1) local tab = { "Sayer at pos", diff --git a/moremesecons_teleporter/depends.txt b/moremesecons_teleporter/depends.txt index 9408116..b6d53b6 100644 --- a/moremesecons_teleporter/depends.txt +++ b/moremesecons_teleporter/depends.txt @@ -1,2 +1,3 @@ mesecons vector_extras +moremesecons_utils diff --git a/moremesecons_teleporter/init.lua b/moremesecons_teleporter/init.lua index eda95dd..baf5e8f 100644 --- a/moremesecons_teleporter/init.lua +++ b/moremesecons_teleporter/init.lua @@ -21,18 +21,8 @@ local function register(pos) end local function teleport_nearest(pos) - local MAX_TELEPORTATION_DISTANCE = tonumber(minetest.setting_get("moremesecons_teleporter.max_t2t_distance")) or 50 - if MAX_TELEPORTATION_DISTANCE <= 0 then - MAX_TELEPORTATION_DISTANCE = 1 - elseif MAX_TELEPORTATION_DISTANCE ~= MAX_TELEPORTATION_DISTANCE then -- NaN - MAX_TELEPORTATION_DISTANCE = 50 - end - local MAX_PLAYER_DISTANCE = tonumber(minetest.setting_get("moremesecons_teleporter.max_p2t_distance")) or 25 - if MAX_PLAYER_DISTANCE <= 0 then - MAX_PLAYER_DISTANCE = 1 - elseif MAX_PLAYER_DISTANCE ~= MAX_PLAYER_DISTANCE then -- NaN - MAX_PLAYER_DISTANCE = 25 - end + local MAX_TELEPORTATION_DISTANCE = moremesecons.setting("teleporter", "max_t2t_distance", 50, 1) + local MAX_PLAYER_DISTANCE = moremesecons.setting("teleporter", "max_p2t_distance", 25, 1) -- Search for the nearest player local nearest = nil @@ -110,7 +100,7 @@ minetest.register_node("moremesecons_teleporter:teleporter", { end, }) -if minetest.setting_getbool("moremesecons_teleporter.enable_lbm") then +if moremesecons.setting("teleporter", "enable_lbm", false) then minetest.register_lbm({ name = "moremesecons_teleporter:add_teleporter", nodenames = {"moremesecons_teleporter:teleporter"}, diff --git a/moremesecons_utils/depends.txt b/moremesecons_utils/depends.txt new file mode 100644 index 0000000..e69de29 diff --git a/moremesecons_utils/init.lua b/moremesecons_utils/init.lua new file mode 100644 index 0000000..de7193a --- /dev/null +++ b/moremesecons_utils/init.lua @@ -0,0 +1,28 @@ +moremesecons = {} + +function moremesecons.setting(modname, settingname, default, min, val_under_min) + local setting = "moremesecons_" .. modname .. "." .. settingname + + if type(default) == "bool" then + local ret = minetest.setting_getbool(setting) + if ret == nil then + ret = default + end + return ret + elseif type(default) == "string" then + return minetest.setting_get(setting) or default + elseif type(default) == "number" then + local ret = tonumber(minetest.setting_get(setting)) or default + if ret ~= ret then -- NaN + minetest.log("warning", "[moremesecons_"..modname.."]: setting '"..setting.."' is NaN. Set to default value "..tostring(default)..".") + ret = default + end + if min then + if ret < min then + minetest.log("warning", "[moremesecons_"..modname.."]: setting '"..setting.."' is under minimum value "..tostring(min)..". Set to "..tostring(val_under_min or min)..".") + ret = val_under_min or min + end + end + return ret + end +end diff --git a/moremesecons_wireless/depends.txt b/moremesecons_wireless/depends.txt index fc46f40..8da9e25 100644 --- a/moremesecons_wireless/depends.txt +++ b/moremesecons_wireless/depends.txt @@ -1,3 +1,4 @@ mesecons vector_extras +moremesecons_utils digilines? diff --git a/moremesecons_wireless/init.lua b/moremesecons_wireless/init.lua index d8bfa43..cf1e698 100644 --- a/moremesecons_wireless/init.lua +++ b/moremesecons_wireless/init.lua @@ -130,12 +130,7 @@ end -- looks big, but should work fast function is_jammed(pos) - local JAMMER_MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_wireless.jammer_max_distance")) or 15 - if JAMMER_MAX_DISTANCE <= 0 then - JAMMER_MAX_DISTANCE = 1 - elseif JAMMER_MAX_DISTANCE ~= JAMMER_MAX_DISTANCE then -- NaN - JAMMER_MAX_DISTANCE = 15 - end + local JAMMER_MAX_DISTANCE = moremesecons.setting("wireless", "jammer_max_distance", 15, 1) local pz,py,px = vector.unpack(pos) for z,yxs in pairs(jammers) do @@ -233,7 +228,7 @@ minetest.register_craft({ } }) -if minetest.setting_getbool("moremesecons_wireless.enable_lbm") then +if moremesecons.setting("wireless", "enable_lbm", false) then minetest.register_lbm({ name = "moremesecons_wireless:add_jammer", nodenames = {"moremesecons_wireless:jammer_on"},