Compare commits

...

21 Commits

Author SHA1 Message Date
25e241daa2 Add safety 2020-11-24 19:23:12 +01:00
a964fd768c Handle entity models 2020-11-24 17:22:06 +01:00
fdfc6fe934 Handle sound client-side 2020-11-23 21:52:06 +01:00
9d26dc4840 Code cleaning 2020-11-18 04:07:48 +01:00
e74b485e7c More support for model[] 2020-11-18 01:58:24 +01:00
09e73745fb Fix duplicate recipes in resolve_aliases (?) 2020-11-18 01:27:19 +01:00
d7619cd778 Fix header desc 2020-11-16 02:30:17 +01:00
cb1e407c55 Add support for model[] for meshnodes 2020-11-16 02:16:09 +01:00
71af07067d More cleaning 2020-11-15 15:33:48 +01:00
6156dd55ad Tweak 2020-11-15 05:12:22 +01:00
b2b43fcf80 Minor cleanup 2020-11-15 04:36:30 +01:00
1d2023325d Replace hypertext by buttons 2020-11-15 03:37:55 +01:00
6c59066f89 Use content_offset on pagenum button 2020-11-15 03:08:41 +01:00
b8bf923125 Set relative font sizes 2020-11-15 03:03:20 +01:00
0de66b1a09 Small cleanup 2020-09-07 00:57:57 +02:00
aa46556020 Fix tooltip 2 2020-08-31 22:46:30 +02:00
ce59bd35d4 Fix tooltip 2020-08-31 15:07:39 +02:00
8c3efefd92 Cleanup 2020-08-29 23:14:59 +02:00
92c6a53fb2 Don't translate labels directly 2020-08-29 18:42:04 +02:00
f17922cf68 Improve header display 2020-08-29 18:34:04 +02:00
f91436d6ce Add chinese translations 2020-08-25 19:19:33 +02:00
2 changed files with 234 additions and 198 deletions

392
init.lua
View File

@ -18,12 +18,12 @@ local singleplayer = core.is_singleplayer()
local reg_items = core.registered_items local reg_items = core.registered_items
local reg_tools = core.registered_tools local reg_tools = core.registered_tools
local reg_entities = core.registered_entities
local reg_aliases = core.registered_aliases local reg_aliases = core.registered_aliases
local log = core.log local log = core.log
local after = core.after local after = core.after
local clr = core.colorize local clr = core.colorize
local sound_play = core.sound_play
local parse_json = core.parse_json local parse_json = core.parse_json
local write_json = core.write_json local write_json = core.write_json
local chat_send = core.chat_send_player local chat_send = core.chat_send_player
@ -54,12 +54,12 @@ local maxn, sort, concat, copy, insert, remove =
table.maxn, table.sort, table.concat, table.copy, table.maxn, table.sort, table.concat, table.copy,
table.insert, table.remove table.insert, table.remove
local fmt, find, gmatch, match, sub, split, upper, lower = local sprintf, find, gmatch, match, sub, split, upper, lower =
string.format, string.find, string.gmatch, string.match, string.format, string.find, string.gmatch, string.match,
string.sub, string.split, string.upper, string.lower string.sub, string.split, string.upper, string.lower
local min, max, floor, ceil, abs = math.min, math.max, math.floor, math.ceil, math.abs local min, max, floor, ceil, abs = math.min, math.max, math.floor, math.ceil, math.abs
local pairs, next, type, unpack = pairs, next, type, unpack local pairs, ipairs, next, type, setmetatable = pairs, ipairs, next, type, setmetatable
local vec_add, vec_mul = vector.add, vector.multiply local vec_add, vec_mul = vector.add, vector.multiply
local ROWS = 9 local ROWS = 9
@ -98,7 +98,7 @@ local PNG = {
next_hover = "craftguide_next_icon_hover.png", next_hover = "craftguide_next_icon_hover.png",
} }
local FMT = { local fs_elements = {
box = "box[%f,%f;%f,%f;%s]", box = "box[%f,%f;%f,%f;%s]",
label = "label[%f,%f;%s]", label = "label[%f,%f;%s]",
image = "image[%f,%f;%f,%f;%s]", image = "image[%f,%f;%f,%f;%s]",
@ -106,18 +106,20 @@ local FMT = {
tooltip = "tooltip[%f,%f;%f,%f;%s]", tooltip = "tooltip[%f,%f;%f,%f;%s]",
hypertext = "hypertext[%f,%f;%f,%f;;%s]", hypertext = "hypertext[%f,%f;%f,%f;;%s]",
item_image = "item_image[%f,%f;%f,%f;%s]", item_image = "item_image[%f,%f;%f,%f;%s]",
bg9 = "background9[%f,%f;%f,%f;%s;false;%u]",
model = "model[%f,%f;%f,%f;%s;%s;%s;0,0;true]",
image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]", image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]",
animated_image = "animated_image[%f,%f;%f,%f;;%s;%u;%u]", animated_image = "animated_image[%f,%f;%f,%f;;%s;%u;%u]",
item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]", item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]",
arrow = "image_button[%f,%f;0.8,0.8;%s;%s;;;false;%s]", arrow = "image_button[%f,%f;0.8,0.8;%s;%s;;;false;%s]",
} }
local styles = fmt([[ local styles = sprintf([[
style[filter;border=false] style[filter;border=false]
style_type[label,field;font_size=16] style_type[label,field;font_size=+0]
style_type[image_button;border=false] style_type[image_button;border=false;sound=craftguide_click]
style_type[button;border=false;font=bold;font_size=18] style_type[button;border=false;font=bold;font_size=+2;content_offset=0]
style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s] style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s;sound=craftguide_click]
style[search;fgimg=%s;fgimg_hovered=%s] style[search;fgimg=%s;fgimg_hovered=%s]
style[clear;fgimg=%s;fgimg_hovered=%s] style[clear;fgimg=%s;fgimg_hovered=%s]
style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s] style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
@ -143,7 +145,7 @@ local function get_lang_code(name)
end end
local function outdated(name) local function outdated(name)
local fs = fmt([[ local fs = sprintf([[
size[6.6,1.3] size[6.6,1.3]
image[0,0;1,1;%s] image[0,0;1,1;%s]
label[1,0;%s] label[1,0;%s]
@ -156,17 +158,6 @@ local function outdated(name)
return show_formspec(name, "craftguide", fs) return show_formspec(name, "craftguide", fs)
end end
local function mul_elem(elem, n)
local fstr, elems = "", {}
for i = 1, n do
fstr = fstr .. "%s"
elems[i] = elem
end
return fmt(fstr, unpack(elems))
end
craftguide.group_stereotypes = { craftguide.group_stereotypes = {
dye = "dye:white", dye = "dye:white",
wool = "wool:white", wool = "wool:white",
@ -226,12 +217,17 @@ local group_names = {
["color_dark_green,dye"] = S"Any dark green dye", ["color_dark_green,dye"] = S"Any dark green dye",
} }
craftguide.model_alias = {
["boats:boat"] = {name = "boats:boat", drawtype = "entity"},
--["carts:cart"] = {name = "carts:cart", drawtype = "entity"}, -- the cart animation is broken
}
local function err(str) local function err(str)
return log("error", str) return log("error", str)
end end
local function msg(name, str) local function msg(name, str)
return chat_send(name, fmt("[craftguide] %s", str)) return chat_send(name, sprintf("[craftguide] %s", str))
end end
local function is_str(x) local function is_str(x)
@ -254,6 +250,10 @@ local function is_group(item)
return sub(item, 1, 6) == "group:" return sub(item, 1, 6) == "group:"
end end
local function fmt(elem, ...)
return sprintf(fs_elements[elem], ...)
end
local function clean_name(item) local function clean_name(item)
if sub(item, 1, 1) == ":" then if sub(item, 1, 1) == ":" then
item = sub(item, 2) item = sub(item, 2)
@ -393,7 +393,7 @@ function craftguide.register_craft(def)
if true_str(def.url) then if true_str(def.url) then
if not http then if not http then
return err(fmt([[craftguide.register_craft(): Unable to reach %s. return err(sprintf([[craftguide.register_craft(): Unable to reach %s.
No HTTP support for this mod: add it to the `secure.http_mods` or No HTTP support for this mod: add it to the `secure.http_mods` or
`secure.trusted_mods` setting.]], def.url)) `secure.trusted_mods` setting.]], def.url))
end end
@ -680,7 +680,7 @@ local function drop_table(name, drop)
craftguide.register_craft{ craftguide.register_craft{
type = rarity and "digging_chance" or "digging", type = rarity and "digging_chance" or "digging",
items = {name}, items = {name},
output = fmt("%s %u", dname, dcount), output = sprintf("%s %u", dname, dcount),
rarity = rarity, rarity = rarity,
tools = di.tools, tools = di.tools,
} }
@ -819,19 +819,15 @@ local function is_fav(favs, query_item)
end end
local function weird_desc(str) local function weird_desc(str)
return not true_str(str) or find(str, "[\\]*") or not find(str, "%u") return not true_str(str) or find(str, "\n") or not find(str, "%u")
end end
local function toupper(str) local function toupper(str)
return str:gsub("%f[%w]%l", upper):gsub("_", " ") return str:gsub("%f[%w]%l", upper):gsub("_", " ")
end end
local function strip_newline(str) local function snip(str, limit)
return match(str, "[^\n]*") return #str > limit and sprintf("%s...", sub(str, 1, limit - 3)) or str
end
local function strip_prefix(str)
return match(str, ".*@.*%)(.*)()") or str
end end
local function get_desc(item, lang_code) local function get_desc(item, lang_code)
@ -843,11 +839,10 @@ local function get_desc(item, lang_code)
if def then if def then
local desc = def.description local desc = def.description
desc = lang_code and translate(lang_code, desc) or desc
if true_str(desc) then if true_str(desc) then
desc = translate(lang_code, desc) desc = desc:trim():match("[^\n]*")
desc = desc:trim()
desc = strip_newline(desc)
desc = strip_prefix(desc)
if not find(desc, "%u") then if not find(desc, "%u") then
desc = toupper(desc) desc = toupper(desc)
@ -863,7 +858,7 @@ local function get_desc(item, lang_code)
return S("Unknown Item (@1)", item) return S("Unknown Item (@1)", item)
end end
local function get_tooltip(item, info, lang_code) local function get_tooltip(item, info)
local tooltip local tooltip
if info.groups then if info.groups then
@ -881,11 +876,11 @@ local function get_tooltip(item, info, lang_code)
tooltip = S("Any item belonging to the group(s): @1", groupstr) tooltip = S("Any item belonging to the group(s): @1", groupstr)
end end
else else
tooltip = get_desc(item, lang_code) tooltip = get_desc(item)
end end
local function add(str) local function add(str)
return fmt("%s\n%s", tooltip, str) return sprintf("%s\n%s", tooltip, str)
end end
if info.cooktime then if info.cooktime then
@ -899,7 +894,7 @@ local function get_tooltip(item, info, lang_code)
if info.replace then if info.replace then
for i = 1, #info.replace.items do for i = 1, #info.replace.items do
local rpl = match(info.replace.items[i], "%S+") local rpl = match(info.replace.items[i], "%S+")
local desc = clr("#ff0", get_desc(rpl, lang_code)) local desc = clr("#ff0", get_desc(rpl))
if info.replace.type == "cooking" then if info.replace.type == "cooking" then
tooltip = add(S("Replaced by @1 on smelting", desc)) tooltip = add(S("Replaced by @1 on smelting", desc))
@ -926,22 +921,22 @@ local function get_tooltip(item, info, lang_code)
if several then if several then
for i = 1, #info.tools do for i = 1, #info.tools do
names = fmt("%s\t\t- %s\n", names = sprintf("%s\t\t- %s\n",
names, clr("#ff0", get_desc(info.tools[i], lang_code))) names, clr("#ff0", get_desc(info.tools[i])))
end end
tooltip = add(S("Only drop if using one of these tools: @1", tooltip = add(S("Only drop if using one of these tools: @1",
sub(names, 1, -2))) sub(names, 1, -2)))
else else
tooltip = add(S("Only drop if using this tool: @1", tooltip = add(S("Only drop if using this tool: @1",
clr("#ff0", get_desc(info.tools[1], lang_code)))) clr("#ff0", get_desc(info.tools[1]))))
end end
end end
return fmt("tooltip[%s;%s]", item, ESC(tooltip)) return sprintf("tooltip[%s;%s]", item, ESC(tooltip))
end end
local function get_output_fs(lang_code, fs, rcp, shapeless, right, btn_size, _btn_size, spacing) local function get_output_fs(fs, rcp, shapeless, right, btn_size, _btn_size, spacing)
local custom_recipe = craft_types[rcp.type] local custom_recipe = craft_types[rcp.type]
if custom_recipe or shapeless or rcp.type == "cooking" then if custom_recipe or shapeless or rcp.type == "cooking" then
@ -949,46 +944,42 @@ local function get_output_fs(lang_code, fs, rcp, shapeless, right, btn_size, _bt
shapeless and "shapeless" or "furnace" shapeless and "shapeless" or "furnace"
if not custom_recipe then if not custom_recipe then
icon = fmt("craftguide_%s.png^[resize:16x16", icon) icon = sprintf("craftguide_%s.png^[resize:16x16", icon)
end end
local pos_x = right + btn_size + 0.1 local pos_x = right + btn_size + 0.1
local pos_y = YOFFSET + (sfinv_only and 1.55 or -0.45) + spacing local pos_y = YOFFSET + (sfinv_only and 1.55 or -0.45) + spacing
if sub(icon, 1, 18) == "craftguide_furnace" then if sub(icon, 1, 18) == "craftguide_furnace" then
fs[#fs + 1] = fmt(FMT.animated_image, fs(fmt("animated_image", pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180))
pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180)
else else
fs[#fs + 1] = fmt(FMT.image, pos_x, pos_y, 0.5, 0.5, icon) fs(fmt("image", pos_x, pos_y, 0.5, 0.5, icon))
end end
local tooltip = custom_recipe and custom_recipe.description or local tooltip = custom_recipe and custom_recipe.description or
shapeless and S"Shapeless" or S"Cooking" shapeless and S"Shapeless" or S"Cooking"
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y, 0.5, 0.5, ESC(tooltip)) fs(fmt("tooltip", pos_x, pos_y, 0.5, 0.5, ESC(tooltip)))
end end
local arrow_X = right + (_btn_size or ITEM_BTN_SIZE) local arrow_X = right + (_btn_size or ITEM_BTN_SIZE)
local X = arrow_X + 0.9 local X = arrow_X + 0.9
local Y = YOFFSET + (sfinv_only and 2 or 0) + spacing local Y = YOFFSET + (sfinv_only and 2 or 0) + spacing
fs[#fs + 1] = fmt(FMT.image, arrow_X, Y + 0.2, 0.9, 0.7, PNG.arrow) fs(fmt("image", arrow_X, Y + 0.2, 0.9, 0.7, PNG.arrow))
if rcp.type == "fuel" then if rcp.type == "fuel" then
fs[#fs + 1] = fmt(FMT.animated_image, X, Y, fs(fmt("animated_image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180))
ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180)
else else
local item = rcp.output local item = rcp.output
item = clean_name(item) item = clean_name(item)
local name = match(item, "%S*") local name = match(item, "%S*")
fs[#fs + 1] = fmt(FMT.image, X, Y, fs(fmt("image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected))
ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected)
local _name = sfinv_only and name or fmt("_%s", name) local _name = sfinv_only and name or sprintf("_%s", name)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs(fmt("item_image_button", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, _name, ""))
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, _name, "")
local def = reg_items[name] local def = reg_items[name]
local unknown = not def or nil local unknown = not def or nil
@ -1006,22 +997,21 @@ local function get_output_fs(lang_code, fs, rcp, shapeless, right, btn_size, _bt
} }
if next(infos) then if next(infos) then
fs[#fs + 1] = get_tooltip(_name, infos, lang_code) fs(get_tooltip(_name, infos))
end end
if infos.burntime then if infos.burntime then
fs[#fs + 1] = fmt(FMT.image, fs(fmt("image",
X + 1, YOFFSET + (sfinv_only and 2 or 0.1) + spacing, X + 1, YOFFSET + (sfinv_only and 2 or 0.1) + spacing,
0.6, 0.4, PNG.arrow) 0.6, 0.4, PNG.arrow),
fmt("animated_image",
fs[#fs + 1] = fmt(FMT.animated_image,
X + 1.6, YOFFSET + (sfinv_only and 1.85 or 0) + spacing, X + 1.6, YOFFSET + (sfinv_only and 1.85 or 0) + spacing,
0.6, 0.6, PNG.fire_anim, 8, 180) 0.6, 0.6, PNG.fire_anim, 8, 180))
end end
end end
end end
local function get_grid_fs(lang_code, fs, rcp, spacing) local function get_grid_fs(fs, rcp, spacing)
local width = rcp.width or 1 local width = rcp.width or 1
local right, btn_size, _btn_size = 0, ITEM_BTN_SIZE local right, btn_size, _btn_size = 0, ITEM_BTN_SIZE
local cooktime, shapeless local cooktime, shapeless
@ -1037,10 +1027,10 @@ local function get_grid_fs(lang_code, fs, rcp, spacing)
local rows = ceil(maxn(rcp.items) / width) local rows = ceil(maxn(rcp.items) / width)
if width > WH_LIMIT or rows > WH_LIMIT then if width > WH_LIMIT or rows > WH_LIMIT then
fs[#fs + 1] = fmt(FMT.label, fs(fmt("label",
XOFFSET + (sfinv_only and -1.5 or -1.6), XOFFSET + (sfinv_only and -1.5 or -1.6),
YOFFSET + (sfinv_only and 0.5 or spacing), YOFFSET + (sfinv_only and 0.5 or spacing),
ES("Recipe's too big to be displayed (@1x@2)", width, rows)) ES("Recipe's too big to be displayed (@1x@2)", width, rows)))
return concat(fs) return concat(fs)
end end
@ -1048,7 +1038,7 @@ local function get_grid_fs(lang_code, fs, rcp, spacing)
local large_recipe = width > 3 or rows > 3 local large_recipe = width > 3 or rows > 3
if large_recipe then if large_recipe then
fs[#fs + 1] = "style_type[item_image_button;border=true]" fs("style_type[item_image_button;border=true]")
end end
for i = 1, width * rows do for i = 1, width * rows do
@ -1098,7 +1088,8 @@ local function get_grid_fs(lang_code, fs, rcp, spacing)
end end
if not added then if not added then
label = fmt("%s%s\nR", label ~= "" and "\n" or "", label) label = sprintf("%s%s\nR",
label ~= "" and "\n" or "", label)
replace.items[#replace.items + 1] = replacement[2] replace.items[#replace.items + 1] = replacement[2]
end end
end end
@ -1107,19 +1098,12 @@ local function get_grid_fs(lang_code, fs, rcp, spacing)
Y = Y + (sfinv_only and 2 or 0) Y = Y + (sfinv_only and 2 or 0)
if not large_recipe then if not large_recipe then
fs[#fs + 1] = fmt(FMT.image, X, Y, btn_size, btn_size, PNG.selected) fs(fmt("image", X, Y, btn_size, btn_size, PNG.selected))
end end
local btn_name = "" local btn_name = groups and sprintf("group|%s|%s", groups[1], item) or item
if groups then fs(fmt("item_image_button", X, Y, btn_size, btn_size, item, btn_name, label))
btn_name = fmt("group|%s|%s", groups[1], item)
elseif item ~= "" then
btn_name = item
end
fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y, btn_size, btn_size, item, btn_name, label)
local def = reg_items[name] local def = reg_items[name]
local unknown = not def or nil local unknown = not def or nil
@ -1138,105 +1122,138 @@ local function get_grid_fs(lang_code, fs, rcp, spacing)
} }
if next(infos) then if next(infos) then
fs[#fs + 1] = get_tooltip(btn_name, infos, lang_code) fs(get_tooltip(btn_name, infos))
end end
end end
if large_recipe then if large_recipe then
fs[#fs + 1] = "style_type[item_image_button;border=false]" fs("style_type[item_image_button;border=false]")
end end
get_output_fs(lang_code, fs, rcp, shapeless, right, btn_size, _btn_size, spacing) get_output_fs(fs, rcp, shapeless, right, btn_size, _btn_size, spacing)
end end
local function get_rcp_lbl(lang_code, show_usages, unum, rnum, fs, panel, spacing, rn, is_recipe) local function get_rcp_lbl(fs, data, panel, spacing, rn, is_recipe)
local lbl local lbl
if (not sfinv_only and is_recipe) or (sfinv_only and not show_usages) then if (not sfinv_only and is_recipe) or (sfinv_only and not data.show_usages) then
lbl = ES("Recipe @1 of @2", rnum, rn) lbl = ES("Recipe @1 of @2", data.rnum, rn)
elseif not sfinv_only or (sfinv_only and show_usages) then elseif not sfinv_only or (sfinv_only and data.show_usages) then
lbl = ES("Usage @1 of @2", unum, rn) lbl = ES("Usage @1 of @2", data.unum, rn)
elseif sfinv_only then elseif sfinv_only then
lbl = show_usages and ES("Usage @1 of @2", unum, rn) or lbl = data.show_usages and ES("Usage @1 of @2", data.unum, rn) or
ES("Recipe @1 of @2", rnum, rn) ES("Recipe @1 of @2", data.rnum, rn)
end end
lbl = translate(lang_code, lbl) local _lbl = translate(data.lang_code, lbl)
local lbl_len = #lbl:gsub("[\128-\191]", "") -- Count chars, not bytes in UTF-8 strings local lbl_len = #_lbl:gsub("[\128-\191]", "") -- Count chars, not bytes in UTF-8 strings
local shift = min(0.9, abs(13 - max(13, lbl_len)) * 0.1) local shift = min(0.9, abs(12 - max(12, lbl_len)) * 0.1)
fs[#fs + 1] = fmt(FMT.label, fs(fmt("label",
XOFFSET + (sfinv_only and 2.3 or 1.6) - shift, XOFFSET + (sfinv_only and 2.3 or 1.6) - shift,
YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl) YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl))
if rn > 1 then if rn > 1 then
local btn_suffix = is_recipe and "recipe" or "usage" local btn_suffix = is_recipe and "recipe" or "usage"
local prev_name = fmt("prev_%s", btn_suffix) local prev_name = sprintf("prev_%s", btn_suffix)
local next_name = fmt("next_%s", btn_suffix) local next_name = sprintf("next_%s", btn_suffix)
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1) local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing) local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing)
fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2), fs(fmt("arrow", x_arrow - shift, y_arrow, PNG.prev, prev_name, ""),
x_arrow - shift, y_arrow, PNG.prev, prev_name, "", fmt("arrow", x_arrow + 1.8, y_arrow, PNG.next, next_name, ""))
x_arrow + 1.8, y_arrow, PNG.next, next_name, "")
end end
local rcp = is_recipe and panel.rcp[rnum] or panel.rcp[unum] local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum]
get_grid_fs(lang_code, fs, rcp, spacing) get_grid_fs(fs, rcp, spacing)
end end
local function get_title_fs(query_item, lang_code, favs, fs, spacing) local function get_title_fs(query_item, favs, lang_code, fs, spacing)
local desc = ESC(get_desc(query_item, lang_code)) fs("style_type[label;font=bold;font_size=+6]",
desc = #desc > 33 and fmt("%s...", sub(desc, 1, 30)) or desc fmt("label", 8.75, spacing - 0.1, snip(ESC(get_desc(query_item, lang_code)), 45)),
local t_desc = query_item "style_type[label;font=mono;font_size=+0]",
t_desc = #t_desc > 40 and fmt("%s...", sub(t_desc, 1, 37)) or t_desc fmt("label", 8.75, spacing + 0.3, clr("#7bf", snip(query_item, 35))),
"style_type[label;font=normal]")
fs[#fs + 1] = fmt(FMT.hypertext, local def = reg_items[query_item]
9.05, spacing - 0.1, 5.85, 1.2, local model_alias = craftguide.model_alias[query_item]
fmt("<item name=%s float=right width=64 height=64 rotate=yes>" ..
"<big><b>%s</b></big>\n<style color=#7bf font=mono>%s</style>", if def.drawtype == "mesh" or model_alias then
query_item, desc, t_desc)) if model_alias then
if model_alias.drawtype == "entity" then
def = reg_entities[model_alias.name]
local init_props = def.initial_properties
def.textures = init_props and init_props.textures or def.textures
def.mesh = init_props and init_props.mesh or def.mesh
else
def = reg_items[model_alias.name]
end
end
local tiles = def.tiles or def.textures or {}
local t = {}
for _, v in ipairs(tiles) do
local _name
if v.color then
local hex = sprintf("%02x", v.color)
while #hex < 8 do
hex = "0" .. hex
end
_name = sprintf("%s^[multiply:%s", v.name,
sprintf("#%s%s", sub(hex, 3), sub(hex, 1, 2)))
elseif v.animation then
_name = sprintf("%s^[verticalframe:%u:0", v.name,
v.animation.aspect_h)
end
t[#t + 1] = _name or v.name or v
end
while #t < 6 do
t[#t + 1] = t[#t]
end
fs(fmt("model", 13.5, spacing - 0.2, 1.2, 1.2, "", def.mesh, concat(t, ",")))
else
fs(fmt("hypertext", 13.8, spacing - 0.15, 1.1, 1.3,
sprintf("<item name=%s width=64 rotate=yes>", query_item)))
end
local fav = is_fav(favs, query_item) local fav = is_fav(favs, query_item)
local nfavs = #favs local nfavs = #favs
if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then
local fav_marked = fmt("craftguide_fav%s.png", fav and "_off" or "") local fav_marked = sprintf("craftguide_fav%s.png", fav and "_off" or "")
fs[#fs + 1] = fmt( fs(sprintf("style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
"style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]", sprintf("craftguide_fav%s.png", fav and "" or "_off"),
fmt("craftguide_fav%s.png", fav and "" or "_off"), fav_marked, fav_marked) fav_marked, fav_marked),
fmt("image_button", 8.25, spacing + 0.15, 0.5, 0.45, "", "fav", ""),
fs[#fs + 1] = fmt(FMT.image_button, 8.25, spacing + 0.15, 0.5, 0.45, "", "fav", "") sprintf("tooltip[fav;%s]", fav and ES"Unmark this item" or ES"Mark this item"))
fs[#fs + 1] = fmt("tooltip[fav;%s]",
fav and ES"Unmark this item" or ES"Mark this item")
else else
fs[#fs + 1] = fmt( fs(sprintf("style[fav_no;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
"style[fav_no;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]", "craftguide_fav_off.png", PNG.nothing, PNG.nothing),
"craftguide_fav_off.png", PNG.nothing, PNG.nothing) fmt("image_button", 8.25, spacing + 0.15, 0.5, 0.45, "", "fav_no", ""),
sprintf("tooltip[fav_no;%s]", ES"Cannot mark this item. Bookmark limit reached."))
fs[#fs + 1] = fmt(FMT.image_button,
8.25, spacing + 0.15, 0.5, 0.45, "", "fav_no", "")
fs[#fs + 1] = fmt("tooltip[fav_no;%s]",
ES"Cannot mark this item. Limit of bookmarks reached.")
end end
end end
local function get_panels(lang_code, query_item, recipes, usages, show_usages, local function get_panels(data, fs)
favs, unum, rnum, fs)
local _title = {name = "title", height = 1.2} local _title = {name = "title", height = 1.2}
local _favs = {name = "favs", height = 1.91} local _favs = {name = "favs", height = 1.91}
local _recipes = {name = "recipes", rcp = recipes, height = 3.5} local _recipes = {name = "recipes", rcp = data.recipes, height = 3.5}
local _usages = {name = "usages", rcp = usages, height = 3.5} local _usages = {name = "usages", rcp = data.usages, height = 3.5}
local panels = {_title, _recipes, _usages, _favs} local panels = {_title, _recipes, _usages, _favs}
if sfinv_only then if sfinv_only then
panels = {show_usages and _usages or _recipes} panels = {data.show_usages and _usages or _recipes}
end end
for idx = 1, #panels do for idx = 1, #panels do
@ -1249,94 +1266,85 @@ local function get_panels(lang_code, query_item, recipes, usages, show_usages,
end end
local rn = panel.rcp and #panel.rcp local rn = panel.rcp and #panel.rcp
local is_recipe = sfinv_only and not show_usages or panel.name == "recipes" local is_recipe = sfinv_only and not data.show_usages or panel.name == "recipes"
local recipe_or_usage = panel.name == "recipes" or panel.name == "usages" local recipe_or_usage = panel.name == "recipes" or panel.name == "usages"
if rn then if rn then
get_rcp_lbl(lang_code, show_usages, unum, rnum, fs, panel, get_rcp_lbl(fs, data, panel, spacing, rn, is_recipe)
spacing, rn, is_recipe)
end end
if sfinv_only then return end if sfinv_only then return end
fs[#fs + 1] = fmt("background9[8.1,%f;6.6,%f;%s;false;%d]", fs(fmt("bg9", 8.1, -0.2 + spacing, 6.6, panel.height, PNG.bg_full, 10))
-0.2 + spacing, panel.height, PNG.bg_full, 10)
if recipe_or_usage and not rn then if recipe_or_usage and not rn then
local lbl = is_recipe and ES"No recipes" or ES"No usages" local lbl = is_recipe and ES"No recipes" or ES"No usages"
fs(fmt("button", 8, YOFFSET + spacing + 0.1, 6.8, 1, "", lbl))
fs[#fs + 1] = fmt(FMT.hypertext,
8.29, YOFFSET + spacing + 0.3, 6.8, 1,
fmt("<center><style size=20><b>%s</b></style></center>",
translate(lang_code, lbl)))
elseif panel.name == "title" then elseif panel.name == "title" then
get_title_fs(query_item, lang_code, favs, fs, spacing) get_title_fs(data.query_item, data.favs, data.lang_code, fs, spacing)
elseif panel.name == "favs" then elseif panel.name == "favs" then
fs[#fs + 1] = fmt(FMT.label, 8.3, spacing - 0.15, ES"Bookmarks") fs(fmt("label", 8.3, spacing - 0.15, ES"Bookmarks"))
for i = 1, #favs do for i = 1, #data.favs do
local item = favs[i] local item = data.favs[i]
local X = 7.85 + (i - 0.5) local X = 7.85 + (i - 0.5)
local Y = spacing + 0.4 local Y = spacing + 0.4
if query_item == item then if data.query_item == item then
fs[#fs + 1] = fmt(FMT.image, X, Y, fs(fmt("image",
ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected) X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected))
end end
fs[#fs + 1] = fmt(FMT.item_image_button, fs(fmt("item_image_button",
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, "") X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, ""))
end end
end end
end end
end end
local function make_fs(data) local function make_fs(data)
local fs = {} local fs = setmetatable({}, {
__call = function(t, ...)
t[#t + 1] = concat({...})
end
})
fs[#fs + 1] = fmt([[ fs(sprintf([[
size[%f,%f] size[%f,%f]
no_prepend[] no_prepend[]
bgcolor[#0000] bgcolor[#0000]
]], ]],
9 + (data.query_item and 6.7 or 0) - 1.2, LINES - 0.3) 9 + (data.query_item and 6.7 or 0) - 1.2, LINES - 0.3), styles)
if not sfinv_only then if not sfinv_only then
fs[#fs + 1] = fmt("background9[-0.15,-0.2;%f,%f;%s;false;%d]", fs(fmt("bg9", -0.15, -0.2, 9 - 0.9, LINES + 0.4, PNG.bg_full, 10))
9 - 0.9, LINES + 0.4, PNG.bg_full, 10)
end end
fs[#fs + 1] = styles fs(sprintf([[
fs[#fs + 1] = fmt([[
field[0.4,0.2;2.6,1;filter;;%s] field[0.4,0.2;2.6,1;filter;;%s]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
box[0,0;2.5,0.6;#bababa25] box[0,0;2.5,0.6;#bababa25]
]], ]], ESC(data.filter)),
ESC(data.filter)) fmt("image_button", 2.6, -0.06, 0.85, 0.85, "", "search", ""),
fmt("image_button", 3.3, -0.06, 0.85, 0.85, "", "clear", ""))
fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 2),
2.6, -0.06, 0.85, 0.85, "", "search", "",
3.3, -0.06, 0.85, 0.85, "", "clear", "")
if sfinv_only then if sfinv_only then
fs[#fs + 1] = "container[0.2,0]" fs("container[0.2,0]")
end end
fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 2), fs(fmt("image_button", 5.35, -0.06, 0.85, 0.85, "", "prev_page", ""),
5.35, -0.06, 0.85, 0.85, "", "prev_page", "", fmt("image_button", 7.1, -0.06, 0.85, 0.85, "", "next_page", ""))
7.1, -0.06, 0.85, 0.85, "", "next_page", "")
data.pagemax = max(1, ceil(#data.items / IPP)) data.pagemax = max(1, ceil(#data.items / IPP))
fs[#fs + 1] = fmt(FMT.button, fs(fmt("button",
5.97, -0.06, 1.36, 0.85, "pagenum", 5.97, -0.06, 1.36, 0.85, "pagenum",
fmt("%s / %u", clr("#ff0", data.pagenum), data.pagemax)) sprintf("%s / %u", clr("#ff0", data.pagenum), data.pagemax)))
if sfinv_only then if sfinv_only then
fs[#fs + 1] = "container_end[]" fs("container_end[]")
end end
if #data.items == 0 then if #data.items == 0 then
@ -1346,10 +1354,7 @@ local function make_fs(data)
lbl = ES"Collect items to reveal more recipes" lbl = ES"Collect items to reveal more recipes"
end end
fs[#fs + 1] = fmt(FMT.hypertext, fs(fmt("button", -0.25, 3, 8.3, 1, "", lbl))
0.05, 3, 8.29, 1,
fmt("<center><style size=20><b>%s</b></style></center>",
translate(data.lang_code, lbl)))
end end
local first_item = (data.pagenum - 1) * IPP local first_item = (data.pagenum - 1) * IPP
@ -1364,16 +1369,14 @@ local function make_fs(data)
Y = Y - (Y * 0.08) - 0.15 Y = Y - (Y * 0.08) - 0.15
if data.query_item == item then if data.query_item == item then
fs[#fs + 1] = fmt(FMT.image, X, Y, 1, 1, PNG.selected) fs(fmt("image", X, Y, 1, 1, PNG.selected))
end end
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]", fs(fmt("item_image_button", X, Y, 1, 1, item, sprintf("%s_inv", item), ""))
X, Y, 1, 1, item, item)
end end
if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then
get_panels(data.lang_code, data.query_item, data.recipes, data.usages, get_panels(data, fs)
data.show_usages, data.favs, data.unum, data.rnum, fs)
end end
return concat(fs) return concat(fs)
@ -1425,7 +1428,7 @@ local function search(data)
local item = data.items_raw[i] local item = data.items_raw[i]
local def = reg_items[item] local def = reg_items[item]
local desc = lower(translate(data.lang_code, def and def.description)) or "" local desc = lower(translate(data.lang_code, def and def.description)) or ""
local search_in = fmt("%s %s", item, desc) local search_in = sprintf("%s %s", item, desc)
local to_add local to_add
if search_filter then if search_filter then
@ -1542,9 +1545,8 @@ local function resolve_aliases(hash)
local rcp_old = recipes_cache[oldname][i] local rcp_old = recipes_cache[oldname][i]
for j = 1, #recipes_cache[newname] do for j = 1, #recipes_cache[newname] do
local rcp_new = recipes_cache[newname][j] local rcp_new = copy(recipes_cache[newname][j])
rcp_new.type = nil rcp_new.output = oldname
rcp_new.method = nil
if table_eq(rcp_old, rcp_new) then if table_eq(rcp_old, rcp_new) then
similar = true similar = true
@ -1649,10 +1651,6 @@ local function fields(player, _f)
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
if not _f.key_enter_field then
sound_play("craftguide_click", {to_player = name, gain = 0.2})
end
if _f.clear then if _f.clear then
reset_data(data) reset_data(data)
@ -1677,7 +1675,7 @@ local function fields(player, _f)
data.pagenum = 1 data.pagenum = 1
search(data) search(data)
elseif _f.prev_page or _f.next_page or _f.pagenum then elseif _f.prev_page or _f.next_page then
if data.pagemax == 1 then return end if data.pagemax == 1 then return end
data.pagenum = data.pagenum - (_f.prev_page and 1 or -1) data.pagenum = data.pagenum - (_f.prev_page and 1 or -1)
@ -2142,14 +2140,12 @@ function craftguide.show(name, item, show_usages)
if not recipes and not usages then if not recipes and not usages then
if not recipes_cache[item] and not usages_cache[item] then if not recipes_cache[item] and not usages_cache[item] then
return false, msg(name, fmt("%s: %s", return false, msg(name, sprintf("%s: %s",
S"No recipe or usage for this item", S"No recipe or usage for this item", get_desc(item)))
get_desc(item, data.lang_code)))
end end
return false, msg(name, fmt("%s: %s", return false, msg(name, sprintf("%s: %s",
S"You don't know a recipe or usage for this item", S"You don't know a recipe or usage for this item", get_desc(item)))
get_desc(item, data.lang_code)))
end end
data.query_item = item data.query_item = item

View File

@ -0,0 +1,40 @@
# textdomain: craftguide
Any black dye=任何黑染料
Any black flower=任何黑花
Any blue dye=任何蓝染料
Any blue flower=任何蓝花
Any brown dye=任何棕染料
Any coal=任何煤炭
Any cyan dye=任何青染料
Any dark green dye=任何暗绿染料
Any dark grey dye=任何暗灰染料
Any green dye=任何绿染料
Any green flower=任何绿花
Any grey dye=任何灰染料
Any item belonging to the group(s): @1=任何属于以下组别的物品:@1
Any kind of stone block=任何石方块
Any magenta dye=任何品红染料
Any orange dye=任何橙染料
Any orange flower=任何橙花
Any pink dye=任何粉红染料
Any red dye=任何红染料
Any red flower=任何红花
Any sand=任何沙
Any stick=任何棒
Any vessel=任何容器
Any violet dye=任何紫染料
Any violet flower=任何紫花
Any white dye=任何白染料
Any white flower=任何白花
Any wood planks=任何木板
Any wool=任何羊毛
Any yellow dye=任何黄染料
Any yellow flower=任何黄花
Cooking time: @1=熔炼时间为:@1
No items to show=没有可显示的项目。
Recipe @1 of @2=第@1个配方共@2个
Usage @1 of @2=第@1个用法共@2个
Recipe is too big to be displayed=配方太大,无法显示。
Shapeless=无序配方
Unknown Item=不明物品