Random Teleportation

Discussion in 'Plugin Development' started by DoggyCode™, Jul 5, 2015.

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

    DoggyCode™

    Do anyone have any basic code that will teleport a sender to a random player online?

    For example, if I type '/rtp', it will teleport me to a random player. Please no advanced comments xD Example code would be highly appreciated!
     
  2. Offline

    SuperSniper

    Some leftover code from one of my private plugins:

    Code:
                            Player player = (Player) sender;
                           
                            Location originalLocation = player.getLocation();
                           
                           Random random = new Random();
                           
                            Location teleportLocation = null;
                           
                           int x = random.nextInt(100) + 1;
                           int y = 150;
                           int z = random.nextInt(100) + 1;
                           
                           boolean isOnLand = false;
                           
                           while (isOnLand == false) {
    
                                    teleportLocation = new Location(player.getWorld(), x, y, z);
                                   
                                   if (teleportLocation.getBlock().getType() != Material.AIR) {
                                            isOnLand = true;
                                   } else y--;
                           
                           }
                           
                            player.teleport(new Location(player.getWorld(), teleportLocation.getX(), teleportLocation.getY() + 1, teleportLocation.getZ()));
                           
                            player.sendMessage(ChatColor.GREEN + "You have been teleported " + (int)teleportLocation.distance(originalLocation) +" blocks away!");
                           
                           return true;
    
     
  3. Offline

    poepdrolify

    Add your own things:
    Code:
           Random r = new Random();
            ArrayList<Player> players = new ArrayList<Player>();
            for(Player online : Bukkit.getServer().getOnlinePlayers()) {
                if(online == p) {
                }
                else
                players.add(online);
            }
            int index = r.nextInt(players.size());
            Player loc = (Player) players.get(index);
            p.teleport(loc);
    I used it in a method, otherwise you have to declare Player p
     
  4. Offline

    DoggyCode™

    Thank you! But if you read my post, it says, teleport to a random "Player" xD
     
  5. Offline

    SuperSniper

    oh, oops xD

    I dont know how to loop through all the players & tp to a random one.
     
  6. Offline

    DoggyCode™

    Don't worry, @SuperSniper , @poepdrolify 's method worked.
     
Thread Status:
Not open for further replies.

Share This Page