Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Recipes Mod
- show_recipes= {}
- show_recipes.recipes_size = 0
- show_recipes.show_items = function(name, pagenum)
- minetest.log("action", "pagenum="..pagenum)
- local pagemax = math.floor((show_recipes.recipes_size - 1) / (6*4) + 1)
- local button_prev = ""
- if pagenum > 1 then
- button_prev = "button[5.3,6.5;1.6,1;show_recipes_prev;"..(pagenum-1).."]"
- end
- local button_next = ""
- if pagenum < pagemax then
- button_next = "button[5.7,6.5;1.6,1;show_recipes_next;"..(pagenum+1).."]"
- end
- minetest.show_formspec(name, "show_recipes:mainform",
- "size[13, 7.5]"..
- "list[detached:show_recipes;main;0.3,0.5;4,6;"..((pagenum - 1) * 6 * 4).."]"..
- button_prev..
- button_next..
- "label[5,1.5;Recipe:]"..
- "list[detached:show_recipes;item;5,2;1,1;]"
- )
- end
- show_recipes.show_recipes = function(name, itemname, recipeidx)
- minetest.log("action", "itemname="..itemname.." recipeidx="..recipeidx)
- local recipes = minetest.get_all_craft_recipes(itemname)
- local button_prev = ""
- if recipeidx > 1 then
- button_prev = "button[0.3,6.5;1.6,1;show_recipe_prev;"..(recipeidx-1).."]"
- end
- local button_next = ""
- if recipeidx < #recipes then
- button_next = "button[2.7,6.5;1.6,1;show_recipe_next;"..(recipeidx+1).."]"
- end
- minetest.log("action", "Recipe for "..itemname)
- --recipes[recipeidx].items
- local item_str = ""
- for i=1,9 do
- if recipes[recipeidx].items[i] then
- item_str = item_str .. "item_image["..(5 + (i-1)%3)..","..(2 + math.floor((i - 1)/3))..";1,1;"..recipes[recipeidx].items[i].."]"
- minetest.log("action", " "..i.." -> "..recipes[recipeidx].items[i])
- end
- end
- minetest.show_formspec(name, "show_recipes:recipes_form",
- "size[13, 7.5]"..
- "field[5,0.5;5,1;itemname;;"..itemname.."]"..
- "item_image[1.3,0.5;1,1;"..itemname.."]"..
- button_prev..
- button_next..
- "label[5,1.5;Recipe:]"..
- item_str
- )
- end
- minetest.register_chatcommand("showrecipes", {
- privs = {},
- params = nil,
- description = "Show recipes list",
- func = function(name, param)
- show_recipes.show_items(name, 1)
- end,
- })
- minetest.after(0, function()
- --local table_to_array = function(t)
- -- local res = {}
- -- local index = 1
- -- for k, v in pairs(t) do
- -- res[index] = k
- -- index = index + 1
- -- end
- -- return res
- --end
- local inv = minetest.create_detached_inventory("show_recipes", {
- allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
- minetest.log("action", "Recipes move from main:"..from_index.." to item:"..to_index.." count="..count)
- if from_list == "main" and to_list == "item" and count == 1 then
- show_recipes.show_recipes(player:get_player_name(), inv:get_stack(from_list, from_index):get_name(), 1)
- end
- return 0
- end,
- allow_put = function(inv, listname, index, stack, player)
- minetest.log("action", "Recipes put")
- return 0
- end,
- allow_take = function(inv, listname, index, stack, player)
- minetest.log("action", "Recipes take")
- return 0
- end,
- on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
- minetest.log("action", "Recipes on_move")
- end,
- on_put = function(inv, listname, index, stack, player)
- minetest.log("action", "Recipes on_put")
- end,
- on_take = function(inv, listname, index, stack, player)
- minetest.log("action", "Recipes on_take")
- end,
- })
- local craftable_list = {}
- for name,_ in pairs(minetest.registered_items) do
- if name and name ~= "" then
- local recipes = minetest.get_all_craft_recipes(name)
- if recipes then
- --minetest.log("action", "Get item "..name)
- for i,v in ipairs(recipes) do
- --local items_str = ", items={"
- --for k,v in pairs(v.items) do
- -- items_str = items_str .. k .."="..v..","
- --end
- --items_str = items_str .. "}"
- --minetest.log("action", i.." {type="..v.type..
- --", width="..v.width..
- --", output="..v.output..
- --items_str
- --)
- --show_recipes.recipes[name] = recipes
- table.insert(craftable_list, name)
- break
- end
- else
- -- minetest.log("action", "Item "..name.." has no recipes.")
- end
- end
- end
- table.sort(craftable_list)
- inv:set_size("main", #craftable_list)
- inv:set_size("item", 1)
- for _,itemname in ipairs(craftable_list) do
- inv:add_item("main", ItemStack(itemname))
- end
- show_recipes.recipes_size = #craftable_list
- minetest.log("action", "Recipes library size"..show_recipes.recipes_size)
- end)
- minetest.register_on_player_receive_fields(function(player, formname, fields)
- if formname == "show_recipes:mainform" then
- local str = "{"
- for k,v in pairs(fields) do
- str = str .. k .. "=" .. v .. ","
- end
- str = str .. "}"
- minetest.log("action", "Receive fields="..str)
- if fields.show_recipes_next then
- show_recipes.show_items(player:get_player_name(), tonumber(fields.show_recipes_next))
- elseif fields.show_recipes_prev then
- show_recipes.show_items(player:get_player_name(), tonumber(fields.show_recipes_prev))
- end
- end
- if formname == "show_recipes:recipes_form" then
- if fields.show_recipe_next then
- show_recipes.show_recipes(player:get_player_name(), fields.itemname, tonumber(fields.show_recipe_next))
- elseif fields.show_recipe_prev then
- show_recipes.show_recipes(player:get_player_name(), fields.itemname, tonumber(fields.show_recipe_prev))
- end
- end
- end)
- -- minetest.get_all_craft_recipes
- -- recipes type,width,output,items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement