Solved Spawn each player to a different spawn point

Discussion in 'Spigot Plugin Development' started by CrackleHarvest36, Feb 2, 2020.

Thread Status:
Not open for further replies.
  1. Hey there! This is a bit hard to explain what I'm trying to do, but I'm basically trying to spawn each player at a different set spawn point. Basically, I have a minigame that can have up to 16 players, and I have 16 spawn points for the minigame. I want each player to be teleported to a different spawn point. Hope this makes sense, thank you!
     
  2. Loop 16 times, each time get a player with the index of i from the collection of players and teleport them to the location with the index of i in the collection of locations.
     
  3. How exactly would I do this. Sorry, I'm a bit new.
     
  4. 1. make a List<Location> that contains all these spawn points
    2. draw a (random?) location from that list and teleport the player to it
     
  5. With a for-loop?
     
  6. Won't work, he needs all players to be teleported to different locations
     
  7. For int loop and ArrayList#get
     
  8. That's exactly what I suggested in my first post. In the last comment I meant that drawing a random location won't work because 2 players could get teleported to the same location.
     
  9. of course it works, just gotta remove the location that was randomly drawn from the list and then each player gets TP'd to a unique location
     
  10. This is just an idea how you could do that.
    I didn't use my IDE, I hope it works.
    Code (Java):
    List<Location> spawns = new List<Location>();
    Random random = new Random();

    //For each loop to loop through all online players
    for(Player player : Bukkit.getOnlinePlayers()) {
      Location current = spawns.get(random.nextInt(spawns.size()));
      player.teleport(current);
     
      //Removes it from the list so that the spawn can only be used once.
      spawns.remove(current);
    }
     
     
    • Winner Winner x 1
  11. Why not just use
    Code (Java):
    List<Location> locs = ...
    List<Player> players = ...
    for (int i = 0; i < players.size(); i++) {
        players.get(i).teleport(locs.get(i));
    }
    More efficient than using randoms and removing from the list.
     
  12. Ended up using this. Thank you!!
     
Thread Status:
Not open for further replies.