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