PlayerInteractEvent when using a bow?

Discussion in 'Plugin Development' started by FrozenBrain, Jul 17, 2011.

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

    FrozenBrain

    Hey,

    I'm trying to prevent people from shooting arrows. Therefore I register the PlayerInteractEvent and check if the player has a bow selected. This works great if the player rightclicks a block with his bow but not if he rightclicks in the air. The onPlayerInteract method is just not called.

    Is this craftbukkits or my fault?

    Thanks,
    Frozen
     
  2. Offline

    beatcomet

    Source?
     
  3. Offline

    Tster

    It is somethig like
    public void onPlayerInteract(PlayerInteractEvent event)
    if (event.getPlayer.getIteminHand.gettypeid == 261 && (event.getaction == action.rightclickblock || event.getaction == action.rightclickair)
    Do up formatting and that should work
     
  4. Offline

    FrozenBrain

    Ooops, sorry.
    Code:
    public void onPlayerInteract(PlayerInteractEvent event) {
    		if(event.isCancelled()) return;
    		Player player = event.getPlayer();
    		ItemStack item = event.getItem();
    		if(item == null) return;
    
            player.sendMessage(item.getType().name());
        }
    As I said, it works if I rightclick a block but not if I rightclick air.
     
  5. Offline

    Shamebot

  6. Offline

    DrBowe

    I actually ran into an issue with cancelling a bow-fire event not too long ago, and while I'm not sure if this is the same issue as yours, I fixed it by upping the priority of PLAYER_INTERACT.

    Here's the actual code I used to cancell it:
    Code:java
    1.  
    2. if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    3. event.getPlayer().sendMessage("You have right clicked");//debug
    4. if(event.getPlayer().getItemInHand().getType().equals(Material.BOW))
    5. {
    6. event.getPlayer().sendMessage("No shooting for you!");//debug 2
    7. event.setCancelled(true);
    8. }
    9.  
     
  7. Offline

    FrozenBrain

    Priority is already highest.

    Looks like it doesn't work because isCancelled returns true (I have not cancelled it before). However, the arrow is still shot. Any ideas?
     
  8. Offline

    Shamebot

  9. Offline

    FrozenBrain

    Last edited by a moderator: May 17, 2016
Thread Status:
Not open for further replies.

Share This Page