From 4a15625de7652efcfd5c66e952ec92433c5d7042 Mon Sep 17 00:00:00 2001 From: cora Date: Tue, 19 Mar 2024 22:00:28 +0100 Subject: [PATCH] Add table.count function to mcl_util --- .luacheckrc | 1 + mods/CORE/mcl_util/init.lua | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/.luacheckrc b/.luacheckrc index 200ea3514..c75295608 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -216,6 +216,7 @@ read_globals = { "key_value_swap", "shuffle", reverse = { read_only = false }, + count = { read_only = false }, } }, diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 41675262a..bf3b45fda 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -36,6 +36,16 @@ function table.reverse(t) end end +function table.count(t, does_it_count) + local r = 0 + for k, v in pairs(t) do + if does_it_count == nil or ( type(does_it_count) == "function" and does_it_count(k, v) ) then + r = r + 1 + end + end + return r +end + local LOGGING_ON = minetest.settings:get_bool("mcl_logging_default", false) local LOG_MODULE = "[MCL2]" function mcl_util.mcl_log(message, module, bypass_default_logger)