Compare commits

...

12 Commits
1.8.1 ... 1.9a

5 changed files with 480 additions and 285 deletions

11
API.md
View File

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

View File

@ -19,4 +19,4 @@ For developers, `craftguide` also has a [modding API](https://github.com/minetes
Love this mod? Donations are appreciated: https://www.paypal.me/jpg84240
![Preview2](https://i.imgur.com/IbexMwQ.png)
![Preview2](https://i.imgur.com/GVQvufb.png)

609
init.lua
View File

@ -14,18 +14,23 @@ local searches = {}
local recipes_cache = {}
local usages_cache = {}
local fuel_cache = {}
local alias_cache = {}
local toolrepair
local progressive_mode = core.settings:get_bool("craftguide_progressive_mode")
local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
local reg_items = core.registered_items
local reg_nodes = core.registered_nodes
local reg_tools = core.registered_tools
local reg_entities = core.registered_nodes
local reg_aliases = core.registered_aliases
local reg_craftitems = core.registered_craftitems
local log = core.log
local after = core.after
local clr = core.colorize
local reg_tools = core.registered_tools
local reg_items = core.registered_items
local chat_send = core.chat_send_player
local show_formspec = core.show_formspec
local globalstep = core.register_globalstep
local on_shutdown = core.register_on_shutdown
@ -51,29 +56,29 @@ local S = CORE_VERSION >= 500 and core.get_translator("craftguide") or
end)
end
local maxn, sort, concat, copy, insert =
table.maxn, table.sort, table.concat, table.copy, table.insert
local maxn, sort, concat, copy, insert, remove =
table.maxn, table.sort, table.concat, table.copy,
table.insert, table.remove
local fmt, find, gmatch, match, sub, split, upper, lower =
string.format, string.find, string.gmatch, string.match,
string.sub, string.split, string.upper, string.lower
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
local pairs, next = pairs, next
local pairs, next, type = pairs, next, type
local vec_add, vec_mul = vector.add, vector.multiply
local ROWS = sfinv_only and 9 or 11
local LINES = 5
local ROWS = 9
local LINES = sfinv_only and 5 or 9
local IPP = ROWS * LINES
local WH_LIMIT = 8
local XOFFSET = sfinv_only and 3.83 or 4.66
local YOFFSET = sfinv_only and 6 or 6.6
craftguide.background = "craftguide_bg_full.png#10"
local XOFFSET = sfinv_only and 3.83 or 11.2
local YOFFSET = sfinv_only and 6 or 1
local PNG = {
bg = "craftguide_bg.png",
bg_full = "craftguide_bg_full.png",
search = "craftguide_search_icon.png",
clear = "craftguide_clear_icon.png",
prev = "craftguide_next_icon.png^\\[transformFX",
@ -105,6 +110,114 @@ craftguide.group_stereotypes = {
mesecon_conductor_craftable = "mesecons:wire_00000000_off",
}
local function err(str)
return log("error", str)
end
local function msg(name, str)
return chat_send(name, fmt("[craftguide] %s", clr("#FFFF00", str)))
end
local function clean_str(str)
return match(str, "%S*")
end
local function is_str(x)
return type(x) == "string" and clean_str(x)
end
local function is_table(x)
return type(x) == "table"
end
local function is_func(x)
return type(x) == "function"
end
local function is_group(item)
return sub(item, 1, 6) == "group:"
end
local function array_diff(t1, t2)
local hash = {}
for i = 1, #t1 do
local v = t1[i]
hash[v] = true
end
for i = 1, #t2 do
local v = t2[i]
hash[v] = nil
end
local diff, c = {}, 0
for i = 1, #t1 do
local v = t1[i]
if hash[v] then
c = c + 1
diff[c] = v
end
end
return diff
end
local function table_eq(T1, T2)
local avoid_loops = {}
local function recurse(t1, t2)
if type(t1) ~= type(t2) then return end
if not is_table(t1) then
return t1 == t2
end
if avoid_loops[t1] then return
avoid_loops[t1] == t2
end
avoid_loops[t1] = t2
local t2keys = {}
local t2tablekeys = {}
for k in pairs(t2) do
if type(k) == "table" then
insert(t2tablekeys, k)
end
t2keys[k] = true
end
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if type(k1) == "table" then
local ok
for i = 1, #t2tablekeys do
local tk = t2tablekeys[i]
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
remove(t2tablekeys, i)
t2keys[tk] = nil
ok = true
break
end
end
if not ok then return end
else
if v2 == nil then return end
t2keys[k1] = nil
if not recurse(v1, v2) then return end
end
end
if next(t2keys) then return end
return true
end
return recurse(T1, T2)
end
local function table_merge(t1, t2, hash)
t1 = t1 or {}
t2 = t2 or {}
@ -133,30 +246,6 @@ local function table_replace(t, val, new)
end
end
local function err(str)
return log("error", str)
end
local function clean_str(str)
return match(str, "%S*")
end
local function is_str(x)
return type(x) == "string" and clean_str(x)
end
local function is_table(x)
return type(x) == "table"
end
local function is_func(x)
return type(x) == "function"
end
local function is_group(item)
return sub(item, 1, 6) == "group:"
end
local craft_types = {}
function craftguide.register_craft_type(name, def)
@ -333,6 +422,7 @@ end
local function groups_item_in_recipe(item, recipe)
local def = reg_items[item]
if not def then return end
local item_groups = def.groups
for _, recipe_item in pairs(recipe.items) do
@ -440,19 +530,18 @@ local function get_recipes(item, data, player)
end
local no_recipes = not recipes or #recipes == 0
if no_recipes and not usages then
return
elseif usages and no_recipes then
elseif sfinv_only and usages and no_recipes then
data.show_usages = true
end
if data.show_usages then
recipes = apply_recipe_filters(usages_cache[item], player)
if recipes and #recipes == 0 then return end
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
return recipes, usages
end
local function groups_to_items(groups, get_all)
@ -484,6 +573,14 @@ local function repairable(tool)
return toolrepair and def and def.groups and def.groups.disable_repair ~= 1
end
local function get_desc(name)
local def = reg_items[name]
return def and (match(def.description, "%)([%w%s]*)") or def.description) or
(def and match(name, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
S("Unknown Item (@1)", name))
end
local function get_tooltip(item, info)
local tooltip
@ -501,14 +598,7 @@ local function get_tooltip(item, info)
return fmt("tooltip[%s;%s]", item, ESC(tooltip))
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
tooltip = get_desc(reg_items[item])
tooltip = get_desc(item)
local function add(str)
return fmt("%s\n%s", tooltip, str)
@ -523,8 +613,7 @@ local function get_tooltip(item, info)
end
if info.replace then
local def = reg_items[info.replace]
local desc = clr("yellow", get_desc(def, info.replace))
local desc = clr("yellow", get_desc(info.replace))
if info.cooktime then
tooltip = add(S("Replaced by @1 on smelting", desc))
@ -556,13 +645,13 @@ local function get_output_fs(fs, L)
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)
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
L.shapeless and S("Shapeless") or S("Cooking")
if CORE_VERSION >= 500 then
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y + L.spacing, 0.5, 0.5, ESC(tooltip))
end
end
@ -570,21 +659,25 @@ local function get_output_fs(fs, L)
local output_X = arrow_X + 0.9
fs[#fs + 1] = fmt(FMT.image,
arrow_X, YOFFSET + (sfinv_only and 0.9 or 0.2),
arrow_X, YOFFSET + (sfinv_only and 0.9 or 0.2) + L.spacing,
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),
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, PNG.fire)
else
local item = L.recipe.output
local name = clean_str(item)
fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, item, ESC(name), "")
fs[#fs + 1] = fmt(FMT.image,
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, "craftguide_selected.png")
local burntime = fuel_cache[name]
local repair = repairable(name)
@ -597,41 +690,127 @@ local function get_output_fs(fs, L)
if burntime then
fs[#fs + 1] = fmt(FMT.image,
output_X + 1, YOFFSET + (sfinv_only and 0.7 or 0.1),
output_X + 1, YOFFSET + (sfinv_only and 0.7 or 0.1) + L.spacing,
0.6, 0.4, PNG.arrow)
fs[#fs + 1] = fmt(FMT.image,
output_X + 1.6, YOFFSET + (sfinv_only and 0.55 or 0),
output_X + 1.6, YOFFSET + (sfinv_only and 0.55 or 0) + L.spacing,
0.6, 0.6, PNG.fire)
end
end
end
local function get_recipe_fs(data, fs)
local recipe = data.recipes[data.rnum]
local width = recipe.width or 1
local replacements = recipe.replacements
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)
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)
local def = reg_items[L.item]
local namestr = fmt("%s (%s)", pretty_wrap(get_desc(L.item), 25), L.item)
local typestr
if reg_nodes[L.item] then
typestr = fmt("%s (%s)", ESC(S("Node")), def.drawtype)
elseif reg_entities[L.item] then
typestr = ESC(S("Entity"))
elseif reg_craftitems[L.item] then
typestr = ESC(S("Craftitem"))
elseif reg_tools[L.item] then
typestr = ESC(S("Tool"))
end
local groupstr
for k, v in pairs(def.groups or {}) do
groupstr = (groupstr or "") .. fmt("%s(%d), ", pretty_wrap(k, 13), v)
end
groupstr = groupstr and sub(groupstr, 1, -3)
local specs = {
ESC(S("Name")),
ESC(S("Type")),
ESC(S("Groups")),
}
local tstr = ""
for i = 1, #specs do
tstr = tstr .. "#6389FF," .. specs[i] .. ",#FFFFFF,%s,"
end
tstr = sub(tstr, 1, -2)
fs[#fs + 1] = [[
tableoptions[background=#00000000;highlight=#00000000;border=false]
tablecolumns[color;text]
]]
fs[#fs + 1] = fmt("table[8.1,%f;6.3,1.8;itemdef;" .. tstr .. ";0]",
L.y + 0.08,
namestr,
ESC(typestr or S("Unknown")),
ESC(groupstr or S("None")))
end
local function get_info_fs(data, fs)
local t = {recipes = data.recipes, usages = data.usages}
local infonum = 0
local last_y
if sfinv_only then
t = data.show_usages and {usages = data.usages} or {recipes = data.recipes}
end
for k, v in pairs(t) 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 width = rcp.width or 1
local replacements = rcp.replacements
local cooktime, shapeless
if recipe.type == "cooking" then
if rcp.type == "cooking" then
cooktime, width = width, 1
elseif width == 0 and not recipe.custom then
elseif width == 0 and not rcp.custom then
shapeless = true
local n = #recipe.items
local n = #rcp.items
width = (n < 5 and n > 1) and 2 or min(3, max(1, n))
end
local rows = ceil(maxn(recipe.items) / width)
local rows = ceil(maxn(rcp.items) / width)
local rightest, btn_size, s_btn_size = 0, 1.1
local btn_lab = data.show_usages and
ESC(S("Usage @1 of @2", data.rnum, #data.recipes)) or
ESC(S("Recipe @1 of @2", data.rnum, #data.recipes))
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))
end
if sfinv_only then
btn_lab = data.show_usages and
ESC(S("Usage @1 of @2", data.rnum, #v)) or
ESC(S("Recipe @1 of @2", data.rnum, #v))
end
fs[#fs + 1] = fmt(FMT.button,
XOFFSET + (sfinv_only and 1.98 or 2.7),
YOFFSET + (sfinv_only and 1.9 or 1.2),
2.2, 1, "alternate", btn_lab)
XOFFSET + (sfinv_only and 1.98 or 1.12),
YOFFSET + (sfinv_only and 1.9 or 1.2 + spacing),
2.2, 1, "alternate_" .. (k == "recipes" and "recipe" or "usage"), btn_lab)
if width > WH_LIMIT or rows > WH_LIMIT then
fs[#fs + 1] = fmt(FMT.label,
@ -641,12 +820,18 @@ local function get_recipe_fs(data, fs)
return concat(fs)
end
for i = 1, width * rows do
local item = recipe.items[i] or ""
local X = ceil((i - 1) % width - width) + XOFFSET
local Y = ceil(i / width) + YOFFSET - min(2, rows)
local large_recipe = width > 3 or rows > 3
if width > 3 or rows > 3 then
if large_recipe then
fs[#fs + 1] = "style_type[item_image_button;border=true]"
end
for i = 1, width * rows do
local item = rcp.items[i] or ""
local X = ceil((i - 1) % width - width) + XOFFSET
local Y = ceil(i / width) + YOFFSET - min(2, rows) + spacing
if large_recipe then
local xof = 1 - 4 / width
local yof = 1 - 4 / rows
local x_y = width > rows and xof or yof
@ -656,9 +841,9 @@ local function get_recipe_fs(data, fs)
s_btn_size = btn_size
X = (btn_size * ((i - 1) % width) + XOFFSET -
(sfinv_only and 2.83 or (XOFFSET - 2))) * (0.83 - (x_y / 5))
(sfinv_only and 2.83 or 0.5)) * (0.83 - (x_y / 5))
Y = (btn_size * floor((i - 1) / width) +
(sfinv_only and 5.81 or 6.5) + x_y) * (0.86 - (x_y / 5))
(sfinv_only and 5.81 or 4) + x_y) * (0.86 - (x_y / 5))
end
if X > rightest then
@ -690,7 +875,7 @@ local function get_recipe_fs(data, fs)
X, Y + (sfinv_only and 0.7 or 0),
btn_size, btn_size, item, clean_str(item), ESC(label))
local info = {
local infos = {
unknown = not reg_items[item],
groups = groups,
burntime = fuel_cache[item],
@ -698,21 +883,40 @@ local function get_recipe_fs(data, fs)
replace = replace,
}
for _, v in pairs(info) do
if v then
fs[#fs + 1] = get_tooltip(item, info)
for _, info in pairs(infos) do
if info then
fs[#fs + 1] = get_tooltip(item, infos)
break
end
end
if not large_recipe then
fs[#fs + 1] = fmt(FMT.image,
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
get_output_fs(fs, {
recipe = recipe,
recipe = rcp,
shapeless = shapeless,
rightest = rightest,
btn_size = btn_size,
s_btn_size = s_btn_size,
spacing = spacing,
})
end
if not sfinv_only then
get_itemdef_fs(fs, {
item = data.query_item,
y = last_y,
})
end
end
local function make_formspec(name)
@ -722,22 +926,23 @@ local function make_formspec(name)
local fs = {}
if not sfinv_only then
local bg, middle = match(craftguide.background, "([%w_%.]*)([#%d]*)")
bg = bg or ""
middle = tonumber(sub(middle, 2)) or 10
fs[#fs + 1] = [[
size[9.5,8.4]
fs[#fs + 1] = fmt([[
size[%f,%f]
no_prepend[]
bgcolor[#0000]
]]
]],
ROWS + (data.query_item and 6.7 or 0) - 1.2,
LINES - 0.3)
fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[1,1;1,1;%s;true;%d]", bg, middle) or
fmt("background[1,1;1,1;%s;true]", bg)
fmt("background9[-0.15,-0.2;%f,%f;%s;false;%d]",
ROWS - 0.9, LINES + 0.4, PNG.bg_full, 10) or
fmt("background[-0.15,-0.2;%f,%f;%s;false]",
ROWS - 0.9, LINES + 0.4, PNG.bg_full)
end
fs[#fs + 1] = fmt([[
style_type[item_image_button;border=false]
field[0.25,0.2;%f,1;filter;;%s]
field_close_on_enter[filter;false]
]],
@ -755,13 +960,13 @@ local function make_formspec(name)
label[%f,%f;%s / %u]
image_button[%f,-0.05;0.8,0.8;%s;next;;;false;%s^\[colorize:yellow:255]
]],
sfinv_only and 5.45 or 6.83, PNG.prev, PNG.prev,
sfinv_only and 6.35 or 7.85, 0.06, clr("yellow", data.pagenum), data.pagemax,
sfinv_only and 7.2 or 8.75, PNG.next, PNG.next)
sfinv_only and 5.45 or (ROWS * 6.83) / 11, PNG.prev, PNG.prev,
sfinv_only and 6.35 or (ROWS * 7.85) / 11, 0.06, clr("yellow", data.pagenum), data.pagemax,
sfinv_only and 7.2 or (ROWS * 8.75) / 11, PNG.next, PNG.next)
if #data.items == 0 then
local no_item = S("No item to show")
local pos = sfinv_only and 3 or 3.8
local pos = ROWS / 3
if next(recipe_filters) and #init_items > 0 and data.filter == "" then
no_item = S("Collect items to reveal more recipes")
@ -784,10 +989,17 @@ local function make_formspec(name)
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
Y - (Y * 0.1) - 0.1,
1, 1, item, item)
if data.query_item == item then
fs[#fs + 1] = fmt(FMT.image,
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
Y - (Y * 0.1) - 0.1,
1, 1, "craftguide_selected.png")
end
end
if data.recipes and #data.recipes > 0 then
get_recipe_fs(data, fs)
if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then
get_info_fs(data, fs)
end
return concat(fs)
@ -874,13 +1086,6 @@ local function search(data)
data.items = filtered_list
end
local old_register_alias = core.register_alias
core.register_alias = function(old, new)
old_register_alias(old, new)
alias_cache[new] = old
end
--[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not
return the replacements and toolrepair, we have to override
`core.register_craft` and do some reverse engineering.
@ -962,23 +1167,9 @@ local function show_item(def)
end
local function get_init_items()
local c = 0
local hash, c = {}, 0
for name, def in pairs(reg_items) do
if show_item(def) then
local old_name = alias_cache[name]
if old_name then
local old_recipes = recipes_cache[old_name]
local old_usages = usages_cache[old_name]
if old_recipes then
recipes_cache[name] = old_recipes
end
if old_usages then
usages_cache[name] = old_usages
end
end
if not fuel_cache[name] then
cache_fuel(name)
end
@ -992,10 +1183,46 @@ local function get_init_items()
if recipes_cache[name] or usages_cache[name] then
c = c + 1
init_items[c] = name
hash[name] = true
end
end
end
for oldname, newname in pairs(reg_aliases) do
local recipes = recipes_cache[oldname]
if recipes then
if not recipes_cache[newname] then
recipes_cache[newname] = {}
end
local is_similar
for i = 1, #recipes_cache[oldname] do
local rcp_old = recipes_cache[oldname][i]
for j = 1, #recipes_cache[newname] do
local rcp_new = recipes_cache[newname][j]
rcp_new.type = nil
rcp_new.method = nil
if table_eq(rcp_old, rcp_new) then
is_similar = true
break
end
end
if not is_similar then
insert(recipes_cache[newname], rcp_old)
end
end
end
if recipes_cache[oldname] and not hash[newname] then
c = c + 1
init_items[c] = newname
end
end
sort(init_items)
end
@ -1015,9 +1242,11 @@ local function reset_data(data)
data.filter = ""
data.pagenum = 1
data.rnum = 1
data.unum = 1
data.query_item = nil
data.show_usages = nil
data.recipes = nil
data.usages = nil
data.show_usages = nil
data.items = data.items_raw
end
@ -1039,7 +1268,7 @@ local function fields(player, _f)
show_fs(player, name)
return true
elseif _f.alternate then
elseif _f.alternate_recipe then
if #data.recipes == 1 then return end
local num_next = data.rnum + 1
data.rnum = data.recipes[num_next] and num_next or 1
@ -1047,6 +1276,14 @@ local function fields(player, _f)
show_fs(player, name)
return true
elseif _f.alternate_usage then
if #data.usages == 1 then return end
local num_next = data.unum + 1
data.unum = data.usages[num_next] and num_next or 1
show_fs(player, name)
return true
elseif (_f.key_enter_field == "filter" or _f.search) and _f.filter ~= "" then
local str = lower(_f.filter)
if data.filter == str then return end
@ -1085,18 +1322,22 @@ local function fields(player, _f)
item = sub(item, 1, -5)
end
if sfinv_only then
if item ~= data.query_item then
data.show_usages = nil
else
data.show_usages = not data.show_usages
end
end
local recipes = get_recipes(item, data, player)
if not recipes then return end
local recipes, usages = get_recipes(item, data, player)
if not recipes and not usages then return end
data.query_item = item
data.recipes = recipes
data.usages = usages
data.rnum = 1
data.unum = 1
show_fs(player, name)
return true
@ -1226,32 +1467,6 @@ if progressive_mode then
local POLL_FREQ = 0.25
local HUD_TIMER_MAX = 1.5
local function table_diff(t1, t2)
local hash = {}
for i = 1, #t1 do
local v = t1[i]
hash[v] = true
end
for i = 1, #t2 do
local v = t2[i]
hash[v] = nil
end
local diff, c = {}, 0
for i = 1, #t1 do
local v = t1[i]
if hash[v] then
c = c + 1
diff[c] = v
end
end
return diff
end
local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items
@ -1392,7 +1607,7 @@ if progressive_mode then
local data = pdata[name]
local inv_items = get_inv_items(player)
local diff = table_diff(inv_items, data.inv_items)
local diff = array_diff(inv_items, data.inv_items)
if #diff > 0 then
data.inv_items = table_merge(diff, data.inv_items)
@ -1518,6 +1733,39 @@ on_leaveplayer(function(player)
pdata[name] = nil
end)
function craftguide.show(name, item, show_usages)
if not is_str(name) then
return err("craftguide.show(): player name missing")
end
local data = pdata[name]
local player = get_player_by_name(name)
local query_item = data.query_item
reset_data(data)
item = reg_items[item] and item or query_item
local recipes, usages = get_recipes(item, data, player)
if not recipes and not usages then
if not recipes_cache[item] and not usages_cache[item] then
return false, msg(name, S("No recipe or usage for this node"))
end
return false, msg(name, S("You don't know a recipe or usage for this node"))
end
data.query_item = item
data.recipes = recipes
data.usages = usages
if sfinv_only then
data.show_usages = show_usages
end
show_fs(player, name)
end
register_command("craft", {
description = S("Show recipe(s) of the pointed node"),
func = function(name)
@ -1538,61 +1786,10 @@ register_command("craft", {
end
end
local red = clr("red", "[craftguide] ")
if not node_name then
return false, red .. S("No node pointed")
return false, msg(name, S("No node pointed"))
end
local data = pdata[name]
reset_data(data)
local recipes = recipes_cache[node_name]
local usages = usages_cache[node_name]
if recipes then
recipes = apply_recipe_filters(recipes, player)
end
if not recipes or #recipes == 0 then
local ylw = clr("yellow", node_name)
local msg = red .. "%s: " .. ylw
if usages then
recipes = usages_cache[node_name]
if #recipes > 0 then
data.show_usages = true
end
elseif recipes_cache[node_name] then
return false, fmt(msg, S("You don't know a recipe for this node"))
else
return false, fmt(msg, S("No recipe for this node"))
end
end
data.query_item = node_name
data.recipes = recipes
return true, show_fs(player, name)
return true, craftguide.show(name, node_name)
end,
})
function craftguide.show(name, item, show_usages)
if not is_str(name) then
return err("craftguide.show(): player name missing")
end
local data = pdata[name]
local player = get_player_by_name(name)
local query_item = data.query_item
reset_data(data)
item = reg_items[item] and item or query_item
data.query_item = item
data.show_usages = show_usages
data.recipes = get_recipes(item, data, player)
show_fs(player, name)
end

View File

@ -19,5 +19,14 @@ No item to show=
Collect items to reveal more recipes=
Show recipe(s) of the pointed node=
No node pointed=
You don't know a recipe for this node=
No recipe for this node=
You don't know a recipe or usage for this node=
No recipe or usage for this node=
Node=
Entity=
Craftitem=
Tool=
Name=
Type=
Groups=
Unknown=
None=

Binary file not shown.

After

Width:  |  Height:  |  Size: 912 B