package cat.nyaa.autobloodmoon.utils;


import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.util.Vector;

import java.util.Random;

public class RandomLocation {
    public static Location RandomLocation(Location loc, int min, int max) {
        World world = loc.getWorld();
        for (int i = 0; i < 10; i++) {
            Block block = getRandomLocation(loc, min, max);
            if (block != null) {
                Block b = world.getBlockAt(block.getX(), block.getY() - 1, block.getZ());
                if (block.getY() > 5) {
                    if (!b.getType().equals(Material.LAVA)) {
                        return block.getLocation().add(new Vector(0.5D, 0.1D, 0.5D));
                    }
                }
            }
        }
        return null;
    }

    private static Block getRandomLocation(Location loc, int min, int max) {
        World world = loc.getWorld();
        int x = min + new Random().nextInt(max - min) + 1;
        int z = min + new Random().nextInt(max - min) + 1;
        if (new Random().nextBoolean()) {
            x *= -1;
        }
        if (new Random().nextBoolean()) {
            z *= -1;
        }
        return world.getHighestBlockAt((int) loc.getX() + x, (int) loc.getZ() + z);
    }
}

@@ -745,17 +745,17 @@
 0.5D, 0.
-1
+5
 D, 0.5D)
@@ -976,21 +976,58 @@
     
+for (
 int 
-x = min +
+i = 0; i %3C 10; i++) %7B%0A            int x =
  new
@@ -1043,37 +1043,31 @@
 .nextInt(max
- - min) + 1;%0A
+);%0A    
         int 
@@ -1073,14 +1073,8 @@
  z =
- min +
  new
@@ -1098,21 +1098,15 @@
 (max
- - min) + 1;%0A
+);%0A    
     
@@ -1155,16 +1155,20 @@
         
+    
 x *= -1;
@@ -1160,32 +1160,36 @@
        x *= -1;%0A
+    
         %7D%0A      
@@ -1182,32 +1182,36 @@
       %7D%0A        
+    
 if (new Random()
@@ -1240,16 +1240,20 @@
         
+    
 z *= -1;
@@ -1253,32 +1253,36 @@
  *= -1;%0A        
+    
 %7D%0A        return
@@ -1279,82 +1279,551 @@
     
-return world.getHighestBlockAt((int) loc.getX() + x, (int) loc.getZ() + z)
+    if (loc.getX() + x %3C 1.0) %7B%0A                x = x - 1;%0A            %7D%0A            if (loc.getZ() + z %3C 1.0) %7B%0A                z = z - 1;%0A            %7D%0A            Block block = world.getHighestBlockAt((int) loc.getX() + x, (int) loc.getZ() + z);%0A            Location randomLocation = block.getLocation();%0A            randomLocation.setY(loc.getY());%0A            double distance = randomLocation.distance(loc);%0A            if (distance %3C= max && distance %3E min + 1) %7B%0A                return block;%0A            %7D%0A        %7D%0A        return null
 ;%0A  

improve RandomLocation