How do you lock and give chest a name?

I'm making an adventure map and I need to have a command block next to a clock set the block near it to a chest with a custom name with a lock this I what I have so far:

/setblock ~-1 ~-1 ~ Minecraft:trapped_chest 1 {display:{Name:"Locked Chest",Lock:"Chest Key 47"}}

And then it says:

[16:26:39] Data tag parsing failed: Invalid tag encountered, expected '{' as first char.

I'm in 1.8.


The syntax of /setblock is:

/setblock <x> <y> <z> <TimeName> [dataValue] [oldBlockHandling] [dataTag]

Which means that you need to specify an [oldBlockHandling] mode (E.G: replace) before you can specify the [dataTag].

The name tag of an entity or tile entity is CustomName, rather than just Name as it is on items. Also, neither it nor Lock are stored in a Display compound tag.

This should be your fixed command:

/setblock ~-1 ~-1 ~ trapped_chest 1 replace {CustomName:"Locked Chest",Lock:"Chest Key 47"}

Go on top of the thing you want to lock (examples include chest, dispenser, dropper, trapped chest) and type this command. Make sure you are the owner of the world or have Operator rights.

/blockdata ~ ~-1 ~ {Lock:"Key"}

I'll break this up so it is easier to understand.

(/blockdata) You are editing the data of the block

(~ ~-1 ~) You are telling minecraft what you are locking in co ordinates. XYZ

{Lock:"Key"} And you are telling to only open this thing when you have "Key" in your hand.

You may change the word "Key" to whatever you like, just act like it is a password so it's not like 123 or ABC. Someone could rename a block as 123 and open your thing.

Happy locking!


For version 1.16.5:

/setblock ~ ~1 ~ chest{CustomName:'[{"text":"name","italic":false,"color":"yellow"}]',Lock:"password"}