Player interact event getHand();

Discussion in 'Spigot Plugin Development' started by Webmets, Apr 20, 2017.

  1. Hi, for a plugin im working on, I need to detect right click with the offhand. now as easy as it sounds, I check if the action is RIGHT_CLICK_BLOCK or RIGHT_CLICK_AIR, and if so check if the event.getHand(); is OFF_HAND. when I click in mid air, it all works as expected. but if I click on a block instead, it will print out both HAND and OFF_HAND (Im trying to use some debug Syso)
     
  2. I'm sorry, but I don't really understand what your issue is. The PlayerInteactEvent's description says "potentially fired once for each hand". What do you require help with?
     
  3. Basically. im doing a PlayerInteractEvent, and printing out the getHand();
    Code (Text):
    public void onClick(PlayerInteractEvent e) {
        if (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK){
            return;
        }
        System.out.println(e.getHand().toString());
    }
    and when I click in the air, the result is OFF_HAND.
    but when I click on the ground, the result is both HAND and OFF_HAND.
    and when I took a look at the spigot Java doc's, it said nothing about potentially firing for both hands?
     
  4. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerInteractEvent.html
    "Represents an event that is called when a player interacts with an object or air, potentially fired once for each hand. The hand can be determined using getHand().
    This event will fire as cancelled if the vanilla behavior is to do nothing (e.g interacting with air)"
    [​IMG]
     
    • Informative Informative x 1
  5. Just run a quick if statement checking if it's the off hand or not
     
  6. How can I go about doing that, if the event is triggered for each hand?
     
  7. So I looked around and I guess Hand is an equipment spot? I did some testing in eclipse, and this appears to work haven't tried it in game. Just add this into your PlayerInteractEvent method
    Code (Java):
    if(e.getHand() == EquipmentSlot.HAND){
          return;
      }
     
  8. If i do that, but with OFF_HAND. And click with my hand it still triggers. And thats the problem I have
     
  9. I'm a bit confused as to what's going on but I'll try my best.

    No matter what you do, the event is going to fire right? If you add the if statement I put (depending on how you want it, not sure,) at the very top of the code, it should stop all other code from running. Is that not happening? I'm a bit confused
     
  10. Il post full code + a gif of whats wrong in a little bit