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?
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
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.
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
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); }
Still get this error The method get(int) is undefined for the type Collection<capture#3-of ? extends Player>
I'm not at an IDE right now, but change player = players.get(num); to player = players.toArray()[num]; not sure tho.
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?
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