Looping Through Lists, and cancelling?

Discussion in 'Plugin Development' started by kyledag500, May 29, 2014.

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

    kyledag500

    Hey guys, I was wondering if it was possible to cancel looping through a list?

    Say for example:
    Code:
    for(Player p : bukkit.getOnlinePlayers()){
        if(admins.contains(p)){
              //Cancel Loop here.
        }
    }
    So, yea, something like that. I am looking for a player who is in an arraylist or something, and if I find one, cancel. Possible?
     
  2. Offline

    fireblast709

    break or return will stop the loop kyledag500
     
  3. Offline

    mythbusterma

    Do be careful with return though, as it will return the entire method, break only breaks loops.
     
  4. Offline

    renaud444

    You should just break, since you are performing operations with the player found. Also, you should not store a Player object in a list or Map. Instead, store the player's Unique Id, and check from there.
    Code:java
    1. public ArrayList<UUID> admins = new ArrayList<UUID>();
    2. Player admin;
    3. for(Player p : bukkit.getOnlinePlayers()){
    4. if(admins.contains(p.getUniqueId())){
    5. admin = p;
    6. break;
    7. }
    8. }
     
  5. Offline

    Pips

    Not really sure what you're asking. If you want to find out if x player is in the a list of y people, then you'd use this
    Code:java
    1. Boolean isAdmin = listOfAdmins.contains(player);

    If you want to make an array of all online players who are an admin, then you'd use this
    Code:java
    1. ArrayList<String> listOfPlayersThatAreAdmins = new List<String>();
    2. for(Player p : bukkit.getOnlinePlayers()){
    3. if(admins.contains(p.getName())){
    4. listOfPlayersThatAreAdmins.add(p.getName());
    5. }
    6. }


    Not really sure what you're asking :/
     
  6. Offline

    renaud444

    You shouldn't use the player's name, it will cause conflicts when Minecraft 1.8 gets released, and players will be able to change their names. Use the UUID, like I already posted.
     
  7. Offline

    Plo124

    This depends. If you want to use it to send the player a message, and only for a short time, e.g. fixing a snowball metadata to have their name if they throw it, then the username is fine.
    If you are doing something like a kit which has a cooldown, even if its not between restarts, or you are logging data, it is highly recommended to log their uuid.
     
  8. Offline

    RawCode

    totally invalid.

    names cannot be changed at runtime, only persistent data like bans is affected.
     
  9. Offline

    renaud444

    Correct, but if the names are stored in a config file, then the player changes his/her name, and joins back, they will no longer be matched with the method, UNLESS their UniqueId is stored in the list/config file.

    Overall, it's better to use the player's UUID, to get into the good habit.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  10. Offline

    RawCode

    i see hashmap.

    hashmap stored in memory, not config or hdd or mysql.

    UUID have nothing with good habit, it's hysteria caused by stupid youtubers who claim any method marked deprecated "to be removed next week"
     
  11. Offline

    renaud444

    Because he just typed a quick example.. How would you expect a list of admins to persist a reload/restart? Config file.
     
  12. Offline

    RawCode

    List of admins is handmade... renaud444

    Do you know your own UUID?
     
  13. Offline

    Syd

    RawCode
    Names CAN be changed in runtime. A player simply logs off, changes his name and logs back in.
    The server still has the old name in it's HashMap and can't link it to the same player anymore.

    Using UUID for storage IS the way to go nowadays. Names are only player session persistent.
     
    Zupsub likes this.
  14. Offline

    RawCode

    List of admins is handmade... Syd
     
  15. Offline

    Syd

    RawCode
    How do you know?
    All I see is, that the OP tries to check if a player is stored in a List - how this List has been filled is unknown.

    Also, even if it's hand made (I guess you mean "hard coded" with it), it is STILL better to use UUID, than player names, as names still can change. If the data is longer valid than the session, it should be stored in UUID.
     
  16. Offline

    renaud444

    Yes, I do know my own UUID, and the list is only handmade in this example. COMMON SENSE would say to store the list in a YML file.
     
Thread Status:
Not open for further replies.

Share This Page