How to get food that player is eating?

Discussion in 'Plugin Development' started by hubeb, Jul 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    hubeb

    Im trying to change steak so that it gives you hearts and hunger, how would i go about doing this?

    Any help is appreciated.

    Thanks,
    Hubeb
     
  2. Offline

    tylersyme

    Code:
    if (e.getEntity() instanceof Player)
            {
                Player p = (Player) e.getEntity();
                ItemStack foodBeingEaten = p.getItemInHand();
            }
    That should do it :)
     
  3. Offline

    hubeb

    Yeah im stuck on the giving hearts thing. I believe it would be.
    Code:java
    1.  
    2. player.getHealth() + 4; //Sudo of course 4 being 2 hearts
    3.  

    im not positive though.
     
  4. Offline

    tylersyme

    Code:
    p.setHealth(p.getHealth() + 4);
     
    
    that's how you change a player's health by a certain amount
     
  5. Offline

    hubeb

    tylersyme
    Ah ha great, thanks man.
    This would go in PlayerInteractEvent or the EntityRegainHealthEvent?
     
  6. Offline

    tylersyme

    If you only want this to happen when a player eats a consumable item (such as steak for example)
    Then you would use "FoodLevelChangeEvent"

    However, this event occurs when the player's food level goes up OR down.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    hubeb

    tylersyme
    So what if food is full? My plugin stops health regen. And you can only get back hearts if you eat certain foods.
     
  8. Offline

    tylersyme

    In that case, you would use the PlayerInteractEvent, but you would have to check to see if it's a food item in the player's hand
     
  9. Offline

    hubeb

    tylersyme
    Ok is there a way that i could carry out the eating animation as well. Because if i have to do it through the interact event i would have to.
    Code:java
    1.  
    2. player.getItemInHand.getAmount() -1;
    3.  

    And this would not play the eating animation.
     
  10. Offline

    Jake0oo0

    Isn't there a PlayerConsumeEvent or something like that?
     
  11. Offline

    tylersyme

    no, there really isn't :/. Annoying huh
     
  12. Offline

    ZeusAllMighty11

  13. Offline

    hexagon

  14. Offline

    hubeb

  15. Offline

    ZeusAllMighty11

    hubeb

    The client triggers the animation... It already will have sent the animation, THEN it adds hunger saturation (food bars)
     
  16. Offline

    hubeb

    TheGreenGamerHD
    Is there a way to do it with packets? I want it to display the eat animation, because if the players hunger bar is full it will not allow the player to eat.
     
  17. Offline

    ZeusAllMighty11

    fromgate likes this.
  18. how about on right click with food set their food bar to 19 hunger points? might be easier
     
  19. Offline

    hubeb

  20. :confused: it will? if the players hunger bar isn't full they can eat, so just check for PlayerInteractEvent, if the action is right click air/ right click block and if the item in their hand is a food item, then set their hunger to 19, letting them eat
     
Thread Status:
Not open for further replies.

Share This Page