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