incrementaltp: respect physics overrides
[waspsaliva.git] / clientmods / peek / init.lua
bloba65cf219f0408905b730cbc6a18dab0f1c93e704
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)}
6 end
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)
20 end
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)
32 local out = {}
33 for i, v in ipairs(t) do
34 out[i] = f(v)
35 end
36 return out
37 end
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)
43 if items == nil then
44 return nil
45 end
47 local form = formspec_base
48 if include_label then
49 form = formspec_base_label
50 end
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
56 local x = (i - 1) % 9
57 local y = 1 + math.floor((i - 1) / 9) -- +1 for the shulker name
59 if include_label then
60 y = y + (y * 0.2) -- shifts each layer down a bit
61 end
63 local it = formspec_item
64 if include_label then
65 it = formspec_item_label
66 end
68 it = it:gsub("X", x)
69 it = it:gsub("Y", y)
70 if include_label then
71 it = it:gsub("I", v:get_name())
72 it = it:gsub("Z", y + 0.8)
73 it = it:gsub("T", v:get_count())
74 else
75 it = it:gsub("I", v:get_name() .. " " .. tostring(v:get_count()))
76 end
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() .. "]"
83 end
85 form = form .. it
86 end
88 return form
89 end
91 local function get_items(item)
92 local meta = item:get_metadata()
93 local list = minetest.deserialize(meta)
95 if list == nil then
96 return
97 end
99 local items = map(ItemStack, list)
100 return items
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})
110 if fs ~= nil then
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)
120 return list[#list]
123 minetest.register_on_formspec_input(function(formname, fields)
124 if formname == "PeekInventory" then
125 if fields.quit then
126 inventories = {}
127 return true
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)
134 return 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])
144 return true
145 elseif iname:find("mcl_books:.-written_book") then
146 -- to be implemented with bookbot
147 -- bookbot.read(item)
152 end)
154 minetest.register_chatcommand("peek", {
155 description = "Peek inside a Mineclone Shulker box.",
156 func = function()
157 show_form(minetest.localplayer:get_wielded_item())