add some utils
This commit is contained in:
parent
5cddc98e83
commit
0d841607f5
133
plugin/src/main/java/com/alttd/cometskyblock/util/Mth.java
Normal file
133
plugin/src/main/java/com/alttd/cometskyblock/util/Mth.java
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
package com.alttd.cometskyblock.util;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Mth {
|
||||
|
||||
public static int floor(float value) {
|
||||
int i = (int)value;
|
||||
return value < (float)i ? i - 1 : i;
|
||||
}
|
||||
|
||||
public static int floor(double value) {
|
||||
int i = (int)value;
|
||||
return value < (double)i ? i - 1 : i;
|
||||
}
|
||||
|
||||
public static float abs(float value) {
|
||||
return Math.abs(value);
|
||||
}
|
||||
|
||||
public static int abs(int value) {
|
||||
return Math.abs(value);
|
||||
}
|
||||
|
||||
public static int ceil(float value) {
|
||||
int i = (int)value;
|
||||
return value > (float)i ? i + 1 : i;
|
||||
}
|
||||
|
||||
public static int ceil(double value) {
|
||||
int i = (int)value;
|
||||
return value > (double)i ? i + 1 : i;
|
||||
}
|
||||
|
||||
public static int clamp(int value, int min, int max) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
public static long clamp(long value, long min, long max) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
public static float clamp(float value, float min, float max) {
|
||||
return value < min ? min : Math.min(value, max);
|
||||
}
|
||||
|
||||
public static double clamp(double value, double min, double max) {
|
||||
return value < min ? min : Math.min(value, max);
|
||||
}
|
||||
|
||||
public static double absMax(double a, double b) {
|
||||
if (a < 0.0D) {
|
||||
a = -a;
|
||||
}
|
||||
|
||||
if (b < 0.0D) {
|
||||
b = -b;
|
||||
}
|
||||
|
||||
return Math.max(a, b);
|
||||
}
|
||||
|
||||
public static int floorDiv(int dividend, int divisor) {
|
||||
return Math.floorDiv(dividend, divisor);
|
||||
}
|
||||
|
||||
public static int nextInt(Random random, int min, int max) {
|
||||
return min >= max ? min : random.nextInt(max - min + 1) + min;
|
||||
}
|
||||
|
||||
public static float nextFloat(Random random, float min, float max) {
|
||||
return min >= max ? min : random.nextFloat() * (max - min) + min;
|
||||
}
|
||||
|
||||
public static double nextDouble(Random random, double min, double max) {
|
||||
return min >= max ? min : random.nextDouble() * (max - min) + min;
|
||||
}
|
||||
|
||||
public static float catmullrom(float delta, float p0, float p1, float p2, float p3) {
|
||||
return 0.5F * (2.0F * p1 + (p2 - p0) * delta + (2.0F * p0 - 5.0F * p1 + 4.0F * p2 - p3) * delta * delta + (3.0F * p1 - p0 - 3.0F * p2 + p3) * delta * delta * delta);
|
||||
}
|
||||
|
||||
public static double smoothstep(double value) {
|
||||
return value * value * value * (value * (value * 6.0D - 15.0D) + 10.0D);
|
||||
}
|
||||
|
||||
public static double smoothstepDerivative(double value) {
|
||||
return 30.0D * value * value * (value - 1.0D) * (value - 1.0D);
|
||||
}
|
||||
|
||||
public static int sign(double value) {
|
||||
if (value == 0.0D) {
|
||||
return 0;
|
||||
} else {
|
||||
return value > 0.0D ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static float triangleWave(float value, float maxDeviation) {
|
||||
return (Math.abs(value % maxDeviation - maxDeviation * 0.5F) - maxDeviation * 0.25F) / (maxDeviation * 0.25F);
|
||||
}
|
||||
|
||||
public static float square(float n) {
|
||||
return n * n;
|
||||
}
|
||||
|
||||
public static double square(double n) {
|
||||
return n * n;
|
||||
}
|
||||
|
||||
public static int square(int n) {
|
||||
return n * n;
|
||||
}
|
||||
|
||||
public static long square(long n) {
|
||||
return n * n;
|
||||
}
|
||||
|
||||
public static int randomBetweenInclusive(Random random, int min, int max) {
|
||||
return random.nextInt(min, max + 1);
|
||||
}
|
||||
|
||||
public static float randomBetween(Random random, float min, float max) {
|
||||
return random.nextFloat() * (max - min) + min;
|
||||
}
|
||||
|
||||
public static float normal(Random random, float mean, float deviation) {
|
||||
return mean + (float)random.nextGaussian() * deviation;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
17
plugin/src/main/java/com/alttd/cometskyblock/util/Pair.java
Normal file
17
plugin/src/main/java/com/alttd/cometskyblock/util/Pair.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package com.alttd.cometskyblock.util;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Pair<X, Y> {
|
||||
|
||||
private final X x;
|
||||
private final Y y;
|
||||
|
||||
public Pair(X x, Y y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.alttd.cometskyblock.util;
|
||||
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
|
||||
import java.util.NavigableMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
@ConfigSerializable
|
||||
public class WeightedChoice<E> {
|
||||
|
||||
private final NavigableMap<Double, E> map = new TreeMap<>();
|
||||
private double total = 0;
|
||||
|
||||
public WeightedChoice<E> add(double weight, E result) {
|
||||
if (weight <= 0) {
|
||||
return this;
|
||||
}
|
||||
total += weight;
|
||||
map.put(total, result);
|
||||
return this;
|
||||
}
|
||||
|
||||
public E next() {
|
||||
double value = ThreadLocalRandom.current().nextDouble() * total;
|
||||
return map.higherEntry(value).getValue();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user