This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
FreshCoffee/src/main/java/com/thebrokenrail/freshcoffee/util/Util.java

70 lines
2.2 KiB
Java

package com.thebrokenrail.freshcoffee.util;
import com.thebrokenrail.freshcoffee.config.HardcodedConfig;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
public final class Util {
public static final int JAVA_VERSION;
static {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf('.');
if (dot != -1) {
version = version.substring(0, dot);
}
}
JAVA_VERSION = Integer.parseInt(version);
}
public static File getJavaDir(String version) {
return new File(HardcodedConfig.INSTALL_DIR, version);
}
public static void deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
getLogger().debug("Deleting: " + directoryToBeDeleted.getAbsolutePath());
//noinspection ResultOfMethodCallIgnored
directoryToBeDeleted.delete();
}
public static Logger getLogger() {
return LogManager.getLogger(HardcodedConfig.TITLE);
}
static String getMainClass() {
switch (FabricLoader.getInstance().getEnvironmentType()) {
case CLIENT: {
return "net.fabricmc.loader.launch.knot.KnotClient";
}
case SERVER: {
return "net.fabricmc.loader.launch.knot.KnotServer";
}
default: {
throw new UnsupportedOperationException();
}
}
}
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
}