Solved PlayerDrinkEvent workaround does not work as required

Discussion in 'Plugin Development' started by Robin Bi, Aug 16, 2014.

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

    Robin Bi

    Hey there :)


    So for the minigame I'm currently working on i need to check for a player drinking. Unfortunately i could not find a PlayerDrinkEvent or so, so i googled and found this thread.
    According to it, i wrote the following code:

    Code:java
    1. @EventHandler
    2. public void onConsumeItem(PlayerInteractEvent pie) {
    3. if (!(pie.getAction() == Action.RIGHT_CLICK_AIR || pie.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    4.  
    5. final Player p = pie.getPlayer();
    6.  
    7. if (p.getItemInHand() == null) return;
    8. if (!(p.getItemInHand().getType() == Material.MILK_BUCKET)) return;
    9.  
    10.  
    11. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. @Override
    13. public void run() {
    14. if (p.getItemInHand() != null && p.getItemInHand().getType() == Material.MILK_BUCKET) {
    15. p.getInventory().remove(p.getItemInHand());
    16. System.out.println("Success");
    17. }
    18. }
    19. }, 28);
    20. }



    My delay is 28 ticks because i figured out that drinking takes a bit more than 1:38 seconds what is 27,6 ticks.

    The Syso is a feedback for me so i know that the player did not just consume the milk.

    And it almost works fine! Yeah, it might take a lot of ressources when many players drink a bucket of milk, but let's overlook this. My problem is that i cannot test for the player still _drinking_. If the player was only right clicking with the milk in his hand and then released the mouse button, the milk will be removed even though he actually is not drinking anymore.


    I hope someone has an idea on how to fix this :3
    ~Robin Bi
     
  2. Offline

    JustinsCool15

    Robin Bi
    Why are you using a PlayerInteractEvent? Use a PlayerItemConsumeEvent
     
    Robin Bi likes this.
  3. Offline

    Robin Bi

    JustinsCool15 mhmm, did not know this was implemented, thought the thread would still be up to date... Thanks ;)
     
  4. Offline

    DinosParkour

    JustinsCool15 likes this.
Thread Status:
Not open for further replies.

Share This Page