From 1b25d7ab98da05c866509a7e9aab148ace16d1f0 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Thu, 17 Mar 2016 23:39:24 +0100 Subject: [PATCH] Import changes from xdecor --- init.lua | 80 +++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/init.lua b/init.lua index d863675..8474470 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,4 @@ -local craftguide, datas = {}, {} +local craftguide, datas, npp = {}, {}, 8*3 function craftguide:get_recipe(item) if item:sub(1,6) == "group:" then @@ -14,14 +14,8 @@ function craftguide:get_recipe(item) return item end -function craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, player_name) - local inv_size = datas[player_name].size - 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 - +function craftguide:get_formspec(player_name, pagenum, recipe_num) + local data = datas[player_name] local formspec = [[ size[8,6.6;] tablecolumns[color;text;color;text] 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[clear;Reset] table[6,0.18;1.1,0.5;pagenum;#FFFF00,]].. - pagenum..",#FFFFFF,/ "..pagemax.."]".. - "field[0.3,0.32;2.6,1;filter;;"..filter.."]".. + pagenum..",#FFFFFF,/ "..data.pagemax.."]".. + "field[0.3,0.32;2.6,1;filter;;"..data.filter.."]".. 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 s = s + 1 else if i >= npp then break end @@ -49,15 +44,15 @@ function craftguide:get_formspec(stack, pagenum, item, recipe_num, filter, playe end end - if item and minetest.registered_items[item] then - local recipes = minetest.get_all_craft_recipes(item) + if data.item and minetest.registered_items[data.item] then + local recipes = minetest.get_all_craft_recipes(data.item) if recipe_num > #recipes then recipe_num = 1 end if #recipes > 1 then formspec = formspec.. [[ button[0,6;1.6,1;alternate;Alternate] label[0,5.5;Recipe ]]..recipe_num.." of "..#recipes.."]" end - + local type = recipes[recipe_num].type if type == "cooking" then formspec = formspec.. "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 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 - datas[player_name].formspec = formspec - minetest.show_formspec(player_name, "xdecor:crafting_guide", formspec) + data.formspec = formspec + minetest.show_formspec(player_name, "xdecor:craftguide", formspec) end -function craftguide:get_items(filter, player_name) - local items_list = {} +function craftguide:get_items(player_name) + local items_list, data = {}, datas[player_name] for name, def in pairs(minetest.registered_items) do if not (def.groups.not_in_creative_inventory == 1) and minetest.get_craft_recipe(name).items and def.description and def.description ~= "" and - (not filter or def.name:find(filter, 1, true) or - def.description:lower():find(filter, 1, true)) then + (def.name:find(data.filter, 1, true) or + def.description:lower():find(data.filter, 1, true)) then items_list[#items_list+1] = name end end table.sort(items_list) - datas[player_name].items = items_list - datas[player_name].size = #items_list + data.items = items_list + data.size = #items_list + data.pagemax = math.ceil(data.size / npp) end 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 stack = player:get_wielded_item() - local formspec = datas[player_name].formspec - local filter = formspec:match("filter;;([%w_:]+)") or "" + local data = datas[player_name] + local formspec = data.formspec local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1 if fields.clear then - craftguide:get_items(nil, player_name) - craftguide:get_formspec(stack, 1, nil, 1, "", player_name) + data.filter, data.item = "", nil + craftguide:get_items(player_name) + craftguide:get_formspec(player_name, 1, 1) 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 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 - local lowstr = fields.filter:lower() - craftguide:get_items(lowstr, player_name) - craftguide:get_formspec(stack, 1, nil, 1, lowstr, player_name) + data.filter = fields.filter:lower() + craftguide:get_items(player_name) + craftguide:get_formspec(player_name, 1, 1) elseif fields.prev or fields.next then if fields.prev then pagenum = pagenum - 1 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 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 @@ -149,10 +146,11 @@ minetest.register_craftitem(":xdecor:crafting_guide", { local player_name = user:get_player_name() if not datas[player_name] then datas[player_name] = {} - craftguide:get_items(nil, player_name) - craftguide:get_formspec(itemstack, 1, nil, 1, "", player_name) + datas[player_name].filter = "" + craftguide:get_items(player_name) + craftguide:get_formspec(player_name, 1, 1) 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 })