Solved Teleport To Random Location In A Preset Area!

Discussion in 'Plugin Development' started by Scott102, Jan 4, 2015.

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

    Scott102

    Hi, is there a code that I can use if I would want players to teleport randomly in an area? Like when they do /tprandom , they teleport randomly in an arena/area. THANKS!
     
  2. Offline

    Skionz

    AdamQpzm likes this.
  3. Offline

    Scott102

    @Skionz I know how to do the Math.random() * ..... . I want to know how to teleport the players randomly in a specific region, lets say Arena.
     
    Last edited: Jan 4, 2015
  4. Offline

    nj2miami

    Well, if the area is setup as a WorldGuard region, you can hook into it to get the min and max Vectors, which you can use to extract the x, y, z of the region. Then apply some randoms using these min/max numbers and send your player off to the new Location.

    If you aren't using WorldGuard, then you could store the area x, y, z coords in a config and grab them when you need them too of course.

    You will need to add some "safe teleport" validation I would guess to make sure you aren't placing them in a block.

    To teleport a player somewhere, its simply -> Player.teleport(Location)

    Once you have your x, y, z it would look like:

    Player.teleport(new Location(world, x, y, z));

    or

    Location loc = new Location(world, x, y, z);
    Player.teleport(loc);
     
  5. Offline

    Scott102

    @nj2miami

    int randomX = (int) (Math.random() *1000);
    int randomZ = (int) (Math.random() *1000);
    int randomY = player.getWorld().getHighestBlockYAt(randomX, randomZ);

    Location randomtp = new Location(player.getWorld(), randomX, randomY, randomZ);
    player.teleport(randomtp);
    player.sendMessage("You Teleported Randomly!");



    that is my current code, it teleports me to a random location on ground, but it teleports me somewhere in the world I am in, I'm trying to have the player teleport in a specific region in the world. BTW I'm not using worldguard
     
    Last edited: Jan 4, 2015
  6. This happens because that's what you explicitly tell the plugin to do that. Look at your code and try to find the section that ensures it's inside the region. Hint: It doesn't exist. :p

    Follow the advice by @nj2miami - the same logic applies whether you're using WorldGuard or your own region system.
     
  7. Offline

    xMakerx

    @AdamQpzm
    Are you on the same page?

    You should be able to figure it out. Think about ranges.

    Get the minimum and maximum x and z for that region. Choose an x and z randomly. Teleport the player to those 2 and use the getHighestBlockYAt.
     
  8. I'm not sure what you mean :p
     
  9. Offline

    xMakerx

    I meant that if you were thinking the same thing my explanation was trying to get across. Your signature made me follow you.
     
  10. Oh yes, I am on the same page then :p And thanks, I'm glad you like it :)
     
  11. Offline

    nj2miami

    @Scott102 - Since you are not using WorldGuard, which lets you define regions in game, then you will need to define those yourself. You will need to store the x, y, z coords of the two corners of your "region/area".

    Then when you randomize, make sure you are using those as references to lock in your location to something within that area.
     
  12. Offline

    Scott102

    oh okay, Thanks guys. Appreciate the help.
     
  13. Offline

    xMakerx

    You're welcome. This plugin doesn't require WorldGuard from what you've told us. You could create a new YamlConfiguration, store the minimum and maximum points there, load that up and do the same thing over again.
    Please try what I said below and let me know how it goes.

     
Thread Status:
Not open for further replies.

Share This Page