Refactors, optimizations and fixes

This commit is contained in:
pauloue 2019-01-22 01:44:36 +01:00 committed by Jean-Patrick Guerrero
parent 5ee1bea4ce
commit d950c71dbc

531
init.lua

@ -6,6 +6,8 @@ craftguide = {
local mt = minetest local mt = minetest
local player_data = {} local player_data = {}
local init_items = {} local init_items = {}
local recipes_cache = {}
local fuel_cache = {}
local searches = {} local searches = {}
local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode") local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode")
@ -78,23 +80,7 @@ craftguide.register_craft({
items = {"default:stone"}, items = {"default:stone"},
}) })
local function get_recipe(output) local function cache_recipes(output)
local recipe = mt.get_craft_recipe(output)
if recipe.items then
return recipe
end
for i = 1, #craftguide.custom_crafts do
local custom_craft = craftguide.custom_crafts[i]
if custom_craft.output:match("%S*") == output then
return custom_craft
end
end
return {}
end
local function get_recipes(output)
local recipes = mt.get_all_craft_recipes(output) or {} local recipes = mt.get_all_craft_recipes(output) or {}
for i = 1, #craftguide.custom_crafts do for i = 1, #craftguide.custom_crafts do
local custom_craft = craftguide.custom_crafts[i] local custom_craft = craftguide.custom_crafts[i]
@ -103,23 +89,24 @@ local function get_recipes(output)
end end
end end
return recipes if #recipes > 0 then
recipes_cache[output] = recipes
return true
end
end end
local color_codes = { local function get_burntime(item)
red = "#FF0000",
yellow = "#FFFF00",
}
local function colorize(str, color)
color = color or "yellow"
return mt.colorize(color_codes[color], str)
end
local function get_fueltime(item)
return get_result({method = "fuel", width = 1, items = {item}}).time return get_result({method = "fuel", width = 1, items = {item}}).time
end end
local function cache_fuel(item)
local burntime = get_burntime(item)
if burntime > 0 then
fuel_cache[item] = burntime
return true
end
end
local function extract_groups(str) local function extract_groups(str)
return str:sub(7):split(",") return str:sub(7):split(",")
end end
@ -154,85 +141,61 @@ local function groups_to_item(groups)
return "" return ""
end end
local function get_tooltip(item, recipe_type, cooktime, groups) local function get_tooltip(item, groups, cooktime, burntime)
local tooltip, item_desc = "tooltip[" .. item .. ";", "" local tooltip
local fueltime = get_fueltime(item)
local has_extras = groups or recipe_type == "cooking" or fueltime > 0
if reg_items[item] then
if not groups then
item_desc = reg_items[item].description
end
else
return tooltip .. S("Unknown Item (@1)", item) .. "]"
end
if groups then if groups then
local groupstr = "" local groupstr = {}
for i = 1, #groups do for i = 1, #groups do
groupstr = groupstr .. groupstr[#groupstr + 1] = mt.colorize("yellow", groups[i])
colorize(groups[i]) .. (groups[i + 1] and ", " or "")
end end
tooltip = tooltip .. groupstr = concat(groupstr, ", ")
S("Any item belonging to the group(s)") .. ": " .. groupstr tooltip = S("Any item belonging to the group(s):") .. " " .. groupstr
else
tooltip = reg_items[item].description
end end
if recipe_type == "cooking" then if cooktime then
tooltip = tooltip .. item_desc .. "\n" .. tooltip = tooltip .. "\n" .. S("Cooking time:") .. " " ..
S("Cooking time") .. ": " .. colorize(cooktime) mt.colorize("yellow", cooktime)
end end
if fueltime > 0 then if burntime then
tooltip = tooltip .. item_desc .. "\n" .. tooltip = tooltip .. "\n" .. S("Burning time:") .. " " ..
S("Burning time") .. ": " .. colorize(fueltime) mt.colorize("yellow", burntime)
end end
return has_extras and tooltip .. "]" or "" return "tooltip[" .. item .. ";" .. tooltip .. "]"
end end
local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage) local function get_recipe_fs(data, iY)
if not recipes[1] then
return ""
end
local fs = {} local fs = {}
local recipe = data.recipes[data.rnum]
local width = recipe.width
local xoffset = data.iX / 2.15
local cooktime, shapeless
fs[#fs + 1] = fmt("button[%f,%f;%f,%f;%s;%s %u %s %u]", if recipe.type == "cooking" then
iX - (sfinv_only and 2.2 or 2.6), cooktime, width = width, 1
iY + (sfinv_only and 3.9 or 3.3),
2.2,
1,
"alternate",
show_usage and S("Usage") or S("Recipe"),
recipe_num,
S("of"),
#recipes)
local recipe_type = recipes[recipe_num].type
local items = recipes[recipe_num].items
local width = recipes[recipe_num].width
local cooktime = width
if recipe_type == "cooking" then
width = 1
elseif width == 0 then elseif width == 0 then
width = min(3, #items) shapeless = true
width = min(3, #recipe.items)
end end
local rows = ceil(maxn(items) / width) local rows = ceil(maxn(recipe.items) / width)
local rightest, btn_size, s_btn_size = 0, 1.1 local rightest, btn_size, s_btn_size = 0, 1.1
if width > GRID_LIMIT or rows > GRID_LIMIT then if width > GRID_LIMIT or rows > GRID_LIMIT then
fs[#fs + 1] = fmt("label[%f,%f;%s]", fs[#fs + 1] = fmt("label[%f,%f;%s]",
(iX / 2) - 2, (data.iX / 2) - 2,
iY + 2.2, iY + 2.2,
S("Recipe is too big to be displayed (@1x@2)", width, rows)) S("Recipe is too big to be displayed (@1x@2)", width, rows))
return concat(fs) return concat(fs)
end end
for i, item in pairs(items) do for i, item in pairs(recipe.items) do
local X = ceil((i - 1) % width + xoffset - width) - local X = ceil((i - 1) % width + xoffset - width) -
(sfinv_only and 0 or 0.2) (sfinv_only and 0 or 0.2)
local Y = ceil(i / width + (iY + 2) - min(2, rows)) local Y = ceil(i / width + (iY + 2) - min(2, rows))
@ -255,7 +218,6 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
end end
local label = groups and "\nG" or "" local label = groups and "\nG" or ""
local tltp = get_tooltip(item, recipe_type, cooktime, groups)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
X, X,
@ -266,35 +228,40 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
item:match("%S*"), item:match("%S*"),
label) label)
fs[#fs + 1] = tltp local burntime = fuel_cache[item]
if groups or cooktime or burntime then
fs[#fs + 1] = get_tooltip(item, groups, cooktime, burntime)
end
end end
local custom_recipe = craftguide.craft_types[recipe_type] local custom_recipe = craftguide.craft_types[recipe.type]
if recipe_type == "cooking" or custom_recipe or
(recipe_type == "normal" and width == 0) then
local icon = recipe_type == "cooking" and "furnace" or "shapeless" 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 = "craftguide_" .. icon .. ".png^[resize:16x16"
end
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]", fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
rightest + 1.2, rightest + 1.2,
iY + (sfinv_only and 2.2 or 1.7), iY + (sfinv_only and 2.2 or 1.7),
0.5, 0.5,
0.5, 0.5,
custom_recipe and custom_recipe.icon or icon)
"craftguide_" .. icon .. ".png^[resize:16x16")
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]", fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
rightest + 1.2, rightest + 1.2,
iY + (sfinv_only and 2.2 or 1.7), iY + (sfinv_only and 2.2 or 1.7),
0.5, 0.5,
0.5, 0.5,
custom_recipe and custom_recipe.description or tooltip)
recipe_type:gsub("^%l", string.upper))
end end
local output = recipes[recipe_num].output
local output_s = output:match("%S+")
local arrow_X = rightest + (s_btn_size or 1.1) local arrow_X = rightest + (s_btn_size or 1.1)
local output_X = arrow_X + 0.9 local output_X = arrow_X + 0.9
@ -305,7 +272,7 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
0.7, 0.7,
"craftguide_arrow.png") "craftguide_arrow.png")
if output == "BURN" then if recipe.type == "fuel" then
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]", fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
output_X, output_X,
iY + (sfinv_only and 2.68 or 2.18), iY + (sfinv_only and 2.68 or 2.18),
@ -313,33 +280,46 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
1.1, 1.1,
"craftguide_fire.png") "craftguide_fire.png")
else else
local output_name = recipe.output:match("%S+")
local burntime = fuel_cache[output_name]
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
output_X, output_X,
iY + (sfinv_only and 2.7 or 2.2), iY + (sfinv_only and 2.7 or 2.2),
1.1, 1.1,
1.1, 1.1,
output, recipe.output,
output_s) output_name)
if burntime then
fs[#fs + 1] = get_tooltip(output_name, nil, nil, burntime)
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
output_X + 1,
iY + (sfinv_only and 2.83 or 2.33),
0.6,
0.4,
"craftguide_arrow.png")
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
output_X + 1.6,
iY + (sfinv_only and 2.68 or 2.18),
0.6,
0.6,
"craftguide_fire.png")
end
end end
fs[#fs + 1] = get_tooltip(output_s) fs[#fs + 1] = fmt("button[%f,%f;%f,%f;%s;%s %u %s %u]",
data.iX - (sfinv_only and 2.2 or 2.6),
local output_is_fuel = get_fueltime(output) > 0 iY + (sfinv_only and 3.9 or 3.3),
if output_is_fuel then 2.2,
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]", 1,
output_X + 1, "alternate",
iY + (sfinv_only and 2.83 or 2.33), data.show_usages and S("Usage") or S("Recipe"),
0.6, data.rnum,
0.4, S("of"),
"craftguide_arrow.png") #data.recipes)
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
output_X + 1.6,
iY + (sfinv_only and 2.68 or 2.18),
0.6,
0.6,
"craftguide_fire.png")
end
return concat(fs) return concat(fs)
end end
@ -376,16 +356,14 @@ local function make_formspec(player_name)
fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]" fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) .. fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) ..
",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]" ",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]"
fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) .. fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) .. ",0.22;" ..
",0.22;" .. colorize(data.pagenum) .. " / " .. data.pagemax .. "]" mt.colorize("yellow", data.pagenum) .. " / " .. data.pagemax .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) - fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) -
(data.iX >= 11 and 0.08 or 0)) .. (data.iX >= 11 and 0.08 or 0)) ..
",0.12;0.8,0.8;craftguide_next_icon.png;next;]" ",0.12;0.8,0.8;craftguide_next_icon.png;next;]"
fs[#fs + 1] = "field[0.3,0.32;2.5,1;filter;;" .. mt.formspec_escape(data.filter) .. "]" fs[#fs + 1] = "field[0.3,0.32;2.5,1;filter;;" .. mt.formspec_escape(data.filter) .. "]"
local xoffset = data.iX / 2.15 if #data.items == 0 then
if not next(data.items) then
fs[#fs + 1] = fmt("label[%f,%f;%s]", fs[#fs + 1] = fmt("label[%f,%f;%s]",
(data.iX / 2) - 1, (data.iX / 2) - 1,
2, 2,
@ -411,32 +389,19 @@ local function make_formspec(player_name)
name) name)
end end
if data.input and reg_items[data.input] then if data.recipes and #data.recipes > 0 then
local usage = data.show_usage fs[#fs + 1] = get_recipe_fs(data, iY)
fs[#fs + 1] = get_recipe_fs(data.iX,
iY,
xoffset,
data.rnum,
(usage and data.usages or data.recipes_item),
usage)
end end
fs = concat(fs) return concat(fs)
if sfinv_only then
return fs
else
data.formspec = fs
end
end end
local show_fs = function(player, player_name) local show_fs = function(player, player_name)
if sfinv_only then if sfinv_only then
local context = sfinv.get_or_create_context(player) sfinv.set_player_inventory_formspec(player)
sfinv.set_player_inventory_formspec(player, context)
else else
make_formspec(player_name)
local data = player_data[player_name] local data = player_data[player_name]
data.formspec = make_formspec(player_name)
show_formspec(player_name, "craftguide", data.formspec) show_formspec(player_name, "craftguide", data.formspec)
end end
end end
@ -489,68 +454,61 @@ end
local function get_item_usages(item) local function get_item_usages(item)
local usages = {} local usages = {}
for name, def in pairs(reg_items) do for _, recipes in pairs(recipes_cache) do
if not (def.groups.not_in_craft_guide == 1 or for i = 1, #recipes do
def.groups.not_in_creative_inventory == 1) and local recipe = recipes[i]
get_recipe(name).items and if item_in_recipe(item, recipe) then
def.description and def.description ~= "" then usages[#usages + 1] = recipe
local recipes = get_recipes(name)
for i = 1, #recipes do
local recipe = recipes[i]
if item_in_recipe(item, recipe) then
usages[#usages + 1] = recipe
end
end end
end end
end end
if fuel_cache[item] then
usages[#usages + 1] = {type = "fuel", width = 1, items = {item}}
end
return usages return usages
end end
local function get_inv_items(player) local function get_inv_items(player)
local invlist = player:get_inventory():get_list("main") local inv = player:get_inventory()
local inv_items = {} local stacks = inv:get_list("main")
local craftlist = inv:get_list("craft")
for i = 1, #invlist do for i = 1, #craftlist do
local stack = invlist[i] stacks[#stacks + 1] = craftlist[i]
end
local inv_items = {}
for i = 1, #stacks do
local stack = stacks[i]
if not stack:is_empty() then if not stack:is_empty() then
inv_items[#inv_items + 1] = stack:get_name() local name = stack:get_name()
if not inv_items[name] and reg_items[name] then
inv_items[#inv_items + 1] = name
end
end end
end end
return inv_items return inv_items
end end
local function progressive_show_recipe(recipe, inv_items) local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items if item:sub(1,6) == "group:" then
for _, item in pairs(recipe.items) do local groups = extract_groups(item)
local item_in_inv for i = 1, #inv_items do
if item:sub(1,6) == "group:" then local item_groups = reg_items[inv_items[i]].groups
local groups = extract_groups(item) if item_has_groups(item_groups, groups) then
for i = 1, inv_items_size do return true
local item_def = reg_items[inv_items[i]]
if item_def then
local item_groups = item_def.groups
if item_has_groups(item_groups, groups) then
item_in_inv = true
end
end
end
else
for i = 1, inv_items_size do
if inv_items[i] == item then
item_in_inv = true
end
end end
end end
else
if not item_in_inv then for i = 1, #inv_items do
return if inv_items[i] == item then
return true
end
end end
end end
return true
end end
local function progressive_default_filter(recipes, player) local function progressive_default_filter(recipes, player)
@ -562,7 +520,14 @@ local function progressive_default_filter(recipes, player)
local filtered = {} local filtered = {}
for i = 1, #recipes do for i = 1, #recipes do
local recipe = recipes[i] local recipe = recipes[i]
if progressive_show_recipe(recipe, inv_items) then local recipe_in_inv = true
for _, item in pairs(recipe.items) do
if not item_in_inv(item, inv_items) then
recipe_in_inv = false
end
end
if recipe_in_inv then
filtered[#filtered + 1] = recipe filtered[#filtered + 1] = recipe
end end
end end
@ -606,11 +571,13 @@ local function get_progressive_items(player)
local items = {} local items = {}
for i = 1, #init_items do for i = 1, #init_items do
local item = init_items[i] local item = init_items[i]
local recipes = get_recipes(item) local recipes = recipes_cache[item]
recipes = apply_progressive_filters(recipes, player)
if #recipes > 0 then if recipes then
items[#items + 1] = item recipes = apply_progressive_filters(recipes, player)
if #recipes > 0 then
items[#items + 1] = item
end
end end
end end
@ -629,22 +596,22 @@ local function init_data(player, name)
end end
local function reset_data(data) local function reset_data(data)
data.show_usage = nil data.filter = ""
data.filter = "" data.pagenum = 1
data.input = nil data.query_item = nil
data.pagenum = 1 data.show_usages = nil
data.rnum = 1 data.recipes = nil
data.items = progressive_mode and data.progressive_items or init_items data.items = progressive_mode and data.progressive_items or init_items
end end
local function get_init_items() local function get_init_items()
local c = 0 local c = 0
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
local is_fuel = get_fueltime(name) > 0 local is_fuel = cache_fuel(name)
if not (def.groups.not_in_craft_guide == 1 or if not (def.groups.not_in_craft_guide == 1 or
def.groups.not_in_creative_inventory == 1) and def.groups.not_in_creative_inventory == 1) and
(get_recipe(name).items or is_fuel) and def.description and def.description ~= "" and
def.description and def.description ~= "" then (cache_recipes(name) or is_fuel) then
c = c + 1 c = c + 1
init_items[c] = name init_items[c] = name
end end
@ -655,18 +622,7 @@ end
mt.register_on_mods_loaded(get_init_items) mt.register_on_mods_loaded(get_init_items)
local function get_fields(player, ...) local function on_receive_fields(player, fields)
local args, formname, fields = {...}
if sfinv_only then
fields = args[1]
else
formname, fields = args[1], args[2]
end
if not sfinv_only and formname ~= "craftguide" then
return
end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = player_data[player_name] local data = player_data[player_name]
@ -675,18 +631,12 @@ local function get_fields(player, ...)
show_fs(player, player_name) show_fs(player, player_name)
elseif fields.alternate then elseif fields.alternate then
if #(data.show_usage and data.usages or data.recipes_item) == 1 then if #data.recipes == 1 then
return return
end end
local next_i local num_next = data.rnum + 1
if data.show_usage then data.rnum = data.recipes[num_next] and num_next or 1
next_i = data.usages[data.rnum + 1]
else
next_i = data.recipes_item[data.rnum + 1]
end
data.rnum = next_i and data.rnum + 1 or 1
show_fs(player, player_name) show_fs(player, player_name)
elseif (fields.key_enter_field == "filter" or fields.search) and elseif (fields.key_enter_field == "filter" or fields.search) and
@ -702,6 +652,10 @@ local function get_fields(player, ...)
show_fs(player, player_name) show_fs(player, player_name)
elseif fields.prev or fields.next then elseif fields.prev or fields.next then
if data.pagemax == 1 then
return
end
data.pagenum = data.pagenum - (fields.prev and 1 or -1) data.pagenum = data.pagenum - (fields.prev and 1 or -1)
if data.pagenum > data.pagemax then if data.pagenum > data.pagemax then
data.pagenum = 1 data.pagenum = 1
@ -714,66 +668,63 @@ local function get_fields(player, ...)
elseif (fields.size_inc and data.iX < MAX_LIMIT) or elseif (fields.size_inc and data.iX < MAX_LIMIT) or
(fields.size_dec and data.iX > MIN_LIMIT) then (fields.size_dec and data.iX > MIN_LIMIT) then
data.pagenum = 1 data.pagenum = 1
data.iX = data.iX - (fields.size_dec and 1 or -1) data.iX = data.iX + (fields.size_inc and 1 or -1)
show_fs(player, player_name) show_fs(player, player_name)
else for item in pairs(fields) do else
if item:find(":") then local item
if item:sub(-4) == "_inv" then for field in pairs(fields) do
item = item:sub(1,-5) if field:find(":") then
item = field
break
end end
end
local is_fuel = get_fueltime(item) > 0 if not item then
local recipes = get_recipes(item) return
elseif item:sub(-4) == "_inv" then
item = item:sub(1,-5)
end
local is_fuel = fuel_cache[item]
local recipes = recipes_cache[item]
if progressive_mode and recipes then
recipes = apply_progressive_filters(recipes, player)
end
local no_recipes = not recipes or #recipes == 0
if no_recipes and not is_fuel then
return
end
if item ~= data.query_item then
data.show_usages = nil
else
data.show_usages = not data.show_usages
end
if is_fuel and no_recipes then
data.show_usages = true
end
if data.show_usages then
recipes = get_item_usages(item)
if progressive_mode then if progressive_mode then
recipes = apply_progressive_filters(recipes, player) recipes = apply_progressive_filters(recipes, player)
end end
local no_recipes = not next(recipes) if #recipes == 0 then
if no_recipes and (progressive_mode or not is_fuel) then
return return
end end
if item ~= data.input then
data.show_usage = nil
else
data.show_usage = not data.show_usage
end
if not progressive_mode and is_fuel and no_recipes then
data.show_usage = true
end
if data.show_usage then
data.usages = get_item_usages(item)
if is_fuel then
data.usages[#data.usages + 1] = {
width = 1,
type = "normal",
items = {item},
output = "BURN",
}
end
if progressive_mode and next(data.usages) then
data.usages =
apply_progressive_filters(data.usages, player)
end
if not next(data.usages) then
data.show_usage = nil
end
end
data.input = item
data.recipes_item = recipes
data.rnum = 1
show_fs(player, player_name)
end end
end
data.query_item = item
data.recipes = recipes
data.rnum = 1
show_fs(player, player_name)
end end
end end
@ -782,12 +733,8 @@ if sfinv_only then
title = "Craft Guide", title = "Craft Guide",
get = function(self, player, context) get = function(self, player, context)
local player_name = player:get_player_name() local formspec = make_formspec(player:get_player_name())
return sfinv.make_formspec( return sfinv.make_formspec(player, context, formspec)
player,
context,
make_formspec(player_name)
)
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
@ -803,24 +750,28 @@ if sfinv_only then
end, end,
on_player_receive_fields = function(self, player, context, fields) on_player_receive_fields = function(self, player, context, fields)
get_fields(player, fields) on_receive_fields(player, fields)
end, end,
}) })
else else
mt.register_on_player_receive_fields(get_fields) mt.register_on_player_receive_fields(function(player, formname, fields)
if formname == "craftguide" then
on_receive_fields(player, fields)
end
end)
local function on_use(itemstack, user) local function on_use(user)
local player_name = user:get_player_name() local player_name = user:get_player_name()
local data = player_data[player_name] local data = player_data[player_name]
if not data then if not data then
init_data(user, player_name) init_data(user, player_name)
make_formspec(player_name)
data = player_data[player_name] data = player_data[player_name]
data.formspec = make_formspec(player_name)
elseif progressive_mode then elseif progressive_mode then
data.progressive_items = get_progressive_items(user) data.progressive_items = get_progressive_items(user)
filter_items(data) filter_items(data)
make_formspec(player_name) data.formspec = make_formspec(player_name)
end end
show_formspec(player_name, "craftguide", data.formspec) show_formspec(player_name, "craftguide", data.formspec)
@ -833,7 +784,7 @@ else
stack_max = 1, stack_max = 1,
groups = {book = 1}, groups = {book = 1},
on_use = function(itemstack, user) on_use = function(itemstack, user)
on_use(itemstack, user) on_use(user)
end end
}) })
@ -860,7 +811,7 @@ else
end, end,
on_rightclick = function(pos, node, user, itemstack) on_rightclick = function(pos, node, user, itemstack)
on_use(itemstack, user) on_use(user)
end end
}) })
@ -894,7 +845,7 @@ else
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 = "craftguide_book.png", image = "craftguide_book.png",
action = function(player) action = function(player)
on_use(nil, player) on_use(player)
end, end,
}) })
end end
@ -921,7 +872,7 @@ if not progressive_mode then
end end
if not node_name then if not node_name then
return false, colorize("[craftguide] ", "red") .. return false, mt.colorize("red", "[craftguide] ") ..
S("No node pointed") S("No node pointed")
elseif not player_data[name] then elseif not player_data[name] then
init_data(player, name) init_data(player, name)
@ -930,34 +881,26 @@ if not progressive_mode then
local data = player_data[name] local data = player_data[name]
reset_data(data) reset_data(data)
local is_fuel = get_fueltime(node_name) > 0 local recipes = recipes_cache[node_name]
local recipes = get_recipes(node_name)
local no_recipes = not next(recipes) local no_recipes = not next(recipes)
local is_fuel = fuel_cache[node_name]
if no_recipes and not is_fuel then if no_recipes and not is_fuel then
return false, colorize("[craftguide] ", "red") .. return false, mt.colorize("red", "[craftguide] ") ..
S("No recipe for this node:") .. " " .. S("No recipe for this node:") .. " " ..
colorize(node_name) mt.colorize("yellow", node_name)
end end
if is_fuel and no_recipes then if is_fuel and no_recipes then
data.usages = get_item_usages(node_name) recipes = get_item_usages(node_name)
if is_fuel then
data.usages[#data.usages + 1] = {
width = 1,
type = "normal",
items = {node_name},
output = "BURN",
}
end
if next(data.usages) then if #recipes > 0 then
data.show_usage = true data.show_usages = true
end end
end end
data.input = node_name data.query_item = node_name
data.recipes_item = recipes data.recipes = recipes
return true, show_fs(player, name) return true, show_fs(player, name)
end, end,