Teleport each player from arraylist to a random location

Discussion in 'Plugin Development' started by Chrusty45, Sep 7, 2014.

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

    Chrusty45

    For some reason my code teleports ALL players in the list to the same random location. What I want is each player to be teleported to a random location (Not the same location). Here's the code:
    Code:java
    1. public static void teleport() {
    2. World world = Bukkit.getServer().getWorld(Main.uhcConfig.getString("World"));
    3. Random generator = new Random();
    4. int rand_x = generator.nextInt(Main.uhcConfig.getInt("World Border Radius"));
    5. int rand_z = generator.nextInt(Main.uhcConfig.getInt("World Border Radius"));
    6. int y = world.getHighestBlockYAt(rand_x, rand_z) + 2;
    7. boolean safe = false;
    8. Location loc;
    9. while (!safe) {
    10. y++;
    11. loc = new Location(world, rand_x, y, rand_z);
    12. Block block = loc.getBlock();
    13. Material downType = block.getRelative(BlockFace.DOWN).getType();
    14. if (downType != Material.AIR && downType != Material.LAVA && downType != Material.WATER && downType != Material.STATIONARY_WATER) {
    15. if (block.getType() == Material.AIR && block.getRelative(BlockFace.UP).getType() == Material.AIR) {
    16. safe = true;
    17. for (String playerName : Game.getPlayers()) {
    18. Player player = Bukkit.getServer().getPlayer(playerName);
    19. if (player != null) {
    20. rand_x = generator.nextInt(Main.uhcConfig.getInt("World Border Radius"));
    21. rand_z = generator.nextInt(Main.uhcConfig.getInt("World Border Radius"));
    22.  
    23. Chunk c = world.getChunkAt(new Location(world, rand_x, y, rand_z));
    24. world.loadChunk(c);
    25.  
    26. if (c.isLoaded()) {
    27. player.teleport(loc);
    28. }
    29.  
    30. }
    31. }
    32. }
    33. } else if (y >= 254) {
    34. y = world.getHighestBlockYAt(rand_x, rand_z) + 2;
    35. rand_x = generator.nextInt(1000);
    36. rand_z = generator.nextInt(1000);
    37. }
    38. }
    39. }
     
  2. Offline

    masons123456

    Chrusty45 There are several things wrong with your logic, most importantly, it will only run once because the boolean 'safe' is only true once. Also, you seem to check if a location is "safe", but then you loop through every player, create a new random set of coordinates, load that chunk, and then teleport them to the original coordinates, completely disregarding the new random coordinates. Also, there is some odd manipulation of the y value that doesn't seem to make any sense but to teleport the play high above the ground.
     
    TheMintyMate likes this.
  3. Offline

    Chrusty45

    masons123456 Ok, I've rewritten the code to this. It works fine, but sometimes it won't teleport at all and time out the server or it will teleport players into the ground (Sometimes). Most of the time it works just fine, I just cant have the time outs and suffocating in the ground. I can't seem to figure it out.

    Code:java
    1. public static void teleport() {
    2. Random random = new Random();
    3. for(String playerName : Game.getPlayers()) {
    4. Player player = Bukkit.getServer().getPlayer(playerName);
    5. World world = Bukkit.getServer().getWorld(Main.uhcConfig.getString("World"));
    6. int x = random.nextInt(Main.uhcConfig.getInt("World Border Radius"));
    7. int z = random.nextInt(Main.uhcConfig.getInt("World Border Radius"));
    8. int y = world.getHighestBlockYAt(x, z);
    9.  
    10. boolean safe = false;
    11.  
    12. Location loc = new Location(world, x, y, z);
    13.  
    14. Block block = loc.getBlock();
    15. Material blockType = block.getRelative(BlockFace.DOWN).getType();
    16.  
    17. while(!safe) {
    18. if(blockType.isSolid()) {
    19. safe = true;
    20. Chunk c = world.getChunkAt(loc);
    21. world.loadChunk(c);
    22.  
    23. if(!c.isLoaded()) {
    24. c.load();
    25. } else {
    26. player.teleport(loc);
    27. }
    28.  
    29. }
    30. }
    31. }
    32. }
     
  4. Offline

    masons123456

    Chrusty45 Well why would you want to teleport them into a solid block? IMHO, the whole 'while' loops isn't really necessary. Just add like 2 to the highest block at the location, load the chunk, and then teleport. Also, with the current setup, it only teleports them if the chunk is loaded. Replace:
    Code:
    if (!c.isLoaded()) {
        c.load();
    } else {
        player.teleport(loc);
    }
    With:
    Code:
    if (!c.isLoaded()) {
        c.load();
    }
    player.teleport(loc);
    
     
Thread Status:
Not open for further replies.

Share This Page