Updated:
1. Pick a random spot
/summon marker 0 64 0 {Tags:["treasure"]}
/spreadplayers 0 0 0 30 false @e[tag=treasure]
The invisible marker lands somewhere within 30 blocks. See the spreadplayers command.
2. Place the chest
/execute at @e[tag=treasure] run setblock ~ ~ ~ chest{Items:[{Slot:13b,id:"minecraft:nether_star",count:1}]}
The nether star is the prize. Use the loot command for extra random rewards.
3. Hot and cold hints (repeating)
/execute as @a at @s if entity @e[tag=treasure,distance=..10] run title @s actionbar {"text":"Very close!","color":"red"}
/execute as @a at @s if entity @e[tag=treasure,distance=10..30] run title @s actionbar {"text":"Getting warm","color":"yellow"}
Put both lines in repeating command blocks. Players see the hint above the hotbar, and it changes as they walk closer. For a fixed treasure spot, you can also hand out a compass that points to the coordinates.
4. Detect the finder (repeating)
/execute as @a if items entity @s container.* minecraft:nether_star run tellraw @a [{"selector":"@s"},{"text":" found the treasure!","color":"gold"}]
Java 1.20.5+. Stop the command block once it fires.
5. Clean up
/execute at @e[tag=treasure] run setblock ~ ~ ~ air
/kill @e[tag=treasure]
/clear @a nether_star
6. Announce only the first finder
# repeating
execute unless entity @a[tag=winner] as @a if items entity @s container.* minecraft:nether_star run tag @s add winner
# chain 1
execute as @a[tag=winner,tag=!announced] run tellraw @a [{"selector":"@s"},{"text":" found the treasure first!","color":"gold"}]
# chain 2
tag @a[tag=winner] add announced
Step 4 repeats the message every tick while someone holds the star. This version only checks while nobody has the winner tag and announces once, so you do not have to stop the block by hand. Remove both tags before the next round.
7. Hide three chests
/summon marker 0 64 0 {Tags:["treasure"]}
/summon marker 0 64 0 {Tags:["treasure"]}
/summon marker 0 64 0 {Tags:["treasure"]}
/spreadplayers 0 0 10 30 false @e[tag=treasure]
Spread three markers and you get three chests. The third number (10) is the minimum distance between them, so they do not clump together. Step 2 runs at every marker, so it places all three chests.
Troubleshooting
- No chest: check the marker exists.
- No hints: keep the marker alive during the game.
- Instant winner: clear nether stars before starting.
Check your command
The Command Error Doctor checks common syntax mistakes in your browser.