Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

Commit

Permalink
API: fix custom recipe registration
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbith committed Dec 23, 2021
1 parent 16a1865 commit c7f6e1d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion init.lua
Expand Up @@ -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()
25 changes: 14 additions & 11 deletions src/api.lua
@@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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

Expand Down
37 changes: 18 additions & 19 deletions tests/test_custom_recipes.lua
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
})

0 comments on commit c7f6e1d

Please sign in to comment.