mirror of
https://github.com/minetest-mods/craftguide.git
synced 2024-11-22 19:03:43 +01:00
Luacheck cleanup + API doc
This commit is contained in:
parent
03e9538886
commit
7613e7dacb
@ -1,4 +1,3 @@
|
|||||||
unused_args = false
|
|
||||||
allow_defined_top = true
|
allow_defined_top = true
|
||||||
|
|
||||||
read_globals = {
|
read_globals = {
|
||||||
|
49
API.md
49
API.md
@ -97,7 +97,7 @@ mode is implemented as a recipe filter.
|
|||||||
|
|
||||||
#### `craftguide.add_recipe_filter(name, function(recipes, player))`
|
#### `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
|
recipes to be displayed, given the available recipes and an `ObjectRef` to the
|
||||||
user. Each recipe is a table of the form returned by
|
user. Each recipe is a table of the form returned by
|
||||||
`minetest.get_craft_recipe`.
|
`minetest.get_craft_recipe`.
|
||||||
@ -123,7 +123,7 @@ Removes all recipe filters and adds a new one.
|
|||||||
|
|
||||||
#### `craftguide.remove_recipe_filter(name)`
|
#### `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()`
|
#### `craftguide.get_recipe_filters()`
|
||||||
|
|
||||||
@ -134,50 +134,41 @@ Returns a map of recipe filters, indexed by name.
|
|||||||
### Search filters
|
### Search filters
|
||||||
|
|
||||||
Search filters are used to perform specific searches inside the search field.
|
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.
|
- `+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:
|
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` and `type` filters are currently implemented by default.
|
||||||
- The `groups` filter is currently implemented by default.
|
|
||||||
|
|
||||||
#### `craftguide.add_search_filter(name, function(item, values))`
|
#### `craftguide.add_search_filter(name, function(item, values))`
|
||||||
|
|
||||||
Adds a search filter with the given name.
|
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).
|
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
|
```lua
|
||||||
craftguide.add_search_filter("widths", function(item, widths)
|
craftguide.add_search_filter("type", function(item, drawtype)
|
||||||
local has_width
|
if drawtype == "node" then
|
||||||
local recipes = recipes_cache[item]
|
return reg_nodes[item]
|
||||||
|
elseif drawtype == "item" then
|
||||||
if recipes then
|
return reg_craftitems[item]
|
||||||
for i = 1, #recipes do
|
elseif drawtype == "tool" then
|
||||||
local recipe_width = recipes[i].width
|
return reg_tools[item]
|
||||||
for j = 1, #widths do
|
|
||||||
local width = tonumber(widths[j])
|
|
||||||
if width == recipe_width then
|
|
||||||
has_width = true
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return has_width
|
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `craftguide.remove_search_filter(name)`
|
#### `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()`
|
#### `craftguide.get_search_filters()`
|
||||||
|
|
||||||
|
39
init.lua
39
init.lua
@ -16,6 +16,8 @@ local http = core.request_http_api()
|
|||||||
local singleplayer = core.is_singleplayer()
|
local singleplayer = core.is_singleplayer()
|
||||||
|
|
||||||
local reg_items = core.registered_items
|
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_tools = core.registered_tools
|
||||||
local reg_entities = core.registered_entities
|
local reg_entities = core.registered_entities
|
||||||
local reg_aliases = core.registered_aliases
|
local reg_aliases = core.registered_aliases
|
||||||
@ -824,7 +826,7 @@ local function cache_recipes(item)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_recipes(item, data, player)
|
local function get_recipes(player, item)
|
||||||
local clean_item = reg_aliases[item] or item
|
local clean_item = reg_aliases[item] or item
|
||||||
local recipes = recipes_cache[clean_item]
|
local recipes = recipes_cache[clean_item]
|
||||||
local usages = usages_cache[clean_item]
|
local usages = usages_cache[clean_item]
|
||||||
@ -1073,7 +1075,7 @@ local function craft_stack(player, pname, data, craft_rcp)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function select_item(player, name, data, _f)
|
local function select_item(player, data, _f)
|
||||||
local item
|
local item
|
||||||
|
|
||||||
for field in pairs(_f) do
|
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
|
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
|
if not recipes and not usages then return end
|
||||||
|
|
||||||
data.query_item = item
|
data.query_item = item
|
||||||
@ -1757,14 +1759,21 @@ local function search(data)
|
|||||||
local def = reg_items[item]
|
local def = reg_items[item]
|
||||||
local desc = lower(translate(data.lang_code, def and def.description)) or ""
|
local desc = lower(translate(data.lang_code, def and def.description)) or ""
|
||||||
local search_in = sprintf("%s %s", item, desc)
|
local search_in = sprintf("%s %s", item, desc)
|
||||||
local to_add
|
local temp, j, to_add = {}, 1
|
||||||
|
|
||||||
if search_filter then
|
if search_filter then
|
||||||
for filter_name, values in pairs(filters) do
|
for filter_name, values in pairs(filters) do
|
||||||
if values then
|
if values then
|
||||||
local func = search_filters[filter_name]
|
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))
|
find(search_in, search_filter, 1, true))
|
||||||
|
|
||||||
|
if to_add then
|
||||||
|
temp[item] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
j = j + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@ -1804,6 +1813,16 @@ craftguide.add_search_filter("groups", function(item, groups)
|
|||||||
return has_groups
|
return has_groups
|
||||||
end)
|
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
|
--[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not
|
||||||
return the fuel, replacements and toolrepair recipes, we have to
|
return the fuel, replacements and toolrepair recipes, we have to
|
||||||
override `core.register_craft` and do some reverse engineering.
|
override `core.register_craft` and do some reverse engineering.
|
||||||
@ -1932,7 +1951,7 @@ local function get_init_items()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function init_data(player, name)
|
local function init_data(name)
|
||||||
local info = get_player_info(name)
|
local info = get_player_info(name)
|
||||||
|
|
||||||
pdata[name] = {
|
pdata[name] = {
|
||||||
@ -1966,7 +1985,7 @@ on_mods_loaded(get_init_items)
|
|||||||
|
|
||||||
on_joinplayer(function(player)
|
on_joinplayer(function(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
init_data(player, name)
|
init_data(name)
|
||||||
local data = pdata[name]
|
local data = pdata[name]
|
||||||
|
|
||||||
if data.fs_version < MIN_FORMSPEC_VERSION then
|
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
|
elseif _f.craft_rcp or _f.craft_usg then
|
||||||
craft_stack(player, name, data, _f.craft_rcp)
|
craft_stack(player, name, data, _f.craft_rcp)
|
||||||
else
|
else
|
||||||
select_item(player, name, data, _f)
|
select_item(player, data, _f)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true, show_fs(player, name)
|
return true, show_fs(player, name)
|
||||||
@ -2126,7 +2145,7 @@ core.register_craftitem("craftguide:book", {
|
|||||||
wield_image = PNG.book,
|
wield_image = PNG.book,
|
||||||
stack_max = 1,
|
stack_max = 1,
|
||||||
groups = {book = 1},
|
groups = {book = 1},
|
||||||
on_use = function(itemstack, user)
|
on_use = function(_, user)
|
||||||
on_use(user)
|
on_use(user)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
@ -2158,7 +2177,7 @@ core.register_node("craftguide:sign", {
|
|||||||
meta:set_string("infotext", "Crafting Guide Sign")
|
meta:set_string("infotext", "Crafting Guide Sign")
|
||||||
end,
|
end,
|
||||||
|
|
||||||
on_rightclick = function(pos, node, user, itemstack)
|
on_rightclick = function(_, _, user)
|
||||||
on_use(user)
|
on_use(user)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user