TransWikia.com

Test if a player had drank milk

Arqade Asked by TextBasedYoutube on April 10, 2021

I’m trying to make something in minecraft where if the player drinks milk named a specific thing, it executes a command. I’m not good at making datapacks so I haven’t tried anything. How would I do this?

One Answer

First, I set up a scoreboard to detect if any player drinks a milk_bucket:

scoreboard objectives add drank_milk minecraft.used:minecraft.milk_bucket

But this isn't good enough since I need to detect if they drank a milk_bucket with a certain name.

So, I run these commands on a loop:

execute as @a[scores={drank_milk=1},tag=holdingCoolBucket] run say I DRANK A COOL BUCKET
scoreboard players reset @a[scores={drank_milk=1}] drank_milk
tag @a remove holdingCoolBucket
execute as @a[nbt={SelectedItem:{id:"minecraft:milk_bucket",Count:1b,tag:{display:{Name:'{"text":"a very cool bucket"}'}}}}] run tag @s add holdingCoolBucket

Done! Any player that drank a milk_bucket named "a very cool bucket" says "I DRANK A COOL BUCKET" in chat. But let's take a look at how this all works:


The order of the commands is important for this to function, so the commands are not ordered in an intuitive way. So first, let's examine the last command: (which would be the first command if order didn't matter)

execute as @a[nbt={SelectedItem:{id:"minecraft:milk_bucket",Count:1b,tag:{display:{Name:'{"text":"a very cool bucket"}'}}}}] run tag @s add holdingCoolBucket

This is a lot of stuff for a command that means: make all players holding a milk_bucket named "a very cool bucket" run the following command:

tag @s add holdingCoolBucket

This gives a tag of holdingCoolBucket to the player executing the command.

So, to recap, the last command makes all players holding a milk_bucket named "a very cool bucket" give themselves a tag of holdingCoolBucket.


The next command we'll examine is the first one:

execute as @a[scores={drank_milk=1},tag=holdingCoolBucket] run say I DRANK A COOL BUCKET

Again, this command starts off with execute as @a. But not just @a, @a[scores={drank_milk=1},tag=holdingCoolBucket]: all players who have a score of 1 in objective drank_milk and have a tag of holdingCoolBucket.

This command means: make all players (who drank milk and were holding "a very cool bucket") run the following command:

say I DRANK A COOL BUCKET

This is the part you should change to whatever you want.


The second and third command are just commands to reset the tag and score of all players. The only part left to explain is the way the commands are ordered.

The player interactions happen before each command loop. So, when the command loop runs, the player has already drank the milk_bucket and is now holding an unnamed empty bucket. If we were to try and detect if they have a score of 1 in objective drank_milk, we would get a positive result but it's not much help since we can no longer detect if they were holding a named milk_bucket. To get around this, we detect if they were holding it the previous tick.

In the previous tick, the first command can be ignored since the second + third command reset all the players' scores and tags. The last command checks if they are holding the named milk_bucket and gives them a tag.

The command loop ends. Then before the next command loop, the player drinks the bucket and gets the score of 1.

The next command loop starts. The player still has the tag from the previous tick and now has the score. We now know the player drank a milk bucket with the name.

Correct answer by ginkgo on April 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP