How to make an OR statement with Actions?

Discussion in 'Plugin Development' started by Milkywayz, Feb 26, 2012.

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

    Milkywayz

    Hey i need to do the same exact thing for 2 different actions, Left clicking the air and left clicking a block. It wouldn't let me do the normal || statement, so i tried another if statement.. Is there any way I do that? Code snippet:
    Code:java
    1. Action eAction = event.getAction();
    2. if (eAction == Action.LEFT_CLICK_AIR) {
    3. event.getPlayer().getWorld()
    4. .createExplosion(ply.getLocation(), power);
    5. if (eAction == Action.LEFT_CLICK_BLOCK) {
    6. event.getPlayer().getWorld()
    7. .createExplosion(ply.getLocation(), power);
    8. }
    9. }
     
  2. wut?
    Code:
    if(eAction == Action.LEFT_CLICK_AIR || eAction == Action.LEFT_CLICK_BLOCK)
     
  3. Offline

    Zimp

    Since action is an enum it might be easier to use a switch statement

    Code:
    switch(eAction)
    {
        case LEFT_CLICK_AIR:
        case LEFT_CLICK_BLOCK:
                  //do something
                  break;
        default:
                //do something else
    }
    
     
  4. Offline

    Milkywayz

    I already tried that, didn't work.

    Thanks, ill do just that. is default: like else?

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

    Zimp

    Default is where you would put your code in the case that the action is not LEFT_CLICK_AIR or LEFT_CLICK_BLOCK. If you don't want to do anything then you may just want to return out of the function for the default case.
     
  6. Offline

    Milkywayz

    Code:java
    1.  
     
  7. it looks fine, but its not correct however,mayby you missing some important pice of code atr the top, that makes it invalid?
     
  8. Offline

    Milkywayz

    No the code works right, and for once I'm actually getting somewhere when it comes to fixing a bug!
     
  9. for us, it looks like you added to mutch closing braces at the end of it, or just forgot some opening braces at the eginning
     
Thread Status:
Not open for further replies.

Share This Page