minetest_select_item/init.lua

93 lines
2.5 KiB
Lua
Raw Normal View History

2016-09-22 17:24:39 -07:00
select_item = {}
local get_items = function()
2016-09-22 17:48:49 -07:00
local it = {}
for itemstring, itemdef in pairs(minetest.registered_items) do
table.insert(it, {itemstring=itemstring, itemdef=itemdef})
end
return it
2016-09-22 17:24:39 -07:00
end
local check_item = function(itemstring, filter)
local itemdef = minetest.registered_items[itemstring]
2016-09-22 17:48:49 -07:00
if itemstring == "air" or itemstring == "ignore" or itemstring == "unknown" then
2016-09-22 17:24:39 -07:00
return false
end
if itemdef.description == nil or itemdef.description == "" then
return false
end
2016-09-22 17:54:45 -07:00
if itemdef.groups ~= nil and itemdef.groups.not_in_creative_inventory ~= nil then
2016-09-22 17:24:39 -07:00
return false
end
return true
end
local xsize = 18
local ysize = 9
2016-09-22 17:48:49 -07:00
select_item.select_item = function(playername, filter, page)
2016-09-22 17:24:39 -07:00
local form = "size["..xsize..","..(ysize+1).."]"
local items = get_items()
local x = 0
local y = 0.5
2016-09-22 17:48:49 -07:00
if page == nil then page = 1 end
local start = 1 + (page-1) * xsize * ysize
2016-09-22 17:24:39 -07:00
form = form .. "label[0,0;Select an item:]"
2016-09-22 17:48:49 -07:00
for i=start, #items do
local itemstring = items[i].itemstring
local itemdef = items[i].itemdef
2016-09-22 17:24:39 -07:00
if check_item(itemstring, filter) then
local name = "item_"..itemstring
form = form .. "item_image_button["..x..","..y..";1,1;"..itemstring..";"..name..";]"
x = x + 1
if x >= xsize then
x = 0
y = y + 1
if y >= ysize then
break
end
end
end
end
2016-09-22 17:48:49 -07:00
local ynav = (ysize + 0.5)
form = form .. "button[0,"..ynav..";1,1;previous;<]"
form = form .. "button[1,"..ynav..";1,1;next;>]"
2016-09-22 19:00:24 -07:00
form = form .. "button_exit["..(xsize-2)..","..ynav..";2,1;cancel;Cancel]"
2016-09-22 17:48:49 -07:00
minetest.show_formspec(playername, "select_item:page"..page, form)
2016-09-22 17:24:39 -07:00
end
2016-09-22 18:35:34 -07:00
local callbacks = {}
select_item.register_callback = function(callback)
2016-09-22 18:56:40 -07:00
table.insert(callbacks, callback)
2016-09-22 18:35:34 -07:00
end
2016-09-22 17:24:39 -07:00
minetest.register_on_player_receive_fields(function(player, formname, fields)
2016-09-22 17:48:49 -07:00
if string.sub(formname, 1, 16) == "select_item:page" then
2016-09-22 18:35:34 -07:00
local playername = player:get_player_name()
2016-09-22 18:54:41 -07:00
local item
for field,_ in pairs(fields) do
if string.sub(field, 1, 5) == "item_" then
item = string.sub(field, 6, string.len(field))
break
end
end
if item then
2016-09-22 18:56:40 -07:00
for i=1,#callbacks do
2016-09-22 19:00:24 -07:00
callbacks[i](playername, item)
2016-09-22 18:56:40 -07:00
end
2016-09-22 18:54:41 -07:00
end
2016-09-22 17:48:49 -07:00
local page = tonumber(string.sub(formname, 17, string.len(formname)))
if page ~= nil then
if fields.previous and page > 1 then
select_item.select_item(player:get_player_name(), nil, page - 1)
elseif fields.next then
2016-09-22 18:02:45 -07:00
local items = get_items()
local maxpage = (#items / (xsize*ysize))
if page + 1 < maxpage then
2016-09-22 18:35:34 -07:00
select_item.select_item(playername, nil, page + 1)
2016-09-22 18:02:45 -07:00
end
2016-09-22 17:48:49 -07:00
end
end
end
2016-09-22 17:24:39 -07:00
end)