Is there any way in Minecraft to detect who deposited a diamond into a chest using command blocks?

[BACK]
Is there any way in Minecraft to detect who deposited a diamond into a chest using command blocks?
Posted On: April 26, 2024

So far, I've been able to detect when a diamond is placed into a chest's inventory but how can I detect who placed that diamond?


I'd prefer not to use the nearest player because someone else could potentially be falsely identified. (I have a command chain that trades players emeralds in exchange for experience levels and it also sometimes robs random nearby players.).


Some context: I'm making a race to be the first to deposit a diamond. I could constrain the player accessing the chest and just check who is standing on a block, but if I did that, I could also just test the player's inventory directly. But that seems boring. Adding the chest adds to the drama, I feel. Like, if someone fumbles last minute and deposits dirt on accident, someone else could easily win, which is fun.


My command chain is currently:.


if block x y z chest{Items:[{id:"minecraft:diamond"}]}.


say Diamond Detected.


Edit: The diamonds are mined and as such, I will not necessarily be able to tag them for each player. Another user suggested I could scan the inventories of the users at every tick and tag the diamonds in their inventory with their name. I don't know how to do this, though I believe this is the direction where I will find my answer.


Question from user Robert Talada at gaming.stackexchange.com.


Answer:

If you don't know who has each diamond.


Minecraft 1.20.5 changed the syntax of some of the commands a bit but the concept behind this answer should still be valid.


Step one.


Give each player a unique scoreboard ID. See this related question for how to do that.


Step two.


Run the following command in a repeating, always active command block:.


execute as @e[type=item,nbt={Item:{id:"minecraft:diamond"}}] run data merge entity @s {Item:{tag:{Unclaimed:1b}}.


This gives each diamond item on the ground a custom Unclaimed tag, allowing you implement Step 3 without 30+ command blocks per player.


Step three.


Once for each player who could be playing the map, run in a repeating command block:.


clear @a[scores={Id=1}] diamond{Unclaimed:1b} 1.


and then, in a conditional chain command block that the repeating command block is pointing to.


give @a[scores={Id=1}] diamond{PlayerOwns:1b} 1.


Repeat this for each player, using scores={Id=2} and PlayerOwns:2b for the second player and so on.


Step four.


Now that each diamond has a tag describing whose it is, you can use the technique described in ExpertCoder14's answer to determine who put a diamond in the chest.


This has a known bug that if Alice puts a diamond in a container, and then Bob takes the diamond out of the container (and never drops and picks it up again), the diamond is still interpreted as belonging to Alice -- but I actually like that if I understand what you're doing correctly; if you stole someone else's diamond from a chest instead of mining your own, that means you're trying to cheat… karma!.


If you want a more seamless experience, you may want to interest yourself in player inventory modification. With the method above, the diamond is visibly replaced by a different one, you can use this to make a seamless replacement.


Answer from user pppery at gaming.stackexchange.com.


[BACK]
Is there any way in Minecraft to detect who deposited a diamond into a chest using command blocks?
Posted On: April 26, 2024

So far, I've been able to detect when a diamond is placed into a chest's inventory but how can I detect who placed that diamond?


I'd prefer not to use the nearest player because someone else could potentially be falsely identified. (I have a command chain that trades players emeralds in exchange for experience levels and it also sometimes robs random nearby players.).


Some context: I'm making a race to be the first to deposit a diamond. I could constrain the player accessing the chest and just check who is standing on a block, but if I did that, I could also just test the player's inventory directly. But that seems boring. Adding the chest adds to the drama, I feel. Like, if someone fumbles last minute and deposits dirt on accident, someone else could easily win, which is fun.


My command chain is currently:.


if block x y z chest{Items:[{id:"minecraft:diamond"}]}.


say Diamond Detected.


Edit: The diamonds are mined and as such, I will not necessarily be able to tag them for each player. Another user suggested I could scan the inventories of the users at every tick and tag the diamonds in their inventory with their name. I don't know how to do this, though I believe this is the direction where I will find my answer.


Question from user Robert Talada at gaming.stackexchange.com.


Answer:

If you don't know who has each diamond.


Minecraft 1.20.5 changed the syntax of some of the commands a bit but the concept behind this answer should still be valid.


Step one.


Give each player a unique scoreboard ID. See this related question for how to do that.


Step two.


Run the following command in a repeating, always active command block:.


execute as @e[type=item,nbt={Item:{id:"minecraft:diamond"}}] run data merge entity @s {Item:{tag:{Unclaimed:1b}}.


This gives each diamond item on the ground a custom Unclaimed tag, allowing you implement Step 3 without 30+ command blocks per player.


Step three.


Once for each player who could be playing the map, run in a repeating command block:.


clear @a[scores={Id=1}] diamond{Unclaimed:1b} 1.


and then, in a conditional chain command block that the repeating command block is pointing to.


give @a[scores={Id=1}] diamond{PlayerOwns:1b} 1.


Repeat this for each player, using scores={Id=2} and PlayerOwns:2b for the second player and so on.


Step four.


Now that each diamond has a tag describing whose it is, you can use the technique described in ExpertCoder14's answer to determine who put a diamond in the chest.


This has a known bug that if Alice puts a diamond in a container, and then Bob takes the diamond out of the container (and never drops and picks it up again), the diamond is still interpreted as belonging to Alice -- but I actually like that if I understand what you're doing correctly; if you stole someone else's diamond from a chest instead of mining your own, that means you're trying to cheat… karma!.


If you want a more seamless experience, you may want to interest yourself in player inventory modification. With the method above, the diamond is visibly replaced by a different one, you can use this to make a seamless replacement.


Answer from user pppery at gaming.stackexchange.com.


[BACK]

What is the definition of a pug? What is the definition of a pug?

Posted On: February 2, 2023
What is a "pug"? I've heard multiple instances of it used in many MMORPG and FPS games. I've heard of games called "pubs", but those I think reference public games. Is t...[More]


What is Effect Hit Rate? What is Effect Hit Rate?

Posted On: May 15, 2023
Some relics have an 'Effect Hit Rate' stat. What effect is being referred to here and at what point is the hit/no-hit RNG evaluated? Question from user LT_ at gaming.stackexchange.com. A...[More]


What is the music in the Dazed and Confused mission in Far Cry 4? What is the music in the Dazed and Confused mission in Far Cry 4?

Posted On: January 24, 2023
I've just been playing the Dazed and Confused level and was wondering what the music was. It's a mission with Yogi and Reggie done under the influence of drugs. I was curious if it was a no...[More]


WHY IS SALAMI A WEAPON?!? WHY IS SALAMI A WEAPON?!?

Posted On: March 25, 2025
i don’t even know what to say…should i start beating up the steel watch with salami now????Question from user Bellaswannabe at BaldursGate3 at reddit.com.Answer:Yes start beating people up with th...[More]


Risk of Rain 2 is not reading my controller inputs? Risk of Rain 2 is not reading my controller inputs?

Posted On: April 16, 2023
I've run into an issue with Risk of Rain 2 on my steam deck, where it no longer accepts controller inputs. In settings, under controller options, most inputs are labeled with an error saying there...[More]


How does durability work in Breath of the Wild? How does durability work in Breath of the Wild?

Posted On: January 15, 2023
In the tool tips, the game always says to "keep an eye on your durability" or that certain actions might "knock off some durability", but so far I have only seen 3 stages of durabi...[More]


Is it just me or is there something wrong with Professor Garlick? Is it just me or is there something wrong with Professor Garlick?

Posted On: September 5, 2023
Is it just me or is there something wrong with Professor Garlick? Was it really just accidents her former herbalism teacher suffered, or did Garlick have something to do with it? I mean, she fel...[More]


How well does the Nintendo Switch perform in terms of graphics compared to the Xbox One and PlayStation 4? How well does the Nintendo Switch perform in terms of graphics compared to the Xbox One and PlayStation 4?

Posted On: February 28, 2023
I'm interested in playing Hogwarts Legacy on Nintendo Switch. Since the Switch is a mobile console, I'm a little concerned the game's graphics might not as good as on the XBOX or PlaySt...[More]


Whats the best possible score on the credits mini game? Whats the best possible score on the credits mini game?

Posted On: November 7, 2023
In the credits minigame you get a point for every name you hit and lose points whenever an enemy hits you. Since there's only a limited number of names to hit, what's the best possible score y...[More]


What is the difference between Breath of Fire III for the PSX and for the PSP (or PSPGo)? What is the difference between Breath of Fire III for the PSX and for the PSP (or PSPGo)?

Posted On: January 14, 2023
I am aware that some games, (like the early Final Fantasy titles, Disgaea) which are on PSP (more appropriately, PSN) are later versions with a small amount of extra content. Normally it is fairly eas...[More]