From 2ffef6794ab17770cfe8cb4286c15d067c808c4e Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Wed, 11 Sep 2019 16:28:37 +0200 Subject: [PATCH] Do not require to specify width in custom recipes --- API.md | 12 +++++++++++- init.lua | 47 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/API.md b/API.md index 904cbc2..0000814 100644 --- a/API.md +++ b/API.md @@ -19,12 +19,22 @@ craftguide.register_craft_type("digging", { ```Lua craftguide.register_craft({ type = "digging", - width = 1, result = "default:cobble 2", items = {"default:stone"}, }) ``` +```Lua +craftguide.register_craft({ + result = "default:cobble 16", + items = { + "default:stone, default:stone, default:stone", + "default:stone, , default:stone", + "default:stone, default:stone, default:stone", + } +}) +``` + Recipes can also be registered in a Minecraft-like way: ```Lua diff --git a/init.lua b/init.lua index 971c7ff..494345a 100644 --- a/init.lua +++ b/init.lua @@ -121,10 +121,6 @@ local function is_str(x) return type(x) == "string" end -local function is_num(x) - return type(x) == "number" -end - local function is_table(x) return type(x) == "table" end @@ -160,6 +156,10 @@ local function clean_name(item) end function craftguide.register_craft(def) + def.custom = true + def.width = 0 + local c = 1 + if not is_table(def) or not next(def) then return log("error", "craftguide.register_craft(): craft definition missing") end @@ -172,14 +172,6 @@ function craftguide.register_craft(def) return log("error", "craftguide.register_craft(): output missing") end - if not is_table(def.items) then - def.items = {} - end - - if not is_num(def.width) then - def.width = 0 - end - if def.grid then if not is_table(def.grid) then def.grid = {} @@ -198,11 +190,38 @@ function craftguide.register_craft(def) end end - local c = 1 for symbol in gmatch(concat(def.grid), ".") do def.items[c] = def.key[symbol] c = c + 1 end + else + if not is_table(def.items) then + def.items = {} + end + + local len = #def.items + + for i = 1, len do + def.items[i] = def.items[i]:gsub(",,", ", ,") + + local row = split(def.items[i], ",") + local l = #row + + if l > def.width then + def.width = l + end + end + + for i = 1, len do + while #split(def.items[i], ",") < def.width do + def.items[i] = def.items[i] .. ", " + end + end + + for name in gmatch(concat(def.items, ","), "[%s%w_:]+") do + def.items[c] = clean_name(name) + c = c + 1 + end end local output = match(def.output, "%S*") @@ -547,7 +566,7 @@ local function get_recipe_fs(data, fs) if recipe.type == "cooking" then cooktime, width = width, 1 - elseif width == 0 then + elseif width == 0 and not recipe.custom then shapeless = true local n = #recipe.items width = (n < 5 and n > 1) and 2 or min(3, max(1, n))