Player Eat Event

Discussion in 'Plugin Development' started by chenr1, Jun 12, 2012.

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


    Hey guys Just a quick question. is there or is there a Player Eat Event? IF so pleaso post if not please post a workaround. THanks
  2. Offline


    There is no "eat" event. Could you please state what you wish to do clearly so that we can help you.
  3. Offline


    Ok so i need when someone eats soup it heals them.
  4. Offline


    This event is called if a entity is getting back health:

    This event is called, if the entities food level is changed:
  5. Offline


    Unfortunately, I don't think that you can get the food item used for FoodLevelChangeEvent.
    I think your best bet is to listen to PlayerInteractEvent and check the item in their hand to see if it is soup. If it is, remove the soup, add a bowl to their inventory, and give them health. A downside to this might be players could be able to instantly consume soup instead of having to go through the animation. However, I still believe that it is the best option you have.
    MrBluebear3 likes this.
  6. Offline


    This is what you have to do, alternatively you could calculate the amount if ticks it takes for the animation to complete and add a delayed task that heals them after the animation.

    The problem is just that it will still heal them even if they just tap the right button and dont complete the animation.
    But you could possibly add a check to see if the soupbowl is still in their inventory before you heal them, if it is, dont heal them, if its gone, heal them.
    llamasaylol likes this.
  7. Offline


    so... how would one check what item the player has in hand? :S this might be a realy noob question but i just started dealing with plugin (actualy java as a whole) like today so... yeah D:

    anyways i'v been trying to use player.getItemInHand(), which seems to be an ItemStack type of thing. how do i compare it with Material.APPLE for example? or realy, any kind of apple there is xD
  8. Offline


    ItemStack item = player.getItemInHand();
    if (item.getType == Material.APPLE){
    //do stuff
    I doubt that will work because obviously if the food has been eaten, there won't be anything left in his hand.
  9. Offline


    @EnvisionRed oh didn't know there was a .getType() for .getItemInHand()! nice thanks \o/

    however it still doesn't work D: basicaly as i check the item he has in hand when he right-clicks (starts eating) he should still have the item in his hand right? because i want to get the time when he actualy finished eating it (number of that item in his inv decreases) so i trigger something (since there's no PlayerEatEvent)...

    okay so this is what i have so far:

    public void onPlayerClick(PlayerInteractEntityEvent event)
        Player player = event.getPlayer();
        if(player.getItemInHand().getType() == Material.APPLE)
    the "eating" is just for testing purposes. it doesn't show up so... something is wrong?

    EDIT: or perhaps i could simply test when his FoodLevel changes and test if it changed to something higher then what he had before? can that be done? would skip a lot of tests :p
  10. Offline


    I don't think PlayerInteractEntityEvent is what you need here, because he could right click on air or a block, which aren't entities.
    public void onPlayerClick(PlayerInteractEvent event){
    Player player = event.getPlayer();
    if ((event.getAction() == RIGHT_CLICK_BLOCK) || (event.getAction() == RIGHT_CLICK_AIR)){
    if (player.getItemInHand().getType() == MATERIAL.APPLE) {
    //do stuff
  11. Offline


    EnvisionRed oh haha that makes sense xD only problem i get now is it not recognizing RIGHT_CLICK_BLOCK and RIGHT_CLICK_AIR

    do i need to import anything else that's not on this list?:

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
  12. Offline


    My bad, change them to
  13. Offline


    @EnvisionRed right so what i have now is this:

    public void onPlayerClick(PlayerInteractEvent event)
        Player player = event.getPlayer();
        if((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.RIGHT_CLICK_AIR))
            if(player.getItemInHand().getType() == Material.APPLE)
    however when i right click with an apple it doesn't send the test message. does the apple change to another kind entity or something like that when someone starts eating it? :S

    btw i must ask... does me putting u'r name at the beggining of the reply helps or... u know... annoy? i realy dunno cuz i'm new to actualy discussing plugins so maybe u guys don't use it that way? maybe i should stop that

    EDIT: lol nvm! i was missing the @EventHandler -.- idk what it does but fixed it :p thank you very much for your support \o/
  14. Offline


    No, it works the way you think it does. It also gives me an alert that I was tagged in a post so I know that someone has responded to me.
    Two questions for you:
    1. Have you added the eventhandler tag?
    2. Have you registered events from it in your main class?
  15. Offline


    @EnvisionRed right so ugh, if eventhandler tag is this -> @EventHandler so yeah i'v JUST added it and it now works xD but ugh... i have no idea what the second question is about hahaha

    btw, i was wandering, shouldn't

    if(event.getAction() != null){}
    work instead of the big

     if((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.RIGHT_CLICK_AIR)){}
  16. Offline


    No, because there's other actions that are possible, including leftclick events. You don't leftclick to eat food...

    About the registering as a listener part, in your onEnable add:
    getServer().getPluginManager().registerEvents(new Nameofyoureventsclasshere, this);
  17. Offline


    @EnvisionRed oh yeah ofc :p well thanks again for all the support \o/ that's a new hope on my dev path. woot!
  18. Offline


    Just saying, i think if (player.getItemInHand().getType() == MATERIAL.APPLE) {
    should come first, simple because it would cause less lag (rule it out quicker, less IF statements)
    McMhz likes this.
  19. Offline


    I would have just suggested listening for a FoodLevelChangeEvent, then checking the item in the player's hand to see it was mushroom stew.
  20. Offline


    this is the code i used for one of my plugins a while ago for cookies. could be adapted for any other food although i overdid the timing a little :/ should probs be 90L.
    1. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    2. public void onPlayerInteractEvent(PlayerInteractEvent e){
    3. //get all the relative values for comparation
    4. final int l = e.getPlayer().getFoodLevel();
    5. final Player p = e.getPlayer();
    6. final int a = p.getItemInHand().getAmount();
    7. ItemStack i = e.getPlayer().getItemInHand();
    8. if(i.getType().equals(Material.COOKIE)){
    9. //schedule a task to see if they have eaten the cookie(maybe the time could be a little faster idk)
    10. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    11. public void run(){
    12. if(p.getFoodLevel() > l && p.getItemInHand().getAmount() < a){
    13. //do your shizzle
    14. }
    15. },100L);
    16. }
    17. }
    18. }
    bobacadodl likes this.
  21. Offline


    The FoodLevelChangeEvent triggers after you consume the item, so it's impossible to see which item you ate.

    I tried that and it's not working. This is my code:
    1. @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    2. public void onPlayerInteractEvent(PlayerInteractEvent e) {
    3. final Player p = e.getPlayer();
    4. final int l = p.getFoodLevel();
    5. final int a = p.getItemInHand().getAmount();
    6. final int s = p.getInventory().getHeldItemSlot();
    7. final ItemStack i = p.getItemInHand();
    8. p.sendMessage("test1");
    9. if(i.getType() == Material.PUMPKIN_PIE) {
    10. p.sendMessage("test2");
    11. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    12. public void run() {
    13. p.sendMessage("test3");
    14. if(p.getFoodLevel() > l && p.getItemInHand().getAmount() < a && s == p.getInventory().getHeldItemSlot()) {
    15. p.setFoodLevel(p.getFoodLevel() + 2);
    16. p.sendMessage("You have eaten an apple pie!");
    17. } else {
    18. p.sendMessage("You have not eaten an apple pie!");
    19. }
    20. }
    21. }, 100L);
    22. }
    23. }

    I put the test messages for debug purposes. The code doesn't make it to test3.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 26, 2016
  22. TheChugBug
    You do realize you just bumped a year-and-a-half-old thread, right? ;)
  23. Offline


    Lolmewn likes this.
  24. Offline


  25. Offline


    TheChugBug Mate, I know The Gaming Grunts already said this, but I don't want a notification about a post I made two Christmases ago.
Thread Status:
Not open for further replies.

Share This Page