Prevent PlayerInteractEvent from firing

Discussion in 'Plugin Development' started by pkats15, Mar 22, 2014.

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

    pkats15

    Is there any way to make PlayerInteractEvent not fire when PlayerInteractEntityEvent is triggered?
    Any help would be greatly appreciated.:)

    P.S. If you can suggest a better title ;)
     
  2. Offline

    AoH_Ruthless

    pkats15
    Event.setCancelled(true); ?
     
  3. Offline

    pkats15

    Yes but how can I check if the player has clicked on an entity in order to cancel the event?
     
  4. Offline

    Gopaintman

    You can get event.getAction() and then check to make sure that they're only clicking on whatever you to listen to.
     
  5. Offline

    pkats15


    I want to know if a player has right-clicked on an entity.
    If only there was a method player.getTargetEntity() to check if it is null or any other way to check if the player has clicked on an entity.
    Ideally it would be like:
    Code:java
    1. public void onPlayerInteractEvent(PlayerInteractEvent event){
    2. if(event.getPlayer().getTargetEntity()!=null){
    3. event.setCancelled(true);
    4. }
    5. }
    but that's not possible.
    Thanks for the help anyway!:)
     
  6. Offline

    AoH_Ruthless

    pkats15

    First, loop through the world's entities and get locations.

    In the loop:
    If the action is a right click block, use the PlayerInteractEvent.getClickedBlock() method and get the location of the block above it to get the entity's foot level.

    Right Click air is more complicated but you can do something similar.

    Don't do the above..
    in the playerInteractEvent, check if the event's instance is a PlayerInteractEntityEvent. If not, cancel.
     
    Konkz likes this.
  7. Offline

    pkats15

    Could you please give a code exemple?(I'm quite a noobie programmer) and thank you very much for helping
     
  8. Offline

    AoH_Ruthless

    pkats15
    Let's say we were in an EntityDamageEvent.

    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e) { }


    Now we want to check if the the entity damaged was a player. So we check if the entity's instance is a player.

    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e) {
    3. if (e.getEntity() instanceof Player) {
    4. //code
    5. }
    6. }


    Now, apply what I told you to the PlayerInteractEvent.

    Note: I didn't give you the exact correct code because you need to be able to solve your own issues at times. You simply can't rely on other people's code.
     
Thread Status:
Not open for further replies.

Share This Page