From e3f183b9c85aa04622b1dc0ed203c8b1e8b5dd80 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sat, 23 Jan 2021 19:49:28 +0100 Subject: [PATCH] Rework search_filter (cumulative) + API doc --- API.md | 50 +++++++++++++++++++++++--------------------------- init.lua | 47 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 37 deletions(-) diff --git a/API.md b/API.md index 9b479f0..854fdf7 100644 --- a/API.md +++ b/API.md @@ -27,6 +27,12 @@ i3.new_tab { fields = function(player, data, fields) i3.set_fs(player) end, + + -- Determine if the recipe panels must be hidden or not (must return a boolean) + hide_panels = function(player, data) + local name = player:get_player_name() + return core.is_creative_enabled(name) + end, } ``` @@ -142,7 +148,7 @@ mode is implemented as a recipe filter. #### `i3.add_recipe_filter(name, function(recipes, player))` -Adds a recipe filter with the given name. The filter function should return the +Adds a recipe filter with the given `name`. The filter function returns the recipes to be displayed, given the available recipes and an `ObjectRef` to the user. Each recipe is a table of the form returned by `minetest.get_craft_recipe`. @@ -168,7 +174,7 @@ Removes all recipe filters and adds a new one. #### `i3.remove_recipe_filter(name)` -Removes the recipe filter with the given name. +Removes the recipe filter with the given `name`. #### `i3.get_recipe_filters()` @@ -179,50 +185,40 @@ Returns a map of recipe filters, indexed by name. ### Search filters Search filters are used to perform specific searches inside the search field. -They can be used like so: `+=,,<...>` +They can be used like so: ` +=,,<...>` Examples: - `+groups=cracky,crumbly`: search for groups `cracky` and `crumbly` in all items. -- `sand+groups=falling_node`: search for group `falling_node` for items which contain `sand` in their names. +- `sand +groups=falling_node`: search for group `falling_node` for items which contain `sand` in their names. Notes: -- If `optional name` is omitted, the search filter will apply to all items, without pre-filtering. +- If `optional_name` is omitted, the search filter will apply to all items, without pre-filtering. - Filters can be combined. -- The `groups` filter is currently implemented by default. +- The `groups` and `type` filters are currently implemented by default. #### `i3.add_search_filter(name, function(item, values))` -Adds a search filter with the given name. -The search function should return a boolean value (whether the given item should be listed or not). +Adds a search filter with the given `name`. +The search function must return a boolean value (whether the given item should be listed or not). -Example function to show items which contain at least a recipe of given width(s): +Example function sorting items by drawtype: ```lua -i3.add_search_filter("widths", function(item, widths) - local has_width - local recipes = recipes_cache[item] - - if recipes then - for i = 1, #recipes do - local recipe_width = recipes[i].width - for j = 1, #widths do - local width = tonumber(widths[j]) - if width == recipe_width then - has_width = true - break - end - end - end +i3.add_search_filter("type", function(item, drawtype) + if drawtype == "node" then + return reg_nodes[item] + elseif drawtype == "item" then + return reg_craftitems[item] + elseif drawtype == "tool" then + return reg_tools[item] end - - return has_width end) ``` #### `i3.remove_search_filter(name)` -Removes the search filter with the given name. +Removes the search filter with the given `name`. #### `i3.get_search_filters()` diff --git a/init.lua b/init.lua index aff6ac0..7848214 100644 --- a/init.lua +++ b/init.lua @@ -20,6 +20,8 @@ local http = core.request_http_api() local singleplayer = core.is_singleplayer() local reg_items = core.registered_items +local reg_nodes = core.registered_nodes +local reg_craftitems = core.registered_craftitems local reg_tools = core.registered_tools local reg_entities = core.registered_entities local reg_aliases = core.registered_aliases @@ -749,14 +751,21 @@ local function search(data) local def = reg_items[item] local desc = lower(translate(data.lang_code, def and def.description)) or "" local search_in = fmt("%s %s", item, desc) - local to_add + local temp, j, to_add = {}, 1 if search_filter then for filter_name, values in pairs(filters) do if values then local func = search_filters[filter_name] - to_add = func(item, values) and (search_filter == "" or + to_add = (j > 1 and temp[item] or j == 1) and + func(item, values) and (search_filter == "" or find(search_in, search_filter, 1, true)) + + if to_add then + temp[item] = true + end + + j = j + 1 end end else @@ -1967,15 +1976,15 @@ local function get_debug_grid(data, fs, full_height) local spacing = 0.2 for x = 0, data.xoffset, spacing do - fs(fmt("box", x, 0, 0.01, full_height, "#f00")) + fs(fmt("box", x, 0, 0.01, full_height, "#ff0")) end for y = 0, full_height, spacing do - fs(fmt("box", 0, y, data.xoffset, 0.01, "#f00")) + fs(fmt("box", 0, y, data.xoffset, 0.01, "#ff0")) end - fs(fmt("box", data.xoffset / 2, 0, 0.01, full_height, "#ff0")) - fs(fmt("box", 0, full_height / 2, data.xoffset, 0.01, "#ff0")) + fs(fmt("box", data.xoffset / 2, 0, 0.01, full_height, "#f00")) + fs(fmt("box", 0, full_height / 2, data.xoffset, 0.01, "#f00")) end local function make_fs(player, data) @@ -1995,9 +2004,13 @@ local function make_fs(player, data) fs(fmt("bg9", 0, 0, data.xoffset, full_height, PNG.bg_full, 10)) - tabs[(data.current_tab or 1)].formspec(player, data, fs) + local tab = tabs[(data.current_tab or 1)] - if data.query_item then + tab.formspec(player, data, fs) + + local hide_panels = tab.hide_panels and tab.hide_panels(player, data) + + if data.query_item and not hide_panels then get_panels(player, data, fs) end @@ -2365,6 +2378,11 @@ i3.new_tab { return set_fs(player) end, + + hide_panels = function(player, data) + local name = player:get_player_name() + return core.is_creative_enabled(name) + end, } i3.new_tab { @@ -2485,8 +2503,7 @@ i3.add_search_filter("groups", function(item, groups) local def = reg_items[item] local has_groups = true - for i = 1, #groups do - local group = groups[i] + for _, group in ipairs(groups) do if not def.groups[group] then has_groups = nil break @@ -2496,6 +2513,16 @@ i3.add_search_filter("groups", function(item, groups) return has_groups end) +i3.add_search_filter("type", function(item, drawtype) + if drawtype == "node" then + return reg_nodes[item] + elseif drawtype == "item" then + return reg_craftitems[item] + elseif drawtype == "tool" then + return reg_tools[item] + end +end) + --[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not return the fuel, replacements and toolrepair recipes, we have to override `core.register_craft` and do some reverse engineering.