Zefram
2014-07-30 12d0c6522bbca906910aae0321cbaa7eb48db8c2
commit | author | age
aa8af0 1
d55ecc 2 technic.recipes = {cooking = {numitems = 1}}
N 3 function technic.register_recipe_type(typename, desc, numitems)
4     numitems = numitems or 1
aa8af0 5     if unified_inventory and unified_inventory.register_craft_type then
N 6         unified_inventory.register_craft_type(typename, {
7             description = desc,
e5cc33 8             height = numitems,
aa8af0 9             width = 1,
N 10         })
11     end
d55ecc 12     technic.recipes[typename] = {numitems = numitems, recipes = {}}
aa8af0 13 end
N 14
d55ecc 15 local function get_recipe_index(items)
N 16     local l = {}
17     for i, stack in ipairs(items) do
18         l[i] = ItemStack(stack):get_name()
19     end
20     table.sort(l)
21     return table.concat(l, "/")
22 end
23
24 local function register_recipe(typename, data)
25     -- Handle aliases
26     for i, stack in ipairs(data.input) do
27         data.input[i] = ItemStack(stack):to_string()
28     end
29     data.output = ItemStack(data.output):to_string()
30     
31     local recipe = {time = data.time, input = {}, output = data.output}
32     local index = get_recipe_index(data.input)
33     for _, stack in ipairs(data.input) do
34         recipe.input[ItemStack(stack):get_name()] = ItemStack(stack):get_count()
35     end
36     
37     technic.recipes[typename].recipes[index] = recipe
aa8af0 38     if unified_inventory then
N 39         unified_inventory.register_craft({
40             type = typename,
41             output = data.output,
d55ecc 42             items = data.input,
aa8af0 43             width = 0,
N 44         })
45     end
46 end
47
d55ecc 48 function technic.register_recipe(typename, data)
N 49     minetest.after(0.01, register_recipe, typename, data) -- Handle aliases
50 end
51
52 function technic.get_recipe(typename, items)
aa8af0 53     if typename == "cooking" then -- Already builtin in Minetest, so use that
d55ecc 54         local result, new_input = minetest.get_craft_result({
aa8af0 55             method = "cooking",
N 56             width = 1,
d55ecc 57             items = items})
aa8af0 58         -- Compatibility layer
N 59         if not result or result.time == 0 then
60             return nil
61         else
62             return {time = result.time,
d55ecc 63                     new_input = new_input.items,
N 64                     output = result.item}
aa8af0 65         end
N 66     end
d55ecc 67     local index = get_recipe_index(items)
N 68     local recipe = technic.recipes[typename].recipes[index]
69     if recipe then
70         local new_input = {}
71         for i, stack in ipairs(items) do
72             if stack:get_count() < recipe.input[stack:get_name()] then
73                 print(stack:get_name())
74                 return nil
75             else
76                 new_input[i] = ItemStack(stack)
77                 new_input[i]:take_item(recipe.input[stack:get_name()])
78             end
79         end
80         return {time = recipe.time,
81                 new_input = new_input,
82                 output = recipe.output}
aa8af0 83     else
N 84         return nil
85     end
86 end
87
88