Right-clicking air with nothing in your hand. Can you intercept that?

Discussion in 'Plugin Development' started by Andred, Jul 11, 2011.

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

    Andred

    I noticed that the RIGHT_CLICK_AIR type of PlayerInteractEvent is not called when you right-click air while your hand is empty. Is there any way to intercept that click? (I seem to remember this being a problem once before that could not be solved... hmm.)
     
  2. Offline

    ItsHarry

    Use PlayerInteractEvent
     
  3. Offline

    DrBowe

    Cant you just use
    Code:java
    1.  
    2. onPlayerInteract(PlayerInteractEvent event){
    3. if(event.getAction() != Action.RIGHT_CLICK_AIR)
    4. return;
    5. if(event.getItem() == null){
    6. //do stuff
    7. }
    8. }
    9.  
     
  4. Offline

    Hretsam

    He just stated that wont work

    Try something like playerAnimation?
     
  5. Offline

    DrBowe

    I find it hard to believe that the event wont fire if you have nothing in your hand, as I use left-clicks with nothing in the player's hand in one of my current projects.
    I'll go test this now

    EDIT:
    That is very bizarre. Is that a known issue, or are they just physically incapable of having that event fire when the player has nothing in their hand?
     
  6. Offline

    Tiax

    Yeah you're right - bare hands don't seem to trigger onPlayerInteract at all when right clicking air :/
     
  7. Offline

    ItsHarry

    Oops, that;s weird
     
  8. Offline

    Shamebot

    I noticed that rightclicking air sometimes doesn't work, but don't know if it's related to the item you're holding.
     
  9. Offline

    Tiax

    onPlayerAnimation won't fire on right click either - just tried.
     
  10. Offline

    Hretsam

    I'm looking at the code now, and the event is not called if your itemInHand is empty (if i'm not mistaken the inventoryslot is null when there is nothing in it)
    Here the a part of the CraftBukkit code i am talking about: (NetServerHandler line 509)
    Code:
    ...
    ItemStack itemstack = this.player.inventory.getItemInHand();
            boolean flag = worldserver.weirdIsOpCache = worldserver.dimension != 0 || this.minecraftServer.serverConfigurationManager.isOp(this.player.name); // CraftBukkit
    
            if (packet15place.face == 255) {
                if (itemstack == null) {  // <-----
                    return;
                }
    
                // CraftBukkit start
                int itemstackAmount = itemstack.count;
                PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(this.player, Action.RIGHT_CLICK_AIR, itemstack);
                if (event.useItemInHand() != Event.Result.DENY) {
                    this.player.itemInWorldManager.useItem(this.player, this.player.world, itemstack);
                }
    ...
     
  11. Offline

    DrBowe

    That's very strange. I'm shocked there's not something on the repo, if this event is physically incapable of being called...
     
  12. Offline

    Andred

    Interesting, interesting.
     
  13. Offline

    ItsHarry

    Indeed, very interesting.
     
  14. Offline

    mooman219

    How does Mcmmo do it then?

    When i Right click while using it, it registers the event
     
  15. Offline

    DrBowe

    Actually, that's an excellent question! :/
    My guess would be that it uses some fancy coding involving CraftBukkit, to direcly bypass the built-in "if the hand is empty, don't fire" boolean.

    @nossr50
    If you'd like to share your secrets, we'd be very grateful ;)
     
  16. Offline

    nossr50

    Actually it does not work in mcMMO, right clicking to activate Berserk on air doesn't trigger, it only works on blocks. It works with the other tools obviously, but thats because you are not bare handed.

    This is a bug I reported to bukkit ages ago and still hasn't been fixed
     
    MusicalCreeper01 likes this.
  17. Offline

    DrBowe

    Huh. People should vote this up, honestly. There's no reason why this event shouldnt be fired wihout an item. :/
    It may seem arbitrary, for such a small, specific request...but it can ruin concepts for certain people.
     
  18. Offline

    Hretsam

    I did some testing, and from the looks of it, the bug is client side.
    It never gets a packet if there is nothing in the player's hand..
    So you cant do much about it, till Mojang fixes/adds that.
     
  19. Offline

    DrBowe

    @Hretsam
    Bummer. Oh well, it's not that important. Just a minor inconvenience.
     
  20. I noticed this with NukeNow. Theres no way to fix it? darn!
     
  21. Offline

    Matterz

    I also have a problem with right clicking air not triggering the event even if the player's hand isn't empty...
     
  22. Offline

    Afforess

    Looking at MCP client code:

    Code:
    				itemStack var9 = this.thePlayer.inventory.getCurrentItem();
    				if(var9 != null && this.playerController.sendUseItem(this.thePlayer, this.theWorld, var9)) {
    					this.entityRenderer.itemRenderer.func_9450_c();
    				}
    
    sendUseItem is where the packet is sent.

    Seems easy to remove the null item check for BukkitContrib clients.
     
  23. Offline

    Jucko13

    srry for bumping such an old thread, but there is an second problem. Not only the right click in the air with your bare hands but also the RightClick on a block that is made by SendBlockChange won't fire the PlayerInteractEvent...
    and it doesn't even matter if you hold something in your hand or not...
    this is so enoying, now i have to rewrite the half of my plugin because i need to use something else...
     
  24. Offline

    desht

    Not sure that's a bug, the block you see in that case isn't real.

    BTW, Right-clicking air does in fact work now - you get a null clicked block from event.getClickedBlock().
     
  25. Offline

    Firefly

    Unless I'm missing something it should be as simple as:

    if (event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == Material.AIR) {
    //Do code here
    }

    EDIT: I haven't actually tested this, but I doubt it would work since Bukkit probably can't return the event a null ItemStack.
     
  26. Offline

    krisdestruction

Thread Status:
Not open for further replies.

Share This Page