From f9a64170fb1acb542b218c5440edadecb89b7d07 Mon Sep 17 00:00:00 2001 From: poikilos <7557867+poikilos@users.noreply.github.com> Date: Tue, 2 Jan 2024 19:20:45 -0500 Subject: [PATCH 1/2] Make a player clickable and list all known inventories, even those not in get_inv but encoded as Lua in metadata. --- init.lua | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 4 deletions(-) diff --git a/init.lua b/init.lua index 5c2a393..e923114 100644 --- a/init.lua +++ b/init.lua @@ -2,7 +2,7 @@ -- Metadata Tools -- -- A mod providing write and read access to a nodes' metadata using commands --- (c) 2015-2016 ßÿ Lymkwi/LeMagnesium/Mg and Paly2; (c) 2017-2022 Poikilos +-- (c) 2015-2016 ßÿ Lymkwi/LeMagnesium/Mg and Paly2; & (c) 2017-2024 Poikilos -- License: [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/) -- -- Version: Poikilos fork of 1.2.2 @@ -307,7 +307,10 @@ minetest.register_craftitem("metatools:stick",{ "[metatools::stick] You pointed at nothing." ) return - elseif pointed_thing.type == "object" then + elseif pointed_thing.type == "object" and pointed_thing.ref and pointed_thing.ref:get_luaentity() then + -- It must be a mob + -- (Otherwise [if not get_luaentity(),] it must be a player character, + -- so see next elseif case local pointedObjRef = pointed_thing.ref -- local objAsStr = minetest.serialize(pointedObjRef) -- ^ if param is pointed_thing or pointed_thing.ref, minetest.serialize causes "2021-11-14 16:45:39: ERROR[Main]: ServerError: AsyncErr: ServerThread::run Lua: Runtime error from mod 'metatools' in callback item_OnUse(): /home/owner/minetest/bin/../builtin/common/serialize.lua:151: Can't serialize data of type userdata" @@ -331,7 +334,8 @@ minetest.register_craftitem("metatools:stick",{ -- end -- minetest.log("action", "[metatools] You pointed at an object: " .. objAsStr) local luaEntity = pointedObjRef:get_luaentity() - -- INFO: For player name, use user:get_player_name() + -- NOTE: For player name, use user:get_player_name() + -- NOTE: luaEntity is nil when clicking player! minetest.chat_send_player( username, " LuaEntity.name: " .. luaEntity.name @@ -364,8 +368,119 @@ minetest.register_craftitem("metatools:stick",{ -- username, -- yamlSerializeTable(animation.range, " range") -- ) - -- else type is usually "node" + -- else will return early (See "if not nodepos or ..." below)! + elseif pointed_thing.type == "object" and pointed_thing.ref and pointed_thing.ref:is_player() then + -- NOTE pointed_thing.ref.get_luaentity() is nil in this and all successive cases + -- local pointedObjRef = pointed_thing.ref + local player = pointed_thing.ref + local inv = player:get_inventory() + if pointed_thing.ref:get_player_name() then + minetest.chat_send_player( + username, + " player name: " .. pointed_thing.ref:get_player_name() + ) + else + minetest.chat_send_player( + username, + " player name: nil" + ) + end + if pointed_thing.ref:get_entity_name() then + minetest.chat_send_player( + username, + " entity name: " .. pointed_thing.ref:get_entity_name() + ) + end + minetest.chat_send_player( + username, + " inventories:" + ) + -- local pointedEntitySAO = player:getluaobject() + -- local pointedPlayerSAO = player:getplayersao() + -- local pointedRemotePlayer = player:getplayer() + local inventories = { + ["main"] = 32, + ["hand"] = 1, + ["craft"] = 1, + ["craftpreview"] = 1, + ["craftresult"] = 1, + ["bag1"] = 1, + ["bag1contents"] = 32, + ["bag2"] = 1, + ["bag2contents"] = 32, + ["bag3"] = 1, + ["bag3contents"] = 32, + ["bag4"] = 1, + ["bag4contents"] = 32, + ["more_chests:wifi"] = 32, + ["enderchest"] = 32 + } + -- NOTE: "hunger" is also an inventory but it is used as a savable variable + for inv_name, inv_max in pairs(inventories) do + local i = 0 + local inv_count = 0 + local inv_block_text = "" + for y=1,4,1 do + local sep = "" + local line = "" + for x=1,8,1 do + i = i + 1 -- 1 to 32 + if i > inv_max then + break + end + line = line .. sep .. inv:get_stack(inv_name, i):get_name() + if inv:get_stack(inv_name, i):get_count() > 1 then + line = line .. " " .. inv:get_stack(inv_name, i):get_count() + inv_count = inv_count + 1 + end + sep = ", " + end + if i > inv_max then + break + end + inv_block_text = inv_block_text .. " - " .. line .. "\n" + end + if inv_count > 0 then + minetest.chat_send_player( + username, + " " .. inv_name .." inventory:\n" .. inv_block_text + ) + end + end + -- Next get player metadata "3d_armor_inventory" (contains a Lua return statement returning the 3d_armor table for the player) + local known_metas = {"3d_armor_inventory", "unified_inventory:bags", "sethome:home"} + for _, meta_name in pairs(known_metas) do + + local attribute_meta = player:get_meta() -- see also 3d_armor init.lua + -- Other attributes: local known_metas = {"hbsprint:sprinting", "hbsprint:stamina", "homedecor:player_skin", "hunger_ng:eating_timestamp", "hunger_ng:hunger", "hunger_ng:hunger_bar", "skinsdb:skin_key", "sprinting", "stamina"} + local content_lua = attribute_meta:get_string(meta_name) + if content_lua then + minetest.chat_send_player( + username, + " " .. meta_name .. " = " .. content_lua + ) + end + end + elseif pointed_thing.type == "object" and pointed_thing.ref then + -- NOTE pointed_thing.ref.get_luaentity() is nil in this and all successive cases + -- local pointedObjRef = pointed_thing.ref + minetest.chat_send_player( + username, + "A non-player non-Lua ObjRef was clicked (NotImplemented in this version of metatools):" + ) + if pointed_thing.ref:get_entity_name() then + minetest.chat_send_player( + username, + " entity name: " .. pointed_thing.ref:get_entity_name() + ) + else + minetest.chat_send_player( + username, + " entity name: nil" + ) + end end + local nodepos = pointed_thing.under -- > * `under` refers to the node position behind the pointed face -- > * `above` refers to the node position in front of the pointed face. From a0fc49e3be12e2862dd918b1a83f7d4e52842d6f Mon Sep 17 00:00:00 2001 From: poikilos <7557867+poikilos@users.noreply.github.com> Date: Wed, 3 Jan 2024 08:44:04 -0500 Subject: [PATCH 2/2] Fix: Count an inventory slot if >0 (not >1). Add comments. --- init.lua | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/init.lua b/init.lua index e923114..f9cf145 100644 --- a/init.lua +++ b/init.lua @@ -399,21 +399,21 @@ minetest.register_craftitem("metatools:stick",{ -- local pointedPlayerSAO = player:getplayersao() -- local pointedRemotePlayer = player:getplayer() local inventories = { - ["main"] = 32, - ["hand"] = 1, - ["craft"] = 1, - ["craftpreview"] = 1, - ["craftresult"] = 1, - ["bag1"] = 1, - ["bag1contents"] = 32, + ["main"] = 32, -- The regular inventory grid + ["hand"] = 1, -- item shown in hand in 3D (?) + ["craft"] = 9, -- the 3x3 area for crafting + ["craftpreview"] = 1, -- you can see but not take from preview + ["craftresult"] = 1, -- you take the item from this to craft it + ["bag1"] = 1, -- For bag itself (bag item placed here enables bag1contents) + ["bag1contents"] = 32, -- The items in the bag ["bag2"] = 1, ["bag2contents"] = 32, ["bag3"] = 1, ["bag3contents"] = 32, ["bag4"] = 1, ["bag4contents"] = 32, - ["more_chests:wifi"] = 32, - ["enderchest"] = 32 + ["more_chests:wifi"] = 32, -- on player since all chests are shared + ["enderchest"] = 32 -- legacy, TM Mojang. See wifi instead. } -- NOTE: "hunger" is also an inventory but it is used as a savable variable for inv_name, inv_max in pairs(inventories) do @@ -423,23 +423,27 @@ minetest.register_craftitem("metatools:stick",{ for y=1,4,1 do local sep = "" local line = "" - for x=1,8,1 do + local cols = 8 + if inv_max == 9 then + cols = 3 + end + for x=1,cols,1 do i = i + 1 -- 1 to 32 if i > inv_max then break end line = line .. sep .. inv:get_stack(inv_name, i):get_name() - if inv:get_stack(inv_name, i):get_count() > 1 then + if inv:get_stack(inv_name, i):get_count() > 0 then line = line .. " " .. inv:get_stack(inv_name, i):get_count() inv_count = inv_count + 1 end sep = ", " - end + end -- end for column if i > inv_max then break end inv_block_text = inv_block_text .. " - " .. line .. "\n" - end + end -- end for row (newline) if inv_count > 0 then minetest.chat_send_player( username, @@ -450,11 +454,10 @@ minetest.register_craftitem("metatools:stick",{ -- Next get player metadata "3d_armor_inventory" (contains a Lua return statement returning the 3d_armor table for the player) local known_metas = {"3d_armor_inventory", "unified_inventory:bags", "sethome:home"} for _, meta_name in pairs(known_metas) do - local attribute_meta = player:get_meta() -- see also 3d_armor init.lua -- Other attributes: local known_metas = {"hbsprint:sprinting", "hbsprint:stamina", "homedecor:player_skin", "hunger_ng:eating_timestamp", "hunger_ng:hunger", "hunger_ng:hunger_bar", "skinsdb:skin_key", "sprinting", "stamina"} local content_lua = attribute_meta:get_string(meta_name) - if content_lua then + if content_lua and content_lua ~= "" then minetest.chat_send_player( username, " " .. meta_name .. " = " .. content_lua