Getting all player locations

Discussion in 'Plugin Development' started by Big_Mac_dev, Jul 2, 2014.

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

    Big_Mac_dev

    I need to get all the online players and then get all of their locations. I looked other places but I was still having trouble. I created an array for online players and one for locations.

    Code:java
    1. Player[] players = Bukkit.getOnlinePlayers();
    2.  
    3. ArrayList<Location> locations = new ArrayList<Location>();


    How would I loop through it and play a sound at everyones location. I imagine with a for loop but Im not having any luck.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Zettelkasten

    There are two ways to "iterate" (loop) through an array:
    Code:java
    1. Player[] players = Bukkit.getOnlinePlayers();
    2. for(int i = 0; i < players.length; i++) {
    3. Player player = players[i];
    4. }
    5. for(Player player : players) {
    6. }[/i]

    (The second method is basically a short form for the first)

    In the loop you can do anything you want with your players and it will be done to all players currently online, for example getting their locations etc.

    For playing the sounds you don't need to store the locations in an list because you can just do player.playSound(...) and it will have the same effect but be more performance-effective.
     
  3. Offline

    Big_Mac_dev

  4. Offline

    tommyhoogstra

    Zettelkasten you can just do
    Code:java
    1. for(Player p : Bukkit.getOnlinePlayers()){
    2. //code
    3. }
     
  5. Remember Bukkit.getOnlinePlayers() will no longer return an Array. Be prepared to switch over to a Collection<Player>.
     
Thread Status:
Not open for further replies.

Share This Page