Inventories: Update chapter

#104
This commit is contained in:
rubenwardy 2022-07-31 17:57:55 +01:00
parent b5fcac7ed5
commit bc8820c4b6

View File

@ -19,6 +19,9 @@ that be a player inventory, a node inventory, or a detached inventory.
- [What are ItemStacks and Inventories?](#what-are-itemstacks-and-inventories) - [What are ItemStacks and Inventories?](#what-are-itemstacks-and-inventories)
- [ItemStacks](#itemstacks) - [ItemStacks](#itemstacks)
- [Inventory Locations](#inventory-locations) - [Inventory Locations](#inventory-locations)
- [Node Inventories](#node-inventories)
- [Player Inventories](#player-inventories)
- [Detached Inventories](#detached-inventories)
- [Lists](#lists) - [Lists](#lists)
- [Size and Width](#size-and-width) - [Size and Width](#size-and-width)
- [Checking Contents](#checking-contents) - [Checking Contents](#checking-contents)
@ -33,21 +36,21 @@ that be a player inventory, a node inventory, or a detached inventory.
An ItemStack is the data behind a single cell in an inventory. An ItemStack is the data behind a single cell in an inventory.
An *inventory* is a collection of *inventory lists*, each of which An *inventory* is a collection of *inventory lists*, each of which is a 2D grid
is a 2D grid of ItemStacks. of ItemStacks. Inventory lists are referred to as *lists* in the context of
Inventory lists are simply called *lists* in the context inventories.
of inventories.
The point of an inventory is to allow multiple grids when Players Players and nodes only have a single inventory; lists enable you to have
and Nodes only have at most one inventory in them. multiple grids within that inventory. By default, the player has the "main" list
for the bulk of its inventory and a few lists for the crafting system.
## ItemStacks ## ItemStacks
ItemStacks have four components to them: name, count, wear and metadata. ItemStacks have four components to them: `name`, `count`, `wear`, and metadata.
The item name may be the item name of a registered item, an alias, or an unknown The item name may be the item name of a registered item, an alias, or an unknown
item name. item name. Unknown items are common when users uninstall mods, or when mods
Unknown items are common when users uninstall mods, or when mods remove items without remove items without precautions, such as registering aliases.
precautions, such as registering aliases.
```lua ```lua
print(stack:get_name()) print(stack:get_name())
@ -58,19 +61,14 @@ if not stack:is_known() then
end end
``` ```
The count will always be 0 or greater. The count will always be 0 or greater. Through normal gameplay, the count should
Through normal gameplay, the count should be no more than the maximum stack size be no more than the maximum stack size of the item - `stack_max`. However, admin
of the item - `stack_max`. commands and buggy mods may result in stacks exceeding the maximum size.
However, admin commands and buggy mods may result in stacks exceeding the maximum
size.
```lua ```lua
print(stack:get_stack_max()) print(stack:get_stack_max())
``` ```
An ItemStack can be empty, in which case the count will be 0. An ItemStack can be empty, in which case the count will be 0.
```lua ```lua
@ -78,7 +76,7 @@ print(stack:get_count())
stack:set_count(10) stack:set_count(10)
``` ```
ItemStacks can be constructed in multiple ways using the ItemStack function. ItemStacks can be constructed in multiple ways using the ItemStack function:
```lua ```lua
ItemStack() -- name="", count=0 ItemStack() -- name="", count=0
@ -87,24 +85,31 @@ ItemStack("default:stone 30")
ItemStack({ name = "default:wood", count = 10 }) ItemStack({ name = "default:wood", count = 10 })
``` ```
Item metadata is an unlimited key-value store for data about the item. Item metadata is an unlimited key-value store for data about the item. Key-value
Key-value means that you use a name (called the key) to access the data (called the value). means that you use a name (called the key) to access the data (called the
Some keys have special meaning, such as `description` which is used to have a per-stack value). Some keys have special meaning, such as `description` which is used to
item description. have a per-stack item description. This will be covered in more detail in the
This will be covered in more detail in the Metadata and Storage chapter. [Storage and Metadata](../map/storage.html) chapter.
## Inventory Locations ## Inventory Locations
An Inventory Location is where and how the inventory is stored. An Inventory Location is where and how the inventory is stored. There are three
There are three types of inventory location: player, node, and detached. types of inventory location: player, node, and detached. An inventory is
An inventory is directly tied to one and only one location - updating the inventory directly tied to one and only one location - updating the inventory will cause
will cause it to update immediately. it to update immediately.
Node inventories are related to the position of a specific node, such as a chest. ### Node Inventories
The node must be loaded because it is stored in [node metadata](../map/storage.html#metadata).
Node inventories are related to the position of a specific node, such as a
chest. The node must be loaded because it is stored in
[node metadata](../map/storage.html#metadata).
```lua ```lua
local inv = minetest.get_inventory({ type="node", pos={x=1, y=2, z=3} }) -- In a node def
on_punch = function(pos, node)
local inv = minetest.get_inventory({ type="node", pos=pos })
-- now use the inventory
end,
``` ```
The above obtains an *inventory reference*, commonly referred to as *InvRef*. The above obtains an *inventory reference*, commonly referred to as *InvRef*.
@ -118,6 +123,8 @@ The location of an inventory reference can be found like so:
local location = inv:get_location() local location = inv:get_location()
``` ```
### Player Inventories
Player inventories can be obtained similarly or using a player reference. Player inventories can be obtained similarly or using a player reference.
The player must be online to access their inventory. The player must be online to access their inventory.
@ -127,8 +134,10 @@ local inv = minetest.get_inventory({ type="player", name="player1" })
local inv = player:get_inventory() local inv = player:get_inventory()
``` ```
A detached inventory is one which is independent of players or nodes. ### Detached Inventories
Detached inventories also don't save over a restart.
A detached inventory is one that is independent of players or nodes. Detached
inventories also don't save over a restart.
```lua ```lua
local inv = minetest.get_inventory({ local inv = minetest.get_inventory({
@ -177,9 +186,10 @@ On the contrary, action callbacks - starting with `on_` - don't have a return va
## Lists ## Lists
Inventory Lists are a concept used to allow multiple grids to be stored inside a single location. Inventory Lists are a concept used to allow multiple grids to be stored inside a
This is especially useful for the player as there are a number of common lists single location. This is especially useful for the player as there are several
which all games have, such as the *main* inventory and *craft* slots. common lists that all games have, such as the *main* inventory and *craft*
slots.
### Size and Width ### Size and Width