From 82f277c1ab04f4c141edc5b8ba13c413511e0c49 Mon Sep 17 00:00:00 2001 From: Aaron Suen Date: Wed, 18 Sep 2024 07:48:40 -0400 Subject: [PATCH] Add command to analyze lua entities --- szutil_basics/init.lua | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/szutil_basics/init.lua b/szutil_basics/init.lua index 9f0184d..be346a0 100644 --- a/szutil_basics/init.lua +++ b/szutil_basics/init.lua @@ -1,8 +1,8 @@ -- LUALOCALS < --------------------------------------------------------- -local ipairs, minetest, string, table - = ipairs, minetest, string, table -local string_format, table_concat, table_remove - = string.format, table.concat, table.remove +local ipairs, minetest, pairs, string, table + = ipairs, minetest, pairs, string, table +local string_format, table_concat, table_remove, table_sort + = string.format, table.concat, table.remove, table.sort -- LUALOCALS > --------------------------------------------------------- -- Chat command to completely destroy a player account, including auth. @@ -52,6 +52,35 @@ do }) end +-- Chat command to analyze entities +minetest.register_chatcommand("luaentities", { + description = "Summary of luaentities by type", + privs = {["debug"] = true}, + func = function() + local sum = {} + local keys = {} + for _, ent in pairs(minetest.luaentities) do + local n = ent.name or "?" + if not sum[n] then + sum[n] = 1 + keys[#keys + 1] = n + else + sum[n] = sum[n] + 1 + end + end + table_sort(keys, function(a, b) + local sa = sum[a] + local sb = sum[b] + return sa == sb and a < b or sa > sb + end) + for i = 1, #keys do + local k = keys[i] + keys[i] = string_format("%q=%d", k, sum[k]) + end + return true, table_concat(keys, ", ") + end + }) + -- Moderators and admins can always bypass player limits, -- important for emergency access. minetest.register_can_bypass_userlimit(function(name)