Random locations

Discussion in 'Plugin Development' started by cfil360, Apr 28, 2014.

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

    cfil360

    I am trying to generate a random location in an arena, but it won't let me generate a negative number, how would i accomplish this if one of the points has either a x, y, or z that is negative?
    Code:java
    1. Pos1 = -704, 238, 1153
    2. Pos2 = -1078, -37, 652
    3.  
    4.  
     
  2. Offline

    BillyBobJoe168

    Why cant you just generate 2 numbers? If it is 0, teleport to Pos1 and if it is 1, teleport to Pos2.
     
  3. Offline

    cfil360

    BillyBobJoe168 because i want to get a random location within the arena to spawn a chest mid game.
     
  4. Offline

    BillyGalbreath

    Lets say you want a random number between -1000 and 1000. Just do this:

    Code:java
    1.  
    2. Random random=new Random();
    3. int randomNumber=(random.nextInt(2000)-1000);
    4.  
     
    cfil360 likes this.
  5. Offline

    cfil360

    BillyGalbreath that's what im saying both my numbers are negative. I could use Math.abs to the 1st number but then it won't generate a number within the arena.
     
  6. Offline

    BillyGalbreath

    Ah, then use this formula:

    .nextInt(max - min + 1) + min;

    Min and max can be negative, as long as max is indeed > min.
     
    cfil360 likes this.
  7. Offline

    cfil360

    well it didn't error, but it isn't generating a location inside of those specifications aka the arena.
     
  8. Offline

    BillyGalbreath

    Show some code. The formula works, I promise.
     
    cfil360 likes this.
  9. Offline

    cfil360

    BillyGalbreath here you go.
    Code:java
    1. Random r = new Random();
    2.  
    3. Vector min = arena.getCuboid().getMinimumPoint();
    4. Vector max = arena.getCuboid().getMaximumPoint();
    5. //randomly select a location btwn the min block x/y/z and the max block x/y/z
    6. Location loc = new Location(arena.getSpawnLocations().get(0).getWorld(),
    7. r.nextInt(max.getBlockX() - min.getBlockX() + 1) + min.getBlockX(),
    8. r.nextInt(max.getBlockY() - min.getBlockY() + 1) + min.getBlockY(),
    9. r.nextInt(max.getBlockZ() - min.getBlockZ() + 1) + min.getBlockZ());//define location
    10. final Location realLoc = new Location(loc.getWorld(), loc.getX(), loc.getWorld().getHighestBlockYAt(loc), loc.getZ());
    11. Block block = realLoc.getWorld().getBlockAt(realLoc);//get the block
    12. block.setType(Material.CHEST);
    13.  
    14. if(block.getType() == Material.CHEST) {//test to see if it's the correct block
    15. Chest chest = (Chest) block.getState();//cast the block to chest
    16. chest.getBlockInventory().clear(); //clear chest inventory before adding to it
    17. chest = GameSetup.getInstance().fillChest(chest);//fill the chest
    18. chest.update();
    19.  
    20. }
    21.  
    22. arena.sendSound(Sound.NOTE_PLING);
    23.  
    24. arena.sendMessage(ChatColor.GOLD + "A surprise was spotted at " + realLoc.getBlockX() + ", " + realLoc.getBlockY() + ", " + realLoc.getBlockZ() + "!");
     
  10. Offline

    Maurdekye

    cfil360 Assuming your min is, say, -500, and your max is 500.
    You can generate the x and z coordinates using this formula, as explained above by BillyGalbreath, and then take the y from the highest block at that pair;
    Code:java
    1. Random rand = new Random();
    2. int x = rand.nextInt(1001) - 500; // random(max - min + 1) + min
    3. int z = rand.nextInt(1001) - 500;
    4. int y = currentWorld.getHighestBlockAt(x, z);
    5. Location spawnLocation = new Location(currentWorld, x, y, z);


    Edit: Ninja'd.
    And incorrect labeling.
     
    cfil360 likes this.
  11. Offline

    cfil360

    Maurdekye you can look at my code above, the problem is that when i get the cuboid, it doesn't necessarily mean both numbers will be positive. In this case the x of both the min and x is negative.
     
  12. Offline

    Maurdekye

    cfil360 Your code is probably working correctly; the chests are likely just spawning inside the ground / up in the air, because you have a random Y coordinate. You'll want to use the generated x and z to get the highest block at that coordinate pair, and spawn a chest above that.
     
    cfil360 and BillyGalbreath like this.
  13. Offline

    BillyGalbreath

    Hmm. Looks good to me. What makes you say its not generating a valid location? Have you tried outputting numbers in debug? Make sure max is actually max, what does x y z return? What are the two points calculated, etc.

    What maurdekye said makes perfect sense. :)
     
    cfil360 likes this.
  14. Offline

    cfil360

    Maurdekye BillyGalbreath that isn't the problem. I am teleporting to the location as i have it print out the location. There is a chest there and it works perfectly fine, but it is outside of the coordinates.

    edit: I changed my code above to the complete code i am using. Sorry about the formatting, this java code tag doesn't maintain my format.
     
  15. Offline

    Maurdekye

    cfil360 Print out the generated numbers into the console, as well as your min and max values. There's likely a dissension somewhere in your code that's modifying those values before they reach their destination.
     
    cfil360 likes this.
  16. Offline

    cfil360

    Maurdekye BillyGalbreath found out what i did wrong. I have the arena cuboid set to a square, but the arena itself is only a circle within that much larger square. I am sorry to waste your time. That thought never even crossed my mind. Thanks for your help.
     
  17. Offline

    Maurdekye

    cfil360 If you need to make a generated value is within that circle / cylinder / sphere, just check its distance from the centerpoint of the circle; if it's greater, then generate the number again until it's less.
     
  18. Offline

    BillyGalbreath

Thread Status:
Not open for further replies.

Share This Page