API: fix custom recipe registration

This commit is contained in:
Jean-Patrick Guerrero 2021-12-24 00:36:13 +01:00
parent 16a1865e11
commit c7f6e1db62
3 changed files with 33 additions and 31 deletions

View File

@ -257,4 +257,4 @@ end
--i3.files.tests.tabs()
--i3.files.tests.operators()
--i3.files.tests.compression()
--i3.files.tests.custom_recipes(http)
--i3.files.tests.custom_recipes()

View File

@ -1,5 +1,5 @@
local make_fs = i3.files.gui()
local http = ...
local make_fs = i3.files.gui()
IMPORT("gmatch", "split")
IMPORT("S", "err", "fmt", "reg_items")
@ -22,7 +22,7 @@ end
function i3.register_craft(def)
local width, c = 0, 0
if true_str(def.url) then
if http and true_str(def.url) then
http.fetch({url = def.url}, function(result)
if result.succeeded then
local t = core.parse_json(result.data)
@ -51,7 +51,7 @@ function i3.register_craft(def)
def.result = nil
end
if not true_str(def.output) then
if not true_str(def.output) and not def.url then
return err "i3.register_craft: output missing"
end
@ -69,9 +69,7 @@ function i3.register_craft(def)
end
local cp = copy(def.grid)
sort(cp, function(a, b)
return #a > #b
end)
sort(cp, function(a, b) return #a > #b end)
width = #cp[1]
@ -86,7 +84,8 @@ function i3.register_craft(def)
def.items[c] = def.key[symbol]
end
else
local items, len = def.items, #def.items
local items = copy(def.items)
local len = #items
def.items = {}
for i = 1, len do
@ -98,14 +97,18 @@ function i3.register_craft(def)
end
for i = 1, len do
while #split(items[i], ",") < width do
while #split(items[i], ",", true) < width do
items[i] = fmt("%s,", items[i])
end
end
for name in gmatch(concat(items, ","), "[%s%w_:]+") do
c++
def.items[c] = clean_name(name)
for _, line in ipairs(items) do
line = split(line, ",", true)
for _, v in ipairs(line) do
c++
def.items[c] = clean_name(v)
end
end
end

View File

@ -38,6 +38,24 @@ i3.register_craft({
items = {"default:copper_ingot 7, default:tin_ingot, default:steel_ingot 2"},
})
i3.register_craft {
result = "default:tree",
items = {
"default:wood",
"",
"default:wood"
},
}
i3.register_craft {
result = "default:cobble 16",
items = {
"default:stone, default:stone",
"default:stone, , default:stone",
", default:stone, default:stone",
}
}
i3.register_craft({
grid = {
"X",
@ -311,22 +329,3 @@ i3.register_craft({
},
result = "default:mese 3",
})
i3.register_craft({
grid = {
"X #",
" ## ",
"X#X#",
"#X#X#",
"X X##X#X",
" ## ",
"#X#X#",
"#X#X#",
"X #",
},
key = {
['#'] = "default:wood",
['X'] = "default:glass",
},
result = "default:mese 3",
})