Add tool to inspect node metadata

This commit is contained in:
Elias Åström 2023-05-07 11:11:59 +02:00
parent 0cda458d2e
commit 651fd0843a
2 changed files with 82 additions and 0 deletions

79
metatool/init.lua Normal file

@ -0,0 +1,79 @@
local function show_message(username, message)
local vsize = 2
minetest.show_formspec(
username,
"doc_identifier:error_missing_item_info",
"size[10,"..vsize..";]" ..
"textarea[0.5,0.2;10,"..(vsize-0.2)..";;;"..minetest.formspec_escape(message).."]" ..
"button_exit[3.75,"..(-0.5+vsize)..";3,1;okay;"..minetest.formspec_escape("OK").."]"
)
end
local function show_meta(itemstack, user, pointed_thing)
local username = user:get_player_name()
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
local meta = {}
for k, v in pairs(minetest.get_meta(pointed_thing.under):to_table().fields) do
meta[k] = minetest.deserialize(v)
end
local msg =
"name = " .. tostring(node.name) .. "\n" ..
"param1 = " .. tostring(node.param1) .. "\n" ..
"param2 = " .. tostring(node.param2) .. "\n" ..
"meta = " .. minetest.serialize(meta):gsub("^return ", "") .. "\n"
show_message(username, msg)
end
end
local function solid_mode(itemstack, user, pointed_thing)
-- Use pointed node's on_rightclick function first, if present
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
return ItemStack("metatool:metatool_solid")
end
local function liquid_mode(itemstack, user, pointed_thing)
-- Use pointed node's on_rightclick function first, if present
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
return ItemStack("metatool:metatool_liquid")
end
minetest.register_tool("metatool:metatool_solid", {
description = "Tool to inspect node metadata",
inventory_image = "doc_identifier_identifier.png^[invert:rgb",
_mcl_toollike_wield = true,
liquids_pointable = false,
on_use = show_meta,
on_secondary_use = liquid_mode,
on_place = liquid_mode,
})
minetest.register_tool("metatool:metatool_liquid", {
description = "Tool to inspect node metadata",
inventory_image = "doc_identifier_identifier_liquid.png^[invert:rgb",
_mcl_toollike_wield = true,
liquids_pointable = true,
on_use = show_meta,
on_secondary_use = solid_mode,
on_place = solid_mode,
})
minetest.register_alias("metatool:metatool", "metatool:metatool_solid")

3
metatool/mod.conf Normal file

@ -0,0 +1,3 @@
name = metatool
description = Tool to inspect node metadata
author = ryvnf