From 61ae85a09cf818e3cc9a95cbcb7ab9932dc990c4 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Fri, 18 Jan 2019 17:15:25 +0100 Subject: [PATCH] Progressive mode: Add functions in API to add/set/get filters --- README.md | 26 ++++++++++++++++---------- init.lua | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6be78cc..f9a2972 100644 --- a/README.md +++ b/README.md @@ -44,25 +44,31 @@ craftguide.register_craft({ ### Progressive mode -#### `craftguide.progressive_filter_recipes(recipes, player)` +#### `craftguide.add_progressive_filter(function(recipes, player))` -This function is used to filter recipes when progressive mode is enabled. It can -be overridden to change the recipes that are normally displayed. - -The function should return the recipes to be displayed, given the available -recipes and an `ObjectRef` to the craft guide user. Each recipe is a table of -the form returned by `minetest.get_craft_recipe`. +This function adds a recipe filter when progressive mode is enabled. +The default `craftguide` filter will still be used. Example function to hide recipes for items from a mod called "secretstuff": ```lua -function craftguide.progressive_filter_recipes(recipes, player) +craftguide.add_progressive_filter(function(recipes) local filtered = {} for _, recipe in ipairs(recipes) do - if recipe.output:sub(1, 12) ~= "secretstuff:" then + if recipe.output:sub(1,12) ~= "secretstuff:" then filtered[#filtered + 1] = recipe end end + return filtered -end +end) ``` + +#### `craftguide.set_progressive_filter(function(recipes, player))` + +This function sets an unique recipes filter when progressive mode is enabled. +The default `craftguide` progressive filter will be overridden. + +#### `craftguide.get_progressive_filters()` + +This function returns all progressive filters that are applied to recipes in progressive mode. diff --git a/init.lua b/init.lua index 48241d4..7ec8a44 100644 --- a/init.lua +++ b/init.lua @@ -553,7 +553,7 @@ local function progressive_show_recipe(recipe, inv_items) return true end -function craftguide.progressive_filter_recipes(recipes, player) +local function progressive_default_filter(recipes, player) local inv_items = get_inv_items(player) if #inv_items == 0 then return {} @@ -570,12 +570,35 @@ function craftguide.progressive_filter_recipes(recipes, player) return filtered end +local progressive_filters = {progressive_default_filter} + +function craftguide.add_progressive_filter(func) + progressive_filters[#progressive_filters + 1] = func +end + +function craftguide.set_progressive_filter(func) + progressive_filters = {func} +end + +function craftguide.get_progressive_filters() + return progressive_filters +end + +local function apply_progressive_filters(recipes, player) + for i = 1, #progressive_filters do + local func = progressive_filters[i] + recipes = func(recipes, player) + end + + return recipes +end + local function get_progressive_items(player) local items = {} for i = 1, #init_items do local item = init_items[i] local recipes = get_recipes(item) - recipes = craftguide.progressive_filter_recipes(recipes, player) + recipes = apply_progressive_filters(recipes, player) if #recipes > 0 then items[#items + 1] = item @@ -695,7 +718,7 @@ local function get_fields(player, ...) local recipes = get_recipes(item) if progressive_mode then - recipes = craftguide.progressive_filter_recipes(recipes, player) + recipes = apply_progressive_filters(recipes, player) end local no_recipes = not next(recipes) @@ -725,8 +748,9 @@ local function get_fields(player, ...) } end - if progressive_mode then - data.usages = craftguide.progressive_filter_recipes(data.usages, player) + if progressive_mode and next(data.usages) then + data.usages = + apply_progressive_filters(data.usages, player) end if not next(data.usages) then @@ -933,7 +957,7 @@ end --[[ Custom recipes (>3x3) test code -mt.register_craftitem("craftguide:custom_recipe_test", { +mt.register_craftitem(":secretstuff:custom_recipe_test", { description = "Custom Recipe Test", }) @@ -943,12 +967,12 @@ for x = 1, 6 do for i = 1, 10 - x do cr[x][i] = {} for j = 1, 10 - x do - cr[x][i][j] = "group:wood" + cr[x][i][j] = "group:sand" end end mt.register_craft({ - output = "craftguide:custom_recipe_test", + output = "secretstuff:custom_recipe_test", recipe = cr[x] }) end