Solved Teleport per player to per location and not repeat location

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

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

    Eskillu

    How can i do to teleport: X player to X location, Y player to Y location etc, randomly and not repeat location to other player?
     
  2. Offline

    masons123456

    Eskillu Nobody will provide you with the code, but you can teleport players using:
    Code:
    somePlayer.teleport(someLocation);
    and create random integers using Java's built-in Random:
    Code:
    Random random = new Random();
    r.nextInt(maxValue);
     
  3. Offline

    Eskillu

    Ok, thank you!
     
  4. Offline

    firecombat4

    You would want to get the current location the player is at and then get a random integer using the Random nextInt method, add the random integer to the current location as you want to get the location randomly around the player. I would suggest not using a random Y value as you may end up suffocating a player. I have written up a method quickly below which may help you,
    Code:
    public void spawnRandomLocation(int radius, Player player) {
     
    //Create an instance of the random class so we can get a random integer
    Random rand = new Random();
     
    //The players x location, will be added to the randomly generated x
    int xOffset = player.getLocation().getBlockX();
    //The players z location, will be added to the randomly generated z
    int zOffset = player.getLocation().getBlockZ();
     
    //Get a random integer within the radius
    int x = rand.nextInt(radius);
    int z = rand.nextInt(radius);
     
    //Get the players world (Used to create a new location instance)
    World w = player.getWorld();       
     
    //Create a new location where the player will be teleported too, adding the xOffset to the random x and the zOffset to the random z
    Location tpLoc = new Location(w, x+xOffset, 1, z+zOffset);
     
    //Get the highest block at the teleport location so the player wont be teleported into a block
    tpLoc = w.getHighestBlockAt(tpLoc).getLocation();
     
    //Finally teleport the player
    player.teleport(tpLoc);
    Additionally I would do some checks to make sure the player isn't going to be teleported into a cactus or surface lava. But that should get you going.
     
  5. Offline

    Eskillu

    thank you too firecombat4
     
Thread Status:
Not open for further replies.

Share This Page