Progressive mode: Add functions in API to add/set/get filters

This commit is contained in:
Jean-Patrick Guerrero 2019-01-18 17:15:25 +01:00
parent c4fe467e6f
commit 61ae85a09c
2 changed files with 48 additions and 18 deletions

@ -44,25 +44,31 @@ craftguide.register_craft({
### Progressive mode ### 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 This function adds a recipe filter when progressive mode is enabled.
be overridden to change the recipes that are normally displayed. The default `craftguide` filter will still be used.
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`.
Example function to hide recipes for items from a mod called "secretstuff": Example function to hide recipes for items from a mod called "secretstuff":
```lua ```lua
function craftguide.progressive_filter_recipes(recipes, player) craftguide.add_progressive_filter(function(recipes)
local filtered = {} local filtered = {}
for _, recipe in ipairs(recipes) do 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 filtered[#filtered + 1] = recipe
end end
end end
return filtered 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.

@ -553,7 +553,7 @@ local function progressive_show_recipe(recipe, inv_items)
return true return true
end end
function craftguide.progressive_filter_recipes(recipes, player) local function progressive_default_filter(recipes, player)
local inv_items = get_inv_items(player) local inv_items = get_inv_items(player)
if #inv_items == 0 then if #inv_items == 0 then
return {} return {}
@ -570,12 +570,35 @@ function craftguide.progressive_filter_recipes(recipes, player)
return filtered return filtered
end 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 function get_progressive_items(player)
local items = {} local items = {}
for i = 1, #init_items do for i = 1, #init_items do
local item = init_items[i] local item = init_items[i]
local recipes = get_recipes(item) local recipes = get_recipes(item)
recipes = craftguide.progressive_filter_recipes(recipes, player) recipes = apply_progressive_filters(recipes, player)
if #recipes > 0 then if #recipes > 0 then
items[#items + 1] = item items[#items + 1] = item
@ -695,7 +718,7 @@ local function get_fields(player, ...)
local recipes = get_recipes(item) local recipes = get_recipes(item)
if progressive_mode then if progressive_mode then
recipes = craftguide.progressive_filter_recipes(recipes, player) recipes = apply_progressive_filters(recipes, player)
end end
local no_recipes = not next(recipes) local no_recipes = not next(recipes)
@ -725,8 +748,9 @@ local function get_fields(player, ...)
} }
end end
if progressive_mode then if progressive_mode and next(data.usages) then
data.usages = craftguide.progressive_filter_recipes(data.usages, player) data.usages =
apply_progressive_filters(data.usages, player)
end end
if not next(data.usages) then if not next(data.usages) then
@ -933,7 +957,7 @@ end
--[[ Custom recipes (>3x3) test code --[[ Custom recipes (>3x3) test code
mt.register_craftitem("craftguide:custom_recipe_test", { mt.register_craftitem(":secretstuff:custom_recipe_test", {
description = "Custom Recipe Test", description = "Custom Recipe Test",
}) })
@ -943,12 +967,12 @@ for x = 1, 6 do
for i = 1, 10 - x do for i = 1, 10 - x do
cr[x][i] = {} cr[x][i] = {}
for j = 1, 10 - x do for j = 1, 10 - x do
cr[x][i][j] = "group:wood" cr[x][i][j] = "group:sand"
end end
end end
mt.register_craft({ mt.register_craft({
output = "craftguide:custom_recipe_test", output = "secretstuff:custom_recipe_test",
recipe = cr[x] recipe = cr[x]
}) })
end end