Solved onPlayerEat Event?

Discussion in 'Plugin Development' started by inventorman101, Jul 12, 2013.

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

    inventorman101

    How to I trigger an event when a player eats? Also how do I add more hunger when the item is eaten and cancel the Hunger effect (Because it is rotten flesh) Thank you!
     
  2. Offline

    xTrollxDudex

    inventorman101
    Easy if you look into the javadocs. It's PlayerConsumeItemEvent
     
  3. Offline

    Tirelessly

    PlayerItemConsumeEvent

    Guess you didn't look into the javadocs.

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

    xTrollxDudex

    Tirelessly
    Actually I did. You know those moments when you're about to do something but then you're like wait what?
     
  5. Offline

    inventorman101

    Actually I did what package was it in??? btw Thanks guys!
     
  6. Offline

    xTrollxDudex

  7. Offline

    inventorman101

    Lol, I looked there XD whatever. So exactly how do I setup this event method? And also how do I add to the hunger bar. xTrollxDudex
     
  8. Offline

    Tirelessly

    You have 4 plugins and don't know how to set up an event..?
     
    TheGreenGamerHD likes this.
  9. Offline

    xTrollxDudex

    inventorman101 tag Tirelessly because I might screw up(like in the post above)
    Code:java
    1. private final int foodBars = 4
    2. @EventHandler
    3. public void onRottenConsume(PlayerItemConsumeEvent e){
    4. Player p = e.getPlayer();
    5. if(e.getItem() == new ItemStack(Material.ROTTEN_FLESH)){
    6. e.setCancelled(true);
    7. p.setFoodLevel(player.getFoodLevel() + foodBars);
    8. }
    9. }

    I've heard that the animation still plays but if you cancel it it will only cancel the food level going up not the animation. Food level is calculated from 0-20, where the odd numbers represent something and a half hunger.
     
  10. Offline

    inventorman101

    I don't think it worked it was just the same as eating regular Rotten Flesh

    xTrollxDudex
    Tirelessly

    Code:java
    1.  
    2. public class EventListener extends JavaPlugin implements Listener
    3. {
    4. private final int foodBars = 6;
    5. @EventHandler
    6. public void onRottenConsume(PlayerItemConsumeEvent e)
    7. {
    8.  
    9. Player p = e.getPlayer();
    10. if(e.getItem() == new ItemStack(Material.ROTTEN_FLESH))
    11. {
    12. ItemMeta im = e.getItem().getItemMeta();
    13. List<String> lore = im.getLore();
    14. if(lore != null)
    15. {
    16. if(e.getItem().getItemMeta().getDisplayName().equals("Boiled Flesh"))
    17. {
    18. e.setCancelled(true);
    19. p.setFoodLevel(p.getFoodLevel() + foodBars);
    20. }
    21. }
    22. }
    23. }
    24.  
    25. }
    26.  


    I have done several Events but never have done anything food related...

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

    xTrollxDudex

    inventorman101
    Is this a joke or are you being serious? Where's your onEnable() and disable?
     
    Scr3am likes this.
  12. Offline

    Scr3am

    That's what I was wondering.
     
    xTrollxDudex likes this.
  13. Offline

    xTrollxDudex

    Scr3am
    I wasn't really expecting an answer lol but that was for "effect"
    By the way, I've never worked with food events either-look at https://jd.bukkit.org/dev/apidocs/ for help(@inventorman101 )
     
  14. Offline

    inventorman101

  15. Offline

    xTrollxDudex

  16. Offline

    ERROR372

  17. Offline

    Scr3am

    ...But it extends JavaPlugin...?
     
  18. Offline

    inventorman101

    I dunno but it still works so... why does it matter ANYWAY back on topic, My code above does not work so can anyone figure out why?
     
  19. Offline

    xTrollxDudex

  20. Offline

    inventorman101

    Sure xTrollxDudex

    Code:java
    1.  
    2. public void onEnable()
    3. {
    4. getServer().getPluginManager().registerEvents(new EventListener(), this);
    5. PluginDescriptionFile pdfFile = this.getDescription();
    6. logger.info(pdfFile.getName()+ " Version " + pdfFile.getVersion()+ " Has Been Enabled!");
    7.  
    8.  
    9. ItemStack item = new ItemStack(Material.ROTTEN_FLESH, 1);
    10. ItemMeta meta = item.getItemMeta();
    11. meta.setDisplayName("Boiled Flesh");
    12. ArrayList<String> lore = new ArrayList<String>();
    13. lore.add(ChatColor.BLUE + "+3 Hunger");
    14. meta.setLore(lore);
    15. item.setItemMeta(meta);
    16.  
    17. FurnaceRecipe boiledFlesh = new FurnaceRecipe(new ItemStack(item), Material.ROTTEN_FLESH);
    18. getServer().addRecipe(boiledFlesh);
    19. }
    20.  
     
  21. Offline

    Scr3am

    Lulz just kidding, I know what you meant.
     
    ERROR372 likes this.
  22. Offline

    inventorman101

    I meant it still works with extends JavaPlugin. My listener is not working

    Dang it! Bukkit is out of lava :( https://jd.bukkit.org/dev/apidocs/

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

    xTrollxDudex

    inventorman101
    Remove your if(lore != null) and your List lore.
    Now, remove the if statement checking for the display name.
    Replace it with
    if(im.getDisplayName().equals("Boiled Flesh") && im.getLore() != null)
     
  24. Offline

    inventorman101

  25. Have you tried this?
    Code:java
    1. if(event.getItem().getType() == Material.ROTTEN_FLESH){
    2. event.setCancelled(true);
    3. event.getPlayer().removePotionEffect(PotionEffectType.HUNGER);
    4. }
     
  26. Offline

    inventorman101

    mollekake
    I will try this, Thanks

    mollekake
    That didn't seem to work, but I also need to check for a special Display name to see if says "Boiled Flesh"

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

    MrMag518

    When comaparing an ItemStack to another, you don't only check if the itemstacks is the same material, you also check if it has the same item meta and durability etc, that's why it's not working.

    Also, don't extend JavaPlugin in any other class other than your main one..
     
  28. Offline

    inventorman101

    Nvm, it works! Thanks guys :)
     
Thread Status:
Not open for further replies.

Share This Page