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