From af9973f8d716fbbcc576e6425dc8dca3e7025e77 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 16 Jan 2024 15:43:42 +0100 Subject: [PATCH] registered item list with unknown node marker --- handlers/items.lua | 3 +++ init.lua | 8 +++++++- items.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ items.spec.lua | 12 ++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 handlers/items.lua create mode 100644 items.lua create mode 100644 items.spec.lua diff --git a/handlers/items.lua b/handlers/items.lua new file mode 100644 index 0000000..6e2830c --- /dev/null +++ b/handlers/items.lua @@ -0,0 +1,3 @@ +mtui.register_on_command("get_item_list", function() + return mtui.items +end) \ No newline at end of file diff --git a/init.lua b/init.lua index da6b846..458ee8b 100644 --- a/init.lua +++ b/init.lua @@ -6,8 +6,11 @@ end print("[mtui] initializing") mtui = { + mod_storage = minetest.get_mod_storage(), url = minetest.settings:get("mtui.url") or "http://127.0.0.1:8080", - key = minetest.settings:get("mtui.key") + key = minetest.settings:get("mtui.key"), + -- map of name to bool (known node) + items = {} } assert(mtui.key, "no key found") @@ -18,6 +21,7 @@ loadfile(MP.."/bridge_tx.lua")(http) dofile(MP.."/common.lua") dofile(MP.."/tan.lua") dofile(MP.."/stats.lua") +dofile(MP.."/items.lua") dofile(MP.."/log.lua") dofile(MP.."/log_file.lua") dofile(MP.."/log_technic.lua") @@ -27,6 +31,7 @@ dofile(MP.."/controls/builtin.lua") dofile(MP.."/controls/restart_if_empty.lua") dofile(MP.."/handlers/ping.lua") +dofile(MP.."/handlers/items.lua") dofile(MP.."/handlers/chat.lua") dofile(MP.."/handlers/execute_command.lua") dofile(MP.."/handlers/lua.lua") @@ -61,6 +66,7 @@ end if minetest.get_modpath("mtt") and mtt.enabled then dofile(MP.."/mtt.lua") dofile(MP.."/common.spec.lua") + dofile(MP.."/items.spec.lua") end if minetest.get_modpath("monitoring") then diff --git a/items.lua b/items.lua new file mode 100644 index 0000000..83adc70 --- /dev/null +++ b/items.lua @@ -0,0 +1,42 @@ + +local function check_registered_items() + mtui.items = minetest.deserialize(mtui.mod_storage:get_string("registered_items")) or {} + + -- assemble node-list from registered lbm's + local lbm_nodes = {} + for _, lbm in ipairs(minetest.registered_lbms) do + if type(lbm.nodenames) == "string" then + -- duh, list as string + lbm_nodes[lbm.nodenames] = true + else + -- proper list, add all regardless if they are a "group:*" + for _, nodename in ipairs(lbm.nodenames) do + lbm_nodes[nodename] = true + end + end + end + + -- mark all currently registered items + for name in pairs(minetest.registered_items) do + mtui.items[name] = true + end + + -- check for items that don't exist anymore + for name in pairs(mtui.items) do + if not minetest.registered_items[name] and + not minetest.registered_aliases[name] and + not lbm_nodes[name] then + -- item does not exist anymore, mark it as such + mtui.items[name] = false + end + end + + -- save current list back + mtui.mod_storage:set_string("registered_items", minetest.serialize(mtui.items)) +end + +-- execute check after mods loaded and other things are set up +minetest.register_on_mods_loaded(function() + minetest.after(0, check_registered_items) +end) + diff --git a/items.spec.lua b/items.spec.lua new file mode 100644 index 0000000..0bb3694 --- /dev/null +++ b/items.spec.lua @@ -0,0 +1,12 @@ + +mtt.register("items", function(callback) + assert(mtui.items) + + local count = 0 + for _ in pairs(mtui.items) do + count = count + 1 + end + + assert(count > 0) + callback() +end) \ No newline at end of file