From c7f6e1db62dda1cbd05a945889d8405533efa2d8 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Fri, 24 Dec 2021 00:36:13 +0100 Subject: [PATCH] API: fix custom recipe registration --- init.lua | 2 +- src/api.lua | 25 ++++++++++++----------- tests/test_custom_recipes.lua | 37 +++++++++++++++++------------------ 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/init.lua b/init.lua index 28524cf..a7582aa 100644 --- a/init.lua +++ b/init.lua @@ -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() diff --git a/src/api.lua b/src/api.lua index ac75c9b..876f8c2 100644 --- a/src/api.lua +++ b/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") @@ -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 diff --git a/tests/test_custom_recipes.lua b/tests/test_custom_recipes.lua index fe15def..0bcabcf 100644 --- a/tests/test_custom_recipes.lua +++ b/tests/test_custom_recipes.lua @@ -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", -})