ArrayList is empty?!

Discussion in 'Plugin Development' started by AvarionDE, Apr 19, 2014.

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

    AvarionDE

    Hey guys,

    I started to create my own bukkit plugin few days ago. Now I stuck on a problem I can not solve:

    Code:java
    1. public ArrayList<String> onlinePlayers = new ArrayList<String>();
    2.  
    3. public void logIn(PlayerJoinEvent e){
    4. onlinePlayers.add(e.getPlayer().getName());
    5. Bukkit.broadcastMessage(onlinePlayers.get(0) + "has joined the server!");
    6. }


    My aim is to teleport all players on the server to a fixed location. First of all I used the code above to save the names of all players which are currently on the server. Secondly i coded a scheduler which runs this teleport() method when the counter reaches 0:

    Code:java
    1. public void teleport(){
    2. for(int i = 0; i > onlinePlayers.size(); i++){
    3. getServer().getPlayer(onlinePlayers.get(i)).teleport(location);
    4. {
    5. }


    Everytime i try to run the plugin and the teleport() method is called bukkit tells me an error that the arraylist is empty or something like this. For me it didn't make sense so I tried give out the player name in the teleport() method to prove if the list saved a string:

    Code:java
    1. public void teleport(){
    2. Bukkit.broadcastMessage("Your name is " + onlinePlayers[0]);
    3. for(int i = 0; i > onlinePlayers.size(); i++){
    4. getServer().getPlayer(onlinePlayers.get(i)).teleport(location);
    5. {
    6. }


    This was also creepy because the broadcastMessage method also couldn't find any string or name in the arrayList but before in the LogIn() method the server gave out a name:

    Code:java
    1. public ArrayList<String> onlinePlayers = new ArrayList<String>();
    2.  
    3. public void logIn(PlayerJoinEvent e){
    4. onlinePlayers.add(e.getPlayer().getName());
    5. Bukkit.broadcastMessage(onlinePlayers.get(0) + "has joined the server!");
    6. }

    So my question to you: Why could it be that at first the string is saved to the arraylist but whenever i call the teleport or another method the server says that it's empty???
    I hope you can fix the problem guys because this is really annoying :D
     
  2. Offline

    TomFromCollege

    Why are you creating a list for online players?
    To loop through all online players:
    Code:java
    1. for(Player player : Bukkit.getOnlinePlayers()) {
    2. player.teleport(Location);
    3. }
     
    AvarionDE likes this.
  3. Offline

    AvarionDE


    OMG! Thanks alot dude! You saved my life! :D
     
Thread Status:
Not open for further replies.

Share This Page