minetest_select_item/API.md

102 lines
3.7 KiB
Markdown
Raw Permalink Normal View History

2018-05-14 13:32:01 -07:00
# Select Item API
With this API you can open the item selection dialog as well as
catch events when players select an item from this dialog.
You can safely optionally depend on this mod, just make sure
2018-05-14 18:18:42 -07:00
to check for the mod's existence first (`minetest.get_modpath`
2018-05-14 13:32:01 -07:00
returns non-`nil` value).
## Functions
2018-05-17 15:37:43 -07:00
### `select_item.show_dialog(playername, dialogname, filter, compare)`
2018-05-14 13:32:01 -07:00
Shows an item selection dialog to a player. The player can choose
one item (which triggers a callback) or abort selection
(in which case nothing happens).
2018-05-28 11:04:20 -07:00
By default, this displays all items with the exception of unknown
items and `ignore`. This also includes items which players may
normally not be supposed to see, like those usually not found in
so-called “creative inventories”. You should set the `filter` argument
2018-05-14 13:32:01 -07:00
to filter out unwanted items.
2018-05-28 11:04:20 -07:00
The items are also sorted by a sorting rule.
2018-05-14 13:32:01 -07:00
#### Parameters
* `playername`: Name of player to show dialog to
* `dialogname`: Identifier of the dialog (must not contain “%%”)
2018-05-17 15:37:43 -07:00
* `filter`: (optional) Filter function to narrow down the visible
items (see below)
* `compare`: (optional) Custom compare function for sorting,
used in `table.sort`.
2018-05-14 13:32:01 -07:00
2018-05-17 06:06:23 -07:00
Recommended form of `dialogname` is “`<modname>:<name>`”. Almost all
names are allowed, but they must never contain the substring “%%”.
Example: `example:select_my_item`
2018-05-17 05:46:19 -07:00
2018-05-17 15:37:43 -07:00
Default sorting sorts items alphabetically by itemstring. It
moves items with empty description to the end, preceded by items
with description, but `not_in_creative_inventory=1`, and then
everything else to the beginning.
2018-05-28 11:04:20 -07:00
##### Filter function
2018-05-14 13:32:01 -07:00
The filter function has the function signature `filter(itemstring)`.
This function will be called for each item with the itemstring
2018-05-28 11:04:20 -07:00
given as argument. The function must return `true` if the item
2018-05-14 13:32:01 -07:00
in question is allowed in the selection dialog and `false` if
it must not appear.
You can also choose one of the following pre-defined filter functions:
* `select_item.filters.creative`: Removes all items with group
`not_in_creative_inventory=1` and/or empty `description`
* `select_item.filters.all`: Does not filter anything. Same as `nil`.
### `select_item.register_on_select_item(callback)`
Register a call function `callback` to the `select_item` mod.
2018-05-17 05:10:36 -07:00
Whenever a player selects an item or cancels the selection,
`callback` is called.
2018-05-14 13:32:01 -07:00
#### `callback` function
This has the function signature `callback(playername, dialogname, itemstring)`.
2018-05-14 13:32:01 -07:00
2018-05-28 11:04:20 -07:00
* `playername` is the name of the player who selected the item
* `dialogname` is the dialog identifier of the item selection dialog being used
2018-05-17 05:10:36 -07:00
* `itemstring` is the itemstring of the chosen item or `nil` if aborted
2018-05-14 13:32:01 -07:00
2018-05-17 05:10:36 -07:00
Normally, if the player pushes any button, the formspec is closed.
2018-05-28 11:04:20 -07:00
But if you return `false` in this callback, the formspec is *not* closed.
2018-05-14 19:31:39 -07:00
2018-05-14 13:32:01 -07:00
## Examples
2018-05-28 11:04:20 -07:00
Display all items from Creative inventory to Player 1:
2018-05-14 13:32:01 -07:00
```
select_item.show_dialog("Player 1", "example:creative", select_item.filters.creative)
2018-05-14 13:32:01 -07:00
```
Display all flammable to Player 1:
2018-05-28 11:04:20 -07:00
2018-05-14 13:32:01 -07:00
```
select_item.show_dialog("Player 1", "example:flammable", function(itemstring)
2018-05-28 11:04:20 -07:00
if minetest.get_item_group(itemstring, "flammable") >= 1 then
2018-05-14 13:32:01 -07:00
return true
else
return false
end
end
```
Note the different values for `dialogname`.
Adding a selected to the player's inventory after player selected item in the “Creative” dialog
above:
2018-05-28 11:04:20 -07:00
2018-05-14 13:32:01 -07:00
```
select_item.register_on_select_item(function(playername, dialogname, itemstring)
2018-05-28 11:04:20 -07:00
--[[ Check for the dialog type you care about. This check should almost always be done
to ensure interoperability with other mods. ]]
if dialogname == "example:creative" then
2018-05-28 11:04:20 -07:00
local inv = minetest.get_inventory({type="player", location=playername})
inv:add_item("main", ItemStack(itemstring))
end
2018-05-14 13:32:01 -07:00
end)
```