Solved Getting a safe location

Discussion in 'Spigot Plugin Development' started by DeliciousShoesHD, Jul 13, 2016.

  1. Hey guys!

    I have a problem. I am trying to code that the chest would spawn on a random location. The location must be safe, meaning that chest can spawn on the block. I do not want chests to spawn underground or in the air. Is there a simple way to get it working? I know how to code everything else I need, but I have no idea how to do that.
     
  2. @DeliciousShoesHD

    To force the chest to spawn above ground, use World#getHighestBlockAt(Location). To ensure it doesn't spawn in the air, just add one to the location.
     
  3. 1? To which coordinate should I add 1? Pretty sure Y, idk.
     
  4. lol yes add one to the y value
     
  5. How do I get a random location with a safe y coord?
     
  6. Randomize the X and Z coordinates and use what was said above for the Y.
     
  7. Like that?

    Code (Text):
    Random rn = new Random();
    int x = rn.nextInt(1000) + 1;
    int z = rn.nextInt(1000) + 1;
    Location loc = new Location(world, x, World.getHighestBlockAt(x, z), z);
     
  8. @DeliciousShoesHD
    getHighestBlockAt(int, int) isn't a static method, make sure you use your world variable. Otherwise, yes, that will work.
     
  9. World world = Bukkit.getWorld("world");
    and then in location change World to world?
     
  10. p.teleport(p.getHighestBlockAt(254));

    //typos may be available because I'm on my phone
     
  11. Why not use ThreadLocalRandom?
     
  12. I don't know, I always use Random
     
  13. @camden

    why would you use a ThreadLocalRandom if OP is not running an asynchronous task? ThreadLocalRandom gives better performance when a program is using random numbers from different threads, which is not applicable to this situation. I doubt you would get any performance improvement over using it in this case.

    disclaimer: i haven't ran any actual test myself and i would need to see proof of performance differences before ever deciding to switch