7

This question already has an answer here:

Is there some kind of command that I could use to test when the player eats something, then after the player has eaten that thing, give them 2 hearts? I have a feeling it might be a testfor command....

marked as duplicate by Gigazelle, arghtype, Schism, Kappei, Dragonrage Aug 11 '18 at 19:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

Similarly to how you might prevent someone from using a diamond sword, the easiest way of doing this in vanilla is to use scoreboards. Unfortunately, there is no way to check for all foods with a single scoreboard objective; you'll have to set up an objective for each food item. Additionally, there was a bug in previous versions that prevents doing what you asked, and you'll need to use 1.8 snapshots or later.

For this example, I'll use bread, but you need to cover each food item. First set the scoreboard objectives for each food type to be tracked:

/scoreboard objectives add ateBread stat.useItem.minecraft.bread

You don't need to do this from a command block, any op can run the command.

Next, for each food item, you'll need a pair of command blocks, with the first of the pair feeding into the second with a comparator. The first looks for players that have eaten a food item, and gives them 2 hearts (4HP), the second resets those players scoreboard objective for that food item:

effect @a[score_ateBread_min=1] 6
scoreboard players set @a[score_usedBread_min=1] usedBread 0

Finally, hook all the command block pairs to a fast clock, and you're set.

Now there are some drawbacks here. First, if their hunger bar is full, they won't be able to eat, and thus won't be able to regenerate using this method. Second, again if their hunger bar is full (or nearly full), they'll regenerate naturally, which may not be the desired outcome. The following video provides a way around this, and will also set up everything from a single command block.

It does a few things differently than what I'm showing above, but the end result is the same.

  • Thanks for your help! I think it is probably pretty obvious as to why I want to know this. – DatEpicCoderGuyWhoPrograms Apr 20 '14 at 2:40
  • I also just had this idea where it tests for food in a players hand, then removes said food if the players health isn't at max, and gives the player some health. – DatEpicCoderGuyWhoPrograms Apr 20 '14 at 2:43
  • could you give the link for the vid? The video won't load on my crappy iphone. – DatEpicCoderGuyWhoPrograms Apr 20 '14 at 2:45
  • Checking if a player is holding a food item is possible, but it's much harder, and is too complicated to easily implement. As for the video: youtu.be/BUxlOvU4P94 – MBraedley Apr 20 '14 at 3:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.