In this chapter you will learn how to use manipulate inventories, whether that
is a player inventory, a node inventory, or a detached inventory.
This chapter assumes that you already know how to create and manipulate
ItemStacks.
Basic Concepts.
Types of Inventories.
Player Inventories.
Node Inventories.
Detached Inventories.
InvRef and Lists.
Type of inventory.
List sizes.
List is empty.
Lua Tables.
Lua Tables for Lists.
InvRef, Items and Stacks.
Adding to a list.
Checking for room.
Taking items.
Contains.
Manipulating Stacks.
Basic Concepts
Components of an inventory:
An Inventory is a collection of Inventory Lists (also called a list when in the context of inventories).
An Inventory List is an array of slots. (By array, I mean a table indexed by numbers).
A slot is a place a stack can be - there may be a stack there or there may not.
An InvRef is an object that represents an inventory, and has functions to manipulate it.
There are three ways you can get inventories:
Player Inventories - an inventory attached to a player.
Node Inventories - an inventory attached to a node.
Detached Inventories - an inventory which is not attached to a node or player.
Types of Inventories
There are three types of Inventories.
Player Inventories.
This is what you see when you press i.
A player inventory usually has two grids, one for the main inventory, one for crafting.
Node Inventories.
An inventory related to a position, such as a chest.
The node must be loaded, as it’s stored in Node Metadata.
Detached Inventories
A detached inventory is independent of players and nodes.
One example of a detached inventory is the creative inventory is detached,
as all players see the same inventory.
You may also use this if you want multiple chests to share the same inventory.
This is how you get a detached inventory:
And this is how you can create one:
Creates a detached inventory. If it already exists, it is cleared.
You can supply a table of callbacks.
InvRef and Lists
Type of Inventory
You can check where the inventory is from by doing:
It will return a table like the one passed to minetest.get_inventory().
If the location is unknown, {type="undefined"} is returned.
List sizes
Inventory lists have a size, for example main has size of 32 slots by default.
They also have a width, which is used to divide them into a grid.
List is empty
Lua Tables
You can convert an inventory to a Lua table using:
It will be in this form:
You can then set an inventory like this:
Please note that the sizes of lists will not change.
Lua Tables for Lists
You can do the same as above, but for individual lists
It will be in this form:
You can then set the list like this:
Please note that the sizes of lists will not change.
InvRef, Items and Items
Adding to a list
"main" is the name of the list you’re adding to.
Checking for room
Taking items
Contains
This works if the item count is split up over multiple stacks,
for example looking for “default:stone 200” will work if there
are stacks of 99 + 95 + 6.
Manipulating Stacks
Finally, you can manipulate individual stacks like so: