How to test for a named chest?

I'm doing a project in Minecraft 1.10 where I want to detect a named chest and put an item in it. The player puts down a chest that's named Mailbox, and the command blocks are supposed to detect it. I tried using the execute command to detect the chest, and then blockdata to put an item in that chest, but never got to work correctly.

Does anyone know how to do this?


Solution 1:

Blocks don't keep names once placed. So finding a placed block with a name is not possible. Only items keep the name, once you shift their state all properties are restored to default.

Names are not stored in blockdata!!

But, a chest has a block entity associated with it that holds additional data about the block. The block's block entity ID is chest. As stated on the Minecraft Wiki:

CustomName: Optional. The name of this container, which will display in its GUI where the default name ordinarily is.

Lock: Optional. When not blank, prevents the container from being opened unless the opener is holding an item whose name matches this string.

Items: List of items in the container.

LootTable: Optional. Loot table to be used to fill the chest when it is next opened, or the items are otherwise interacted with.

LootTableSeed: Optional. Seed for generating the loot table. 0 or omitted will use a random seed. Both loot table tags will be removed once the items have been generated.

The name of a chest is stored in the CustomName tag. So, to test for it, run this on a superfast clock:

/testforblock x y z chest -1 {CustomName:"The Name"}