Getting all items from an ArrayList

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 21, 2016.

  1. I have an array list filled with players, but I don't know how to get all of them to be used in a for loop. How would i do this?
     
  2. To obtain all elements from an arraylist, you can utilize a for loop.

    Code (Text):

    for(Player players : arrayList) {}
     
    Then of course, you can use players and execute any methods to all elements in the arraylist.
     
  3. either
    Code (Text):
    for(Player player : playerList) {
    //Do stuff
    }
    Or (Java 8 only):
    Code (Text):
    playerList.forEach(player -> {
    //Do stuff
    });
     
  4. This is what I was trying
    Code (Text):

            for(String player : red){
                Bukkit.broadcastMessage("r");
                Bukkit.getPlayer(player).teleport(locr);
            }
     
    But it wouldn't do anything
     
  5. Is the List a List<String> or a List<Player>?
     
  6. Are there any values in the list?
     
  7. Yes, I have the values obtained from a config and i am looking at them now
     
  8. Are there any errors in the console or anything like that?
     
  9. Is the "r" message being broadcasted? If so, it means that getPlayer(player) is returning null, which means the string name does not match any online player.

    If "r" is not being broadcasted, it means the list is empty. I suggest you add a null check inside the loop, and a length check before it, for debugging.
    Code (Java):
    Player getp = Bukkit.getPlayer(player);
    if(getp != null)
        getp.teleport()....
    Oh, if this is meant to store long-lasting data, use UUIDs instead of player names, to anticipate player name changes. player.getUniqueId().toString(); and UUID.fromString(); i believe are the methods used.
     
  10. r i not being broadcast, and maybe its how i am adding the players. I getting the players stored into a config and then adding them to them to the list using this:
    Code (Text):
     red.addAll(teams.getStringList("Teams.Red"));
    And this is how the config is:
    Code (Text):

    Teams:
      Red:
        Xisuma: true
        xTheGamerPlayz: false
     
     
  11. That's not a valid String list, that's why. A string list in yaml would be
    Code (Text):
    Teams:
        Red:
            - Something
            - Something Else
     
    • Agree Agree x 1
  12. I am not at all familiar with yml (i use .txt or .nbt, because .yml is a flaming disaster with highly fallible formatting), but that looks like a list of named booleans, not strings.
    Also make sure the indentation is correct and there are no newlines/tab whitespace, yml hates that stuff.
     
  13. Ok i get what you're saying, So how would I store player names in a StringList?
     
  14. With the kind of format that I showed you
     
  15. Im sorry, but i do not know how to store it in the format you showed me

    Edit: Should i try this?
    Code (Text):
    teams.getStringList("Teams.Red").add(p);
     
    #16 TheGamerPlayz, May 21, 2016
    Last edited: May 21, 2016
  16. (ಠ_ಠ)

    Use the docs, luke..
     
  17. The docs are weak with this one