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

View File

@ -56,13 +56,13 @@ local function table_merge(t, t2)
return t
end
local function table_clean(t)
local function clean_items(items)
local hash, ct = {}, {}
for i = 1, #t do
local v = t[i]
if not hash[v] then
ct[#ct + 1] = v
hash[v] = true
for i = 1, #items do
local item = items[i]
if not hash[item] and reg_items[item] then
ct[#ct + 1] = item
hash[item] = true
end
end
@ -410,9 +410,6 @@ local function make_formspec(name)
fs[#fs + 1] = [[
no_prepend[]
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") .. "]"
@ -424,6 +421,12 @@ local function make_formspec(name)
",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]"
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[clear;" .. S("Reset") .. "]"
fs[#fs + 1] = "tooltip[prev;" .. S("Previous page") .. "]"
@ -725,6 +728,18 @@ local function on_receive_fields(player, fields)
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
sfinv.register_page("craftguide:craftguide", {
title = "Craft Guide",
@ -843,18 +858,6 @@ else
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
local function recipe_in_inv(recipe, inv_items)
for _, item in pairs(recipe.items) do
@ -870,7 +873,7 @@ if progressive_mode then
local name = player:get_player_name()
local discovered = player_data[name].inv_items
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
return {}
@ -910,8 +913,9 @@ if progressive_mode then
save_meta(players[i])
end
end)
else
mt.register_chatcommand("craft", {
end
mt.register_chatcommand("craft", {
description = S("Show recipe(s) of the pointed node"),
func = function(name)
local player = mt.get_player_by_name(name)
@ -941,7 +945,11 @@ else
local recipes = recipes_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
recipes = get_item_usages(node_name)
if #recipes > 0 then
@ -959,8 +967,7 @@ else
return true, show_fs(player, name)
end,
})
end
})
--[[ Custom recipes (>3x3) test code