Solved Teleporting players to different locations

Discussion in 'Spigot Plugin Development' started by DatsMaboi, Jun 9, 2018.

  1. I'm trying to go through all the online players and teleport them to different locations that i have set.
    This is what i have tried
    Code (Java):
    public void startGame() {
            Collection<? extends Player> players = Bukkit.getOnlinePlayers();
            ArrayList<Location> locations = new ArrayList<Location>();
           
            Location platform1 = new Location(Bukkit.getWorld("world"), 1, 20, 1);
            locations.add(platform1);
            Location platform2 = new Location(Bukkit.getWorld("world"), 2, 20, 1);
            locations.add(platform2);
            Location platform3 = new Location(Bukkit.getWorld("world"), 3, 20, 1);
            locations.add(platform3);
            Location platform4 = new Location(Bukkit.getWorld("world"), 4, 20, 1);
            locations.add(platform4);
            Location platform5 = new Location(Bukkit.getWorld("world"), 5, 20, 1);
            locations.add(platform5);
            Location platform6 = new Location(Bukkit.getWorld("world"), 6, 20, 1);
            locations.add(platform6);
            Location platform7 = new Location(Bukkit.getWorld("world"), 7, 20, 1);
            locations.add(platform7);
            Location platform8 = new Location(Bukkit.getWorld("world"), 8, 20, 1);
            locations.add(platform8);
           
           
            for(int i = 0; i < players.size(); i++)
            {
                Player p = (Player) players;
               
                p.teleport(platform1);
            }
        }
    anyone know what i'm doing wrong?
     
  2. Please post your error from the console (called a stacktrace)
     
  3. You are only teleporting them to platform1, as you can see in your for-loop. If you wish to use all of them, try to replace "platform1" with the entries in the arraylist of locations
     
  4. how would i do that?
     
  5. Agreed, sorry didn't realise that was the error haha. Explain what you actually want to happen please, do you want ALL players to be teleported to ALL locations? Or like 1 player to location1, the second player to location2 etc.
     
  6. Use locations.get(i); But be careful though - if you have more players than locations it will cause issues since there are only 8 entries in the locations array
     
  7. Your for loop is also wrong. If it's what I think it is (teleport player 1 to platform1 etc, here's the implementation of mine)
    Code (Text):
    for(int i = 0; i < players.size(); i++) {
        int num = (i % 8) + 1;
        Player player = players.toArray()[num];
        Location loc = locations.get(num);
        player.teleport(loc);
    }
     
    #7 JustRayz, Jun 9, 2018
    Last edited: Jun 9, 2018
    • Winner Winner x 1
  8. i want 1 player to each location
     
  9. erm players.get(num) does not work
     
  10. sorry. try it again now.
     
  11. Still get this error
    The method get(int) is undefined for the type Collection<capture#3-of ? extends

    Player>
     
  12. I'm not at an IDE right now, but change player = players.get(num); to player = players.toArray()[num]; not sure tho.
     
  13. Code (Java):
    for(int i = 0; i < players.size(); i++) {
                int num = (i % 8) + 1;
                Player player = (Player) players.toArray()[num];
                Location loc = locations.get(num);
                player.teleport(loc);
            }
    i had to add the cast (Player) is that ok?
     
  14. Yeah that's fine. There's a better way but since you know it'll be players it's fine.
    And change your declaration of players to Collection<? extends Player> players = instance.getServer().getOnlinePlayers(); where instance is your class extending JavaPlugin
     
    • Useful Useful x 1