Is it possible to detect a player consuming food items?

The idea is that a certain number of random people on the server are identified to have a food allergy – let’s pick wheat for an example. Then, when those players eat any item that contains wheat (bread, cake, and cookie in vanilla MC), they will get the poison effect for a certain amount of time.

This seems to be three steps:

  1. using /scoreboard teams add wheatAllergy to make a team named wheatAllergy,
  2. using /scoreboard teams join wheatAllergy @r[limit=X] to specify X random players and place them on team wheatAllergy, and
  3. somehow identifying when players on wheatAllergy eat any food that contains wheat and applying a poison effect to them.

I assume I would need one command block for each bread, cake, and cookie, but I’m unsure on what those commands would be, exactly. Any ideas?

For bonus points: Same as above, but for contact allergies: i.e., when players on, say, team eggAllergy are hit by a thrown egg, or when players on team dogAllergy touch wolves, tamed or untamed. I feel like contact should be easier than eating, but I’m still unsure of what the command would be.

Answer

You can make scoreboard objectives of type minecraft.used:minecraft.<food item name>, for example

/scoreboard objectives add eatBread minecraft.used:minecraft.bread

The only food item this doesn’t work for is cake, since cake is not eaten in item form. To detect eating cake, use the objective type minecraft.custom:minecraft.eat_slice_cake instead.

As a minor note, it is not necessary, or even a good idea, to determine whether players are allergic using teams. Players can only be on one team at a time; what if you want a player to be allergic to both wheat and fish? Instead, I recommend that you use tags, like so:

/tag @r[limit=X] add wheatAllergy

Then, to detect that a specific player has eaten a food they are allergic to, you can use

effect give @a[tag=wheatAllergy,scores={eatBread=1..}] minecraft:poison 10
scoreboard players reset @a[scores={eatBread=1..}] eatBread

effect give @a[tag=wheatAllergy,scores={eatCake=1..}] minecraft:poison 10
scoreboard players reset @a[scores={eatCake=1..}] eatCake

effect give @a[tag=wheatAllergy,scores={eatCookie=1..}] minecraft:poison 10
scoreboard players reset @a[scores={eatCookie=1..}] eatCookie

# And so on...

Attribution
Source : Link , Question Author : DonielF , Answer Author : AjaxGb

Leave a Comment