From abe667472ebb18a41197edb1622e541636987f99 Mon Sep 17 00:00:00 2001 From: OgelGames Date: Fri, 3 Jul 2026 02:05:27 +1000 Subject: [PATCH] Localize global variables and strengthen luacheck config (#34) --- .luacheckrc | 22 ++-------------------- utils/actions.lua | 45 ++++++++++++++++++++++----------------------- utils/base.lua | 22 ++++++++++++++-------- utils/formspec.lua | 2 +- 4 files changed, 39 insertions(+), 52 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 5ee1cab..86dd45e 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,25 +1,7 @@ -std = "lua51+minetest" unused_args = false -allow_defined_top = true -max_line_length = 999 - -stds.minetest = { - read_globals = { - "minetest", - "VoxelManip", - "VoxelArea", - "PseudoRandom", - "ItemStack", - "default", - table = { - fields = { - "copy", - }, - }, - } -} read_globals = { + "minetest", + "default", "pipeworks", } - diff --git a/utils/actions.lua b/utils/actions.lua index c7bfb74..a6990b8 100644 --- a/utils/actions.lua +++ b/utils/actions.lua @@ -1,17 +1,18 @@ local function get_inventory_auth_string(player, meta, pos, label) - return player:get_player_name() .. " tried to access a locked " .. label .. " belonging to " .. meta:get_string("owner") .. " at " .. minetest.pos_to_string(pos) + local name = player:get_player_name() + local owner = meta:get_string("owner") + local pos_str = minetest.pos_to_string(pos) + return ("%s tried to access a locked %s belonging to %s at %s"):format(name, label, owner, pos_str) end - -function has_locked_chest_privilege(meta, player) +local function has_locked_chest_privilege(meta, player) if player:get_player_name() ~= meta:get_string("owner") then return false end return true end - -function get_allow_metadata_inventory_move(t) +local function get_allow_metadata_inventory_move(t) setmetatable(t, {__index={check_privs=has_locked_chest_privilege}}) local label, check_privs = t[1], t.check_privs return function(pos, from_list, from_index, to_list, to_index, count, player) @@ -24,7 +25,7 @@ function get_allow_metadata_inventory_move(t) end end -function get_allow_metadata_inventory_put(t) +local function get_allow_metadata_inventory_put(t) setmetatable(t, {__index={check_privs=has_locked_chest_privilege}}) local label, check_privs = t[1], t.check_privs return function(pos, listname, index, stack, player) @@ -37,7 +38,7 @@ function get_allow_metadata_inventory_put(t) end end -function get_allow_metadata_inventory_take(t) +local function get_allow_metadata_inventory_take(t) setmetatable(t, {__index={check_privs=has_locked_chest_privilege}}) local label, check_privs = t[1], t.check_privs return function(pos, listname, index, stack, player) @@ -50,41 +51,39 @@ function get_allow_metadata_inventory_take(t) end end - - local function get_inventory_action_string(player, pos, action, label) - return player:get_player_name() .. " moves stuff " .. action .. " locked " .. label .. " at " .. minetest.pos_to_string(pos) + local name = player:get_player_name() + local pos_str = minetest.pos_to_string(pos) + return ("%s moves stuff %s locked %s at %s"):format(name, action, label, pos_str) end - -function get_on_metadata_inventory_move(label) +local function get_on_metadata_inventory_move(label) return function(pos, from_list, from_index, to_list, to_index, count, player) minetest.log("action", get_inventory_action_string(player, pos, "in", label) ) end end -function get_on_metadata_inventory_put(label) +local function get_on_metadata_inventory_put(label) return function(pos, listname, index, stack, player) minetest.log("action", get_inventory_action_string(player, pos, "to", label)) end end -function get_on_metadata_inventory_take(label) +local function get_on_metadata_inventory_take(label) return function(pos, listname, index, stack, player) minetest.log("action", get_inventory_action_string(player, pos, "from", label)) end end - -actions = { - has_locked_chest_privilege = has_locked_chest_privilege, - get_allow_metadata_inventory_move = get_allow_metadata_inventory_move, - get_allow_metadata_inventory_put = get_allow_metadata_inventory_put, - get_allow_metadata_inventory_take = get_allow_metadata_inventory_take, - get_on_metadata_inventory_move = get_on_metadata_inventory_move, - get_on_metadata_inventory_put = get_on_metadata_inventory_put, - get_on_metadata_inventory_take = get_on_metadata_inventory_take, +local actions = { + has_locked_chest_privilege = has_locked_chest_privilege, + get_allow_metadata_inventory_move = get_allow_metadata_inventory_move, + get_allow_metadata_inventory_put = get_allow_metadata_inventory_put, + get_allow_metadata_inventory_take = get_allow_metadata_inventory_take, + get_on_metadata_inventory_move = get_on_metadata_inventory_move, + get_on_metadata_inventory_put = get_on_metadata_inventory_put, + get_on_metadata_inventory_take = get_on_metadata_inventory_take, } return actions diff --git a/utils/base.lua b/utils/base.lua index 92bd4bb..e5a32a4 100644 --- a/utils/base.lua +++ b/utils/base.lua @@ -1,6 +1,6 @@ -- NOTE: `require` is not allowed with mod security on -- `dofile` with a return on the module is used instead -generate_formspec_string = dofile(minetest.get_modpath("more_chests").."/utils/formspec.lua") +local generate_formspec_string = dofile(minetest.get_modpath("more_chests").."/utils/formspec.lua") local actions = dofile(minetest.get_modpath("more_chests").."/utils/actions.lua") local S = minetest.get_translator("more_chests") @@ -14,7 +14,7 @@ local function parse_action(value, default_getter) end end -function generate_chest_def(def) +local function generate_chest_def(def) -- TODO assert def.size in ("big", "small") local out = { description = def.description, @@ -59,12 +59,18 @@ function generate_chest_def(def) end, } -- register log actions, NOTE passing an anonymous function to avoid getting the default if not necessary - out.allow_metadata_inventory_move = parse_action(def.allow_metadata_inventory_move, function() actions.get_allow_metadata_inventory_move{def.type} end) - out.allow_metadata_inventory_put = parse_action(def.allow_metadata_inventory_put, function() actions.get_allow_metadata_inventory_put{def.type} end) - out.allow_metadata_inventory_take = parse_action(def.allow_metadata_inventory_take, function() actions.get_allow_metadata_inventory_take{def.type} end) - out.on_metadata_inventory_move = parse_action(def.on_metadata_inventory_move, function() actions.get_on_metadata_inventory_move(def.type) end) - out.on_metadata_inventory_put = parse_action(def.on_metadata_inventory_put, function() actions.get_on_metadata_inventory_put(def.type) end) - out.on_metadata_inventory_take = parse_action(def.on_metadata_inventory_take, function() actions.get_on_metadata_inventory_take(def.type) end) + out.allow_metadata_inventory_move = parse_action(def.allow_metadata_inventory_move, + function() actions.get_allow_metadata_inventory_move{def.type} end) + out.allow_metadata_inventory_put = parse_action(def.allow_metadata_inventory_put, + function() actions.get_allow_metadata_inventory_put{def.type} end) + out.allow_metadata_inventory_take = parse_action(def.allow_metadata_inventory_take, + function() actions.get_allow_metadata_inventory_take{def.type} end) + out.on_metadata_inventory_move = parse_action(def.on_metadata_inventory_move, + function() actions.get_on_metadata_inventory_move(def.type) end) + out.on_metadata_inventory_put = parse_action(def.on_metadata_inventory_put, + function() actions.get_on_metadata_inventory_put(def.type) end) + out.on_metadata_inventory_take = parse_action(def.on_metadata_inventory_take, + function() actions.get_on_metadata_inventory_take(def.type) end) -- if model is not a simple block handle node_box attribute if def.node_box then out.drawtype = "nodebox" diff --git a/utils/formspec.lua b/utils/formspec.lua index 1452e61..62d7157 100644 --- a/utils/formspec.lua +++ b/utils/formspec.lua @@ -1,4 +1,4 @@ -function generate(size, inventory_name) +local function generate(size, inventory_name) local cfg -- chest inventory name