Do not require to specify width in custom recipes

This commit is contained in:
Jean-Patrick Guerrero 2019-09-11 16:28:37 +02:00
parent fb1b0cf869
commit 2ffef6794a
2 changed files with 44 additions and 15 deletions

12
API.md
View File

@ -19,12 +19,22 @@ craftguide.register_craft_type("digging", {
```Lua ```Lua
craftguide.register_craft({ craftguide.register_craft({
type = "digging", type = "digging",
width = 1,
result = "default:cobble 2", result = "default:cobble 2",
items = {"default:stone"}, 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: Recipes can also be registered in a Minecraft-like way:
```Lua ```Lua

View File

@ -121,10 +121,6 @@ local function is_str(x)
return type(x) == "string" return type(x) == "string"
end end
local function is_num(x)
return type(x) == "number"
end
local function is_table(x) local function is_table(x)
return type(x) == "table" return type(x) == "table"
end end
@ -160,6 +156,10 @@ local function clean_name(item)
end end
function craftguide.register_craft(def) function craftguide.register_craft(def)
def.custom = true
def.width = 0
local c = 1
if not is_table(def) or not next(def) then if not is_table(def) or not next(def) then
return log("error", "craftguide.register_craft(): craft definition missing") return log("error", "craftguide.register_craft(): craft definition missing")
end end
@ -172,14 +172,6 @@ function craftguide.register_craft(def)
return log("error", "craftguide.register_craft(): output missing") return log("error", "craftguide.register_craft(): output missing")
end 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 def.grid then
if not is_table(def.grid) then if not is_table(def.grid) then
def.grid = {} def.grid = {}
@ -198,11 +190,38 @@ function craftguide.register_craft(def)
end end
end end
local c = 1
for symbol in gmatch(concat(def.grid), ".") do for symbol in gmatch(concat(def.grid), ".") do
def.items[c] = def.key[symbol] def.items[c] = def.key[symbol]
c = c + 1 c = c + 1
end 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 end
local output = match(def.output, "%S*") local output = match(def.output, "%S*")
@ -547,7 +566,7 @@ local function get_recipe_fs(data, fs)
if recipe.type == "cooking" then if recipe.type == "cooking" then
cooktime, width = width, 1 cooktime, width = width, 1
elseif width == 0 then elseif width == 0 and not recipe.custom then
shapeless = true shapeless = true
local n = #recipe.items local n = #recipe.items
width = (n < 5 and n > 1) and 2 or min(3, max(1, n)) width = (n < 5 and n > 1) and 2 or min(3, max(1, n))