summaryrefslogtreecommitdiff
path: root/inspector/init.lua
blob: b09596426cb97319713b4503ead4cd5a5daed759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

--[[

Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>

"inspector" is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1
of the license, or (at your option) any later version.

--]]

local function make_fs(title, desc)
	return "size[12,8]"..
		"label[0.2,0.2;"..title.."]"..
		"textlist[0.2,1.0;11.5,7;;"..
		desc:gsub("\n", ",").."]"..
		"button_exit[11.1,0.2;0.8,0.8;close;x]"
end

local function inspect_pos(pos)
	local node = minetest.get_node(pos)

	local desc = "===== node data =====\n"
	desc = desc .. "name = " .. node.name .. "\n"
	desc = desc .. "param1 = " .. node.param1 .. "\n"
	desc = desc .. "param2 = " .. node.param2 .. "\n"
	local light = minetest.get_node_light({x = pos.x, y = pos.y + 1, z = pos.x}, nil)
	if light then
		desc = desc .. "light = " .. light .. "\n"
	end

	local timer = minetest.get_node_timer(pos)
		if timer:get_timeout() ~= 0 then
		desc = desc .. "==== node timer ====\n"
		desc = desc .. "timeout = " .. timer:get_timeout() .. "\n"
		desc = desc .. "elapsed = " .. timer:get_elapsed() .. "\n"
	end

	local meta = minetest.get_meta(pos)
	local table = meta:to_table()
	local fields = minetest.serialize(table.fields)
	desc = desc .. "==== meta ====\n"
	desc = desc .. "meta.fields = " .. fields .. "\n"
	desc = desc .. "\n"

	local inventory = meta:get_inventory()
	desc = desc .. "meta.inventory = \n"
	for key, list in pairs(inventory:get_lists()) do
		desc = desc .. key .. " : \n"
		local size = #list
		for i = 1, size do
			local stack = list[i]
			if not stack:is_empty() then
				desc = desc .. "\"" .. stack:get_name() .. "\" - " .. stack:get_count() .. "\n"
			end
		end
	end

	local nodedef = minetest.registered_items[node.name]
	if nodedef then  -- Some built in nodes have no nodedef
		desc = desc .. "==== nodedef ====\n"
		desc = desc .. dump(nodedef) .. "\n"
	end

	return minetest.formspec_escape(desc:gsub(",", "\\,"))
end

minetest.register_tool("inspector:inspector", {
	description = "Inspector Tool",
	inventory_image = "inspector.png",
	liquids_pointable = true, -- makes it hard to use underwater.

	on_use = function(itemstack, user, pointed_thing)

		local desc = ""
		local title = ""
		if pointed_thing.type == "nothing" then
			return
		elseif pointed_thing.type == "node" then
			title = "Node information"

			local pos = pointed_thing.under

			if pointed_thing.type ~= "node" then
				desc = "..."
			else
				desc = inspect_pos(pos)
			end

		elseif pointed_thing.type == "object" then
			title = "Object information"

			local ref = pointed_thing.ref
			local entity = ref:get_luaentity()
			desc = minetest.formspec_escape(dump(entity):gsub("\n\n", "\n"))

		end

		minetest.show_formspec(user:get_player_name(), "inspector:inspector", make_fs(title, desc))
	end,
})

minetest.register_chatcommand("inspect", {
	params = "inspect",
	description = "inspect a pos",
	privs = {server = true},
	func = function(name, param)
		local paramlist = {}
		for k in string.gmatch(param, "[^%s]+") do table.insert(paramlist, k) end
		local pos = {x = paramlist[1], y = paramlist[2], z = paramlist[3]}
		local desc = inspect_pos(pos)

		minetest.show_formspec(name, "inspector:inspector", make_fs("Node information", desc))
		return true
	end,
})