mirror of
https://github.com/minetest-mods/MoreMesecons.git
synced 2024-12-29 17:07:35 +01:00
Add a moremesecons_utils mod to reduce redundant code
This commit is contained in:
parent
b9654cca02
commit
75abcb3077
@ -1 +1,2 @@
|
|||||||
mesecons
|
mesecons
|
||||||
|
moremesecons_utils
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
local function initialize_data(meta)
|
local function initialize_data(meta)
|
||||||
local NEAREST_MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_commandblock.nearest_max_distance")) or 8
|
local NEAREST_MAX_DISTANCE = moremesecons.setting("commandblock", "nearest_max_distance", 8, 1)
|
||||||
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 commands = meta:get_string("commands")
|
local commands = meta:get_string("commands")
|
||||||
meta:set_string("formspec",
|
meta:set_string("formspec",
|
||||||
@ -72,20 +67,13 @@ local function resolve_commands(commands, pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function commandblock_action_on(pos, node)
|
local function commandblock_action_on(pos, node)
|
||||||
local NEAREST_MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_commandblock.nearest_max_distance")) or 8
|
local NEAREST_MAX_DISTANCE = moremesecons.setting("commandblock", "nearest_max_distance", 8, 1)
|
||||||
if NEAREST_MAX_DISTANCE <= 0 then
|
|
||||||
NEAREST_MAX_DISTANCE = 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local accepted_commands = {}
|
local accepted_commands = {}
|
||||||
do
|
do
|
||||||
local commands_str = minetest.setting_get("moremesecons_commandblock.authorized_commands")
|
local commands_str = moremesecons.setting("commandblock", "authorized_commands", "tell")
|
||||||
if commands_str then
|
for command in string.gmatch(commands_str, "([^ ]+)") do
|
||||||
for command in string.gmatch(commands_str, "([^ ]+)") do
|
accepted_commands[command] = true
|
||||||
accepted_commands[command] = true
|
|
||||||
end
|
|
||||||
else
|
|
||||||
accepted_commands = {tell = true}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
mesecons
|
mesecons
|
||||||
vector_extras
|
vector_extras
|
||||||
|
moremesecons_utils
|
||||||
|
@ -16,12 +16,7 @@ local function remove_jammer(pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function is_jammed(pos)
|
local function is_jammed(pos)
|
||||||
local JAMMER_MAX_DISTANCE = tonumber(minetest.setting_get("moresecons_jammer.max_distance")) or 10
|
local JAMMER_MAX_DISTANCE = moremesecons.setting("jammer", "max_distance", 10, 1)
|
||||||
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 pz,py,px = vector.unpack(pos)
|
local pz,py,px = vector.unpack(pos)
|
||||||
for z,yxs in pairs(jammers) do
|
for z,yxs in pairs(jammers) do
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
mesecons
|
mesecons
|
||||||
mesecons_materials
|
mesecons_materials
|
||||||
|
moremesecons_utils
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
local kill_nearest_player = function(pos)
|
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
|
local MAX_DISTANCE = moremesecons.setting("playerkiller", "max_distance", 8, 1)
|
||||||
if MAX_DISTANCE <= 0 then
|
|
||||||
MAX_DISTANCE = 8
|
|
||||||
elseif MAX_DISTANCE ~= MAX_DISTANCE then -- NaN
|
|
||||||
MAX_DISTANCE = 8
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Search the nearest player
|
-- Search the nearest player
|
||||||
local nearest
|
local nearest
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
mesecons
|
mesecons
|
||||||
mesecons_noteblock
|
mesecons_noteblock
|
||||||
|
moremesecons_utils
|
||||||
default
|
default
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
local use_speech_dispatcher = minetest.setting_getbool("moremesecons_sayer.use_speech_dispatcher")
|
local use_speech_dispatcher = moremesecons.setting("sayer", "use_speech_dispatcher", true)
|
||||||
if use_speech_dispatcher == nil then
|
|
||||||
use_speech_dispatcher = true
|
|
||||||
end
|
|
||||||
|
|
||||||
local popen, execute = io.popen, os.execute
|
local popen, execute = io.popen, os.execute
|
||||||
if use_speech_dispatcher then
|
if use_speech_dispatcher then
|
||||||
@ -44,13 +41,7 @@ if use_speech_dispatcher then
|
|||||||
end
|
end
|
||||||
|
|
||||||
function sayer_activate(pos)
|
function sayer_activate(pos)
|
||||||
local MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_sayer.max_distance")) or 8
|
local MAX_DISTANCE = moremesecons.setting("sayer", "max_distance", 8, 1) ^ 2
|
||||||
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 text = minetest.get_meta(pos):get_string("text")
|
local text = minetest.get_meta(pos):get_string("text")
|
||||||
if text == "" then
|
if text == "" then
|
||||||
@ -85,12 +76,7 @@ if use_speech_dispatcher then
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
function sayer_activate(pos)
|
function sayer_activate(pos)
|
||||||
local MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_sayer.max_distance")) or 8
|
local MAX_DISTANCE = moremesecons.setting("sayer", "max_distance", 8, 1)
|
||||||
if MAX_DISTANCE <= 0 then
|
|
||||||
MAX_DISTANCE = 1
|
|
||||||
elseif MAX_DISTANCE ~= MAX_DISTANCE then -- NaN
|
|
||||||
MAX_DISTANCE = 8
|
|
||||||
end
|
|
||||||
|
|
||||||
local tab = {
|
local tab = {
|
||||||
"Sayer at pos",
|
"Sayer at pos",
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
mesecons
|
mesecons
|
||||||
vector_extras
|
vector_extras
|
||||||
|
moremesecons_utils
|
||||||
|
@ -21,18 +21,8 @@ local function register(pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function teleport_nearest(pos)
|
local function teleport_nearest(pos)
|
||||||
local MAX_TELEPORTATION_DISTANCE = tonumber(minetest.setting_get("moremesecons_teleporter.max_t2t_distance")) or 50
|
local MAX_TELEPORTATION_DISTANCE = moremesecons.setting("teleporter", "max_t2t_distance", 50, 1)
|
||||||
if MAX_TELEPORTATION_DISTANCE <= 0 then
|
local MAX_PLAYER_DISTANCE = moremesecons.setting("teleporter", "max_p2t_distance", 25, 1)
|
||||||
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
|
|
||||||
|
|
||||||
-- Search for the nearest player
|
-- Search for the nearest player
|
||||||
local nearest = nil
|
local nearest = nil
|
||||||
@ -110,7 +100,7 @@ minetest.register_node("moremesecons_teleporter:teleporter", {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
if minetest.setting_getbool("moremesecons_teleporter.enable_lbm") then
|
if moremesecons.setting("teleporter", "enable_lbm", false) then
|
||||||
minetest.register_lbm({
|
minetest.register_lbm({
|
||||||
name = "moremesecons_teleporter:add_teleporter",
|
name = "moremesecons_teleporter:add_teleporter",
|
||||||
nodenames = {"moremesecons_teleporter:teleporter"},
|
nodenames = {"moremesecons_teleporter:teleporter"},
|
||||||
|
0
moremesecons_utils/depends.txt
Normal file
0
moremesecons_utils/depends.txt
Normal file
28
moremesecons_utils/init.lua
Normal file
28
moremesecons_utils/init.lua
Normal 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
|
@ -1,3 +1,4 @@
|
|||||||
mesecons
|
mesecons
|
||||||
vector_extras
|
vector_extras
|
||||||
|
moremesecons_utils
|
||||||
digilines?
|
digilines?
|
||||||
|
@ -130,12 +130,7 @@ end
|
|||||||
|
|
||||||
-- looks big, but should work fast
|
-- looks big, but should work fast
|
||||||
function is_jammed(pos)
|
function is_jammed(pos)
|
||||||
local JAMMER_MAX_DISTANCE = tonumber(minetest.setting_get("moremesecons_wireless.jammer_max_distance")) or 15
|
local JAMMER_MAX_DISTANCE = moremesecons.setting("wireless", "jammer_max_distance", 15, 1)
|
||||||
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 pz,py,px = vector.unpack(pos)
|
local pz,py,px = vector.unpack(pos)
|
||||||
for z,yxs in pairs(jammers) do
|
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({
|
minetest.register_lbm({
|
||||||
name = "moremesecons_wireless:add_jammer",
|
name = "moremesecons_wireless:add_jammer",
|
||||||
nodenames = {"moremesecons_wireless:jammer_on"},
|
nodenames = {"moremesecons_wireless:jammer_on"},
|
||||||
|
Loading…
Reference in New Issue
Block a user