Solved onPlayerInteract Is this bugged? hmm

Discussion in 'Plugin Development' started by Cr4zy[Box], Oct 20, 2013.

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

    Cr4zy[Box]

    I m trying to check if player right clicks with a custom item a block.
    So i m doing that like this:

    Code:java
    1. @EventHandler(priority=EventPriority.LOW)
    2. public void onPlayerUse(PlayerInteractEvent event){
    3. Player p = event.getPlayer();
    4.  
    5. if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)
    6. {
    7. p.sendMessage("Close enough!");
    8. Block target = p.getTargetBlock(null, 50);
    9. if(target == null) return;
    10. try{
    11. if(p.getItemInHand().getItemMeta().hasDisplayName() && p.getItemInHand().getItemMeta().getDisplayName() == "Falling Wand")
    12. {
    13. p.sendMessage("This does not show up!");
    14. }
    15. } catch ( Exception e){}
    16. }
    17. }

    But it isnt giving me the message.
    Something i noticed is that it sends the event twice. ( the "close enough" message shows up twice) and i should probably let u know that the method p.getTargetBlock( , ) is Deprecated
     
  2. Offline

    Cr4zy[Box]


    I dont know if that really matters but i changed the code to match your preferences and as expected it still isnt working.
    By the way i found out that i duplicated the plugins in my folder that explains and solves the twice bug thing.

    Fixed it by doing the following:

    Code:java
    1. @EventHandler(priority=EventPriority.LOW)
    2. public void onPlayerUse(PlayerInteractEvent event){
    3. Player p = event.getPlayer();
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK && p.getItemInHand().getType() == Material.BLAZE_ROD)
    5. {
    6. if(p.getItemInHand().hasItemMeta())
    7. {
    8. try
    9. {
    10. if(p.getItemInHand().getType() == MyItem(1).getType())
    11. {
    12. p.sendMessage("This does not show up!");
    13. }
    14. else
    15. {p.sendMessage("Couldnt get worst!");}
    16.  
    17. }catch(Exception e){}
    18. }
    19. }
    20. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page