PlayerInteractEvent

Discussion in 'Plugin Development' started by CoDGuardian, Jul 1, 2012.

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

    CoDGuardian

    Basically, I need to know how to get my code to do something when the player left clicks and do something else when he right clicks. I don't know how to do either of these things. I've heard hints towards PlayerInteractEvent, but im not sure how to use it.
     
  2. Offline

    javoris767

    Code:java
    1. if (event.getAction()== event.getAction().LEFT_CLICK_BLOCK ) {

    Code:java
    1. if (event.getAction()== event.getAction().RIGHT_CLICK_BLOCK ) {

    Something like this I guess.
     
  3. Offline

    EnvisionRed

    Code:
    @EventHandler
    public void clickHandler(PlayerInteractEvent event){
    Action action = event.getAction();
    if ((action == Action.LEFT_CLICK_AIR) || (action == Action.LEFT_CLICK BLOCK)) {
    //do stuff for left click.
    }
    if ((action == Action.RIGHT_CLICK_AIR) || (action == Action.RIGHT_CLICK_BLOCK)) {
    //do stuff for right click.
    }
    }
     
  4. Offline

    javoris767

    or that ^ xD
     
  5. Offline

    CoDGuardian

    Oh thank you. And one more question, if i were trying to make a fireball shoot when a person left clicks, how could i do that? I tried the launchProjectile() method but it blows up instantly, whereas i want mine to fly through the air where ever i am looking. I also tried doing getWorld().spawn(loc,Fireball.class), where loc is my location, however i can't figure out how to set the direction of the fireball.

    Also LEFT_CLICK... and RIGHT_CLICK don't seem like valid things. Unless i don't have the right import?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  6. Offline

    EnvisionRed

    So import it.
     
  7. Offline

    CoDGuardian

    I just tried but it doesn't work. I did import org.bukkit.Action.*; but it says it cannot be resolved.
     
  8. Offline

    EnvisionRed

    org.bukkit.Action is an enum, not a package so just import org.bukkit.Action
     
  9. Offline

    CoDGuardian

    You just said to import the same thing? If you mean do just "import org.bukkit.Action;" i tried that too but same result.
     
  10. Offline

    EnvisionRed

    Nope. You imported org.bukkit.Action.* which cannot be resolved because org.bukkit.Action isn't a package and has no subclasses to import.
    Action is an Enumerator. The import is org.bukkit.event.block.Action;

    Sorry if I mislead you for the org.bukkit.Action thing. I thought you had done some research and weren't just pulling imports out of a hat.
     
  11. Offline

    CoDGuardian

    Oh ok well i added the import but it still says LEFT_CLICK_AIR and the others cannot be resolved.
     
  12. Offline

    EnvisionRed

    Do you have it as
    Code:
    Action.LEFT_CLICK_AIR
    or
    Code:
    LEFT_CLICK_AIR
    ?
    There is a difference.
     
  13. Offline

    CoDGuardian

    Action.LEFT_CLICK_AIR. What's the difference?
     
  14. Offline

    Bobfan

    Make sure you aren't accidentally importing javax.swing.Action, because I did that and it interfered with the import org.bukkit.action.block.
     
Thread Status:
Not open for further replies.

Share This Page