Advertisement
Guest User

Untitled

a guest
Nov 30th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. -- Recipes Mod
  2.  
  3. show_recipes= {}
  4. show_recipes.recipes_size = 0
  5. show_recipes.show_items = function(name, pagenum)
  6.     minetest.log("action", "pagenum="..pagenum)
  7.  
  8.     local pagemax = math.floor((show_recipes.recipes_size - 1) / (6*4) + 1)
  9.  
  10.     local button_prev = ""
  11.     if pagenum > 1 then
  12.         button_prev = "button[5.3,6.5;1.6,1;show_recipes_prev;"..(pagenum-1).."]"
  13.     end
  14.  
  15.     local button_next = ""
  16.     if pagenum < pagemax then
  17.         button_next = "button[5.7,6.5;1.6,1;show_recipes_next;"..(pagenum+1).."]"
  18.     end
  19.  
  20.     minetest.show_formspec(name, "show_recipes:mainform",
  21.     "size[13, 7.5]"..
  22.     "list[detached:show_recipes;main;0.3,0.5;4,6;"..((pagenum - 1) * 6 * 4).."]"..
  23.     button_prev..
  24.     button_next..
  25.     "label[5,1.5;Recipe:]"..
  26.     "list[detached:show_recipes;item;5,2;1,1;]"
  27.     )
  28. end
  29.  
  30. show_recipes.show_recipes = function(name, itemname, recipeidx)
  31.     minetest.log("action", "itemname="..itemname.." recipeidx="..recipeidx)
  32.  
  33.     local recipes = minetest.get_all_craft_recipes(itemname)
  34.     local button_prev = ""
  35.     if recipeidx > 1 then
  36.         button_prev = "button[0.3,6.5;1.6,1;show_recipe_prev;"..(recipeidx-1).."]"
  37.     end
  38.  
  39.     local button_next = ""
  40.     if recipeidx < #recipes then
  41.         button_next = "button[2.7,6.5;1.6,1;show_recipe_next;"..(recipeidx+1).."]"
  42.     end
  43.  
  44.     minetest.log("action", "Recipe for "..itemname)
  45.     --recipes[recipeidx].items
  46.     local item_str = ""
  47.     for i=1,9 do
  48.         if recipes[recipeidx].items[i] then
  49.             item_str = item_str .. "item_image["..(5 + (i-1)%3)..","..(2 + math.floor((i - 1)/3))..";1,1;"..recipes[recipeidx].items[i].."]"
  50.             minetest.log("action", " "..i.." -> "..recipes[recipeidx].items[i])
  51.         end
  52.     end
  53.     minetest.show_formspec(name, "show_recipes:recipes_form",
  54.     "size[13, 7.5]"..
  55.     "field[5,0.5;5,1;itemname;;"..itemname.."]"..
  56.     "item_image[1.3,0.5;1,1;"..itemname.."]"..
  57.     button_prev..
  58.     button_next..
  59.     "label[5,1.5;Recipe:]"..
  60.     item_str
  61.     )
  62. end
  63.  
  64. minetest.register_chatcommand("showrecipes", {
  65.     privs = {},
  66.     params = nil,
  67.     description = "Show recipes list",
  68.     func = function(name, param)
  69.         show_recipes.show_items(name, 1)
  70.     end,
  71. })
  72.  
  73. minetest.after(0, function()
  74.     --local table_to_array = function(t)
  75.     --  local res = {}
  76.     --  local index = 1
  77.     --  for k, v in pairs(t) do
  78.     --      res[index] = k
  79.     --      index = index + 1
  80.     --  end
  81.     --  return res
  82.     --end
  83.     local inv = minetest.create_detached_inventory("show_recipes", {
  84.     allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  85.         minetest.log("action", "Recipes move from main:"..from_index.." to item:"..to_index.." count="..count)
  86.         if from_list == "main" and to_list == "item" and count == 1 then
  87.             show_recipes.show_recipes(player:get_player_name(), inv:get_stack(from_list, from_index):get_name(), 1)
  88.         end
  89.         return 0
  90.     end,
  91.     allow_put = function(inv, listname, index, stack, player)
  92.         minetest.log("action", "Recipes put")
  93.         return 0
  94.     end,
  95.     allow_take = function(inv, listname, index, stack, player)
  96.         minetest.log("action", "Recipes take")
  97.         return 0
  98.     end,
  99.     on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  100.         minetest.log("action", "Recipes on_move")
  101.     end,
  102.     on_put = function(inv, listname, index, stack, player)
  103.         minetest.log("action", "Recipes on_put")
  104.     end,
  105.     on_take = function(inv, listname, index, stack, player)
  106.         minetest.log("action", "Recipes on_take")
  107.     end,
  108.     })
  109.  
  110.     local craftable_list = {}
  111.     for name,_ in pairs(minetest.registered_items) do
  112.         if name and name ~= "" then
  113.             local recipes = minetest.get_all_craft_recipes(name)
  114.             if recipes then
  115.                 --minetest.log("action", "Get item "..name)
  116.                 for i,v in ipairs(recipes) do
  117.                     --local items_str = ", items={"
  118.                     --for k,v in pairs(v.items) do
  119.                     --  items_str = items_str .. k .."="..v..","
  120.                     --end
  121.                     --items_str = items_str .. "}"
  122.                     --minetest.log("action", i.." {type="..v.type..
  123.                     --", width="..v.width..
  124.                     --", output="..v.output..
  125.                     --items_str
  126.                     --)
  127.                     --show_recipes.recipes[name] = recipes
  128.                     table.insert(craftable_list, name)
  129.                     break
  130.                 end
  131.             else
  132.                 -- minetest.log("action", "Item "..name.." has no recipes.")
  133.             end
  134.         end
  135.     end
  136.     table.sort(craftable_list)
  137.     inv:set_size("main", #craftable_list)
  138.     inv:set_size("item", 1)
  139.     for _,itemname in ipairs(craftable_list) do
  140.         inv:add_item("main", ItemStack(itemname))
  141.     end
  142.     show_recipes.recipes_size = #craftable_list
  143.     minetest.log("action", "Recipes library size"..show_recipes.recipes_size)
  144. end)
  145.  
  146. minetest.register_on_player_receive_fields(function(player, formname, fields)
  147.     if formname == "show_recipes:mainform" then
  148.         local str = "{"
  149.         for k,v in pairs(fields) do
  150.             str = str .. k .. "=" .. v .. ","
  151.         end
  152.         str = str .. "}"
  153.         minetest.log("action", "Receive fields="..str)
  154.         if fields.show_recipes_next then
  155.             show_recipes.show_items(player:get_player_name(), tonumber(fields.show_recipes_next))
  156.         elseif fields.show_recipes_prev then
  157.             show_recipes.show_items(player:get_player_name(), tonumber(fields.show_recipes_prev))
  158.         end
  159.     end
  160.     if formname == "show_recipes:recipes_form" then
  161.         if fields.show_recipe_next then
  162.             show_recipes.show_recipes(player:get_player_name(), fields.itemname, tonumber(fields.show_recipe_next))
  163.         elseif fields.show_recipe_prev then
  164.             show_recipes.show_recipes(player:get_player_name(), fields.itemname, tonumber(fields.show_recipe_prev))
  165.         end
  166.     end
  167. end)
  168.  
  169. -- minetest.get_all_craft_recipes
  170. -- recipes type,width,output,items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement