diff --git a/mods/lord/Tools/lord_books/potions.lua b/mods/lord/Tools/lord_books/potions.lua index c3d672329..c9d43e542 100644 --- a/mods/lord/Tools/lord_books/potions.lua +++ b/mods/lord/Tools/lord_books/potions.lua @@ -86,8 +86,10 @@ end --- @param item_name string --- @param item_definition ItemDefinition --- @param i number -function Form.recipe(item_name, item_definition, i) - local dy = i % Form.RECIPES_PER_PAGE +function Form.recipe(item_name, item_definition, i, is_ingredients_page) + local dy = is_ingredients_page + and i % Form.RECIPES_PER_PAGE + or tonumber(item_name:sub(-1)) - 1 -- name of potion has `_1`, `_2`, `_3` postfix at the end local recipe = minetest.get_craft_recipe(item_name, minetest.CraftMethod.POTION, minetest.CraftType.COOKING) @@ -97,30 +99,50 @@ function Form.recipe(item_name, item_definition, i) .. spec.item_image_button(5, 2 + dy, 1, 1, recipe.input[1][2], recipe.input[1][2], '') .. spec.image (6, 2 + dy, 1, 1, 'benches_laboratory.png') .. spec.item_image_button(7, 2 + dy, 1, 1, item_name, item_name, '') +end +--- @param list table +--- @param callback fun(item_name:string,item_definition:ItemDefinition):boolean return `true` for stop iteration +local function foreach_ingredient_in(list, callback) + for item_name, item_definition in pairs(list) do + if callback(item_name, item_definition) then break end + end +end + +--- @param list table> +--- @param callback fun(item_name:string,item_definition:ItemDefinition):boolean return `true` for stop iteration +local function foreach_potion_in(list, callback) + for group_name, group_list in pairs(list) do + for item_name, item_definition in pairs(group_list) do + if callback(item_name, item_definition) then break end + end + end end --- @private --- @param page number --- @return string function Form.list_page(page) - local list = Form.is_ingredients_page(page) + local is_ingredients_page = Form.is_ingredients_page(page) + local list = is_ingredients_page and potions.ingredient.get_all() - or potions.potion.get_all() + or potions.potion.get_all_grouped() - local page_list = Form.is_ingredients_page(page) and page or page - Form.pages_of.ingredients - local items_to_skip = (page_list - 1) * Form.RECIPES_PER_PAGE + local list_page = is_ingredients_page and page or page - Form.pages_of.ingredients + local items_to_skip = (list_page - 1) * Form.RECIPES_PER_PAGE local i = 1 local form_spec = '' - for item_name, item_definition in pairs(list) do + + local foreach_in = is_ingredients_page and foreach_ingredient_in or foreach_potion_in + foreach_in(list, function(item_name, item_definition) if i > items_to_skip + Form.RECIPES_PER_PAGE then - break + return true end if i > items_to_skip then - form_spec = form_spec .. Form.recipe(item_name, item_definition, i) + form_spec = form_spec .. Form.recipe(item_name, item_definition, i, is_ingredients_page) end i = i + 1 - end + end) return form_spec end diff --git a/mods/lord/Tools/lord_potions/src/lord_potions/api/potion.lua b/mods/lord/Tools/lord_potions/src/lord_potions/api/potion.lua index 1cb07d220..8bfee5acb 100644 --- a/mods/lord/Tools/lord_potions/src/lord_potions/api/potion.lua +++ b/mods/lord/Tools/lord_potions/src/lord_potions/api/potion.lua @@ -10,9 +10,13 @@ local colorize = minetest.colorize local potions = { --- @type table|NodeDefinition[] - all_items = {}, + all_items = {}, + --- @type table>|NodeDefinition[][] + all_items_grouped = {}, --- @type table|NodeDefinition[] - lord_items = {}, + lord_items = {}, + --- @type table>|NodeDefinition[][] + lord_items_grouped = {}, } --- @param node_name string technical node name (":"). @@ -43,7 +47,7 @@ local function register_potion_node(item_name, title, description, color, effect 'lord_potions_bottle_content_mask.png' .. '^[colorize:' .. color .. ':170' .. '^[colorize:#000:' .. content_opacity_by_level .. - ')' + ')' local texture = bottle_contents_img .. '^(lord_potions_bottle.png)' minetest.register_node(item_name, { @@ -105,8 +109,13 @@ local function register_potion(name_prefix, title, description, color, effect, l register_potion_node(item_name, title, description, color, effect, level, groups) - potions.all_items[item_name] = minetest.registered_nodes[item_name] - potions.lord_items[item_name] = minetest.registered_nodes[item_name] + potions.all_items_grouped [effect.group] = potions.all_items_grouped [effect.group] or {} + potions.lord_items_grouped[effect.group] = potions.lord_items_grouped[effect.group] or {} + + potions.all_items [item_name] = minetest.registered_nodes[item_name] + potions.lord_items[item_name] = minetest.registered_nodes[item_name] + potions.all_items_grouped [effect.group][item_name] = minetest.registered_nodes[item_name] + potions.lord_items_grouped[effect.group][item_name] = minetest.registered_nodes[item_name] register_potion_craft(item_name, recipe) end @@ -157,10 +166,12 @@ end return { - add_existing = add_existing, - register = register_potion, - register_group = register_potion_group, - get_all = function() return potions.all_items end, + add_existing = add_existing, + register = register_potion, + register_group = register_potion_group, + get_all = function() return potions.all_items end, + get_all_grouped = function() return potions.all_items_grouped end, --- returns only lord internal registered potions. - get_lord = function() return potions.lord_items end, + get_lord = function() return potions.lord_items end, + get_lord_grouped = function() return potions.lord_items_grouped end, }