TransWikia.com

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

Arqade Asked by Mystery on November 1, 2020

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.

2 Answers

This can be done rather easily by matching part of the Items data tag of the chest block entity in a testforblock command:

testforblock <x> <y> <z> minecraft:chest -1 {Items:[{id:minecraft:snowball,Slot:2s}]}

Replace <x> <y> <z> with the coordinates of the chest. The -1 allows all data values of the chest (i.e. all orientations) to match.

As usual, data tag matching is partial, meaning that everything not specified is not considered by the command, such as the Count of snowballs in the above command. I.e. any number of snowballs works, as but they need to be in the third slot from the top left, which is slot 2 because of 0-indexing. Any other items in the chest are also ignored.

You can add more item tags to the Items tag (comma separated, between the []) to match more items.

Correct answer by MrLemon on November 1, 2020

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!"

Answered by MacFreek on November 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP