Solved PlayerInteractEvent being cancelled

Discussion in 'Plugin Development' started by Dablakbandit, May 5, 2014.

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

    Dablakbandit

    I have this code

    Code:
    @EventHandler(priority = EventPriority.LOWEST)
    public void playerInteract(PlayerInteractEvent event){
    if(!event.isCancelled()){
    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)||event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    ItemStack is = event.getPlayer().getItemInHand();
    System.out.print("works");
    if(is!=null&&is.getType()!=null&&is.getItemMeta()!=null&&is.getItemMeta().getDisplayName()!=null){
    //Other stuff
    }
    }
    }
    }
    It gets called when I right click a block, but not when I right click air it does nothing? The event is somehow cancelled when right clicking air? Any insight would be great.

    Notes: I have no other plugins, and nowhere else is PlayerInteractEvent called.

    Thanks in advanced,
    Dablakbandit
     
  2. Offline

    raGan.

    Yes it is. Try to experimenting with "isCancelled()", "useInteractedBlock()", and "useItemInHand()". Print things and try to set those to different values, because experimentation is priceless in this case.
     
  3. Offline

    Dablakbandit

    raGan.
    It works when right clicking on blocks, it prints out works to console, but when right clicking air it does absolutely nothing at all?

    Thanks,
    Dablakbandit

    raGan.
    It seems as though as if, you right click on air, bukkit decides to cancel it by default? What kind of coding is that...

    Thanks,
    Dablakbandit

    Solved

    By default it is cancelled, to fix:

    Code:
        @EventHandler(priority = EventPriority.LOWEST)
        public void playerInteract1(PlayerInteractEvent event){
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                event.setCancelled(false);
            }
        }
    And have all other plugins check if it is cancelled on priority.LOW or higher

    Thanks,
    Dablakbandit

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Dablakbandit Erm, no? Why would it be cancelled by default? As you implied, that would make no sense at all. Why would they do that?

    As it happens, you can easily tell that it works by using an item that has a right click action. If it works, then the event isn't being cancelled, surely.

    One thing I will say though is that the event doesn't fire when right clicking on air with nothing in hand. I guess because in vanilla that shouldn't do anything, nothing happens.
     
  5. Offline

    Dablakbandit

    AdamQpzm
    But if you have say a Eye of Ender in your hand, that does do something doesn't it?

    Thanks,
    Dablakbandit
     
  6. Dablakbandit Yup. Any item, as long as it's not air-on-air.
     
  7. Offline

    Dablakbandit

    AdamQpzm
    #Edit
    It does trigger but sometimes it is already cancelled

    Code:
    public class Test extends JavaPlugin implements Listener{
     
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler(priority = EventPriority.LOWEST)
        public void onPlayerInteract(PlayerInteractEvent event){
            event.getPlayer().sendMessage("" + event.getAction() + " : " + event.isCancelled());
        }
    }
    returns RIGHT_CLICK_AIR : true
    when clicking on an air block with an item in your hand


    Thanks,
    Dablakbandit
     
  8. Dablakbandit Are you sure that it doesn't and it's not just something that you're doing differently? Because I've been able to use RIGHT_CLICK_AIR in a PlayerInteractEvent.
     
  9. Offline

    Dablakbandit

    AdamQpzm
    As I said ^ (sorry I edited it) it does trigger but it is already cancelled

    Thanks,
    Dablakbandit
     
  10. Offline

    Dablakbandit

Thread Status:
Not open for further replies.

Share This Page