Teleporting all players randomly without massive lag

Discussion in 'Spigot Plugin Development' started by cowgod, May 31, 2015.

  1. cowgod

    Supporter

    Is this possible?

    I'm making a new plugin that generates a new world every round. At the start, players are spectators and all spawn at 0,0. When the game starts, all players are randomly teleported somewhere on the map.

    I tried loading all chunks before the game starts, which kinda sorta works, but then if the map is very large, it requires a ton of ram (I already allocated 4gb for it). Sometimes it just crashes.

    If I don't load all chunks, then when I teleport the players, I load just the chunk they are being teleported to, but half the time players end up stuck in some blocks or their client lags out and they disconnect.

    Is there any way to do this reliably?
     
  2. Decide the coords that they're going to start at the beginning of the countdown and load them at an interval.
     
    • Agree Agree x 1
  3. Try something like
    Code (Text):
                    for(Player p: Bukkit.getOnlinePlayers()){
                        World world = Bukkit.getWorld("your world name");
                        Location spawn = new Location(world,-40,97,16); // Spawn loc
                    Location location = new Location(world, 0, 0, 0);  // Location
                    location.setX( spawn.getX() + Math.random() * The maps X * 2 - The Maps X);
                    location.setZ( spawn.getZ() + Math.random() * The Maps Z * 2 - The Maps X);
                   
                    location.setY( world.getHighestBlockAt(location.getBlockX(), location.getBlockZ() ).getY() );
                    p.teleport(location);
                    }
     
    • Optimistic Optimistic x 1