Catch left click on player

Discussion in 'Plugin Development' started by matejdro, Jan 31, 2012.

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

    matejdro

    Any idea how to catch left click on player?

    onPlayerInteract will only trigger right click, when you standing near player and hitting him, same with onPlayerInteractEntity

    onPlayerDamage will not work if PvP is turned off or player is in creative mode.
     
  2. Offline

    hammale

    u sure it wont work with left clicking? im pretty sure ive done it before like this:
    Code:
    if (event.getAction() == Action.LEFT_CLICK_AIR)
    
    but im not sure...
     
  3. Offline

    matejdro

    Only thing I have in event is testing message, so it should trigger for every action.
     
  4. Offline

    Steeveeo

    Have you tried onPlayerDamage with priority set to EventPriority.LOWEST? That will make it run before most plugins. I am pretty sure events like that still traverse down the priority list, though, so even if event.isCancelled() is true, you should still be able to see it as it's passed to your plugin.

    I am curious as to how your listener is setup, mind posting the code?
     
  5. Offline

    matejdro

    AFAIK player damage even won't even get triggered if pvp is turned off in server.properties or player have creative.

    I'm just printing out simple line to test events like this:
    Code:java
    1. @Override
    2. public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    3. Bukkit.getServer().broadcastMessage("something happened!");
    4.  
    5. }
    6.  
     
  6. Offline

    Steeveeo

    Have you tried the new listener system? Maybe something changed there.
     
  7. Offline

    Father Of Time

    If this simple event listener isn't producing any result then likely you aren't registering the event properly. Have you:
    1) created an instance of your event listener
    2) registered that listener instance with bukkit

    Show us how you are initializing and registering the event, because from what I see above you should be getting a broadcast SPAM, mostly because every time anyone left or right clicks anything they will get "something happened!".

    And as stated before, you would differentiate a left from right click with the Action. There is:
    Action.LEFT_CLICK_AIR
    Action.RIGHT_CLICK_AIR
    Action.LEFT_CLICK_BLOCK
    Action.RIGHT_CLICK_BLOCK.

    If you are looking to identify a physical block that was clicked you will need to use the click block, but if you want to identify ANY block punched (including thin air) you will need to use the click air action.

    Hope this helps, good luck!
     
  8. Offline

    matejdro

    Read my first post again, event triggers, but not when you left click on player.
     
  9. Offline

    Father Of Time

    So does it spam "something happened!" every time you punch thin air, or do you have to punch an entity (like a zombie or pig)?

    Oh, I just realized this when looking up the methods in the event:
    It appears that this event is designed to only trigger when a player right clicks an entity, left clicking is an attack and considered "damage" not "interact".

    If the damage event isn't working in creative mode I would bet that it's because the event is being canceled by the creative mode. What i would do to get around this would be to:

    1) listens for the EntityDamage event, and if it's canceled re-enable it.
    2) perform your plug-in's task from the newly enabled damage event.
    3) re-cancel the event.

    I have no idea if this will work or not, but I have seen this suggested as a work around for events that are canceled by other plug-ins.

    Sorry for the confusion, was at work and only skimmed your post. Hope this works for ya, take care!
     
  10. Offline

    MrMag518

    Action.LEFT_CLICK_BLOCK
    Get the block (AIR) that's inside the player.
    (player.getLocation())

    Should work..
     
  11. Offline

    nisovin

    If you really need to be able to left click to interact, I think your only real option is to check PlayerAnimationEvent and look for any entities standing in front of the player.
     
  12. Offline

    ItsHarry

    You could try checking WorldEdit's "compass" source code to see how Sk89q did it
     
  13. Offline

    matejdro

    Just checked it right know, hitting a player with a compass will not teleport you.
     
  14. Offline

    Father Of Time

    Did the enabling and re-disabling the player damage entity event not work?

    Edit: also, maybe if you tell us what you are trying to actually achieve we may be able to provide you with an "alternative" method of handling it, often this is the case.
     
  15. Offline

    matejdro

    I have solved it with hand animation event. Thanks everyone for help!

    I just wanted to detect when player hit another player, even if "victim" was invincible like in gamemode or with pvp turned off.
     
Thread Status:
Not open for further replies.

Share This Page