Compare commits

...

17 Commits
1.10.1 ... 1.11

7 changed files with 256 additions and 167 deletions

2
API.md
View File

@ -204,7 +204,7 @@ You can add a stereotype like so:
craftguide.group_stereotypes.radioactive = "mod:item" craftguide.group_stereotypes.radioactive = "mod:item"
``` ```
#### `craftguide.http_post_data` #### `craftguide.export_url`
If set, the mod will export all the cached recipes and usages in a JSON format If set, the mod will export all the cached recipes and usages in a JSON format
to the given URL (HTTP support is required¹). to the given URL (HTTP support is required¹).

409
init.lua
View File

@ -57,6 +57,10 @@ local S = CORE_VERSION >= 500 and core.get_translator("craftguide") or
end) end)
end end
local ES = function(...)
return ESC(S(...))
end
local maxn, sort, concat, copy, insert, remove = 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
@ -75,7 +79,7 @@ local IPP = ROWS * LINES
local WH_LIMIT = 8 local WH_LIMIT = 8
local XOFFSET = sfinv_only and 3.83 or 11.2 local XOFFSET = sfinv_only and 3.83 or 11.2
local YOFFSET = sfinv_only and 4.9 or 1 local YOFFSET = sfinv_only and 6 or 1
local PNG = { local PNG = {
bg = "craftguide_bg.png", bg = "craftguide_bg.png",
@ -108,6 +112,17 @@ local FMT = {
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 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",
@ -123,7 +138,7 @@ local function err(str)
end end
local function msg(name, str) local function msg(name, str)
return chat_send(name, fmt("[craftguide] %s", clr("#FFFF00", str))) return chat_send(name, fmt("[craftguide] %s", clr("#f00", str)))
end end
local function is_str(x) local function is_str(x)
@ -146,7 +161,7 @@ local function is_group(item)
return sub(item, 1, 6) == "group:" return sub(item, 1, 6) == "group:"
end end
local function clean_item(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)
end end
@ -265,7 +280,7 @@ local craft_types = {}
function craftguide.register_craft_type(name, def) function craftguide.register_craft_type(name, def)
if not true_str(name) then if not true_str(name) then
return err("craftguide.register_craft_type(): name missing") return err"craftguide.register_craft_type(): name missing"
end end
if not is_str(def.description) then if not is_str(def.description) then
@ -284,8 +299,8 @@ 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("No HTTP support for this mod. " .. return err"No HTTP support for this mod. " ..
"Add it to the `secure.http_mods` or `secure.trusted_mods` setting.") "Add it to the `secure.http_mods` or `secure.trusted_mods` setting."
end end
http.fetch({url = def.url}, function(result) http.fetch({url = def.url}, function(result)
@ -301,7 +316,7 @@ function craftguide.register_craft(def)
end end
if not is_table(def) or not next(def) then if not is_table(def) or not next(def) then
return err("craftguide.register_craft(): craft definition missing") return err"craftguide.register_craft(): craft definition missing"
end end
if #def > 1 then if #def > 1 then
@ -317,7 +332,7 @@ function craftguide.register_craft(def)
end end
if not true_str(def.output) then if not true_str(def.output) then
return err("craftguide.register_craft(): output missing") return err"craftguide.register_craft(): output missing"
end end
if not is_table(def.items) then if not is_table(def.items) then
@ -387,9 +402,9 @@ local recipe_filters = {}
function craftguide.add_recipe_filter(name, f) function craftguide.add_recipe_filter(name, f)
if not true_str(name) then if not true_str(name) then
return err("craftguide.add_recipe_filter(): name missing") return err"craftguide.add_recipe_filter(): name missing"
elseif not is_func(f) then elseif not is_func(f) then
return err("craftguide.add_recipe_filter(): function missing") return err"craftguide.add_recipe_filter(): function missing"
end end
recipe_filters[name] = f recipe_filters[name] = f
@ -397,9 +412,9 @@ end
function craftguide.set_recipe_filter(name, f) function craftguide.set_recipe_filter(name, f)
if not is_str(name) then if not is_str(name) then
return err("craftguide.set_recipe_filter(): name missing") return err"craftguide.set_recipe_filter(): name missing"
elseif not is_func(f) then elseif not is_func(f) then
return err("craftguide.set_recipe_filter(): function missing") return err"craftguide.set_recipe_filter(): function missing"
end end
recipe_filters = {[name] = f} recipe_filters = {[name] = f}
@ -425,9 +440,9 @@ local search_filters = {}
function craftguide.add_search_filter(name, f) function craftguide.add_search_filter(name, f)
if not true_str(name) then if not true_str(name) then
return err("craftguide.add_search_filter(): name missing") return err"craftguide.add_search_filter(): name missing"
elseif not is_func(f) then elseif not is_func(f) then
return err("craftguide.add_search_filter(): function missing") return err"craftguide.add_search_filter(): function missing"
end end
search_filters[name] = f search_filters[name] = f
@ -455,8 +470,10 @@ local function extract_groups(str)
end end
local function item_in_recipe(item, recipe) local function item_in_recipe(item, recipe)
local clean_item = reg_aliases[item] or item
for _, recipe_item in pairs(recipe.items) do for _, recipe_item in pairs(recipe.items) do
if recipe_item == item then local clean_recipe_item = reg_aliases[recipe_item] or recipe_item
if clean_recipe_item == clean_item then
return true return true
end end
end end
@ -539,7 +556,7 @@ local function get_usages(item)
end end
local function get_burntime(item) local function get_burntime(item)
return get_craft_result({method = "fuel", items = {item}}).time return get_craft_result{method = "fuel", items = {item}}.time
end end
local function cache_fuel(item) local function cache_fuel(item)
@ -564,8 +581,9 @@ local function cache_recipes(output)
end end
local function get_recipes(item, data, player) local function get_recipes(item, data, player)
local recipes = recipes_cache[item] local clean_item = reg_aliases[item] or item
local usages = usages_cache[item] local recipes = recipes_cache[clean_item]
local usages = usages_cache[clean_item]
if recipes then if recipes then
recipes = apply_recipe_filters(recipes, player) recipes = apply_recipe_filters(recipes, player)
@ -621,6 +639,19 @@ local function 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 is_fav(data)
local fav, i
for j = 1, #data.favs do
if data.favs[j] == data.query_item then
fav = true
i = j
break
end
end
return fav, i
end
local function get_desc(name) local function get_desc(name)
local def = reg_items[name] local def = reg_items[name]
@ -637,7 +668,7 @@ local function get_tooltip(name, info)
for i = 1, #info.groups do for i = 1, #info.groups do
c = c + 1 c = c + 1
groupstr[c] = clr("yellow", info.groups[i]) groupstr[c] = clr("#ff0", info.groups[i])
end end
groupstr = concat(groupstr, ", ") groupstr = concat(groupstr, ", ")
@ -651,15 +682,15 @@ local function get_tooltip(name, info)
end end
if info.cooktime then if info.cooktime then
tooltip = add(S("Cooking time: @1", clr("yellow", info.cooktime))) tooltip = add(S("Cooking time: @1", clr("#ff0", info.cooktime)))
end end
if info.burntime then if info.burntime then
tooltip = add(S("Burning time: @1", clr("yellow", info.burntime))) tooltip = add(S("Burning time: @1", clr("#ff0", info.burntime)))
end end
if info.replace then if info.replace then
local desc = clr("yellow", get_desc(info.replace)) local desc = clr("#ff0", get_desc(info.replace))
if info.cooktime then if info.cooktime then
tooltip = add(S("Replaced by @1 on smelting", desc)) tooltip = add(S("Replaced by @1 on smelting", desc))
@ -671,12 +702,12 @@ local function get_tooltip(name, info)
end end
if info.repair then if info.repair then
tooltip = add(S("Repairable by step of @1", clr("yellow", toolrepair .. "%"))) tooltip = add(S("Repairable by step of @1", clr("#ff0", toolrepair .. "%")))
end end
if info.rarity then if info.rarity then
local chance = (1 / info.rarity) * 100 local chance = (1 / info.rarity) * 100
tooltip = add(S("@1 of chance to drop", clr("yellow", chance .. "%"))) tooltip = add(S("@1 of chance to drop", clr("#ff0", chance .. "%")))
end end
return fmt("tooltip[%s;%s]", name, ESC(tooltip)) return fmt("tooltip[%s;%s]", name, ESC(tooltip))
@ -699,7 +730,7 @@ local function get_output_fs(fs, L)
fs[#fs + 1] = fmt(FMT.image, pos_x, pos_y + L.spacing, 0.5, 0.5, icon) fs[#fs + 1] = fmt(FMT.image, pos_x, pos_y + L.spacing, 0.5, 0.5, icon)
local tooltip = custom_recipe and custom_recipe.description or local tooltip = custom_recipe and custom_recipe.description or
L.shapeless and S("Shapeless") or S("Cooking") L.shapeless and S"Shapeless" or S"Cooking"
if CORE_VERSION >= 500 then if CORE_VERSION >= 500 then
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y + L.spacing, 0.5, 0.5, ESC(tooltip)) fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y + L.spacing, 0.5, 0.5, ESC(tooltip))
@ -719,19 +750,19 @@ local function get_output_fs(fs, L)
1.1, 1.1, PNG.fire) 1.1, 1.1, PNG.fire)
else else
local item = L.recipe.output local item = L.recipe.output
item = clean_item(item) item = clean_name(item)
local name = match(item, "%S*") local name = match(item, "%S*")
fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, item, name, "")
if CORE_VERSION >= 510 then if CORE_VERSION >= 510 then
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing, output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, PNG.selected) 1.1, 1.1, PNG.selected)
end end
fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, item, name, "")
local infos = { local infos = {
unknown = not reg_items[name] or nil, unknown = not reg_items[name] or nil,
burntime = fuel_cache[name], burntime = fuel_cache[name],
@ -776,7 +807,7 @@ local function get_grid_fs(fs, rcp, spacing)
fs[#fs + 1] = fmt(FMT.label, fs[#fs + 1] = fmt(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),
ESC(S("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
@ -789,7 +820,7 @@ local function get_grid_fs(fs, rcp, spacing)
for i = 1, width * rows do for i = 1, width * rows do
local item = rcp.items[i] or "" local item = rcp.items[i] or ""
item = clean_item(item) item = clean_name(item)
local name = match(item, "%S*") local name = match(item, "%S*")
local X = ceil((i - 1) % width - width) + XOFFSET local X = ceil((i - 1) % width - width) + XOFFSET
@ -834,6 +865,11 @@ local function get_grid_fs(fs, rcp, spacing)
end end
end end
if CORE_VERSION >= 510 and not large_recipe then
fs[#fs + 1] = fmt(FMT.image,
X, Y + (sfinv_only and 0.7 or 0), btn_size, btn_size, PNG.selected)
end
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, item, ESC(label)) btn_size, btn_size, item, item, ESC(label))
@ -849,12 +885,6 @@ local function get_grid_fs(fs, rcp, spacing)
if next(infos) then if next(infos) then
fs[#fs + 1] = get_tooltip(item, infos) fs[#fs + 1] = get_tooltip(item, infos)
end end
if CORE_VERSION >= 510 and not large_recipe then
fs[#fs + 1] = fmt(FMT.image,
X, Y + (sfinv_only and 0.7 or 0),
btn_size, btn_size, PNG.selected)
end
end end
if large_recipe then if large_recipe then
@ -873,107 +903,136 @@ local function get_grid_fs(fs, rcp, spacing)
end end
local function get_panels(data, fs) local function get_panels(data, fs)
local panels = {recipes = data.recipes or {}, usages = data.usages or {}} local start_y = CORE_VERSION >= 520 and 0 or 0.33
local x = 0.33 local panels = {
{dat = data.usages or {}, height = 3.5},
{dat = data.recipes or {}, height = 3.5},
}
if sfinv_only then if CORE_VERSION >= 520 and not sfinv_only then
panels.favs = {dat = {}, height = 2.19}
elseif sfinv_only then
panels = data.show_usages and panels = data.show_usages and
{usages = data.usages} or {recipes = data.recipes} {{dat = data.usages}} or {{dat = data.recipes}}
end end
for k, v in pairs(panels) do for k, v in pairs(panels) do
x = x + 1 start_y = start_y + 1
local spacing = (x - 1) * 3.6 local spacing = (start_y - 1) * 3.6
if not sfinv_only then if not sfinv_only then
fs[#fs + 1] = CORE_VERSION >= 510 and fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[8.1,%f;6.6,3.5;%s;false;%d]", fmt("background9[8.1,%f;6.6,%f;%s;false;%d]",
-0.2 + spacing, PNG.bg_full, 10) or -0.2 + spacing, v.height, PNG.bg_full, 10) or
fmt("background[8.1,%f;6.6,3.5;%s;false]", fmt("background[8.1,%f;6.6,%f;%s;false]",
-0.2 + spacing, PNG.bg_full) -0.2 + spacing, v.height, PNG.bg_full)
if k ~= "favs" then
local fav = is_fav(data)
local nfavs = #data.favs
fs[#fs + 1] = fmt(
"style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
fmt("craftguide_fav%s.png", fav and "" or "_off"),
fmt("craftguide_fav%s.png", fav and "_off" or ""),
fmt("craftguide_fav%s.png", fav and "_off" or ""))
if nfavs < 6 or (nfavs >= 6 and fav) then
fs[#fs + 1] = fmt(FMT.image_button,
14, spacing, 0.5, 0.45, "", "fav", "")
end end
local rn = #v fs[#fs + 1] = fmt("tooltip[fav;%s]",
local _rn = tostring(rn) fav and ES"Unmark this item" or ES"Mark this item")
local xof_u = tostring(data.unum) .. _rn end
local xof_r = tostring(data.rnum) .. _rn end
xof_u = max(-0.3, -((#xof_u - 3) * 0.15))
xof_r = max(-0.3, -((#xof_r - 3) * 0.15))
local is_recipe = k == "recipes" local rn = #v.dat
local btn_lab local _rn = tostring(rn)
local xu = tostring(data.unum) .. _rn
local xr = tostring(data.rnum) .. _rn
xu = max(-0.3, -((#xu - 3) * 0.05))
xr = max(-0.3, -((#xr - 3) * 0.05))
local is_recipe = k == 2
local lbl
if not sfinv_only and rn == 0 then if not sfinv_only and rn == 0 then
btn_lab = clr("red", is_recipe and lbl = clr("#f00", is_recipe and ES"No recipes" or ES"No usages")
ESC(S("No recipes")) or ESC(S("No usages")))
elseif (not sfinv_only and is_recipe) or elseif (not sfinv_only and is_recipe) or
(sfinv_only and not data.show_usages) then (sfinv_only and not data.show_usages) then
btn_lab = ESC(S("Recipe @1 of @2", data.rnum, rn)) lbl = ES("Recipe @1 of @2", data.rnum, rn)
elseif not sfinv_only or (sfinv_only and data.show_usages) then elseif not sfinv_only or (sfinv_only and data.show_usages) then
btn_lab = ESC(S("Usage @1 of @2", data.unum, rn)) lbl = ES("Usage @1 of @2", data.unum, rn)
elseif sfinv_only then elseif sfinv_only then
btn_lab = data.show_usages and lbl = data.show_usages and
ESC(S("Usage @1 of @2", data.unum, rn)) or ES("Usage @1 of @2", data.unum, rn) or
ESC(S("Recipe @1 of @2", data.rnum, rn)) ES("Recipe @1 of @2", data.rnum, rn)
end end
fs[#fs + 1] = fmt(FMT.label, fs[#fs + 1] = fmt(FMT.label,
XOFFSET + (sfinv_only and 2.3 or 1.6) + (is_recipe and xof_r or xof_u), XOFFSET + (sfinv_only and 2.3 or 1.6) + (is_recipe and xr or xu),
YOFFSET + (sfinv_only and 3.35 or 1.5 + spacing), YOFFSET + (sfinv_only and 2.3 or 1.5 + spacing), lbl)
btn_lab)
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 = fmt("prev_%s", btn_suffix)
local next_name = fmt("next_%s", btn_suffix) local next_name = fmt("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.25 or 1.4 + spacing) local y_arrow = YOFFSET + (sfinv_only and 2.2 or 1.4 + spacing)
if CORE_VERSION >= 520 then if CORE_VERSION >= 520 then
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
style[%s;border=false;bgimg=%s;bgimg_hovered=%s] x_arrow + (is_recipe and xr or xu), y_arrow,
style[%s;border=false;bgimg=%s;bgimg_hovered=%s] PNG.prev, prev_name, "",
]], x_arrow + 1.8, y_arrow, PNG.next, next_name, "")
prev_name, PNG.prev, PNG.prev_hover,
next_name, PNG.next, PNG.next_hover)
fs[#fs + 1] = fmt(FMT.button .. FMT.button,
x_arrow + (is_recipe and xof_r or xof_u),
y_arrow, 0.8, 0.8, prev_name, "",
x_arrow + 1.8, y_arrow, 0.8, 0.8, next_name, "")
else else
fs[#fs + 1] = fmt(FMT.arrow .. FMT.arrow, fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
x_arrow + (is_recipe and xof_r or xof_u), x_arrow + (is_recipe and xr or xu),
y_arrow, PNG.prev, prev_name, PNG.prev_hover, y_arrow, PNG.prev, prev_name, PNG.prev_hover,
x_arrow + 1.8, y_arrow, PNG.next, next_name, PNG.next_hover) x_arrow + 1.8, y_arrow, PNG.next, next_name, PNG.next_hover)
end end
end end
local rcp = is_recipe and v[data.rnum] or v[data.unum] local rcp = is_recipe and v.dat[data.rnum] or v.dat[data.unum]
if rcp then if rcp then
get_grid_fs(fs, rcp, spacing) get_grid_fs(fs, rcp, spacing)
end end
if k == "favs" and CORE_VERSION >= 520 and not sfinv_only then
fs[#fs + 1] = fmt(FMT.label, 8.3, spacing - 0.1, ES"Bookmarks")
for i = 1, #data.favs do
local item = data.favs[i]
if data.query_item == item then
fs[#fs + 1] = fmt(FMT.image,
7.85 + (i - 0.5), spacing + 0.45,
1.1, 1.1, PNG.selected)
end
fs[#fs + 1] = fmt(FMT.item_image_button,
7.85 + (i - 0.5), spacing + 0.45,
1.1, 1.1, item, item, "")
end
end
end end
end end
local function make_formspec(name) local function make_formspec(name)
local data = pdata[name] local data = pdata[name]
data.pagemax = max(1, ceil(#data.items / IPP))
local fs = {} local fs = {}
if not sfinv_only then
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
size[%f,%f] size[%f,%f]
no_prepend[] no_prepend[]
bgcolor[#0000] bgcolor[#0000]
]], ]], ROWS + (data.query_item and 6.7 or 0) - 1.2, LINES - 0.3)
ROWS + (data.query_item and 6.7 or 0) - 1.2,
LINES - 0.3)
if not sfinv_only then
fs[#fs + 1] = CORE_VERSION >= 510 and fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[-0.15,-0.2;%f,%f;%s;false;%d]", fmt("background9[-0.15,-0.2;%f,%f;%s;false;%d]",
ROWS - 0.9, LINES + 0.4, PNG.bg_full, 10) or ROWS - 0.9, LINES + 0.4, PNG.bg_full, 10) or
@ -982,7 +1041,6 @@ local function make_formspec(name)
end end
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
style_type[item_image_button;border=false]
field[0.25,0.2;%f,1;filter;;%s] field[0.25,0.2;%f,1;filter;;%s]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
]], ]],
@ -990,46 +1048,51 @@ local function make_formspec(name)
if CORE_VERSION >= 520 then if CORE_VERSION >= 520 then
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
style[search;border=false;bgimg=%s;bgimg_hovered=%s] style_type[image_button;border=false]
style[clear;border=false;bgimg=%s;bgimg_hovered=%s] style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s]
style[prev_page;border=false;bgimg=%s;bgimg_hovered=%s] style[search;fgimg=%s;fgimg_hovered=%s]
style[next_page;border=false;bgimg=%s;bgimg_hovered=%s] style[clear;fgimg=%s;fgimg_hovered=%s]
style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[next_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
]], ]],
PNG.selected, PNG.selected,
PNG.search, PNG.search_hover, PNG.search, PNG.search_hover,
PNG.clear, PNG.clear_hover, PNG.clear, PNG.clear_hover,
PNG.prev, PNG.prev_hover, PNG.prev, PNG.prev_hover, PNG.prev_hover,
PNG.next, PNG.next_hover) PNG.next, PNG.next_hover, PNG.next_hover)
fs[#fs + 1] = fmt(FMT.button .. FMT.button .. FMT.button .. FMT.button, fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 4),
sfinv_only and 2.6 or 2.54, -0.12, 0.85, 1, "search", "", sfinv_only and 2.6 or 2.54, -0.06, 0.85, 0.85, "", "search", "",
sfinv_only and 3.3 or 3.25, -0.12, 0.85, 1, "clear", "", sfinv_only and 3.3 or 3.25, -0.06, 0.85, 0.85, "", "clear", "",
sfinv_only and 5.45 or (ROWS * 6.83) / 11, -0.12, 0.85, 1, "prev_page", "", sfinv_only and 5.45 or (ROWS * 6.83) / 11, -0.06, 0.85, 0.85, "", "prev_page", "",
sfinv_only and 7.2 or (ROWS * 8.75) / 11, -0.12, 0.85, 1, "next_page", "") sfinv_only and 7.2 or (ROWS * 8.75) / 11, -0.06, 0.85, 0.85, "", "next_page", "")
else else
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
image_button[%f,-0.12;0.85,1;%s;search;;;false;%s] image_button[%f,-0.06;0.85,0.85;%s;search;;;false;%s]
image_button[%f,-0.12;0.85,1;%s;clear;;;false;%s] image_button[%f,-0.06;0.85,0.85;%s;clear;;;false;%s]
]], ]],
sfinv_only and 2.6 or 2.54, PNG.search, PNG.search_hover, sfinv_only and 2.6 or 2.54, PNG.search, PNG.search_hover,
sfinv_only and 3.3 or 3.25, PNG.clear, PNG.clear_hover) sfinv_only and 3.3 or 3.25, PNG.clear, PNG.clear_hover)
fs[#fs + 1] = fmt(FMT.arrow .. FMT.arrow, fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
sfinv_only and 5.45 or (ROWS * 6.83) / 11, sfinv_only and 5.45 or (ROWS * 6.83) / 11, -0.05,
-0.05, PNG.prev, "prev_page", PNG.prev, PNG.prev, "prev_page", PNG.prev,
sfinv_only and 7.2 or (ROWS * 8.75) / 11, sfinv_only and 7.2 or (ROWS * 8.75) / 11, -0.05,
-0.05, PNG.next, "next_page", PNG.next) PNG.next, "next_page", PNG.next)
end end
data.pagemax = max(1, ceil(#data.items / IPP))
fs[#fs + 1] = fmt("label[%f,%f;%s / %u]", fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
sfinv_only and 6.35 or (ROWS * 7.85) / 11, sfinv_only and 6.35 or (ROWS * 7.85) / 11,
0.06, clr("yellow", data.pagenum), data.pagemax) 0.06, clr("#ff0", data.pagenum), data.pagemax)
if #data.items == 0 then if #data.items == 0 then
local no_item = S("No item to show") local no_item = S"No item to show"
local pos = ROWS / 3 local pos = ROWS / 3
if next(recipe_filters) and #init_items > 0 and data.filter == "" then if next(recipe_filters) and #init_items > 0 and data.filter == "" then
no_item = S("Collect items to reveal more recipes") no_item = S"Collect items to reveal more recipes"
pos = pos - 1 pos = pos - 1
end end
@ -1045,17 +1108,17 @@ local function make_formspec(name)
local X = i % ROWS local X = i % ROWS
local Y = (i % IPP - X) / ROWS + 1 local Y = (i % IPP - X) / ROWS + 1
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]",
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
Y - (Y * 0.1) - 0.1,
1, 1, item, item)
if CORE_VERSION >= 510 and data.query_item == item then if CORE_VERSION >= 510 and data.query_item == item then
fs[#fs + 1] = fmt(FMT.image, fs[#fs + 1] = fmt(FMT.image,
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05, X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
Y - (Y * 0.1) - 0.1, Y - (Y * 0.1) - 0.1,
1, 1, PNG.selected) 1, 1, PNG.selected)
end end
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]",
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
Y - (Y * 0.1) - 0.1,
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
@ -1074,12 +1137,12 @@ local show_fs = function(player, name)
end end
craftguide.register_craft_type("digging", { craftguide.register_craft_type("digging", {
description = ESC(S("Digging")), description = ES"Digging",
icon = "default_tool_steelpick.png", icon = "default_tool_steelpick.png",
}) })
craftguide.register_craft_type("digging_chance", { craftguide.register_craft_type("digging_chance", {
description = ESC(S("Digging Chance")), description = ES"Digging Chance",
icon = "default_tool_mesepick.png", icon = "default_tool_mesepick.png",
}) })
@ -1244,6 +1307,21 @@ core.register_craft = function(def)
end end
end end
local old_clear_craft = core.clear_craft
core.clear_craft = function(def)
old_clear_craft(def)
if true_str(def) then
def = match(def, "%S*")
recipes_cache[def] = nil
fuel_cache[def] = nil
elseif is_table(def) then
return -- TODO
end
end
local function handle_drops_table(name, drop) local function handle_drops_table(name, drop)
-- Code borrowed and modified from unified_inventory -- Code borrowed and modified from unified_inventory
-- https://github.com/minetest-mods/unified_inventory/blob/master/api.lua -- https://github.com/minetest-mods/unified_inventory/blob/master/api.lua
@ -1297,20 +1375,20 @@ local function handle_drops_table(name, drop)
end end
for item, count in pairs(drop_sure) do for item, count in pairs(drop_sure) do
craftguide.register_craft({ craftguide.register_craft{
type = "digging", type = "digging",
items = {name}, items = {name},
output = fmt("%s %u", item, count), output = fmt("%s %u", item, count),
}) }
end end
for item, data in pairs(drop_maybe) do for item, data in pairs(drop_maybe) do
craftguide.register_craft({ craftguide.register_craft{
type = "digging_chance", type = "digging_chance",
items = {name}, items = {name},
output = fmt("%s %u", item, data.output), output = fmt("%s %u", item, data.output),
rarity = data.rarity, rarity = data.rarity,
}) }
end end
end end
@ -1319,11 +1397,11 @@ local function register_drops(name, def)
local dstack = ItemStack(drop) local dstack = ItemStack(drop)
if not dstack:is_empty() and dstack:get_name() ~= name then if not dstack:is_empty() and dstack:get_name() ~= name then
craftguide.register_craft({ craftguide.register_craft{
type = "digging", type = "digging",
items = {name}, items = {name},
output = drop, output = drop,
}) }
elseif is_table(drop) then elseif is_table(drop) then
handle_drops_table(name, drop) handle_drops_table(name, drop)
end end
@ -1339,7 +1417,7 @@ local function handle_aliases(hash)
recipes_cache[newname] = {} recipes_cache[newname] = {}
end end
local is_similar local similar
for i = 1, #recipes_cache[oldname] do for i = 1, #recipes_cache[oldname] do
local rcp_old = recipes_cache[oldname][i] local rcp_old = recipes_cache[oldname][i]
@ -1350,12 +1428,12 @@ local function handle_aliases(hash)
rcp_new.method = nil rcp_new.method = nil
if table_eq(rcp_old, rcp_new) then if table_eq(rcp_old, rcp_new) then
is_similar = true similar = true
break break
end end
end end
if not is_similar then if not similar then
insert(recipes_cache[newname], rcp_old) insert(recipes_cache[newname], rcp_old)
end end
end end
@ -1400,14 +1478,10 @@ local function get_init_items()
local percent, bar, len = (ic * 100) / it, "", 20 local percent, bar, len = (ic * 100) / it, "", 20
for i = 1, len do for i = 1, len do
if i <= percent / (100 / len) then bar = bar .. (i <= percent / (100 / len) and full_char or empty_char)
bar = bar .. full_char
else
bar = bar .. empty_char
end
end end
iop(fmt("[craftguide] Caching data %s (%u%%)\r", bar, percent)) iop(fmt("[craftguide] Caching data %s %u%%\r", bar, percent))
if show_item(def) then if show_item(def) then
if not fuel_cache[name] then if not fuel_cache[name] then
@ -1431,17 +1505,17 @@ local function get_init_items()
handle_aliases(hash) handle_aliases(hash)
sort(init_items) sort(init_items)
if http and true_str(craftguide.http_post_data) then if http and true_str(craftguide.export_url) then
local post_data = { local post_data = {
recipes = recipes_cache, recipes = recipes_cache,
usages = usages_cache, usages = usages_cache,
fuel = fuel_cache, fuel = fuel_cache,
} }
http.fetch_async({ http.fetch_async{
url = craftguide.http_post_data, url = craftguide.export_url,
post_data = write_json(post_data), post_data = write_json(post_data),
}) }
end end
print() print()
@ -1456,6 +1530,7 @@ local function init_data(name)
pagenum = 1, pagenum = 1,
items = items, items = items,
items_raw = items, items_raw = items,
favs = {},
} }
end end
@ -1518,6 +1593,18 @@ local function fields(player, _f)
data.pagenum = data.pagemax data.pagenum = data.pagemax
end end
return true, show_fs(player, name)
elseif _f.fav then
local fav, i = is_fav(data)
local total = #data.favs
if total < 6 and not fav then
data.favs[total + 1] = data.query_item
elseif fav then
remove(data.favs, i)
end
return true, show_fs(player, name) return true, show_fs(player, name)
else else
local item local item
@ -1534,6 +1621,8 @@ local function fields(player, _f)
item = sub(item, 1, -5) item = sub(item, 1, -5)
end end
item = reg_aliases[item] or item
if sfinv_only then if sfinv_only then
if item ~= data.query_item then if item ~= data.query_item then
data.show_usages = nil data.show_usages = nil
@ -1557,7 +1646,7 @@ end
if sfinv_only then if sfinv_only then
sfinv.register_page("craftguide:craftguide", { sfinv.register_page("craftguide:craftguide", {
title = S("Craft Guide"), title = S"Craft Guide",
get = function(self, player, context) get = function(self, player, context)
local name = player:get_player_name() local name = player:get_player_name()
@ -1600,7 +1689,7 @@ else
end end
core.register_craftitem("craftguide:book", { core.register_craftitem("craftguide:book", {
description = S("Crafting Guide"), description = S"Crafting Guide",
inventory_image = PNG.book, inventory_image = PNG.book,
wield_image = PNG.book, wield_image = PNG.book,
stack_max = 1, stack_max = 1,
@ -1611,7 +1700,7 @@ else
}) })
core.register_node("craftguide:sign", { core.register_node("craftguide:sign", {
description = S("Crafting Guide Sign"), description = S"Crafting Guide Sign",
drawtype = "nodebox", drawtype = "nodebox",
tiles = {PNG.sign}, tiles = {PNG.sign},
inventory_image = PNG.sign, inventory_image = PNG.sign,
@ -1637,34 +1726,34 @@ else
end end
}) })
core.register_craft({ core.register_craft{
output = "craftguide:book", output = "craftguide:book",
type = "shapeless", type = "shapeless",
recipe = {"default:book"} recipe = {"default:book"}
}) }
core.register_craft({ core.register_craft{
type = "fuel", type = "fuel",
recipe = "craftguide:book", recipe = "craftguide:book",
burntime = 3 burntime = 3
}) }
core.register_craft({ core.register_craft{
output = "craftguide:sign", output = "craftguide:sign",
type = "shapeless", type = "shapeless",
recipe = {"default:sign_wall_wood"} recipe = {"default:sign_wall_wood"}
}) }
core.register_craft({ core.register_craft{
type = "fuel", type = "fuel",
recipe = "craftguide:sign", recipe = "craftguide:sign",
burntime = 10 burntime = 10
}) }
if rawget(_G, "sfinv_buttons") then if rawget(_G, "sfinv_buttons") then
sfinv_buttons.register_button("craftguide", { sfinv_buttons.register_button("craftguide", {
title = S("Crafting Guide"), title = S"Crafting Guide",
tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"), tooltip = S"Shows a list of available crafting recipes, cooking recipes and fuels",
image = PNG.book, image = PNG.book,
action = function(player) action = function(player)
on_use(player) on_use(player)
@ -1875,29 +1964,29 @@ if progressive_mode then
end end
data.hud = { data.hud = {
bg = player:hud_add({ bg = player:hud_add{
hud_elem_type = "image", hud_elem_type = "image",
position = {x = 0.78, y = 1}, position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1}, alignment = {x = 1, y = 1},
scale = {x = 370, y = 112}, scale = {x = 370, y = 112},
text = PNG.bg, text = PNG.bg,
}), },
book = player:hud_add({ book = player:hud_add{
hud_elem_type = "image", hud_elem_type = "image",
position = {x = 0.79, y = 1.02}, position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1}, alignment = {x = 1, y = 1},
scale = {x = 4, y = 4}, scale = {x = 4, y = 4},
text = PNG.book, text = PNG.book,
}), },
text = player:hud_add({ text = player:hud_add{
hud_elem_type = "text", hud_elem_type = "text",
position = {x = 0.84, y = 1.04}, position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1}, alignment = {x = 1, y = 1},
number = 0xFFFFFF, number = 0xfff,
text = "", text = "",
}), },
} }
end) end)
@ -1946,7 +2035,7 @@ end)
function craftguide.show(name, item, show_usages) function craftguide.show(name, item, show_usages)
if not true_str(name)then if not true_str(name)then
return err("craftguide.show(): player name missing") return err"craftguide.show(): player name missing"
end end
local data = pdata[name] local data = pdata[name]
@ -1961,12 +2050,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, fmt("%s: %s",
S("No recipe or usage for this item"), S"No recipe or usage for this item",
get_desc(item))) get_desc(item)))
end end
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%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)))
end end
@ -1982,7 +2071,7 @@ function craftguide.show(name, item, show_usages)
end end
register_command("craft", { register_command("craft", {
description = S("Show recipe(s) of the pointed node"), description = S"Show recipe(s) of the pointed node",
func = function(name) func = function(name)
local player = get_player_by_name(name) local player = get_player_by_name(name)
local dir = player:get_look_dir() local dir = player:get_look_dir()
@ -2002,7 +2091,7 @@ register_command("craft", {
end end
if not node_name then if not node_name then
return false, msg(name, S("No node pointed")) return false, msg(name, S"No node pointed")
end end
return true, craftguide.show(name, node_name) return true, craftguide.show(name, node_name)

View File

@ -3,7 +3,7 @@ License of source code
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2015-2019 Jean-Patrick Guerrero and contributors. Copyright (c) 2015-2020 Jean-Patrick Guerrero and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB