Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 7613e7d

Browse files
committed
Luacheck cleanup + API doc
1 parent 03e9538 commit 7613e7d

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

.luacheckrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
unused_args = false
21
allow_defined_top = true
32

43
read_globals = {

API.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mode is implemented as a recipe filter.
9797

9898
#### `craftguide.add_recipe_filter(name, function(recipes, player))`
9999

100-
Adds a recipe filter with the given name. The filter function should return the
100+
Adds a recipe filter with the given `name`. The filter function returns the
101101
recipes to be displayed, given the available recipes and an `ObjectRef` to the
102102
user. Each recipe is a table of the form returned by
103103
`minetest.get_craft_recipe`.
@@ -123,7 +123,7 @@ Removes all recipe filters and adds a new one.
123123

124124
#### `craftguide.remove_recipe_filter(name)`
125125

126-
Removes the recipe filter with the given name.
126+
Removes the recipe filter with the given `name`.
127127

128128
#### `craftguide.get_recipe_filters()`
129129

@@ -134,50 +134,41 @@ Returns a map of recipe filters, indexed by name.
134134
### Search filters
135135

136136
Search filters are used to perform specific searches inside the search field.
137-
They can be used like so: `<optional name>+<filter name>=<value1>,<value2>,<...>`
137+
You can cumulate several filters to perform a specific search.
138+
They can be used like so: `<optional_name> +<filter name>=<value1>,<value2>,<...>`
138139

139-
Examples:
140+
Example usages:
140141

141142
- `+groups=cracky,crumbly`: search for groups `cracky` and `crumbly` in all items.
142-
- `sand+groups=falling_node`: search for group `falling_node` for items which contain `sand` in their names.
143+
- `wood +groups=flammable +type=node`: search for group `flammable` amongst items which contain
144+
`wood` in their names AND have a `node` drawtype.
143145

144146
Notes:
145-
- If `optional name` is omitted, the search filter will apply to all items, without pre-filtering.
146-
- Filters can be combined.
147-
- The `groups` filter is currently implemented by default.
147+
- If `optional_name` is omitted, the search filter will apply to all items, without pre-filtering.
148+
- The `groups` and `type` filters are currently implemented by default.
148149

149150
#### `craftguide.add_search_filter(name, function(item, values))`
150151

151-
Adds a search filter with the given name.
152-
The search function should return a boolean value (whether the given item should be listed or not).
152+
Adds a search filter with the given `name`.
153+
The search function must return a boolean value (whether the given item should be listed or not).
153154

154-
Example function to show items which contain at least a recipe of given width(s):
155+
Example function sorting items by drawtype:
155156

156157
```lua
157-
craftguide.add_search_filter("widths", function(item, widths)
158-
local has_width
159-
local recipes = recipes_cache[item]
160-
161-
if recipes then
162-
for i = 1, #recipes do
163-
local recipe_width = recipes[i].width
164-
for j = 1, #widths do
165-
local width = tonumber(widths[j])
166-
if width == recipe_width then
167-
has_width = true
168-
break
169-
end
170-
end
171-
end
158+
craftguide.add_search_filter("type", function(item, drawtype)
159+
if drawtype == "node" then
160+
return reg_nodes[item]
161+
elseif drawtype == "item" then
162+
return reg_craftitems[item]
163+
elseif drawtype == "tool" then
164+
return reg_tools[item]
172165
end
173-
174-
return has_width
175166
end)
176167
```
177168

178169
#### `craftguide.remove_search_filter(name)`
179170

180-
Removes the search filter with the given name.
171+
Removes the search filter with the given `name`.
181172

182173
#### `craftguide.get_search_filters()`
183174

init.lua

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ local http = core.request_http_api()
1616
local singleplayer = core.is_singleplayer()
1717

1818
local reg_items = core.registered_items
19+
local reg_nodes = core.registered_nodes
20+
local reg_craftitems = core.registered_craftitems
1921
local reg_tools = core.registered_tools
2022
local reg_entities = core.registered_entities
2123
local reg_aliases = core.registered_aliases
@@ -824,7 +826,7 @@ local function cache_recipes(item)
824826
end
825827
end
826828

827-
local function get_recipes(item, data, player)
829+
local function get_recipes(player, item)
828830
local clean_item = reg_aliases[item] or item
829831
local recipes = recipes_cache[clean_item]
830832
local usages = usages_cache[clean_item]
@@ -1073,7 +1075,7 @@ local function craft_stack(player, pname, data, craft_rcp)
10731075
end
10741076
end
10751077

1076-
local function select_item(player, name, data, _f)
1078+
local function select_item(player, data, _f)
10771079
local item
10781080

10791081
for field in pairs(_f) do
@@ -1097,7 +1099,7 @@ local function select_item(player, name, data, _f)
10971099

10981100
if item == data.query_item then return end
10991101

1100-
local recipes, usages = get_recipes(item, data, player)
1102+
local recipes, usages = get_recipes(player, item)
11011103
if not recipes and not usages then return end
11021104

11031105
data.query_item = item
@@ -1757,14 +1759,21 @@ local function search(data)
17571759
local def = reg_items[item]
17581760
local desc = lower(translate(data.lang_code, def and def.description)) or ""
17591761
local search_in = sprintf("%s %s", item, desc)
1760-
local to_add
1762+
local temp, j, to_add = {}, 1
17611763

17621764
if search_filter then
17631765
for filter_name, values in pairs(filters) do
17641766
if values then
17651767
local func = search_filters[filter_name]
1766-
to_add = func(item, values) and (search_filter == "" or
1768+
to_add = (j > 1 and temp[item] or j == 1) and
1769+
func(item, values) and (search_filter == "" or
17671770
find(search_in, search_filter, 1, true))
1771+
1772+
if to_add then
1773+
temp[item] = true
1774+
end
1775+
1776+
j = j + 1
17681777
end
17691778
end
17701779
else
@@ -1804,6 +1813,16 @@ craftguide.add_search_filter("groups", function(item, groups)
18041813
return has_groups
18051814
end)
18061815

1816+
craftguide.add_search_filter("type", function(item, drawtype)
1817+
if drawtype == "node" then
1818+
return reg_nodes[item]
1819+
elseif drawtype == "item" then
1820+
return reg_craftitems[item]
1821+
elseif drawtype == "tool" then
1822+
return reg_tools[item]
1823+
end
1824+
end)
1825+
18071826
--[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not
18081827
return the fuel, replacements and toolrepair recipes, we have to
18091828
override `core.register_craft` and do some reverse engineering.
@@ -1932,7 +1951,7 @@ local function get_init_items()
19321951
end
19331952
end
19341953

1935-
local function init_data(player, name)
1954+
local function init_data(name)
19361955
local info = get_player_info(name)
19371956

19381957
pdata[name] = {
@@ -1966,7 +1985,7 @@ on_mods_loaded(get_init_items)
19661985

19671986
on_joinplayer(function(player)
19681987
local name = player:get_player_name()
1969-
init_data(player, name)
1988+
init_data(name)
19701989
local data = pdata[name]
19711990

19721991
if data.fs_version < MIN_FORMSPEC_VERSION then
@@ -2085,7 +2104,7 @@ on_receive_fields(function(player, formname, _f)
20852104
elseif _f.craft_rcp or _f.craft_usg then
20862105
craft_stack(player, name, data, _f.craft_rcp)
20872106
else
2088-
select_item(player, name, data, _f)
2107+
select_item(player, data, _f)
20892108
end
20902109

20912110
return true, show_fs(player, name)
@@ -2126,7 +2145,7 @@ core.register_craftitem("craftguide:book", {
21262145
wield_image = PNG.book,
21272146
stack_max = 1,
21282147
groups = {book = 1},
2129-
on_use = function(itemstack, user)
2148+
on_use = function(_, user)
21302149
on_use(user)
21312150
end
21322151
})
@@ -2158,7 +2177,7 @@ core.register_node("craftguide:sign", {
21582177
meta:set_string("infotext", "Crafting Guide Sign")
21592178
end,
21602179

2161-
on_rightclick = function(pos, node, user, itemstack)
2180+
on_rightclick = function(_, _, user)
21622181
on_use(user)
21632182
end
21642183
})

0 commit comments

Comments
 (0)