Code cleaning

This commit is contained in:
Jean-Patrick Guerrero 2019-09-10 11:28:13 +02:00
parent 00a88a320a
commit fc3f1fece2
2 changed files with 139 additions and 102 deletions

4
API.md

@ -160,6 +160,8 @@ craftguide.group_stereotypes.radioactive = "mod:item"
You can set a custom background theme by overriding this variable: You can set a custom background theme by overriding this variable:
```Lua ```Lua
craftguide.background = "my_custom_bg.png" craftguide.background = "<file_name.png>:<middle>"
``` ```
`middle` (number) refers to a 9-sliced background. Read the engine's Lua API documentation for more info.

235
init.lua

@ -15,7 +15,7 @@ local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G
local log = core.log local log = core.log
local after = core.after local after = core.after
local colorize = core.colorize local clrz = core.colorize
local reg_tools = core.registered_tools local reg_tools = core.registered_tools
local reg_items = core.registered_items local reg_items = core.registered_items
local show_formspec = core.show_formspec local show_formspec = core.show_formspec
@ -54,7 +54,7 @@ local YOFFSET = sfinv_only and 6 or 6.6
local DEV_CORE = sub(core.get_version().string, -3) == "dev" local DEV_CORE = sub(core.get_version().string, -3) == "dev"
craftguide.background = "craftguide_bg_full.png" craftguide.background = "craftguide_bg_full.png:10"
local PNG = { local PNG = {
bg = "craftguide_bg.png", bg = "craftguide_bg.png",
@ -167,6 +167,10 @@ local function get_width(recipe)
return 0 return 0
end end
local function clean_name(item)
return match(item, "%S+")
end
function craftguide.register_craft(def) function craftguide.register_craft(def)
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")
@ -411,7 +415,7 @@ local function groups_to_items(groups, get_all)
end end
end end
return #names > 0 and concat(names, ",") or "" return names
end end
local function not_repairable(tool) local function not_repairable(tool)
@ -419,56 +423,121 @@ local function not_repairable(tool)
return toolrepair and def and def.groups and def.groups.disable_repair == 1 return toolrepair and def and def.groups and def.groups.disable_repair == 1
end end
local function get_description(item, def) local function get_tooltip(item, info)
return def and def.description or
(def and match(item, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
S("Unknown Item (@1)", item))
end
local function get_tooltip(item, burntime, norepair, groups, cooktime, replace)
local tooltip local tooltip
if groups then local function add(str)
return tooltip .. "\n" .. str
end
local function get_desc(def, name)
name = name or item
return def and def.description or
(def and match(name, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
S("Unknown Item (@1)", name))
end
if info.groups then
local groupstr, c = {}, 0 local groupstr, c = {}, 0
for i = 1, #groups do for i = 1, #info.groups do
c = c + 1 c = c + 1
groupstr[c] = colorize("yellow", groups[i]) groupstr[c] = clrz("yellow", info.groups[i])
end end
groupstr = concat(groupstr, ", ") groupstr = concat(groupstr, ", ")
tooltip = S("Any item belonging to the group(s): @1", groupstr) tooltip = S("Any item belonging to the group(s): @1", groupstr)
else else
local def = reg_items[item] local def = reg_items[item]
tooltip = get_description(item, def) tooltip = get_desc(def)
if norepair then if info.norepair then
tooltip = tooltip .. "\n" .. S("This tool cannot be repaired") tooltip = add(S("This tool cannot be repaired"))
end end
end end
if cooktime then if info.cooktime then
tooltip = tooltip .. "\n" .. tooltip = add(S("Cooking time: @1", clrz("yellow", info.cooktime)))
S("Cooking time: @1", colorize("yellow", cooktime))
end end
if burntime then if info.burntime then
tooltip = tooltip .. "\n" .. tooltip = add(S("Burning time: @1", clrz("yellow", info.burntime)))
S("Burning time: @1", colorize("yellow", burntime))
end end
if replace then if info.replace then
local def = reg_items[replace] local def = reg_items[info.replace]
tooltip = tooltip .. "\n" .. tooltip = add(S("Replaced by @1 on crafting",
S("Replaced by @1 on crafting", clrz("yellow", get_desc(def, info.replace))))
colorize("yellow", get_description(replace, def)))
end end
return fmt("tooltip[%s;%s]", item, ESC(tooltip)) return fmt("tooltip[%s;%s]", item, ESC(tooltip))
end end
local function get_recipe_fs(data) local function get_output_fs(fs, L)
local fs = {} local custom_recipe = craft_types[L.recipe.type]
if custom_recipe or L.shapeless or L.recipe.type == "cooking" then
local icon = custom_recipe and custom_recipe.icon or
L.shapeless and "shapeless" or "furnace"
if not custom_recipe then
icon = fmt("craftguide_%s.png^[resize:16x16", icon)
end
local pos_x = L.rightest + L.btn_size + 0.1
local pos_y = YOFFSET + (sfinv_only and 0.25 or -0.45)
fs[#fs + 1] = fmt(FMT.image, pos_x, pos_y, 0.5, 0.5, icon)
local tooltip = custom_recipe and custom_recipe.description or
L.shapeless and S("Shapeless") or S("Cooking")
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
end
local arrow_X = L.rightest + (L.s_btn_size or 1.1)
local output_X = arrow_X + 0.9
fs[#fs + 1] = fmt(FMT.image,
arrow_X, YOFFSET + (sfinv_only and 0.9 or 0.2),
0.9, 0.7, PNG.arrow)
if L.recipe.type == "fuel" then
fs[#fs + 1] = fmt(FMT.image,
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
1.1, 1.1, PNG.fire)
else
local item = L.recipe.output
local name = clean_name(item)
local burntime = fuel_cache[name] and fuel_cache[name].burntime
fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
1.1, 1.1, item, ESC(name), "")
local norepair = not_repairable(item)
if burntime or norepair then
fs[#fs + 1] = get_tooltip(name, {
burntime = burntime,
norepair = norepair,
})
if burntime then
fs[#fs + 1] = fmt(FMT.image,
output_X + 1, YOFFSET + (sfinv_only and 0.7 or 0.1),
0.6, 0.4, PNG.arrow)
fs[#fs + 1] = fmt(FMT.image,
output_X + 1.6, YOFFSET + (sfinv_only and 0.55 or 0),
0.6, 0.6, PNG.fire)
end
end
end
end
local function get_recipe_fs(data, fs)
local recipe = data.recipes[data.rnum] local recipe = data.recipes[data.rnum]
local width = recipe.width local width = recipe.width
local replacements = recipe.replacements local replacements = recipe.replacements
@ -530,7 +599,8 @@ local function get_recipe_fs(data)
if is_group(item) then if is_group(item) then
groups = extract_groups(item) groups = extract_groups(item)
item = groups_to_items(groups) local items = groups_to_items(groups)
item = items[1] or items
end end
local label = groups and "\nG" or "" local label = groups and "\nG" or ""
@ -548,75 +618,38 @@ local function get_recipe_fs(data)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y + (sfinv_only and 0.7 or 0), X, Y + (sfinv_only and 0.7 or 0),
btn_size, btn_size, item, match(item, "%S*"), ESC(label)) btn_size, btn_size, item, clean_name(item), ESC(label))
local burntime = fuel_cache[item] and fuel_cache[item].burntime local burntime = fuel_cache[item] and fuel_cache[item].burntime
if groups or cooktime or burntime or replace then local more_info
fs[#fs + 1] = get_tooltip( local infos = {
item, burntime, nil, groups, cooktime, replace) groups = groups,
burntime = burntime,
cooktime = cooktime,
replace = replace,
norepair = nil,
}
for _, v in pairs(infos) do
if v then
more_info = true
break
end end
end end
local custom_recipe = craft_types[recipe.type] if more_info then
fs[#fs + 1] = get_tooltip(item, infos)
if custom_recipe or shapeless or recipe.type == "cooking" then
local icon = custom_recipe and custom_recipe.icon or
shapeless and "shapeless" or "furnace"
if not custom_recipe then
icon = fmt("craftguide_%s.png^[resize:16x16", icon)
end
local pos_x = rightest + btn_size + 0.1
local pos_y = YOFFSET + (sfinv_only and 0.25 or -0.45)
fs[#fs + 1] = fmt(FMT.image, pos_x, pos_y, 0.5, 0.5, icon)
local tooltip = custom_recipe and custom_recipe.description or
shapeless and S("Shapeless") or S("Cooking")
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
end
local arrow_X = rightest + (s_btn_size or 1.1)
local output_X = arrow_X + 0.9
fs[#fs + 1] = fmt(FMT.image,
arrow_X, YOFFSET + (sfinv_only and 0.9 or 0.2),
0.9, 0.7, PNG.arrow)
if recipe.type == "fuel" then
fs[#fs + 1] = fmt(FMT.image,
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
1.1, 1.1, PNG.fire)
else
local output_name = match(recipe.output, "%S+")
local burntime = fuel_cache[output_name] and fuel_cache[output_name].burntime
fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
1.1, 1.1, recipe.output, ESC(output_name), "")
local norepair = not_repairable(recipe.output)
if burntime or norepair then
fs[#fs + 1] = get_tooltip(output_name, burntime, norepair)
if burntime then
fs[#fs + 1] = fmt(FMT.image,
output_X + 1, YOFFSET + (sfinv_only and 0.7 or 0.1),
0.6, 0.4, PNG.arrow)
fs[#fs + 1] = fmt(FMT.image,
output_X + 1.6, YOFFSET + (sfinv_only and 0.55 or 0),
0.6, 0.6, PNG.fire)
end
end end
end end
return concat(fs) get_output_fs(fs, {
recipe = recipe,
shapeless = shapeless,
rightest = rightest,
btn_size = btn_size,
s_btn_size = s_btn_size,
})
end end
local function make_formspec(name) local function make_formspec(name)
@ -626,13 +659,17 @@ local function make_formspec(name)
local fs = {} local fs = {}
if not sfinv_only then if not sfinv_only then
local bg, middle = match(craftguide.background, "(.-):(%d+)")
bg = bg or ""
middle = middle or "10"
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
size[9.5,8.4] size[9.5,8.4]
no_prepend[] no_prepend[]
bgcolor[#00000000;false] bgcolor[#00000000;false]
background[1,1;1,1;%s;true%s] background[1,1;1,1;%s;true%s]
]], ]],
craftguide.background, DEV_CORE and ";10" or "") bg, DEV_CORE and ";" .. middle or "")
end end
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
@ -650,7 +687,7 @@ local function make_formspec(name)
fs[#fs + 1] = fmt("label[%f,%f;%s / %u]", fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
sfinv_only and 6.35 or 7.85, 0.06, sfinv_only and 6.35 or 7.85, 0.06,
colorize("yellow", data.pagenum), data.pagemax) clrz("yellow", data.pagenum), data.pagemax)
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
image_button[%f,-0.05;0.8,0.8;%s;prev;;;false;%s^\[colorize:yellow:255] image_button[%f,-0.05;0.8,0.8;%s;prev;;;false;%s^\[colorize:yellow:255]
@ -687,7 +724,7 @@ local function make_formspec(name)
end end
if data.recipes and #data.recipes > 0 then if data.recipes and #data.recipes > 0 then
fs[#fs + 1] = get_recipe_fs(data) get_recipe_fs(data, fs)
end end
return concat(fs) return concat(fs)
@ -818,16 +855,14 @@ core.register_craft = function(recipe)
(is_str(recipe.recipe) and recipe.recipe or "") (is_str(recipe.recipe) and recipe.recipe or "")
if output == "" then return end if output == "" then return end
output = output:match(match(output, "%S*")) output = {clean_name(output)}
local groups local groups
if is_group(output) then if is_group(output[1]) then
groups = extract_groups(output) groups = extract_groups(output[1])
output = groups_to_items(groups, true) output = groups_to_items(groups, true)
end end
output = split(output, ",")
for i = 1, #output do for i = 1, #output do
local item = output[i] local item = output[i]
if item == current_alias[1] then if item == current_alias[1] then
@ -1391,7 +1426,7 @@ register_command("craft", {
end end
end end
local red = colorize("red", "[craftguide] ") local red = clrz("red", "[craftguide] ")
if not node_name then if not node_name then
return false, red .. S("No node pointed") return false, red .. S("No node pointed")
@ -1408,7 +1443,7 @@ register_command("craft", {
end end
if not recipes or #recipes == 0 then if not recipes or #recipes == 0 then
local ylw = colorize("yellow", node_name) local ylw = clrz("yellow", node_name)
local msg = red .. "%s: " .. ylw local msg = red .. "%s: " .. ylw
if usages then if usages then