Minecraft – How to allow multiple keys to unlock a container block

minecraft-commandsminecraft-java-edition

I am trying to make a container that unlocks for several keys, so far my testing says that only 1 key can be in place at a time. is there a way around this to have multiple keys?

I'm playing Minecraft Java Edition 1.12.2

Best Answer

Not directly! The Lock tag only accepts one item name. You could have multiple items with the same name instead.

But what you could do is repeatedly test if there is a player near the chest, and if they have an acceptable key, update the chest's Lock tag to match the name of the acceptable item.

For example, let's have our keys named AliceKey and BobKey, given to players Alice and Bob respectively. Summon an invisible armour stand on the chest with the tag chestKeys, then create separate command chains for each possible key:

Rpt: /execute @e[tag=chestKeys] ~ ~ ~ testfor @p {Inventory:[{tag:{display:{Name:"AliceKey"}}}]}
Cnd: /blockdata 12 23 34 {Lock:"AliceKey"}

1.13+

Command syntax was heavily changed in 1.13, and so the commands above won't work. Instead, use the new versions of /execute and /blockdata:

Rpt: /execute at @e[tag=chestKeys] if entity @p[nbt={Inventory:{tag:{display:{Name:'{"text":"AliceKey"}'}}}}] run data merge block 12 23 34 {Lock:"AliceKey"}
Related Topic