Add setting for enabling/disabling progressive mode

This commit is contained in:
Jaidyn Ann 2024-08-13 22:13:17 -05:00
parent 4c3cb4fd6b
commit eff3d8c28e
2 changed files with 53 additions and 25 deletions

View File

@ -7,6 +7,8 @@ local init_items = {}
local recipes_cache = {}
local usages_cache = {}
local progressive_mode = minetest.settings:get_bool("progressive_mode", true)
local group_stereotypes = {
dye = "dye:white",
wool = "wool:white",
@ -158,12 +160,14 @@ minetest.register_on_mods_loaded(function()
end
end
end
for name, def in pairs(minetest.registered_items) do
if recipes_cache[name] or usages_cache[name] then
table.insert(init_items, name)
if not progressive_mode then
for name, def in pairs(minetest.registered_items) do
if recipes_cache[name] or usages_cache[name] then
table.insert(init_items, name)
end
end
table.sort(init_items)
end
table.sort(init_items)
end)
local function coords(i, cols)
@ -319,13 +323,18 @@ end
local function execute_search(data)
local filter = data.filter
local all_items = init_items
if progressive_mode then
all_items = visible_items(data)
end
if filter == "" then
data.items = visible_items(data)
data.items = all_items
return
end
data.items = {}
for _, item in ipairs(visible_items(data)) do
for _, item in ipairs(all_items) do
local def = minetest.registered_items[item]
local desc = def and minetest.get_translated_string(data.lang_code, def.description)
@ -340,11 +349,15 @@ local function on_receive_fields(player, fields)
local data = player_data[name]
if fields.clear then
local all_items = init_items
if progressive_mode then
all_items = visible_items(data)
end
data.filter = ""
data.pagenum = 1
data.prev_item = nil
data.recipes = nil
data.items = visible_items(data)
data.items = all_items
return true
elseif (fields.key_enter_field == "filter" or fields.search)
@ -398,10 +411,14 @@ local function on_receive_fields(player, fields)
else
data.show_usages = nil
end
if data.show_usages then
if data.show_usages and progressive_mode then
data.recipes = known_recipes(data, usages_cache[item])
else
elseif data.show_usages and not progressive_mode then
data.recipes = usages_cache[item]
elseif progressive_mode then
data.recipes = known_recipes(data, recipes_cache[item])
else
data.recipes = recipes_cache[item]
end
data.prev_item = item
data.rnum = 1
@ -528,23 +545,25 @@ function update_known_items(player, item_name)
end
end
minetest.register_on_item_pickup(function(itemstack, picker)
update_known_items(picker, itemstack:get_name())
end)
if progressive_mode then
minetest.register_on_item_pickup(function(itemstack, picker)
update_known_items(picker, itemstack:get_name())
end)
minetest.register_on_player_inventory_action(function(player, action, inv, inv_info)
if action == "put" then
update_known_items(player, inv_info.stack:get_name())
end
end)
minetest.register_on_player_inventory_action(function(player, action, inv, inv_info)
if action == "put" then
update_known_items(player, inv_info.stack:get_name())
end
end)
minetest.register_on_dignode(function(pos, oldnode, digger)
update_known_items(digger, oldnode.name)
end)
minetest.register_on_dignode(function(pos, oldnode, digger)
update_known_items(digger, oldnode.name)
end)
minetest.register_on_craft(function(itemstack, player)
minetest.register_on_craft(function(itemstack, player)
update_known_items(player, itemstack:get_name())
end)
end)
end
-- Go over all items in the players inventory for undiscovered recipes.
function check_player_inventory(player)
@ -566,7 +585,7 @@ function load_player_data(player)
player_data[name] = {
filter = "",
pagenum = 1,
items = {},
items = init_items,
held_items = {},
held_groups = {},
known_items = {},
@ -617,6 +636,10 @@ end
minetest.register_on_joinplayer(function(player)
load_player_data(player)
if not progressive_mode then
return
end
init_player_known_items(player)
check_player_inventory(player)
@ -626,9 +649,13 @@ minetest.register_on_joinplayer(function(player)
end)
minetest.register_on_leaveplayer(function(player)
save_player_data(player)
if progressive_mode then
save_player_data(player)
end
player_data[player:get_player_name()] = nil -- Save memory!
end)
minetest.register_on_shutdown(save_all_player_data)
if progressive_mode then
minetest.register_on_shutdown(save_all_player_data)
end

1
settingtypes.txt Normal file
View File

@ -0,0 +1 @@
progressive_mode (Learn crafting recipes progressively) bool true