ComputerCraft

Inventory management & item sorting

Connect chests to a computer with peripheral.wrap() and move items between containers automatically — no hoppers required.

What you will end up with

A program that reads the contents of an input chest and distributes each item type into a designated output chest. You define the routing rules in the script — it is simple to expand as your storage grows.

What you need

Any inventory that is physically touching the computer (on any of its six sides) can be accessed as a peripheral. The name CC uses for it is the side it is on: "top", "bottom", "left", "right", "front", or "back".

Step 1 — Check what is in a chest

Place a chest on the top of the computer, put some items in it, then open the terminal and run this to inspect its contents:

local chest = peripheral.wrap("top") local items = chest.list() for slot, item in pairs(items) do print(slot, item.name, item.count) end

You will see output like 1 minecraft:cobblestone 64. The item.name field is the full item ID you will use for routing rules.

Step 2 — Move items between two chests

Place a second chest on the bottom of the computer. The following moves everything from the top chest to the bottom chest:

local input = peripheral.wrap("top") local output = peripheral.wrap("bottom") local items = input.list() for slot, item in pairs(items) do input.pushItems(peripheral.getName(output), slot) end print("Done.")
pushItems(targetName, slot) moves the entire stack in slot from this inventory to the target inventory. Use the optional third argument to limit how many items to move.

Step 3 — Simple item sorter

The sorter below reads the input chest continuously and moves each item to the correct output chest based on its name. Extend the routes table to add more item types.

-- sorter.lua -- Routes items from input chest to labelled output chests local input = peripheral.wrap("back") -- Map item ID → side of the output chest local routes = { ["minecraft:cobblestone"] = "left", ["minecraft:iron_ore"] = "right", ["minecraft:coal"] = "top", } local defaultSide = "bottom" -- everything else goes here while true do local items = input.list() for slot, item in pairs(items) do local side = routes[item.name] or defaultSide local target = peripheral.wrap(side) if target then input.pushItems(peripheral.getName(target), slot) end end os.sleep(1) end

Useful inventory API methods

Troubleshooting