Luacheck cleanup + API doc

This commit is contained in:
Jean-Patrick Guerrero 2021-01-23 21:21:28 +01:00
parent 03e9538886
commit 7613e7dacb
3 changed files with 49 additions and 40 deletions

@ -1,4 +1,3 @@
unused_args = false
allow_defined_top = true
read_globals = {

49
API.md

@ -97,7 +97,7 @@ mode is implemented as a recipe filter.
#### `craftguide.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`.
@ -123,7 +123,7 @@ Removes all recipe filters and adds a new one.
#### `craftguide.remove_recipe_filter(name)`
Removes the recipe filter with the given name.
Removes the recipe filter with the given `name`.
#### `craftguide.get_recipe_filters()`
@ -134,50 +134,41 @@ 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: `<optional name>+<filter name>=<value1>,<value2>,<...>`
You can cumulate several filters to perform a specific search.
They can be used like so: `<optional_name> +<filter name>=<value1>,<value2>,<...>`
Examples:
Example usages:
- `+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.
- `wood +groups=flammable +type=node`: search for group `flammable` amongst items which contain
`wood` in their names AND have a `node` drawtype.
Notes:
- 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.
- If `optional_name` is omitted, the search filter will apply to all items, without pre-filtering.
- The `groups` and `type` filters are currently implemented by default.
#### `craftguide.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
craftguide.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
craftguide.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
end
end
return has_width
end)
```
#### `craftguide.remove_search_filter(name)`
Removes the search filter with the given name.
Removes the search filter with the given `name`.
#### `craftguide.get_search_filters()`

@ -16,6 +16,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
@ -824,7 +826,7 @@ local function cache_recipes(item)
end
end
local function get_recipes(item, data, player)
local function get_recipes(player, item)
local clean_item = reg_aliases[item] or item
local recipes = recipes_cache[clean_item]
local usages = usages_cache[clean_item]
@ -1073,7 +1075,7 @@ local function craft_stack(player, pname, data, craft_rcp)
end
end
local function select_item(player, name, data, _f)
local function select_item(player, data, _f)
local item
for field in pairs(_f) do
@ -1097,7 +1099,7 @@ local function select_item(player, name, data, _f)
if item == data.query_item then return end
local recipes, usages = get_recipes(item, data, player)
local recipes, usages = get_recipes(player, item)
if not recipes and not usages then return end
data.query_item = item
@ -1757,14 +1759,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 = sprintf("%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
@ -1804,6 +1813,16 @@ craftguide.add_search_filter("groups", function(item, groups)
return has_groups
end)
craftguide.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.
@ -1932,7 +1951,7 @@ local function get_init_items()
end
end
local function init_data(player, name)
local function init_data(name)
local info = get_player_info(name)
pdata[name] = {
@ -1966,7 +1985,7 @@ on_mods_loaded(get_init_items)
on_joinplayer(function(player)
local name = player:get_player_name()
init_data(player, name)
init_data(name)
local data = pdata[name]
if data.fs_version < MIN_FORMSPEC_VERSION then
@ -2085,7 +2104,7 @@ on_receive_fields(function(player, formname, _f)
elseif _f.craft_rcp or _f.craft_usg then
craft_stack(player, name, data, _f.craft_rcp)
else
select_item(player, name, data, _f)
select_item(player, data, _f)
end
return true, show_fs(player, name)
@ -2126,7 +2145,7 @@ core.register_craftitem("craftguide:book", {
wield_image = PNG.book,
stack_max = 1,
groups = {book = 1},
on_use = function(itemstack, user)
on_use = function(_, user)
on_use(user)
end
})
@ -2158,7 +2177,7 @@ core.register_node("craftguide:sign", {
meta:set_string("infotext", "Crafting Guide Sign")
end,
on_rightclick = function(pos, node, user, itemstack)
on_rightclick = function(_, _, user)
on_use(user)
end
})