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]

Should I get Hogwarts Legacy for Console or PC? Should I get Hogwarts Legacy for Console or PC?

Posted On: May 1, 2023
Looking to buy this game but debating between Pc or Xbox. (Don’t have a steam deck) but leaning toward Xbox so I can play on the couch. Thoughts? Question from user mattgm1995 at HogwartsLega...[More]


Is Hearthstone a game-within-game? Is Hearthstone a game-within-game?

Posted On: August 25, 2023
Hearthstone's visuals suggest that the game is played in a tavern in the World of Warcraft universe on a (possibly magical) game board. Is Hearthstone a game-within-game in WoW? When you play H...[More]


What happens if you transfer a fully EV trained LVL 100 Pokémon from Gen 2 to Pokémon Home? What happens if you transfer a fully EV trained LVL 100 Pokémon from Gen 2 to Pokémon Home?

Posted On: February 16, 2023
Basically, on modern gen Pokémon games, you can only EV train and max up to 2 EV stat at most (255-255 or 252-252-4). But in Pokémon Crystal Gen 2, you could EV train and max all EV stat...[More]


What benefit does Clash Of Clans upgrading walls have? What benefit does Clash Of Clans upgrading walls have?

Posted On: May 8, 2023
The cost for a level one to level two wall upgrade seems huge - 1000 gold per segment. The upgrade screen displays an increase in hitpoints but is this really noticeable? Can a wall breaker still one ...[More]


What is a Turtle Goal in Rocket League? What is a Turtle Goal in Rocket League?

Posted On: May 7, 2023
While playing ranked doubles, I received 20 points for a turtle goal. . What is a turtle goal? Question from user HazyKingdom at gaming.stackexchange.com. Answer: When you score a goal by ...[More]


Can you cast a Patronus Charm in Hogwarts Legacy? Can you cast a Patronus Charm in Hogwarts Legacy?

Posted On: July 19, 2023
Is it possible to use the Patronus Charm in Hogwarts Legacy? If yes, then where and how? Question from user Shaun Roselt at stackexchange. Answer: No. According to an article on videogamer...[More]


Do enemies only drop food after beating the game? Do enemies only drop food after beating the game?

Posted On: October 17, 2023
I've never seen enemies drop food until I started Loitering. Were those drops unlocked by beating the game / seeing the credits? And is it only because I'm Loitering, instead of going to Ne...[More]


In Zelda TOTK, Is there a way to select a material for all arrows? In Zelda TOTK, Is there a way to select a material for all arrows?

Posted On: May 18, 2023
When aiming a bow, pressing ↑ opens the Materials list "Select a material to attach.". Is there a way to avoid having to open this menu for every arrow that you fire, if you wanted t...[More]


Can I skip the launcher when launching Baldurs Gate 3 from Steam? Can I skip the launcher when launching Baldurs Gate 3 from Steam?

Posted On: July 12, 2023
I don't want to have to go through the "Larian Launcher" when I want to play Baldur's Gate 3 in Steam. How can I skip it? Question from user Oak at stackexchange. Answer: To...[More]


Addicted to the game Addicted to the game

Posted On: September 14, 2023
Got the game and ps5 recently. I always try to limit my playtime but alwaysss end up playing for sooooo long! Its super addictive and fun!! Where’s my hufflepuffs?!. Question from user Fiery-...[More]