mirror of
https://github.com/darksaid98/Flashback.git
synced 2026-12-01 14:38:50 +00:00
Public Release
This commit is contained in:
1 parent
666398e480
commit
61bb497a22
126 files changed
+8435
-1240
No files matched your search
@@ -3,6 +3,9 @@ package com.moulberry.flashback;
|
||||
import it.unimi.dsi.fastutil.objects.Object2FloatFunction;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Random;
|
||||
|
||||
public class Utils {
|
||||
|
||||
/*
|
||||
@@ -57,4 +60,92 @@ public class Utils {
|
||||
}
|
||||
}
|
||||
|
||||
public static float fovToFocalLength(float fov) {
|
||||
return 1.0f / (float) Math.tan(Math.toRadians(fov) * 0.5);
|
||||
}
|
||||
|
||||
public static float focalLengthToFov(float focalLength) {
|
||||
return (float) Math.toDegrees(Math.atan(1.0 / focalLength) * 2.0f);
|
||||
}
|
||||
|
||||
public static Random getInternalMathRandom() {
|
||||
for (Class<?> declaredClass : Math.class.getDeclaredClasses()) {
|
||||
try {
|
||||
if (declaredClass.getSimpleName().endsWith("RandomNumberGeneratorHolder")) {
|
||||
Field field = declaredClass.getDeclaredField("randomNumberGenerator");
|
||||
|
||||
if (field.trySetAccessible()) {
|
||||
return (Random) field.get(null);
|
||||
} else {
|
||||
return (Random) UnsafeWrapper.UNSAFE.getObject(UnsafeWrapper.UNSAFE.staticFieldBase(field),
|
||||
UnsafeWrapper.UNSAFE.staticFieldOffset(field));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Flashback.LOGGER.error("Failed to get internal Math random", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final int SECOND_TO_TICKS = 20;
|
||||
private static final int MINUTE_TO_TICKS = SECOND_TO_TICKS * 60;
|
||||
private static final int HOUR_TO_TICKS = MINUTE_TO_TICKS * 60;
|
||||
|
||||
public static int stringToTime(String string) {
|
||||
int number = 0;
|
||||
int modifier = 1;
|
||||
int total = 0;
|
||||
|
||||
for (char c : string.toCharArray()) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
if (modifier > 1) {
|
||||
total += number * modifier;
|
||||
number = 0;
|
||||
modifier = 1;
|
||||
}
|
||||
|
||||
number *= 10;
|
||||
number += c - '0';
|
||||
} else if (c == 's') {
|
||||
modifier *= SECOND_TO_TICKS;
|
||||
} else if (c == 'm') {
|
||||
modifier *= MINUTE_TO_TICKS;
|
||||
} else if (c == 'h') {
|
||||
modifier *= HOUR_TO_TICKS;
|
||||
}
|
||||
}
|
||||
|
||||
total += number * modifier;
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
public static String timeToString(int ticks) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
timeToString(builder, ticks);
|
||||
if (builder.isEmpty()) {
|
||||
return "0s";
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static void timeToString(StringBuilder builder, int ticks) {
|
||||
if (ticks >= HOUR_TO_TICKS) {
|
||||
int hours = ticks / HOUR_TO_TICKS;
|
||||
builder.append(hours).append('h');
|
||||
timeToString(builder, ticks - hours*HOUR_TO_TICKS);
|
||||
} else if (ticks >= MINUTE_TO_TICKS) {
|
||||
int minutes = ticks / MINUTE_TO_TICKS;
|
||||
builder.append(minutes).append('m');
|
||||
timeToString(builder, ticks - minutes*MINUTE_TO_TICKS);
|
||||
} else if (ticks >= SECOND_TO_TICKS) {
|
||||
int seconds = ticks / SECOND_TO_TICKS;
|
||||
builder.append(seconds).append('s');
|
||||
timeToString(builder, ticks - seconds*SECOND_TO_TICKS);
|
||||
} else if (ticks > 0) {
|
||||
builder.append(ticks).append('t');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user