Testing for a an Item in a specific slot in a chest?

Testing for a an Item in a specific slot in a chest? - Photo Of Female Engineer Working On Her Workspace

I know, there are many questions about checking chests for a certain item, but I want to test(using commands obviously) for lets say a snowball in the third slot of a chest. Is this possible in vanilla?

You can adress specific slots when using /replaceitem, but I don't think, that helps.



Best Answer

Since Minecraft 1.13, the best method is to use the /data and /execute if commands.

The following command will return all information about a block entity (such as a chest):

/data get block <x> <y> <z>

which might return -123, 75, 106 has the following block data: {x: -123, yL 75, z: 106, id: "minecraft:chest", Items: [{Slot: 0b, id: "minecraft:bread", Count: 4b}, {Slot: 2b, id: "minecraft:snowball", Count: 1b}]}. So indeed, there is a snowball at the third slot (Slot 2, since the first Slot has number 0).

It is also possible to select a data path (technically, the NBT path):

/data get block <x> <y> <z> Items

/data get block <x> <y> <z> Items[1]

/data get block <x> <y> <z> Items[{Slot: 2b}]

The first form returns the full array of all items. The second and third form select only a single item, either by specifying the index in which they occur in the internal data format [1], or by selecting the Slot in the chest [{Slot: 2b}]. Technically, it matches all items that have a Slot attribute with value 2b.

You can also combine selectors. The following line only selects snowballs in Slot 2b:

/data get block <x> <y> <z> Items[{id: "minecraft:snowball", Slot: 2b}]

If this returns no items, there are no snowballs in the third slot. If there are, you get the details with the number of snowballs.

You can use these selectors also in the /execute command. The following command will only run if there are snowballs in the third slot in the chest.

/execute if data block <x> <y> <z> Items[{id: "minecraft:snowball", Slot:2b}] run msg @a "Let's have a snow fight!"



Pictures about "Testing for a an Item in a specific slot in a chest?"

Testing for a an Item in a specific slot in a chest? - Female Engineer Controlling Flight Simulator
Testing for a an Item in a specific slot in a chest? - Photo of Clear Glass Measuring Cup Lot
Testing for a an Item in a specific slot in a chest? - Young woman watching movie in headphones in messy room





How to EFFICIENTLY MOVE ITEMS in Minecraft 1.16 [Inventory Tips]




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: ThisIsEngineering, ThisIsEngineering, Rodolfo Clix, Andrea Piacquadio