David Leal
2020-06-12 a8daa417c485ee20716ec050d4c676b5c91af773
commit | author | age
e39361 1 local have_ui = minetest.get_modpath("unified_inventory")
aa8af0 2
dd65a6 3 technic.recipes = { cooking = { input_size = 1, output_size = 1 } }
Z 4 function technic.register_recipe_type(typename, origdata)
5     local data = {}
6     for k, v in pairs(origdata) do data[k] = v end
7     data.input_size = data.input_size or 1
8     data.output_size = data.output_size or 1
e39361 9     if have_ui and unified_inventory.register_craft_type and data.output_size == 1 then
aa8af0 10         unified_inventory.register_craft_type(typename, {
dd65a6 11             description = data.description,
daa613 12             width = data.input_size,
Z 13             height = 1,
aa8af0 14         })
N 15     end
dd65a6 16     data.recipes = {}
Z 17     technic.recipes[typename] = data
aa8af0 18 end
N 19
d55ecc 20 local function get_recipe_index(items)
bc0ac5 21     if not items or type(items) ~= "table" then return false end
d55ecc 22     local l = {}
N 23     for i, stack in ipairs(items) do
24         l[i] = ItemStack(stack):get_name()
25     end
26     table.sort(l)
27     return table.concat(l, "/")
28 end
29
30 local function register_recipe(typename, data)
31     -- Handle aliases
32     for i, stack in ipairs(data.input) do
33         data.input[i] = ItemStack(stack):to_string()
34     end
dd65a6 35     if type(data.output) == "table" then
Z 36         for i, v in ipairs(data.output) do
37             data.output[i] = ItemStack(data.output[i]):to_string()
38         end
39     else
40         data.output = ItemStack(data.output):to_string()
41     end
a8daa4 42
d55ecc 43     local recipe = {time = data.time, input = {}, output = data.output}
N 44     local index = get_recipe_index(data.input)
bc0ac5 45     if not index then
VE 46         print("[Technic] ignored registration of garbage recipe!")
47         return
48     end
d55ecc 49     for _, stack in ipairs(data.input) do
N 50         recipe.input[ItemStack(stack):get_name()] = ItemStack(stack):get_count()
51     end
a8daa4 52
d55ecc 53     technic.recipes[typename].recipes[index] = recipe
a5db87 54     if have_ui and technic.recipes[typename].output_size == 1 then
aa8af0 55         unified_inventory.register_craft({
N 56             type = typename,
57             output = data.output,
d55ecc 58             items = data.input,
aa8af0 59             width = 0,
N 60         })
61     end
62 end
63
d55ecc 64 function technic.register_recipe(typename, data)
N 65     minetest.after(0.01, register_recipe, typename, data) -- Handle aliases
66 end
67
68 function technic.get_recipe(typename, items)
aa8af0 69     if typename == "cooking" then -- Already builtin in Minetest, so use that
d55ecc 70         local result, new_input = minetest.get_craft_result({
aa8af0 71             method = "cooking",
N 72             width = 1,
d55ecc 73             items = items})
aa8af0 74         -- Compatibility layer
N 75         if not result or result.time == 0 then
76             return nil
77         else
78             return {time = result.time,
d55ecc 79                     new_input = new_input.items,
N 80                     output = result.item}
aa8af0 81         end
N 82     end
d55ecc 83     local index = get_recipe_index(items)
bc0ac5 84     if not index then
VE 85         print("[Technic] ignored registration of garbage recipe!")
86         return
87     end
d55ecc 88     local recipe = technic.recipes[typename].recipes[index]
N 89     if recipe then
90         local new_input = {}
91         for i, stack in ipairs(items) do
92             if stack:get_count() < recipe.input[stack:get_name()] then
93                 return nil
94             else
95                 new_input[i] = ItemStack(stack)
96                 new_input[i]:take_item(recipe.input[stack:get_name()])
97             end
98         end
99         return {time = recipe.time,
100                 new_input = new_input,
101                 output = recipe.output}
aa8af0 102     else
N 103         return nil
104     end
105 end
106
107