Inventories
Chests, barrels, Create vaults, depots and any block that holds items: read what is inside and move it.
Any block that holds items is a peripheral of type inventory: chests, barrels, shulker boxes, hoppers, furnaces, Create Item Vaults, Depots, Basins, Millstones, and the storage blocks of other mods. A program counts what is inside, looks at each slot, and moves stacks from one inventory to another: the base of every stock alarm, sorter and factory dashboard.
local chest = peripheral.wrap("left")
print(chest.count("minecraft:iron_ingot") .. " iron ingots")
chest.push("right") -- up to 64 items into the chest on the rightchest.size() | How many slots the inventory has: 27 for a chest, 54 for a double chest, 5 for a hopper. |
chest.list() | Every stack of the inventory, in one table: list()[3] is the stack in slot 3, or nil when that slot is empty. |
chest.get(slot) | The stack in one slot. |
chest.count([id]) | Counts the items: all of them, or only those of one id. |
chest.push(target [, slot [, count]]) | Moves items from this inventory into another one of the same computer. |
Setting up
Reaching it. Put the inventory against a face of the computer (peripheral.wrap("left")), or against a Data Cable of the computer's network, from the Minicomputer on: it is then named after its coordinates, like inventory@120,64,-35. peripheral.find("inventory") takes the first one, and peripheral.list("inventory") gives the names of all of them. See Peripherals.
The face matters. The computer sees the inventory through the face that touches it (or touches the cable), and some blocks show different slots on different faces. A furnace under the computer shows its top face: one slot, the smelting input. Beside the computer, it shows a side: the fuel slot. Chests, barrels and Create Item Vaults show everything on every face.
Blocks with several jobs. A Millstone is also a kinetic block and a Basin is also a tank: their wrapped table has the methods of each. Their name on a cable uses their first type (kinetic@... for the Millstone, inventory@... for the Basin), but peripheral.find("inventory") finds them all.
Vaults on a cable. A Create Item Vault touching the cable with several of its blocks appears under several names, one per block: they all reach the same vault.
What slots look like. Slots are numbered from 1, in the block's own order. A stack is described by a table:
| field | content |
|---|---|
name | the item id, with its namespace: "minecraft:iron_ingot", "create:andesite_alloy" |
count | how many items are in the slot |
display | the name a player sees on the item, a name given with an anvil included |
A Create Item Vault has 20 slots per block (unless the server changed it). A Depot has 9: slot 1 is the item lying on it, slots 2 to 9 hold what a machine made from it.
Reading
How many slots the inventory has: 27 for a chest, 54 for a double chest, 5 for a hopper.
- number
- the number of slots, through this face
local chest = peripheral.wrap("left")
print(chest.size(), "slots")When the block is not an inventory any more (broken, replaced), every method of this page stops the program with not an inventory anymore.
Every stack of the inventory, in one table: list()[3] is the stack in slot 3, or nil when that slot is empty.
- table
- the stacks, indexed by slot number; empty slots are missing
Because empty slots leave holes, walk the table with pairs, not ipairs (which stops at the first hole) and not # (which means nothing for a table with holes):
local chest = peripheral.wrap("left")
for slot, stack in pairs(chest.list()) do
print(slot, stack.count, stack.display)
endTo go through the slots in their order, count from 1 to size() and look each slot up:
local chest = peripheral.wrap("left")
local items = chest.list()
for slot = 1, chest.size() do
if items[slot] then
print(slot .. ": " .. items[slot].name)
end
endThe call costs one instruction per slot, and the table takes memory for each stack: on a large vault, read it once and keep the table rather than calling list() in a loop.
See also inventory.get() inventory.count()
The stack in one slot.
slotnumber- a slot number, from 1 to
size()
- table|nil
- the stack in that slot,
{name =, count =, display =}, ornilwhen it is empty
Cheaper than list() when you only care about one slot, for example the item lying on a Depot:
local depot = peripheral.wrap("top")
local stack = depot.get(1)
if stack then
print("on the depot: " .. stack.count .. " " .. stack.display)
else
print("the depot is empty")
endA slot number outside the inventory also returns nil. A number with decimals stops the program with bad argument #1 to 'get' (number has no integer representation).
See also inventory.list()
Counts the items: all of them, or only those of one id.
idstring optional- an item id, like
"minecraft:coal"; every item when left out
- number
- how many items the inventory holds, of that id or in all
local vault = peripheral.wrap("left")
print(vault.count()) -- everything
print(vault.count("minecraft:coal")) -- only coal
print(vault.count("create:andesite_alloy"))The id must be complete, with its namespace: count("coal") counts nothing and returns 0. To find the exact id of an item, look at list(), or press F3 + H in game to show ids in tooltips.
See also inventory.list()
Moving items
Moves items from this inventory into another one of the same computer.
targetstring- the peripheral name of the inventory to fill: a face (
"right") or a cable name ("inventory@120,64,-35") slotnumber optional- take only from this slot; every slot, in order, when left out or
nil countnumber optional- the most items to move, 64 when left out
- number
- how many items actually moved
local chest = peripheral.wrap("left")
chest.push("right") -- up to 64 items, from the first slots that have some
chest.push("right", 1, 16) -- 16 items from slot 1
chest.push("right", nil, 640) -- up to 640 items, from any slot- The target is a name, the one you would give to
peripheral.wrap, not a wrapped table. Get the names withperipheral.list("inventory"). countlimits the total moved by this call, over all the slots it visits; it is not limited to one stack.- It returns the number of items that moved: 0 when the source is empty, the target full, or the target refuses that item (a furnace's fuel slot refuses iron).
- Items are never destroyed. In the rare case where the target takes less than it promised and the source refuses the rest back, the rest drops on the ground above the source.
- Both inventories are reached through the face that touches the computer or the cable, so the face of the target decides which slots it fills (the input of a furnace from above, its fuel from the side).
- When
targetnames no inventory, the program stops withno inventory 'right'. Aslotbeyondsize()can stop it with an internal error: check it first.
There is no pull: to move items the other way, wrap the other inventory and push from it.
See also inventory.list()
Examples
Emptying a chest into a vault
The miners drop their loot in a chest on the left of the computer; a Create Item Vault stands on the right. Every ten seconds, everything goes into the vault.
local chest = peripheral.wrap("left")
while true do
local moved = chest.push("right", nil, 64 * chest.size())
if moved > 0 then
print(moved .. " items stored")
end
sleep(10)
endA stock report, largest first
Adds up the stacks by item and prints the ten largest totals. Here the content of a small chest is written out, so you can see the shape of what list() returns, holes included; in the world, the first line would be local items = vault.list().
local items = {
[1] = {name = "minecraft:iron_ingot", count = 64, display = "Iron Ingot"},
[2] = {name = "minecraft:coal", count = 40, display = "Coal"},
[4] = {name = "minecraft:iron_ingot", count = 23, display = "Iron Ingot"},
[7] = {name = "create:andesite_alloy", count = 64, display = "Andesite Alloy"},
[9] = {name = "minecraft:coal", count = 64, display = "Coal"},
}
local totals = {}
for _, stack in pairs(items) do
totals[stack.display] = (totals[stack.display] or 0) + stack.count
end
local rows = {}
for name, total in pairs(totals) do
table.insert(rows, {name = name, total = total})
end
table.sort(rows, function(a, b) return a.total > b.total end)
for i = 1, math.min(10, #rows) do
print(string.format("%5d %s", rows[i].total, rows[i].name))
end104 Coal 87 Iron Ingot 64 Andesite Alloy
An automatic sorter
Everything arrives in a chest on top of the computer (from a hopper, a Create funnel or a belt). Iron, gold and coal go to their own chests on the cable; anything else goes to the overflow chest on the left. The names come from peripheral.list("inventory").
local input = peripheral.wrap("top")
local ROUTES = {
["minecraft:iron_ingot"] = "inventory@120,64,-35",
["minecraft:gold_ingot"] = "inventory@121,64,-35",
["minecraft:coal"] = "inventory@122,64,-35",
}
local OVERFLOW = "left"
while true do
for slot, stack in pairs(input.list()) do
local target = ROUTES[stack.name] or OVERFLOW
input.push(target, slot, stack.count)
end
sleep(2)
endThe Automatic item sorter recipe builds a complete sorter, and Stock alarm and display board watches a stock level.