Sorting the storage
Throw everything you bring home into one chest, and a program sorts it: building blocks into one chest, food into another. What it doesn't know stays in the input chest. Next to it stands a trash can that empties itself.
Functions: GET_INVENTORY, MOVE_ITEMS, DELETE_ITEMS, and SAY
Needs an operator
MOVE_ITEMS and DELETE_ITEMS move items without a player, so they only work if the owner of the timer block is an operator. In the world of the tutorials, you are one. On a server, the administrator can allow them for everybody with allowInventoryChanges=true in config/c0de.properties; without that right, the program crashes with PERMISSION_DENIED. There is no way around it for everyone, but GET_INVENTORY always works: a program can still tell you what is in a chest, and hoppers can do simple sorting without a program.
What you need
- an empty sheet for each program
- for the sorter: a timer block, three chests and a hopper, plus some cobblestone, bread and a diamond
- for the trash can: a second timer block, a fourth chest and some rotten flesh
- for the exercises: rotten flesh, poisonous potatoes and a fifth chest
The idea
GET_INVENTORY gives the content of a chest as an array of ITEM_STACK structs, one per slot: each has an id, like minecraft:bread, and a count. An empty slot has the id "" and the count 0.
MOVE_ITEMS moves up to count items with an id from one inventory to another and says how many it moved – fewer if the target is full. DELETE_ITEMS removes items for good.
What goes where is a decision of its own, and it's pure computation: an item ID goes in, a chest number comes out. That part is easy to test without any chest.
The rules
PROGRAM sortrules
NOOP
VARIABLE blocks ARRAY STRING
BEGIN
"minecraft:cobblestone" "minecraft:dirt" "minecraft:sand" "minecraft:gravel"
END VARIABLE
VARIABLE food ARRAY STRING
BEGIN
"minecraft:bread" "minecraft:apple" "minecraft:carrot" "minecraft:potato"
END VARIABLE
NOOP
FUNCTION chestFor
PARAMETER id STRING
RESULT chest INTEGER
BEGIN
EACH name blocks
BEGIN
IF name = id ;
BEGIN
ASSIGN chest 1 ;
END IF
END EACH
EACH name food
BEGIN
IF name = id ;
BEGIN
ASSIGN chest 2 ;
END IF
END EACH
END FUNCTIONchestFor gives 1 for building blocks, 2 for food and 0 for everything else. The program runner checks it with minecraft:dirt (1), minecraft:bread (2) and minecraft:diamond (0).
The sorter
PROGRAM sorter
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE blocks ARRAY STRING
BEGIN
"minecraft:cobblestone" "minecraft:dirt" "minecraft:sand" "minecraft:gravel"
END VARIABLE
VARIABLE food ARRAY STRING
BEGIN
"minecraft:bread" "minecraft:apple" "minecraft:carrot" "minecraft:potato"
END VARIABLE
NOOP
FUNCTION chestFor
PARAMETER id STRING
RESULT chest INTEGER
BEGIN
EACH name blocks
BEGIN
IF name = id ;
BEGIN
ASSIGN chest 1 ;
END IF
END EACH
EACH name food
BEGIN
IF name = id ;
BEGIN
ASSIGN chest 2 ;
END IF
END EACH
END FUNCTION
NOOP
FUNCTION sort
BEGIN
VARIABLE input STRUCT c0de/minecraft/POSITION
VARIABLE target STRUCT c0de/minecraft/POSITION
VARIABLE slots ARRAY STRUCT c0de/minecraft/ITEM_STACK
VARIABLE chest INTEGER
VARIABLE moved INTEGER
ASSIGN input.x PID.x + 2 ;
ASSIGN input.y PID.y ;
ASSIGN input.z PID.z ;
ASSIGN target input ;
CALL c0de/minecraft/GET_INVENTORY
PARAMETER position input ;
RESULT result slots
EACH slot slots
BEGIN
CALL chestFor
PARAMETER id slot.id ;
RESULT chest chest
IF slot.count > 0 AND chest > 0 ;
BEGIN
ASSIGN target.x input.x + 2 * chest ;
CALL c0de/minecraft/MOVE_ITEMS
PARAMETER from input ;
PARAMETER to target ;
PARAMETER id slot.id ;
PARAMETER count slot.count ;
RESULT result moved
IF moved < slot.count ;
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "Chest " ⊕ chest ⊕ " is full." ;
END IF
END IF
END EACH
END FUNCTIONBuilding and running it
Put everything in a row from west to east:
| What | Where, seen from the timer block |
|---|---|
| the input chest | 2 blocks east |
| chest 1, building blocks | 4 blocks east |
| chest 2, food | 6 blocks east |
Leave a block free between them: two chests side by side join into a double chest. The input chest doesn't touch the timer block either, or the timer block would use it as its output. Put the hopper on top of the input chest, so you can throw items in from above.
Set the timer block to sorter and sort, with an interval of 100 ticks. Throw cobblestone, bread and a diamond into the hopper: within five seconds, the cobblestone is in chest 1, the bread in chest 2, and the diamond stays in the input chest.

How it works
- EACH slot. The loop goes through all 27 slots of the input chest. For an empty slot,
chestForgives 0, like for an unknown item, and nothing happens. - The target is a copy of the input position whose
xmoves 2 blocks east per chest number:input.x + 2 * chest. A third chest would simply be 2 blocks further. - A full chest. MOVE_ITEMS returns how many items it moved. If that's fewer than the slot held, the target is full, and the chat says so. The rest stays in the input chest and is tried again in the next run.
The trash can
The second program is a chest that eats everything you put into it.
PROGRAM trashcan
NOOP
INCLUDE c0de/minecraft
NOOP
FUNCTION empty
BEGIN
VARIABLE trash STRUCT c0de/minecraft/POSITION
VARIABLE slots ARRAY STRUCT c0de/minecraft/ITEM_STACK
VARIABLE removed INTEGER
VARIABLE total INTEGER
ASSIGN trash.x PID.x + 2 ;
ASSIGN trash.y PID.y ;
ASSIGN trash.z PID.z ;
CALL c0de/minecraft/GET_INVENTORY
PARAMETER position trash ;
RESULT result slots
EACH slot slots
BEGIN
IF slot.count > 0 ;
BEGIN
CALL c0de/minecraft/DELETE_ITEMS
PARAMETER position trash ;
PARAMETER id slot.id ;
PARAMETER count slot.count ;
RESULT result removed
ASSIGN total total + removed ;
END IF
END EACH
IF total > 0 ;
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "The trash can ate " ⊕ total ⊕ " items." ;
END IF
END FUNCTIONPlace a chest 2 blocks east of the second timer block, set the timer block to trashcan and empty with an interval of 200 ticks, and put rotten flesh into the chest. Within ten seconds it's gone, and the chat tells how many items were eaten. DELETE_ITEMS returns how many it removed, like MOVE_ITEMS.
Try it yourself
- Let the sorter delete rotten flesh and poisonous potatoes right away, instead of keeping them in the input chest.
Solution
A third list, junk, for which chestFor gives -1. The sorter calls DELETE_ITEMS for those slots instead of MOVE_ITEMS.
PROGRAM junksorter
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE blocks ARRAY STRING
BEGIN
"minecraft:cobblestone" "minecraft:dirt" "minecraft:sand" "minecraft:gravel"
END VARIABLE
VARIABLE food ARRAY STRING
BEGIN
"minecraft:bread" "minecraft:apple" "minecraft:carrot" "minecraft:potato"
END VARIABLE
VARIABLE junk ARRAY STRING
BEGIN
"minecraft:rotten_flesh" "minecraft:poisonous_potato"
END VARIABLE
NOOP
FUNCTION chestFor
PARAMETER id STRING
RESULT chest INTEGER
BEGIN
EACH name blocks
BEGIN
IF name = id ;
BEGIN
ASSIGN chest 1 ;
END IF
END EACH
EACH name food
BEGIN
IF name = id ;
BEGIN
ASSIGN chest 2 ;
END IF
END EACH
EACH name junk
BEGIN
IF name = id ;
BEGIN
ASSIGN chest -1 ;
END IF
END EACH
END FUNCTION
NOOP
FUNCTION sort
BEGIN
VARIABLE input STRUCT c0de/minecraft/POSITION
VARIABLE target STRUCT c0de/minecraft/POSITION
VARIABLE slots ARRAY STRUCT c0de/minecraft/ITEM_STACK
VARIABLE chest INTEGER
VARIABLE moved INTEGER
ASSIGN input.x PID.x + 2 ;
ASSIGN input.y PID.y ;
ASSIGN input.z PID.z ;
ASSIGN target input ;
CALL c0de/minecraft/GET_INVENTORY
PARAMETER position input ;
RESULT result slots
EACH slot slots
BEGIN
CALL chestFor
PARAMETER id slot.id ;
RESULT chest chest
IF slot.count > 0 AND chest = -1 ;
BEGIN
CALL c0de/minecraft/DELETE_ITEMS
PARAMETER position input ;
PARAMETER id slot.id ;
PARAMETER count slot.count ;
END IF
IF slot.count > 0 AND chest > 0 ;
BEGIN
ASSIGN target.x input.x + 2 * chest ;
CALL c0de/minecraft/MOVE_ITEMS
PARAMETER from input ;
PARAMETER to target ;
PARAMETER id slot.id ;
PARAMETER count slot.count ;
RESULT result moved
IF moved < slot.count ;
BEGIN
CALL c0de/minecraft/SAY
PARAMETER message "Chest " ⊕ chest ⊕ " is full." ;
END IF
END IF
END EACH
END FUNCTION- Everything unknown should go into a chest 3, 8 blocks east of the timer block, instead of staying in the input chest.
Solution
Start chestFor with ASSIGN chest 3 ;. The two lists then overwrite it for the known items. Empty slots get 3 too, but slot.count > 0 skips them.
Read more
- The functions of this recipe: GET_INVENTORY, MOVE_ITEMS, DELETE_ITEMS and SAY
- Changing the world and Owner and permissions in the guide
- Programs in the world: who may do what, and why a chest next to a trigger block becomes its output
- EACH loops in the guide