Hide items without recipes/usages in survival mode

This commit is contained in:
Jean-Patrick Guerrero 2021-12-06 23:42:27 +01:00
parent cacb9a29fd
commit cffdf77e6a
3 changed files with 44 additions and 23 deletions

View File

@ -149,10 +149,11 @@ function i3.add_search_filter(name, f)
end end
function i3.get_recipes(item) function i3.get_recipes(item)
return { item = core.registered_aliases[item] or item
recipes = i3.recipes_cache[item], local recipes = i3.recipes_cache[item]
usages = i3.usages_cache[item] local usages = i3.usages_cache[item]
}
return {recipes = recipes, usages = usages}
end end
function i3.set_fs(player) function i3.set_fs(player)

View File

@ -1,5 +1,6 @@
local ItemStack = ItemStack local ItemStack = ItemStack
local loadstring = loadstring local loadstring = loadstring
local reg_items = core.registered_items
local translate = core.get_translated_string local translate = core.get_translated_string
local vec_new, vec_add, vec_mul = vector.new, vector.add, vector.multiply local vec_new, vec_add, vec_mul = vector.new, vector.add, vector.multiply
local sort, concat, insert = table.sort, table.concat, table.insert local sort, concat, insert = table.sort, table.concat, table.insert
@ -99,7 +100,7 @@ local function search(data)
for i = 1, #data.items_raw do for i = 1, #data.items_raw do
local item = data.items_raw[i] local item = data.items_raw[i]
local def = core.registered_items[item] local def = reg_items[item]
local desc = lower(translate(data.lang_code, def and def.description)) or "" local desc = lower(translate(data.lang_code, def and def.description)) or ""
local search_in = fmt("%s %s", item, desc) local search_in = fmt("%s %s", item, desc)
local temp, j, to_add = {}, 1 local temp, j, to_add = {}, 1
@ -250,7 +251,7 @@ local function groups_to_items(groups, get_all)
if not get_all and #groups == 1 then if not get_all and #groups == 1 then
local group = groups[1] local group = groups[1]
local stereotype = i3.group_stereotypes[group] local stereotype = i3.group_stereotypes[group]
local def = core.registered_items[stereotype] local def = reg_items[stereotype]
if valid_item(def) then if valid_item(def) then
return stereotype return stereotype
@ -259,7 +260,7 @@ local function groups_to_items(groups, get_all)
local names = {} local names = {}
for name, def in pairs(core.registered_items) do for name, def in pairs(reg_items) do
if valid_item(def) and item_has_groups(def.groups, groups) then if valid_item(def) and item_has_groups(def.groups, groups) then
if get_all then if get_all then
insert(names, name) insert(names, name)
@ -340,9 +341,9 @@ local function spawn_item(player, stack)
end end
local function get_recipes(player, item) local function get_recipes(player, item)
local clean_item = core.registered_aliases[item] or item item = core.registered_aliases[item] or item
local recipes = i3.recipes_cache[clean_item] local recipes = i3.recipes_cache[item]
local usages = i3.usages_cache[clean_item] local usages = i3.usages_cache[item]
if recipes then if recipes then
recipes = apply_recipe_filters(recipes, player) recipes = apply_recipe_filters(recipes, player)

View File

@ -16,7 +16,7 @@ IMPORT("S", "ES", "translate", "ItemStack", "toupper")
IMPORT("groups_to_items", "compression_active", "compressible") IMPORT("groups_to_items", "compression_active", "compressible")
IMPORT("true_str", "is_fav", "is_num", "get_group", "str_to_pos") IMPORT("true_str", "is_fav", "is_num", "get_group", "str_to_pos")
IMPORT("maxn", "sort", "concat", "copy", "insert", "remove", "unpack") IMPORT("maxn", "sort", "concat", "copy", "insert", "remove", "unpack")
IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups") IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups", "get_recipes")
local function fmt(elem, ...) local function fmt(elem, ...)
if not fs_elements[elem] then if not fs_elements[elem] then
@ -642,11 +642,11 @@ local function get_inventory_fs(player, data, fs)
max_val += (award_list_nb * 13) max_val += (award_list_nb * 13)
elseif data.subcat == 5 then elseif data.subcat == 5 then
local wp_nb = #data.waypoints local wp = #data.waypoints
if wp_nb > 0 then if wp > 0 then
local mul = (wp_nb > 8 and 7) or (wp_nb > 4 and 6) or 5 local mul = (wp > 8 and 7) or (wp > 4 and 6) or 5
max_val += 11 + (wp_nb * mul) max_val += 11 + (wp * mul)
end end
end end
@ -1147,7 +1147,7 @@ local function get_export_fs(fs, data, is_recipe, is_usage, max_stacks_rcp, max_
fmt("craft_%s", name), ES("Craft (×@1)", stack_fs)) fmt("craft_%s", name), ES("Craft (×@1)", stack_fs))
end end
local function get_rcp_extra(player, fs, data, panel, is_recipe, is_usage) local function get_rcp_extra(fs, player, data, panel, is_recipe, is_usage)
fs"container[0,0.075]" fs"container[0,0.075]"
local rn = panel.rcp and #panel.rcp local rn = panel.rcp and #panel.rcp
@ -1186,7 +1186,7 @@ local function get_rcp_extra(player, fs, data, panel, is_recipe, is_usage)
fs"container_end[]" fs"container_end[]"
end end
local function get_items_fs(fs, data, full_height) local function hide_items(player, data)
if compression_active(data) then if compression_active(data) then
local new = {} local new = {}
@ -1200,6 +1200,25 @@ local function get_items_fs(fs, data, full_height)
data.items = new data.items = new
end end
if not core.is_creative_enabled(data.player_name) then
local new = {}
for i = 1, #data.items do
local item = data.items[i]
local recipes, usages = get_recipes(player, item)
if recipes or usages then
insert(new, item)
end
end
data.items = new
end
end
local function get_items_fs(fs, player, data, full_height)
hide_items(player, data)
local items = data.alt_items or data.items or {} local items = data.alt_items or data.items or {}
local rows, lines = 8, 12 local rows, lines = 8, 12
local ipp = rows * lines local ipp = rows * lines
@ -1289,7 +1308,7 @@ local function get_favs(fs, data)
end end
end end
local function get_panels(player, data, fs, full_height) local function get_panels(fs, player, data, full_height)
local _title = {name = "title", height = 1.4} local _title = {name = "title", height = 1.4}
local _favs = {name = "favs", height = 2.23} local _favs = {name = "favs", height = 2.23}
local _items = {name = "items", height = full_height} local _items = {name = "items", height = full_height}
@ -1318,9 +1337,9 @@ local function get_panels(player, data, fs, full_height)
local is_recipe, is_usage = panel.name == "recipes", panel.name == "usages" local is_recipe, is_usage = panel.name == "recipes", panel.name == "usages"
if is_recipe or is_usage then if is_recipe or is_usage then
get_rcp_extra(player, fs, data, panel, is_recipe, is_usage) get_rcp_extra(fs, player, data, panel, is_recipe, is_usage)
elseif panel.name == "items" then elseif panel.name == "items" then
get_items_fs(fs, data, full_height) get_items_fs(fs, player, data, full_height)
elseif panel.name == "title" then elseif panel.name == "title" then
get_header(fs, data) get_header(fs, data)
elseif panel.name == "favs" then elseif panel.name == "favs" then
@ -1329,7 +1348,7 @@ local function get_panels(player, data, fs, full_height)
end end
end end
local function get_tabs_fs(player, data, fs, full_height) local function get_tabs_fs(fs, player, data, full_height)
local tab_len, tab_hgh, c, over = 3, 0.5, 0 local tab_len, tab_hgh, c, over = 3, 0.5, 0
local _tabs = copy(i3.tabs) local _tabs = copy(i3.tabs)
@ -1430,10 +1449,10 @@ local function make_fs(player, data)
tab.formspec(player, data, fs) tab.formspec(player, data, fs)
end end
get_panels(player, data, fs, full_height) get_panels(fs, player, data, full_height)
if #i3.tabs > 1 then if #i3.tabs > 1 then
get_tabs_fs(player, data, fs, full_height) get_tabs_fs(fs, player, data, full_height)
end end
--get_debug_grid(data, fs, full_height) --get_debug_grid(data, fs, full_height)