Quick Question

Discussion in 'Plugin Development' started by GRocksMc, Mar 28, 2017.

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

    GRocksMc

    Is there a way to simplify if(player.getWorld().getName().equals("something") || player.getWorld().getName().equals("something1"))

    Thanks!
     
  2. Offline

    mehboss

    The line? Well, you can make them variables instead or you can for-loop through an array if you have a bunch of worlds you are checking.
     
  3. Offline

    GRocksMc

    @mehboss

    Is there a way to do it without using the "||"?
     
  4. Offline

    mehboss

    Using a for-loop I suppose.
     
  5. Offline

    GRocksMc

    @mehboss

    Is there any way to put both Strings in the .equals()?
     
  6. Offline

    mehboss

    Yes, using a for-loop.
    • Add all the worlds you want in the array or list.
    • For-loop through the array or list to get its values.
    Or, check if the world if is in the array by doing array.contains
    vvvvvvvv
     
    Last edited: Mar 29, 2017
  7. Online

    timtower Moderator Moderator

    mehboss likes this.
  8. Offline

    mine-care

    @timtower @mehboss @GRocksMc
    Wouldn't that be a waste of resources?
    Alocating a list, adding the two values and all that to make an if condition check a single boolean rather than two?
    Why not just:
    Code:
    String wName = player.getWorld().getName();
    if(wName.equals("something") || wName.equals("something1")){ ... }
    
     
  9. Online

    timtower Moderator Moderator

    @mine-care That is not expandable to 20 worlds.
     
  10. Offline

    mine-care

    @timtower Well then you can make a method(String one, String... rest) and then compare 'one' to each of 'rest' and return true if one is equal to at least one of 'rest':
    Code:
    boolean isOneEqual(String one, String... rest){
       FOREACH String s from rest:
          IF one.equals(s) then:
             return true;
          endIF;
       endFor;
       return false;
    }
    
    But judging by the OP's posts i deduced that he was aiming to check just two words :p
     
  11. Online

    timtower Moderator Moderator

    @mine-care You do realize that you just wrote a contains function right?
     
  12. Offline

    mine-care

    @timtower Yes. I am forth to knowing what things do before using them ;) if the OP was to use a List then depending on the implementation the contains method would be different, for ArrayList it is very similar to the one above however using a linkedlist its not :p
    But again my main point is this
    If thats not the case then a list sounds like a good solution
     
Thread Status:
Not open for further replies.

Share This Page