Fix crash in progressive mode + refacto

This commit is contained in:
Jean-Patrick Guerrero 2019-10-08 00:38:37 +02:00
parent e16c23c29c
commit abd9274798

154
init.lua

@ -535,10 +535,12 @@ local function get_recipes(item, data, player)
if not sfinv_only or (sfinv_only and data.show_usages) then
usages = apply_recipe_filters(usages, player)
if usages and #usages == 0 then return end
end
return recipes, usages
local no_usages = not usages or #usages == 0
return not no_recipes and recipes or nil,
not no_usages and usages or nil
end
local function groups_to_items(groups, get_all)
@ -652,7 +654,7 @@ local function get_output_fs(fs, L)
end
end
local arrow_X = L.rightest + (L.s_btn_size or 1.1)
local arrow_X = L.rightest + (L._btn_size or 1.1)
local output_X = arrow_X + 0.9
fs[#fs + 1] = fmt(FMT.image,
@ -703,17 +705,17 @@ local function pretty_wrap(str, limit)
return #str > limit + 3 and sub(str, 1, limit) .. "..." or str
end
local function get_itemdef_fs(fs, L)
local function get_itemdef_fs(fs, item, last_y)
fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[8.1,%f;6.6,2.19;%s;false;%d]",
L.y, PNG.bg_full, 10) or
fmt("background[8.1,%f;6.6,2.19;%s;false]", L.y, PNG.bg_full)
last_y, PNG.bg_full, 10) or
fmt("background[8.1,%f;6.6,2.19;%s;false]", last_y, PNG.bg_full)
local specs = {
ESC(S("Name")),
}
local namestr = fmt("%s (%s)", pretty_wrap(get_desc(L.item), 25), L.item)
local namestr = fmt("%s (%s)", pretty_wrap(get_desc(item), 25), item)
local tstr = ""
for i = 1, #specs do
@ -727,27 +729,13 @@ local function get_itemdef_fs(fs, L)
]]
fs[#fs + 1] = fmt("table[8.1,%f;6.3,1.8;itemdef;" .. tstr .. ";0]",
L.y + 0.08, namestr)
last_y + 0.08, namestr)
end
local function get_info_fs(data, fs)
local panels = {recipes = data.recipes, usages = data.usages}
local infonum = 0
local last_y
if sfinv_only then
panels = data.show_usages and
{usages = data.usages} or {recipes = data.recipes}
end
for k, v in pairs(panels) do
infonum = infonum + 1
local spacing = (infonum - 1) * 3.6
last_y = -0.2 + infonum * 3.6
local rcp = k == "recipes" and v[data.rnum] or v[data.unum]
local function get_grid_fs(fs, rcp, spacing)
local width = rcp.width or 1
local replacements = rcp.replacements
local rightest, btn_size, _btn_size = 0, 1.1
local cooktime, shapeless
if rcp.type == "cooking" then
@ -759,52 +747,13 @@ local function get_info_fs(data, fs)
end
local rows = ceil(maxn(rcp.items) / width)
local rightest, btn_size, s_btn_size = 0, 1.1
if not sfinv_only then
fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[8.1,%f;6.6,3.5;%s;false;%d]",
-0.2 + spacing, PNG.bg_full, 10) or
fmt("background[8.1,%f;6.6,3.5;%s;false]",
-0.2 + spacing, PNG.bg_full)
end
local btn_lab
if (not sfinv_only and k == "recipes") or
(sfinv_only and not data.show_usages) then
btn_lab = ESC(S("Recipe @1 of @2", data.rnum, #v))
elseif not sfinv_only or (sfinv_only and data.show_usages) then
btn_lab = ESC(S("Usage @1 of @2", data.unum, #v))
elseif sfinv_only then
btn_lab = data.show_usages and
ESC(S("Usage @1 of @2", data.unum, #v)) or
ESC(S("Recipe @1 of @2", data.rnum, #v))
end
fs[#fs + 1] = fmt(FMT.label,
XOFFSET + (sfinv_only and 2.3 or 1.6),
YOFFSET + (sfinv_only and 2.2 or 1.5 + spacing),
btn_lab)
if #v > 1 then
local btn_suffix = k == "recipes" and "recipe" or "usage"
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 2.1 or 1.4 + spacing)
fs[#fs + 1] = fmt(FMT.arrow .. FMT.arrow,
x_arrow, y_arrow, PNG.prev,
fmt("prev_%s", btn_suffix), PNG.prev,
x_arrow + 1.8, y_arrow, PNG.next,
fmt("next_%s", btn_suffix), PNG.next)
end
if width > WH_LIMIT or rows > WH_LIMIT then
fs[#fs + 1] = fmt(FMT.label,
sfinv_only and 2 or 3, 7,
ESC(S("Recipe's too big to be displayed (@1x@2)", width, rows)))
XOFFSET + (sfinv_only and -1.5 or -1.6),
YOFFSET + (sfinv_only and 0.5 or spacing),
ESC(S("Recipe's too big to be displayed (@1x@2)",
width, rows)))
return concat(fs)
end
@ -827,7 +776,7 @@ local function get_info_fs(data, fs)
btn_size = width > rows and
(3.5 + (xof * 2)) / width or (3.5 + (yof * 2)) / rows
s_btn_size = btn_size
_btn_size = btn_size
X = (btn_size * ((i - 1) % width) + XOFFSET -
(sfinv_only and 2.83 or 0.5)) * (0.83 - (x_y / 5))
@ -884,27 +833,82 @@ local function get_info_fs(data, fs)
X, Y + (sfinv_only and 0.7 or 0),
btn_size, btn_size, "craftguide_selected.png")
end
end
if large_recipe then
fs[#fs + 1] = "style_type[item_image_button;border=false]"
end
end
get_output_fs(fs, {
recipe = rcp,
shapeless = shapeless,
rightest = rightest,
btn_size = btn_size,
s_btn_size = s_btn_size,
_btn_size = _btn_size,
spacing = spacing,
})
end
local function get_panels(data, fs)
local panels = {recipes = data.recipes, usages = data.usages}
local x, last_y = 0
if sfinv_only then
panels = data.show_usages and
{usages = data.usages} or {recipes = data.recipes}
end
for k, v in pairs(panels) do
x = x + 1
local spacing = (x - 1) * 3.6
last_y = -0.2 + x * 3.6
if not sfinv_only then
get_itemdef_fs(fs, {
item = data.query_item,
y = last_y,
})
fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[8.1,%f;6.6,3.5;%s;false;%d]",
-0.2 + spacing, PNG.bg_full, 10) or
fmt("background[8.1,%f;6.6,3.5;%s;false]",
-0.2 + spacing, PNG.bg_full)
end
local btn_lab
if (not sfinv_only and k == "recipes") or
(sfinv_only and not data.show_usages) then
btn_lab = ESC(S("Recipe @1 of @2", data.rnum, #v))
elseif not sfinv_only or (sfinv_only and data.show_usages) then
btn_lab = ESC(S("Usage @1 of @2", data.unum, #v))
elseif sfinv_only then
btn_lab = data.show_usages and
ESC(S("Usage @1 of @2", data.unum, #v)) or
ESC(S("Recipe @1 of @2", data.rnum, #v))
end
fs[#fs + 1] = fmt(FMT.label,
XOFFSET + (sfinv_only and 2.3 or 1.6),
YOFFSET + (sfinv_only and 2.2 or 1.5 + spacing),
btn_lab)
if #v > 1 then
local btn_suffix = k == "recipes" and "recipe" or "usage"
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 2.1 or 1.4 + spacing)
fs[#fs + 1] = fmt(FMT.arrow .. FMT.arrow,
x_arrow, y_arrow, PNG.prev,
fmt("prev_%s", btn_suffix), PNG.prev,
x_arrow + 1.8, y_arrow, PNG.next,
fmt("next_%s", btn_suffix), PNG.next)
end
local rcp = k == "recipes" and v[data.rnum] or v[data.unum]
get_grid_fs(fs, rcp, spacing)
end
if not sfinv_only then
get_itemdef_fs(fs, data.query_item, last_y)
end
end
@ -989,7 +993,7 @@ local function make_formspec(name)
end
if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then
get_info_fs(data, fs)
get_panels(data, fs)
end
return concat(fs)