Trying to manually trigger PlayerItemConsumeEvent

Discussion in 'Plugin Development' started by Paxination, Dec 25, 2013.

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

    Paxination

    Code:java
    1. public boolean isFood(Material material){
    2. if(material.equals(Material.COOKED_BEEF)){
    3. return true;
    4. }
    5.  
    6. if(material.equals(Material.APPLE)){
    7. return true;
    8. }
    9.  
    10. if(material.equals(Material.COOKED_CHICKEN)){
    11. return true;
    12. }
    13.  
    14. if(material.equals(Material.COOKED_FISH)){
    15. return true;
    16. }
    17.  
    18. if(material.equals(Material.MELON)){
    19. return true;
    20. }
    21.  
    22. if(material.equals(Material.BAKED_POTATO)){
    23. return true;
    24. }
    25.  
    26. if(material.equals(Material.BREAD)){
    27. return true;
    28. }
    29.  
    30. if(material.equals(Material.COOKIE)){
    31. return true;
    32. }
    33.  
    34. if(material.equals(Material.MUSHROOM_SOUP)){
    35. return true;
    36. }
    37.  
    38. if(material.equals(Material.GRILLED_PORK)){
    39. return true;
    40. }
    41. return false;
    42. }
    43.  
    44. @EventHandler(priority = EventPriority.LOWEST)
    45. public void OnPlayerAttackAnimal(EntityDamageByEntityEvent event){
    46. if(event.getDamager() instanceof Player && event.getEntity() instanceof Player){
    47. Player medic = (Player) event.getDamager();
    48. Player player = (Player) event.getEntity();
    49. if(isFood(medic.getItemInHand().getType())){
    50. Bukkit.getServer().getPluginManager().callEvent(new PlayerItemConsumeEvent(player,medic.getItemInHand()));
    51. return;
    52. }
    53. }


    So the above code isnt working. No errors. not sure if the consume event is firing like im trying to make it.
     
  2. Offline

    xTigerRebornx

    Paxination I'd reccomend creating a new variable for the PlayerItemConsumeEvent, then callEvent() on that vartiable, so that you can try and get some data from it and print a debug message to check if it fires
     
  3. Offline

    Paxination

    xTigerRebornx

    That event wasnt working, im assuming since we have pvp disabled and other things.

    Code:java
    1. @EventHandler(priority = EventPriority.LOWEST)
    2. public void OnPlayerInteractEntity(PlayerInteractEntityEvent event){
    3. if(event.getRightClicked() instanceof Player){
    4. if(isFood(event.getPlayer().getItemInHand().getType())){
    5. Player player = (Player) event.getRightClicked();
    6. Bukkit.getServer().getPluginManager().callEvent(new PlayerItemConsumeEvent(player,event.getPlayer().getItemInHand()));
    7. plugin.getServer().broadcastMessage("some one was fed");
    8. }
    9. }


    Code:java
    1. @EventHandler(priority = EventPriority.LOWEST)
    2. public void OnPlayerEat(PlayerItemConsumeEvent event){
    3. plugin.getServer().broadcastMessage("player eating");
    4. }


    How ever, it fires with PlayerInteractEntityEvent, but, the player does not consume the item. I am trying to make it so if you smack a player with food in your hand it heals them just like if they ate it. But its not working.
     
  4. Offline

    xTigerRebornx

    Paxination Firing the event and making a player eat something are 2 different things. Firing the event makes the server think that the player has eaten something, but you still have to actually do stuff to make them eat it. You could play the eating sound, give them hunger, and remove one of the item
     
    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page