126 lines
3.3 KiB
Lua

select_item = {}
select_item.predef_filters = {}
-- Creative Inventory items
select_item.predef_filters.creative = function(itemstring)
local itemdef = minetest.registered_items[itemstring]
if itemstring == "air" then
return false
end
if itemdef.description == nil or itemdef.description == "" then
return false
end
if minetest.get_item_group(itemstring, "not_in_creative_inventory") == 1 then
return false
end
return true
end
-- No filtering
select_item.predef_filters.all = function()
return true
end
local check_item = function(itemstring, filter)
local itemdef = minetest.registered_items[itemstring]
if itemstring == "" or itemstring == "unknown" or itemstring == "ignore" or itemdef == nil then
return
end
if type(filter) == "function" then
if filter(itemstring) == false then
return false
end
end
return true
end
local get_items = function(filter)
local it = {}
for itemstring, itemdef in pairs(minetest.registered_items) do
if check_item(itemstring, filter) then
table.insert(it, {itemstring=itemstring, itemdef=itemdef})
end
end
-- Sort alphabetically by itemstring
local compare = function(t1, t2)
return t1.itemstring < t2.itemstring
end
table.sort(it, compare)
return it
end
local xsize = 12
local ysize = 9
select_item.select_item = function(playername, filter, page)
local form = "size["..xsize..","..(ysize+1).."]"
local items = get_items(filter)
local x = 0
local y = 0.5
if page == nil then page = 1 end
local start = 1 + (page-1) * xsize * ysize
form = form .. "label[0,0;Select an item:]"
for i=start, #items do
local itemstring = items[i].itemstring
local itemdef = items[i].itemdef
local name = "item_"..itemstring
form = form .. "item_image_button["..x..","..y..";1,1;"..itemstring..";"..name..";]"
if itemdef.description == nil or itemdef.description == "" then
form = form .. "tooltip["..name..";"..itemstring.."]"
end
x = x + 1
if x >= xsize then
x = 0
y = y + 1
if y >= ysize then
break
end
end
end
local ynav = (ysize + 0.5)
form = form .. "button[0,"..ynav..";1,1;previous;<]"
form = form .. "button[1,"..ynav..";1,1;next;>]"
form = form .. "label[2,"..ynav..";"..minetest.formspec_escape(string.format("Page %d", page)).."]"
form = form .. "button_exit["..(xsize-2)..","..ynav..";2,1;cancel;Cancel]"
minetest.show_formspec(playername, "select_item:page"..page, form)
end
local callbacks = {}
select_item.register_callback = function(callback)
table.insert(callbacks, callback)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if string.sub(formname, 1, 16) == "select_item:page" then
local playername = player:get_player_name()
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
for i=1,#callbacks do
callbacks[i](playername, item)
end
end
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
local items = get_items()
local maxpage = (#items / (xsize*ysize)) + 1
if page + 1 < maxpage then
select_item.select_item(playername, nil, page + 1)
end
end
end
end
end)