GitSystem/src/main/java/com/io/yutian/gemsystem/util/RandomUtil.java
2024-08-12 17:40:52 +08:00

60 lines
1.5 KiB
Java

package com.io.yutian.gemsystem.util;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.List;
import java.util.Random;
public class RandomUtil {
public static boolean random(double d) {
return d >= new Random().nextFloat();
}
public static Color getRandomColor() {
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
return Color.fromRGB(r, g, b);
}
public static Location getRandomLocation(World world, double minX, double maxX, double minZ, double maxZ) {
double rx = getRandomDouble(minX, maxX, 3);
double rz = getRandomDouble(minZ, maxZ, 3);
int y = world.getHighestBlockAt((int)rx, (int)rz).getY()+1;
Location location = new Location(world, rx, y, rz);
return location;
}
public static <E> E getRandomElement(List<E> list) {
if (list.size() == 0) {
return null;
}
return list.get(getRandomInt(0, list.size()-1));
}
public static String getRandomString(String[] array) {
Random r = new Random();
return array[getRandomInt(0, array.length)];
}
public static int getRandomInt(int min, int max) {
if (min == max) {
return max;
}
Random r = new Random();
int i = min < max ? min : max;
int a = min < max ? max : min;
return r.nextInt(a - i + 1) + i;
}
public static double getRandomDouble(double min, double max, int scl) {
int pow = (int) Math.pow(10, scl);
return Math.floor((Math.random() * (max - min) + min) * pow) / pow;
}
}