From c41d0371c051d9402282db62cb9d1bb09dff0ae9 Mon Sep 17 00:00:00 2001 From: HybridDog Date: Mon, 8 Mar 2021 19:34:21 +0100 Subject: [PATCH] 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 --- moremesecons_commandblock/init.lua | 2 +- moremesecons_dual_delayer/init.lua | 8 ++++---- moremesecons_induction_transmitter/init.lua | 4 ---- moremesecons_luablock/init.lua | 20 ++++++++++---------- moremesecons_luacontroller_tool/init.lua | 19 +++++++++++-------- moremesecons_switchtorch/init.lua | 2 +- moremesecons_teleporter/init.lua | 6 +++--- moremesecons_timegate/init.lua | 2 +- moremesecons_utils/init.lua | 10 +++++----- moremesecons_wireless/init.lua | 7 +++---- 10 files changed, 39 insertions(+), 41 deletions(-) diff --git a/moremesecons_commandblock/init.lua b/moremesecons_commandblock/init.lua index 9826e93..024c6b3 100644 --- a/moremesecons_commandblock/init.lua +++ b/moremesecons_commandblock/init.lua @@ -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 diff --git a/moremesecons_dual_delayer/init.lua b/moremesecons_dual_delayer/init.lua index 436dc73..d4a9818 100644 --- a/moremesecons_dual_delayer/init.lua +++ b/moremesecons_dual_delayer/init.lua @@ -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 diff --git a/moremesecons_induction_transmitter/init.lua b/moremesecons_induction_transmitter/init.lua index 1ca1ba0..cca1e4c 100644 --- a/moremesecons_induction_transmitter/init.lua +++ b/moremesecons_induction_transmitter/init.lua @@ -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}) diff --git a/moremesecons_luablock/init.lua b/moremesecons_luablock/init.lua index 7ce07c6..24bc7ee 100644 --- a/moremesecons_luablock/init.lua +++ b/moremesecons_luablock/init.lua @@ -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 }} }) diff --git a/moremesecons_luacontroller_tool/init.lua b/moremesecons_luacontroller_tool/init.lua index ca53801..54b6cb0 100644 --- a/moremesecons_luacontroller_tool/init.lua +++ b/moremesecons_luacontroller_tool/init.lua @@ -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 diff --git a/moremesecons_switchtorch/init.lua b/moremesecons_switchtorch/init.lua index 9b0c1b7..2001f03 100644 --- a/moremesecons_switchtorch/init.lua +++ b/moremesecons_switchtorch/init.lua @@ -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 diff --git a/moremesecons_teleporter/init.lua b/moremesecons_teleporter/init.lua index 3647128..df1d03c 100644 --- a/moremesecons_teleporter/init.lua +++ b/moremesecons_teleporter/init.lua @@ -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 diff --git a/moremesecons_timegate/init.lua b/moremesecons_timegate/init.lua index 719ddd7..da614c4 100644 --- a/moremesecons_timegate/init.lua +++ b/moremesecons_timegate/init.lua @@ -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 }, diff --git a/moremesecons_utils/init.lua b/moremesecons_utils/init.lua index 6f79fde..a6cbcc2 100644 --- a/moremesecons_utils/init.lua +++ b/moremesecons_utils/init.lua @@ -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() +--]] diff --git a/moremesecons_wireless/init.lua b/moremesecons_wireless/init.lua index 268e7cf..28623ed 100644 --- a/moremesecons_wireless/init.lua +++ b/moremesecons_wireless/init.lua @@ -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)