1 -- CC0/Unlicense system32 2020
3 local function parse_coord(c
)
4 c
= string.split(c
, " ")
5 return {x
= tonumber(c
[1] or 0), y
= tonumber(c
[2] or 0), z
= tonumber(c
[3] or 0)}
8 minetest
.register_chatcommand("cpeek", {
9 func
= function(params
)
10 local oldpos
= minetest
.localplayer
:get_pos()
12 local c
= parse_coord(params
)
13 local dist
= vector
.distance(c
, oldpos
)
14 local d
= tostring(c
.x
) .. "," .. tostring(c
.y
) .. "," .. tostring(c
.z
)
15 local f
= "size[10,10]\nlabel[0,0;Can access: " .. tostring(dist
< 6) .. "(" .. tostring(dist
) .. ")]\nlist[nodemeta:" .. d
.. ";main;0,0.5;9,3;]"
17 minetest
.localplayer
:set_pos(c
)
18 minetest
.show_formspec("ChestPeek", f
)
19 minetest
.localplayer
:set_pos(oldpos
)
24 local formspec_template
= "size[9,Y]label[0,0;L]button[8,0;1,1;up;^Up^]"
25 local formspec_base
= formspec_template
:gsub("Y", "4")
26 local formspec_base_label
= formspec_template
:gsub("Y", "4.5")
28 local formspec_item
= "\nitem_image_button[X,Y;1,1;I;N;]"
29 local formspec_item_label
= formspec_item
.. "\nlabel[X,Z;T]"
31 local function map(f
, t
)
33 for i
, v
in ipairs(t
) do
39 local inventories
= {}
41 -- include_label because i implemented the label then realized item buttons did it themselves
42 local function make_formspec(name
, items
, include_label
)
47 local form
= formspec_base
49 form
= formspec_base_label
52 -- color strip cause yellow is unreadible with default styling
53 form
= form
:gsub("L", minetest
.formspec_escape(minetest
.strip_colors(name
)))
55 for i
, v
in ipairs(items
) do
57 local y
= 1 + math
.floor((i
- 1) / 9) -- +1 for the shulker name
60 y
= y
+ (y
* 0.2) -- shifts each layer down a bit
63 local it
= formspec_item
65 it
= formspec_item_label
71 it
= it
:gsub("I", v
:get_name())
72 it
= it
:gsub("Z", y
+ 0.8)
73 it
= it
:gsub("T", v
:get_count())
75 it
= it
:gsub("I", v
:get_name() .. " " .. tostring(v
:get_count()))
78 local item_name
= "button" .. tostring(i
)
79 it
= it
:gsub("N", item_name
)
81 if minetest
.get_item_def(v
:get_name()).description
~= v
:get_description() then
82 it
= it
.. "tooltip[" .. item_name
.. ";" .. v
:get_description() .. "]"
91 local function get_items(item
)
92 local meta
= item
:get_metadata()
93 local list
= minetest
.deserialize(meta
)
99 local items
= map(ItemStack
, list
)
103 local function make_list(name
, items
, prevent_push
)
104 local fs
= make_formspec(name
, items
)
106 if not prevent_push
then
107 table.insert(inventories
, {name
= name
, items
= items
})
111 minetest
.show_formspec("PeekInventory", fs
)
115 local function show_form(shulker
)
116 make_list(shulker
:get_description(), get_items(shulker
))
119 local function top(list
)
123 minetest
.register_on_formspec_input(function(formname
, fields
)
124 if formname
== "PeekInventory" then
130 if fields
.up
and #inventories
> 1 then
131 table.remove(inventories
)
132 local t
= top(inventories
)
133 make_list(t
.name
, t
.items
, true)
137 for k
, v
in pairs(fields
) do
138 if k
:find("button") then
139 local idx
= tonumber(k
:match("([0-9]+)"))
140 local item
= top(inventories
).items
[idx
]
141 local iname
= item
:get_name()
142 if iname
:find("mcl_chests:.-_shulker_box") then
143 show_form(top(inventories
).items
[idx
])
145 elseif iname
:find("mcl_books:.-written_book") then
146 -- to be implemented with bookbot
147 -- bookbot.read(item)
154 minetest
.register_chatcommand("peek", {
155 description
= "Peek inside a Mineclone Shulker box.",
157 show_form(minetest
.localplayer
:get_wielded_item())