Add a moremesecons_utils mod to reduce redundant code

This commit is contained in:
upsilon 2017-04-19 11:08:58 +02:00
parent b9654cca02
commit 75abcb3077
No known key found for this signature in database
GPG Key ID: A80DAE1F266E1C3C
14 changed files with 49 additions and 66 deletions

View File

@ -1 +1,2 @@
mesecons
moremesecons_utils

View File

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

View File

@ -1,2 +1,3 @@
mesecons
vector_extras
moremesecons_utils

View File

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

View File

@ -1,2 +1,3 @@
mesecons
mesecons_materials
moremesecons_utils

View File

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

View File

@ -1,3 +1,4 @@
mesecons
mesecons_noteblock
moremesecons_utils
default

View File

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

View File

@ -1,2 +1,3 @@
mesecons
vector_extras
moremesecons_utils

View File

@ -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"},

View File

View File

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

View File

@ -1,3 +1,4 @@
mesecons
vector_extras
moremesecons_utils
digilines?

View File

@ -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"},