Solved Generating a Random Location that is Safe

Discussion in 'Spigot Help' started by OutcastChris, Jul 10, 2018.

  1. I am trying to generate a location that isn't over water, lava or fire and if it is then it will generate a new location. However this does not work, can anyone help me fix this!

    Code (Text):
    private Location generate(World world, double max, double min, float yaw, float pitch) {
            Random random = new Random();
            Random random2 = new Random();
            int x = (int) (random.nextInt((int) (max-min)) + min);
            int z = (int) (random2.nextInt((int) (max-min)) + min);
            Location location = new Location(world, x, world.getHighestBlockYAt(x,z), z, yaw, pitch);
            int y = (int) (location.getY() - 1);
            Material below = world.getBlockAt(x, y, z).getType();
            while (below == Material.WATER || below == Material.LAVA || below == Material.FIRE) {
                x = (int) (random.nextInt((int) (max-min)) + min);
                z = (int) (random2.nextInt((int) (max-min)) + min);
                location = new Location(world, x, world.getHighestBlockYAt(x,z), z, yaw, pitch);
                y = (int) (location.getY() - 1);
                below = world.getBlockAt(x, y, z).getType();
            }
            return location;
        }
     
  2. Check if the new location is a transparant block (check both feet and body, otherwise you will suffocate) and test if of the new location is a solid block. If not of that is true, create a new location and check if it's safe like the previous sentence.
     
  3. Are there any errors?
     
  4. No there aren't any errors it just don't recognise the blocks.
     
  5. Try printing block's coords. So check if they are right.
     
  6. The Blocks Co-ords are right its just the checking of the solids.
     
  7. So, try printing block's material.. man come on, debug..
     
    • Funny Funny x 1
  8. I managed to fix it, I just needed to check for stationary.

    Code (Text):

    private Location generate(World world, double max, double min, float yaw, float pitch, Player player) {
        Random random = new Random();
        Material below = null;
        Location location = null;
        while (below == null || !(below.isSolid())) {
            int x = (int) (random.nextInt((int) (max-min)) + min);
            int z = (int) (random.nextInt((int) (max-min)) + min);
            location = new Location(world, x, world.getHighestBlockYAt(x,z), z, yaw, pitch);
            int y = (int) (location.getY() - 1);
            below = world.getBlockAt(x, y, z).getType();
            player.sendMessage(String.valueOf(below));
        }
        return location;
    }
     
     
    #8 OutcastChris, Jul 10, 2018
    Last edited: Jul 10, 2018
  9. MiniDigger

    Supporter

    don't create a new random instance every time. you also don't need two random instances. just have one random instance as a field, it will supply you with an infinite stream of random stuff.
    it might also make sense to use a do-while loop to get rid of all the duplicated code you have there.