Teleporting Method Being Stoopid.

Discussion in 'Spigot Plugin Development' started by Remy2402, May 9, 2015.

  1. Hello people of the internet. I have a method that teleports all online players to different locations which are specified in the onEnable and added to an arraylist. So I use this method to teleport them. But for some reason it teleports all players to one single location instead of all 12 of them. Here is the method.

    Code (Text):
     public void teleportPlayers() {
            Collection<? extends Player> players = Bukkit.getOnlinePlayers();
            for (Player onlinep : players) {
                for (int i = 0; i < players.size(); i++) {
                    onlinep.teleport(GoldBreaker.getInstance().spawns.get(i));
                }
            }
        }
     
  2. You're teleporting all players to all locations in the list meaning the last location counts.
     
  3. I don't understand what you are saying. Could you be a bit more detailed?
     
  4. You are looping through all the players, for each player you're also looping through <amount of players> spawns. You're teleporting each player to every location you looped through. So the last location counts thus all players get teleported to the same location.
     
  5. Could you give me a way to fix this?
     
  6. Code (Text):
        public void teleportPlayers() {
            Collection<? extends Player> players = Bukkit.getOnlinePlayers();
            int count = 0;
            for (Player onlinep : players) {
                onlinep.teleport(GoldBreaker.getInstance().spawns.get(count));
                count++;
            }
        }
    This or an iterator.
     
    • Useful Useful x 1
  7. Thanks That has fixed it :)