filter /craft recipes, fix crash, fix sfinv form

This commit is contained in:
Paul Ouellette 2019-02-06 14:37:43 -05:00
parent 65a8707a69
commit 85ece2c8b9

@ -56,13 +56,13 @@ local function table_merge(t, t2)
return t return t
end end
local function table_clean(t) local function clean_items(items)
local hash, ct = {}, {} local hash, ct = {}, {}
for i = 1, #t do for i = 1, #items do
local v = t[i] local item = items[i]
if not hash[v] then if not hash[item] and reg_items[item] then
ct[#ct + 1] = v ct[#ct + 1] = item
hash[v] = true hash[item] = true
end end
end end
@ -410,9 +410,6 @@ local function make_formspec(name)
fs[#fs + 1] = [[ fs[#fs + 1] = [[
no_prepend[] no_prepend[]
background[1,1;1,1;craftguide_bg.png;true] background[1,1;1,1;craftguide_bg.png;true]
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false]
]] ]]
fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]" fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]"
@ -424,6 +421,12 @@ local function make_formspec(name)
",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]" ",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]"
end end
fs[#fs + 1] = [[
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false]
]]
fs[#fs + 1] = "tooltip[search;" .. S("Search") .. "]" fs[#fs + 1] = "tooltip[search;" .. S("Search") .. "]"
fs[#fs + 1] = "tooltip[clear;" .. S("Reset") .. "]" fs[#fs + 1] = "tooltip[clear;" .. S("Reset") .. "]"
fs[#fs + 1] = "tooltip[prev;" .. S("Previous page") .. "]" fs[#fs + 1] = "tooltip[prev;" .. S("Previous page") .. "]"
@ -725,6 +728,18 @@ local function on_receive_fields(player, fields)
end end
end end
mt.register_on_mods_loaded(get_init_items)
mt.register_on_joinplayer(function(player)
local name = player:get_player_name()
init_data(player, name)
end)
mt.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_data[name] = nil
end)
if sfinv_only then if sfinv_only then
sfinv.register_page("craftguide:craftguide", { sfinv.register_page("craftguide:craftguide", {
title = "Craft Guide", title = "Craft Guide",
@ -843,18 +858,6 @@ else
end end
end end
mt.register_on_mods_loaded(get_init_items)
mt.register_on_joinplayer(function(player)
local name = player:get_player_name()
init_data(player, name)
end)
mt.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_data[name] = nil
end)
if progressive_mode then if progressive_mode then
local function recipe_in_inv(recipe, inv_items) local function recipe_in_inv(recipe, inv_items)
for _, item in pairs(recipe.items) do for _, item in pairs(recipe.items) do
@ -870,7 +873,7 @@ if progressive_mode then
local name = player:get_player_name() local name = player:get_player_name()
local discovered = player_data[name].inv_items local discovered = player_data[name].inv_items
local inv_items = get_inv_items(player) local inv_items = get_inv_items(player)
discovered = table_clean(table_merge(discovered, inv_items)) discovered = clean_items(table_merge(discovered, inv_items))
if #discovered == 0 then if #discovered == 0 then
return {} return {}
@ -910,7 +913,8 @@ if progressive_mode then
save_meta(players[i]) save_meta(players[i])
end end
end) end)
else end
mt.register_chatcommand("craft", { mt.register_chatcommand("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)
@ -941,7 +945,11 @@ else
local recipes = recipes_cache[node_name] local recipes = recipes_cache[node_name]
local is_fuel = fuel_cache[node_name] local is_fuel = fuel_cache[node_name]
if not recipes then if recipes then
recipes = apply_recipe_filters(recipes, player)
end
if not recipes or #recipes == 0 then
if is_fuel then if is_fuel then
recipes = get_item_usages(node_name) recipes = get_item_usages(node_name)
if #recipes > 0 then if #recipes > 0 then
@ -960,7 +968,6 @@ else
return true, show_fs(player, name) return true, show_fs(player, name)
end, end,
}) })
end
--[[ Custom recipes (>3x3) test code --[[ Custom recipes (>3x3) test code