Various code fixes detected by luacheck

* Luablock: Fix the missing position argument when executing the lua code fails
* Wireless: Fix the missing id argument
* Localize several variables
* Some other small changes, e.g. dead code removal
This commit is contained in:
HybridDog 2021-03-08 19:34:21 +01:00 committed by Pierre-Adrien Langrognet
parent 28292dc2a5
commit c41d0371c0
10 changed files with 39 additions and 41 deletions

@ -62,7 +62,7 @@ local function resolve_commands(commands, pos)
nearest = player:get_player_name()
end
end
new_commands = commands:gsub("@nearest", nearest)
local new_commands = commands:gsub("@nearest", nearest)
return new_commands, min_distance, new_commands ~= commands
end

@ -17,19 +17,19 @@ end
local dual_delayer_activate = function(pos, node)
mesecon.receptor_on(pos, {dual_delayer_get_output_rules(node)[1]}) -- Turn on the port 1
minetest.swap_node(pos, {name = "moremesecons_dual_delayer:dual_delayer_10", param2 = node.param2})
minetest.after(0.4, function(pos, node)
minetest.after(0.4, function()
mesecon.receptor_on(pos, {dual_delayer_get_output_rules(node)[2]}) -- Turn on the port 2
minetest.swap_node(pos, {name = "moremesecons_dual_delayer:dual_delayer_11", param2 = node.param2})
end, pos, node)
end)
end
local dual_delayer_deactivate = function(pos, node, link)
mesecon.receptor_off(pos, {dual_delayer_get_output_rules(node)[2]}) -- Turn off the port 2
minetest.swap_node(pos, {name = "moremesecons_dual_delayer:dual_delayer_10", param2 = node.param2})
minetest.after(0.4, function(pos, node)
minetest.after(0.4, function()
mesecon.receptor_off(pos, {dual_delayer_get_output_rules(node)[1]}) -- Turn off the port 1
minetest.swap_node(pos, {name = "moremesecons_dual_delayer:dual_delayer_00", param2 = node.param2})
end, pos, node)
end)
end

@ -18,10 +18,6 @@ local function induction_transmitter_get_output_rules(node)
return {vector.multiply(minetest.facedir_to_dir(node.param2), 2)}
end
local function induction_transmitter_get_virtual_output_rules(node)
return {minetest.facedir_to_dir(node.param2)}
end
local function act(pos, node, state)
minetest.swap_node(pos, {name = "moremesecons_induction_transmitter:induction_transmitter_"..state, param2 = node.param2})

@ -128,33 +128,33 @@ minetest.register_node("moremesecons_luablock:luablock", {
env.pos = table.copy(npos)
env.mem = minetest.deserialize(meta:get_string("mem")) or {}
local func, err
local func, err_syntax
if _VERSION == "Lua 5.1" then
func, err = loadstring(code)
func, err_syntax = loadstring(code)
if func then
setfenv(func, env)
end
else
func, err = load(code, nil, "t", env)
func, err_syntax = load(code, nil, "t", env)
end
if not func then
meta:set_string("errmsg", err)
make_formspec(meta, pos)
meta:set_string("errmsg", err_syntax)
make_formspec(meta, npos)
return
end
local good, err = pcall(func)
local good, err_runtime = pcall(func)
if not good then -- Runtime error
meta:set_string("errmsg", err)
make_formspec(meta, pos)
if not good then
meta:set_string("errmsg", err_runtime)
make_formspec(meta, npos)
return
end
meta:set_string("mem", minetest.serialize(env.mem))
meta:set_string("errmsg", "")
make_formspec(meta, pos)
make_formspec(meta, npos)
end
}}
})

@ -76,14 +76,17 @@ end]]
local file_path = minetest.get_worldpath().."/MoreMesecons_lctt"
-- load templates from a compressed file
local templates_file = io.open(file_path, "rb")
if templates_file then
local templates_raw = templates_file:read("*all")
io.close(templates_file)
if templates_raw
and templates_raw ~= "" then
for name,t in pairs(minetest.deserialize(minetest.decompress(templates_raw))) do
templates[name] = t
do
local templates_file = io.open(file_path, "rb")
if templates_file then
local templates_raw = templates_file:read("*all")
io.close(templates_file)
if templates_raw
and templates_raw ~= "" then
local data = minetest.deserialize(minetest.decompress(templates_raw))
for name,t in pairs(data) do
templates[name] = t
end
end
end
end

@ -80,7 +80,7 @@ minetest.register_node("moremesecons_switchtorch:switchtorch_on", {
selection_box = torch_selectionbox,
groups = {dig_immediate=3, not_in_creative_inventory = 1},
drop = "moremesecons_switchtorch:switchtorch_off",
light_source = LIGHT_MAX-5,
light_source = 9,
mesecons = {receptor = {
state = mesecon.state.on,
rules = torch_get_output_rules

@ -24,12 +24,12 @@ local function teleport_nearest(pos)
-- Search for the nearest player
local nearest = nil
local min_distance = MAX_PLAYER_DISTANCE
local min_distance_player = MAX_PLAYER_DISTANCE
local players = minetest.get_connected_players()
for _, player in pairs(players) do
local distance = vector.distance(pos, player:getpos())
if distance <= min_distance then
min_distance = distance
if distance <= min_distance_player then
min_distance_player = distance
nearest = player
end
end

@ -35,7 +35,7 @@ local function timegate_activate(pos, node)
end)
end
boxes = {{ -6/16, -8/16, -6/16, 6/16, -7/16, 6/16 }, -- the main slab
local boxes = {{ -6/16, -8/16, -6/16, 6/16, -7/16, 6/16 }, -- the main slab
{ -2/16, -7/16, -4/16, 2/16, -26/64, -3/16 }, -- the jeweled "on" indicator
{ -3/16, -7/16, -3/16, 3/16, -26/64, -2/16 },

@ -243,12 +243,11 @@ MapDataStorage.__index = {
indices[i] = ("%.17g"):format(vi)
values[i] = v
end
result = {
return minetest.serialize({
version = "MapDataStorage_v1",
indices = "return {" .. table.concat(indices, ",") .. "}",
values = minetest.serialize(values),
}
return minetest.serialize(result)
})
end,
}
MapDataStorage.deserialize = function(txtdata)
@ -317,6 +316,7 @@ end
--[[
-- This testing code shows an example usage of the MapDataStorage code
local function do_test()
print("Test if iter returns correct positions when a lot is set")
@ -377,5 +377,5 @@ local function do_test()
--~ data:iterAll()
end
--~ do_test()
do_test()
--]]

@ -186,10 +186,10 @@ function wireless_receptor_off(pos, id, network, check)
end
end
function activate_network(owner, channel)
local function activate_network(owner, channel)
local network = wireless[owner][channel]
for _, wl_pos in pairs(network.members) do
wireless_receptor_on(wl_pos, id, network)
for i, wl_pos in pairs(network.members) do
wireless_receptor_on(wl_pos, i, network)
end
end
@ -300,7 +300,6 @@ mesecon.register_node("moremesecons_wireless:wireless", {
mesecon.receptor_off(pos)
end,
after_place_node = function(pos, placer)
local placername = placer:get_player_name()
set_owner(pos, placer:get_player_name())
end,
on_receive_fields = function(pos, _, fields, player)