Solved Random teleport someone x radius around a certain set warp.

Discussion in 'Plugin Development' started by kayc01, Feb 18, 2016.

Thread Status:
Not open for further replies.
  1. Hey, just wanted to check something.
    I have a command that is teleporting all players in a list to a random location within a maximum radius length based off of a set spawn warp point. (To get the cords of the center of the map).

    I just wanted to make sure this is doing what i think it is. It seems to be working but hard to tell without loads of trial and errors.

    So it can teleport the players in the list to a random location based off + or - by 340 centering off of warp "spawn"'s cords for example for the X radius.

    This is correct right?

    Code:
    for (int i = 1; i <= arenaSize; i++) {
                                  int playerSpot = i -1;
                                
                                  Player playerInArena = inArena2.get(playerSpot);
                                 
                                     Random random = new Random();
                                 
                                  double x = settings.getData().getDouble("warps.spawn.x");
                                  double z = settings.getData().getDouble("warps.spawn.z");
                                 
                                  int x2 = (int) x;
                                  int z2 = (int) z;
                                 
                                  int RanX = random.nextInt(x2 + 340) +-1;
                                  int RanY = 150;
                                  int RanZ = random.nextInt(z2 + 320) +-1;
                                 
                                  Location teleportLocation = new Location(playerInArena.getWorld(), RanX, RanY, RanZ);
                                  playerInArena.teleport(teleportLocation);
     
  2. Offline

    Lightspeed

    If there are no errors that you know of . . it doesen't hurt to test it with mobs or somthing.
    Even take the random system and see if it will tp u at a random point around your player's location maybe in a command.

    Also instead of ranY I suggest <world>.getHighestBlockAt(RanX, RanZ);
     
  3. Thanks for this! RanY is meant to teleport them into the air and make them fall to the group. To give the feeling of h1z1's parachute.
    Just to confirm, does +- 1 mean + or - 1 (either is acceptable, negative or positive) or will it add 1, then minus 1 so its 0?
     
  4. Offline

    Lightspeed

    I don't know pfft I don't usally ever do math in java :L
    You can get the highest block then incrase it a bit so no one has a longer fall ;)
    I use
    Code:
    Random rand = new Random();
    int generateInt(int min, int max)
    {
        int randInt = rand.nextInt(min - max) + 1 + min;
        return randInt;
    }
    so for the random i'd add random -16 min and 16 max for x and z to there spawn location
    Just image spawning over a ravine with a paracute 10 hour slow glide down into it . . . . Even the pros faces look like> [sheep]
     
  5. Offline

    Xerox262

    @kayc01 If it's + -1 then it will subtract 1 from the number.
     
    Lightspeed likes this.
  6. Based off what i have, how can i make it that its saying either + or - up to 340 as a maximum blocks from the cords of spawn.
    I don't want everyone to be sent to only bigger numbers of the cords up to 340, i want it to be also be able to teleport some players 340 less that spawns X cords randomly.


    Thanks ill keep this in mind, a little confused by it though.
    Don't see how its using the cords of my set warp "spawn" point and saying they can only be teleported within 16 (for example) blocks away in any direction.
     
  7. Offline

    Lightspeed

    This can be used to create a randomly generated x and z to have a random location in a set radius around your warp.
    Code:
    int x = generateInt(-16, 16);
    int z = generateInt(-16, 16);
    <warpLocation>.add(x, <warpLocation>.getHighestBlockAt(x, z), z);
    
    And there ya go a random location with a random spawnpoint in an area around it.
    Simple eh?
     
  8. But how does that get my warp location? I get my warp location by doing this:


    Code:
    double x = settings.getData().getDouble("warps.spawn.x");
                                  double z = settings.getData().getDouble("warps.spawn.z");
    
    int x2 = (int) x;
                                  int z2 = (int) z;
                                
                                  int RanX = random.nextInt(x2 + 340) +-1;
                                  int RanY = 150;
                                  int RanZ = random.nextInt(z2 + 320) +-1;
                                
                                  Location teleportLocation = new Location(playerInArena.getWorld(), RanX, RanY, RanZ);
                                  playerInArena.teleport(teleportLocation);
    If i mocked up your code like i just did, and changed things it said teleportLocation could not be defined to a type when i put it in <warpLocation> on your code.

    Sorry, sounds stupid but yeah it needs to get the cords from that file. Mind helping me out there?

    This is what i mocked up:
    Code:
                                  double x = settings.getData().getDouble("warps.spawn.x");
                                  double z = settings.getData().getDouble("warps.spawn.z");
                                  double y = 150;
                                
                                  Location teleportLocation2 = new Location(playerInArena.getWorld(), x, y, z);
                                
                                          int Genx = generateInt(-340, 340);
                                     int Genz = generateInt(-16, 16);
                                     teleportLocation2.add(x, teleportLocation2.getHighestBlockAt(x, z), z);
    However i get an error on getHighestBlockAt saying: The method getHighestBlockAt(double, double) is undefined for the type Location

    Thanks.
    EDIT:

    No error's on this, think this makes sense.


    Code:
    double x = settings.getData().getDouble("warps.spawn.x");
                                  double z = settings.getData().getDouble("warps.spawn.z");
                                  double y = 150;
                                 
                                  Location teleportLocation2 = new Location(playerInArena.getWorld(), x, y, z);
                                 
                                          int Genx = generateInt(-340, 340);
                                          int Genz = generateInt(-320, 320);
                                    
                                     teleportLocation2.add(Genx, y, Genz);
                                     playerInArena.teleport(teleportLocation2);
     
    Last edited: Feb 18, 2016
  9. Offline

    Lightspeed

    add the randomly generated number for both x and y 2 instances
    and getHighestBlock is world not location
    SO
    double x = settings.getData().getDouble("warps.spawn.x") + generateInt(-16, 16);
    double z = settings.getData().getDouble("warps.spawn.z") + generateInt(-16, 16);
    double y = location.getWorld().getHighestBlockAt(x, z) + 50;
    xyz done. all randomized with 50 blocks above the ground always.

    this will make an area for the number you put in the generate int that will randomly generate a radius around your location
     
  10. Offline

    Xerox262

    @kayc01 You want to get a number between -360 and +360? There's no actual way to do this with the random class, however there's a way to cheat it. If you want a possible negative number all you do is double what you're searching for then remove the original amount, this way it will be between the numbers.

    Below is how you would get a number between -50 and 50
    Code:
    int numberBetween = random.nextInt(101) - 50; // 0 is a number too, you have to account for that
     
    Lightspeed likes this.
  11. GetHihestBlockAt does not allow (double, double), it needs (int, int) however my warps are double's.

    Also, before when i tried the two:

    int Genx = generateInt(-340, 340);
    int Genz = generateInt(-320, 320);

    In console there was this error:

    Caused by: java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_73]
    at me.Ckay.pvp.Main.generateInt(Main.java:117) ~[?:?]
    at me.Ckay.pvp.Main.onCommand(Main.java:679) ~[?:?]
     
  12. Offline

    Xerox262

    @kayc01 You cannot call Random#nextInt(); on a negative number, you need to use what I explained above
     
  13. Offline

    mcdorli

    Learn java before attempting to dive in bukkit.

    1.: Just round your location and cast it to an int.
    2.: Random can only use positiive numbers, if you want to use values between a negative and a positive nmbers, then you need to do Random#nextInt(2x) - x
     
  14. Offline

    Xerox262

    @mcdorli If you're going for 2x then it would be 2x +1 to make up for the number 0 in the middle.
     
  15. How would i use this then? As it is between -340 and 340 off of the spawn cords would it be.


    Code:
    Random random = new Random();
                                   
                                     int numberBetweenX = random.nextInt(681) - 340;
                                     int numberBetweenZ = random.nextInt(641) - 320;
                                   
                                     double x = settings.getData().getDouble("warps.spawn.x");
                                     double z = settings.getData().getDouble("warps.spawn.z");
                                   
                                     int x2 = (int) x;
                                     int z2 = (int) z;
                                   
                                     int RanX = x2 + numberBetweenX;
                                     int RanZ = z2 + numberBetweenZ;
                                     int RanY = 150;
                                   
                                     Location teleportLocation3 = new Location(playerInArena.getWorld(), RanX, RanZ, RanY);
                                     playerInArena.teleport(teleportLocation3);
    Thanks a lot so far guys! Starting to make sense about the whole positive's thing.

    Realized a quick thing,
    As these are +'s


    Code:
    int RanX = x2 + numberBetweenX;
                                     int RanZ = z2 + numberBetweenZ;
    Does that mean its only going to be higher than the cords of x2 and z2?
     
    Xerox262 likes this.
  16. Offline

    Xerox262

    @kayc01 Yep, that's how you would do it, however you don't really need those extra variables and assuming this is in a for loop there's no need to create a new random object for each player, nor is there a need to get the spawn x and spawn z every time if it doesn't change.

    Like I said before, if it was a + - then it would subtract not add, so for example your code runs, it generates 2 numbers for you, lets say it generates

    betweenX is -300 and betweenZ is 245
    Then it adds it
    x + - 300 // cancels out the + and subtracts 300
    z + 245 // adds 245 to the number
     
  17. That's great! Derr my brain lol, the + - cancellation is basic math rules that i should of remembered from high school.
    Ill give it a test now, thanks for you help. I also moved out the random and double x, y stuff from the loop. :) !

    Marked as solved, thanks for the help everyone but mainly @Xerox262 for the alternate solution.
    It works great, there is more variation in the random tp now :D

    P.S: If anyone notices, i got the RanZ and RanY the wrong way around the first time so i got teleported like 600 blocks into the sky. ahah

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 18, 2016
    Xerox262 likes this.
  18. Offline

    Lightspeed

    Sorry for that derp wasn't thinking.
    I can only do 126 lines of code that work per month. >:C
     
Thread Status:
Not open for further replies.

Share This Page