-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
62 lines (58 loc) · 1.63 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
if minetest.get_modpath("crafting") then
-- spairs function licensed CC-BY-SA 3.0
-- https://stackoverflow.com/questions/15706270/sort-a-table-in-lua
function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
local function combine(recipe)
local item_counts = {}
for _, itemstring in pairs(recipe.items) do -- must use pairs here because some slots are nil.
local stack = ItemStack(itemstring)
local item_name = stack:get_name()
item_counts[item_name] = (item_counts[item_name] or 0) + stack:get_count()
end
local stacks = {}
for name, count in pairs(item_counts) do
table.insert(stacks, name.." "..count)
end
return stacks
end
minetest.register_on_mods_loaded(function()
for name in spairs(minetest.registered_items) do
local recipes = minetest.get_all_craft_recipes(name)
if recipes then
for _, recipe in ipairs(recipes) do
if recipe.method == "normal" or "cooking" then
local crafting_type = "inv"
if recipe.method == "cooking" then
crafting_type = "furnace"
end
crafting.register_recipe({
type = crafting_type,
output = recipe.output,
items = combine(recipe),
always_known = true,
})
end
end
end
end
end)
end