Solved RIGHT_CLICK_BLOCK is called twice in PlayerInteractEvent

Discussion in 'Plugin Development' started by nirpisa201, Apr 26, 2016.

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

    nirpisa201

    Code:
    @EventHandler
        public void PlayerClickBlock(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getAction() == Action.LEFT_CLICK_BLOCK){
                p.sendMessage("left");
            }else if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
                p.sendMessage("right");
            }
        }
    When I right click, I get "right" twice
    Any suggestions?
     
  2. Offline

    timtower Ninja on the waves Moderator

  3. Offline

    krizzdawg

    You are making this run twice. your saying if it isnt not a left click then send the message "right", also your saying if the player right clicks the air then send them the message "right".
    Try to remove the "else" and just create another if like this.
    1. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    2. p.sendMessage("left");
    3. }
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    5. p.sendMessage("right");
    6. }
    7. }
     
  4. Offline

    Konato_K

    @nirpisa201 Someone said here that in 1.9 the event gets called twice for right click for the off hand (the "left" one)
    If you're not in 1.9 then your mouse is a bad person, you registered the listener twice or there is another event handler being a horrible person.

    @krizzdawg The else is perfectly fine there the code won't get executed twice since the second if is part of the else
     
  5. Offline

    Cosmicluck

    @nirpisa201 I just had the same issue, and after looking it up I found out how to fix it.

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            EquipmentSlot e = event.getHand(); //Get the hand of the event and set it to 'e'.
            if (e.equals(EquipmentSlot.HAND)) { //If the event is fired by HAND (main hand)
               //Do stuff
            }
        }
     
    nahkd123 likes this.
  6. Offline

    nirpisa201

    @Cosmicluck there is no function "getHand()" in PlayerInteractEvent
     
  7. Offline

    Cosmicluck

    Are you using 1.9? Cause I had the same issue with 1.9 and that fixed it.
     
  8. Offline

    nirpisa201

    Yes I am using spigot server for minecraft 1.9
     
  9. Offline

    I Al Istannen

    @nirpisa201
    Do you have a 1.9 Spigot jar in the Build Path? (In eclipse: Right click Project -> Properties -> "Java Build Path" on the left -> "Libraries" tab and then look there.

    It is definitly there in 1.9, as you can see here.
     
  10. Offline

    MisterErwin

Thread Status:
Not open for further replies.

Share This Page