Import changes from xdecor

This commit is contained in:
Jean-Patrick Guerrero 2016-03-17 23:39:24 +01:00
parent 91cdc435dc
commit 1b25d7ab98

View File

@ -1,4 +1,4 @@
local craftguide, datas = {}, {} local craftguide, datas, npp = {}, {}, 8*3
function craftguide:get_recipe(item) function craftguide:get_recipe(item)
if item:sub(1,6) == "group:" then if item:sub(1,6) == "group:" then
@ -14,14 +14,8 @@ function craftguide:get_recipe(item)
return item return item
end end
function craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, player_name) function craftguide:get_formspec(player_name, pagenum, recipe_num)
local inv_size = datas[player_name].size local data = datas[player_name]
local npp, i, s = 8*3, 0, 0
local pagemax = math.ceil(inv_size / npp)
if pagenum > pagemax then pagenum = 1
elseif pagenum == 0 then pagenum = pagemax end
local formspec = [[ size[8,6.6;] local formspec = [[ size[8,6.6;]
tablecolumns[color;text;color;text] tablecolumns[color;text;color;text]
tableoptions[background=#00000000;highlight=#00000000;border=false] tableoptions[background=#00000000;highlight=#00000000;border=false]
@ -32,11 +26,12 @@ function craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, playe
tooltip[search;Search] tooltip[search;Search]
tooltip[clear;Reset] tooltip[clear;Reset]
table[6,0.18;1.1,0.5;pagenum;#FFFF00,]].. table[6,0.18;1.1,0.5;pagenum;#FFFF00,]]..
pagenum..",#FFFFFF,/ "..pagemax.."]".. pagenum..",#FFFFFF,/ "..data.pagemax.."]"..
"field[0.3,0.32;2.6,1;filter;;"..filter.."]".. "field[0.3,0.32;2.6,1;filter;;"..data.filter.."]"..
default.gui_bg..default.gui_bg_img default.gui_bg..default.gui_bg_img
for _, name in pairs(datas[player_name].items) do local i, s = 0, 0
for _, name in pairs(data.items) do
if s < (pagenum - 1) * npp then if s < (pagenum - 1) * npp then
s = s + 1 s = s + 1
else if i >= npp then break end else if i >= npp then break end
@ -49,15 +44,15 @@ function craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, playe
end end
end end
if item and minetest.registered_items[item] then if data.item and minetest.registered_items[data.item] then
local recipes = minetest.get_all_craft_recipes(item) local recipes = minetest.get_all_craft_recipes(data.item)
if recipe_num > #recipes then recipe_num = 1 end if recipe_num > #recipes then recipe_num = 1 end
if #recipes > 1 then formspec = formspec.. if #recipes > 1 then formspec = formspec..
[[ button[0,6;1.6,1;alternate;Alternate] [[ button[0,6;1.6,1;alternate;Alternate]
label[0,5.5;Recipe ]]..recipe_num.." of "..#recipes.."]" label[0,5.5;Recipe ]]..recipe_num.." of "..#recipes.."]"
end end
local type = recipes[recipe_num].type local type = recipes[recipe_num].type
if type == "cooking" then formspec = formspec.. if type == "cooking" then formspec = formspec..
"image[3.75,4.6;0.5,0.5;default_furnace_front.png]" "image[3.75,4.6;0.5,0.5;default_furnace_front.png]"
@ -82,58 +77,60 @@ function craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, playe
local output = recipes[recipe_num].output local output = recipes[recipe_num].output
formspec = formspec..[[ image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90] formspec = formspec..[[ image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]
item_image_button[2.5,5;1,1;]]..output..";"..item..";]" item_image_button[2.5,5;1,1;]]..output..";"..data.item..";]"
end end
datas[player_name].formspec = formspec data.formspec = formspec
minetest.show_formspec(player_name, "xdecor:crafting_guide", formspec) minetest.show_formspec(player_name, "xdecor:craftguide", formspec)
end end
function craftguide:get_items(filter, player_name) function craftguide:get_items(player_name)
local items_list = {} local items_list, data = {}, datas[player_name]
for name, def in pairs(minetest.registered_items) do for name, def in pairs(minetest.registered_items) do
if not (def.groups.not_in_creative_inventory == 1) and if not (def.groups.not_in_creative_inventory == 1) and
minetest.get_craft_recipe(name).items and minetest.get_craft_recipe(name).items and
def.description and def.description ~= "" and def.description and def.description ~= "" and
(not filter or def.name:find(filter, 1, true) or (def.name:find(data.filter, 1, true) or
def.description:lower():find(filter, 1, true)) then def.description:lower():find(data.filter, 1, true)) then
items_list[#items_list+1] = name items_list[#items_list+1] = name
end end
end end
table.sort(items_list) table.sort(items_list)
datas[player_name].items = items_list data.items = items_list
datas[player_name].size = #items_list data.size = #items_list
data.pagemax = math.ceil(data.size / npp)
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "xdecor:crafting_guide" then return end if formname ~= "xdecor:craftguide" then return end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local stack = player:get_wielded_item() local data = datas[player_name]
local formspec = datas[player_name].formspec local formspec = data.formspec
local filter = formspec:match("filter;;([%w_:]+)") or ""
local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1 local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
if fields.clear then if fields.clear then
craftguide:get_items(nil, player_name) data.filter, data.item = "", nil
craftguide:get_formspec(stack, 1, nil, 1, "", player_name) craftguide:get_items(player_name)
craftguide:get_formspec(player_name, 1, 1)
elseif fields.alternate then elseif fields.alternate then
local item = formspec:match("item_image_button%[.*;([%w_:]+);") or 1
local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
recipe_num = recipe_num + 1 recipe_num = recipe_num + 1
craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, player_name) craftguide:get_formspec(player_name, pagenum, recipe_num)
elseif fields.search then elseif fields.search then
local lowstr = fields.filter:lower() data.filter = fields.filter:lower()
craftguide:get_items(lowstr, player_name) craftguide:get_items(player_name)
craftguide:get_formspec(stack, 1, nil, 1, lowstr, player_name) craftguide:get_formspec(player_name, 1, 1)
elseif fields.prev or fields.next then elseif fields.prev or fields.next then
if fields.prev then pagenum = pagenum - 1 if fields.prev then pagenum = pagenum - 1
else pagenum = pagenum + 1 end else pagenum = pagenum + 1 end
craftguide:get_formspec(stack, pagenum, nil, 1, filter, player_name) if pagenum > data.pagemax then pagenum = 1
elseif pagenum == 0 then pagenum = data.pagemax end
craftguide:get_formspec(player_name, pagenum, 1)
else for item in pairs(fields) do else for item in pairs(fields) do
if minetest.get_craft_recipe(item).items then if minetest.get_craft_recipe(item).items then
craftguide:get_formspec(stack, pagenum, item, 1, filter, player_name) data.item = item
craftguide:get_formspec(player_name, pagenum, 1)
end end
end end
end end
@ -149,10 +146,11 @@ minetest.register_craftitem(":xdecor:crafting_guide", {
local player_name = user:get_player_name() local player_name = user:get_player_name()
if not datas[player_name] then if not datas[player_name] then
datas[player_name] = {} datas[player_name] = {}
craftguide:get_items(nil, player_name) datas[player_name].filter = ""
craftguide:get_formspec(itemstack, 1, nil, 1, "", player_name) craftguide:get_items(player_name)
craftguide:get_formspec(player_name, 1, 1)
else else
minetest.show_formspec(player_name, "xdecor:crafting_guide", datas[player_name].formspec) minetest.show_formspec(player_name, "xdecor:craftguide", datas[player_name].formspec)
end end
end end
}) })