diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index 17f1f74..445dbcb 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -370,17 +370,32 @@ public class Server { private String getJavaCommand() { ServerLauncherController controller = ServerLauncherController.getInstance(); - if (serverVersion.toLowerCase().contains("latest")) { + if (versionAtLeast("1.17")) { return controller.getJavaCommand(); + } else { + return controller.getOldJavaCommand(); + } + } + + /** + * Checks if the current server version is at least the given version + * + * @param version

The version to require

+ * @return

True if the server version is at least the given version

+ */ + private boolean versionAtLeast(String version) { + if (serverVersion.toLowerCase().contains("latest")) { + return true; } else if (serverVersion.contains(".") && serverVersion.split("\\.").length >= 2) { try { - if (Integer.parseInt(serverVersion.split("\\.")[1]) >= 17) { - return controller.getJavaCommand(); - } + return Integer.parseInt(serverVersion.split("\\.")[0]) >= + Integer.parseInt(version.split("\\.")[0]) && + Integer.parseInt(serverVersion.split("\\.")[1]) >= + Integer.parseInt(version.split("\\.")[1]); } catch (NumberFormatException ignored) { } } - return controller.getOldJavaCommand(); + return false; } /** @@ -389,17 +404,7 @@ public class Server { * @throws IOException

If the process cannot be started

*/ private void startServerProcess() throws IOException { - ProcessBuilder builder; - String serverPath; - //Decide the path of the .jar file to be executed - if (type.getName().equals("Custom")) { - serverPath = this.path + File.separator + serverVersion; - } else { - serverPath = jarDirectory + this.type.getName() + serverVersion + ".jar"; - } - builder = new ProcessBuilder(getJavaCommand(), "-Xmx" + this.maxRam, "-Xms512M", - "-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", serverPath, - "nogui"); + ProcessBuilder builder = new ProcessBuilder(generateServerProcessArguments()); builder.directory(new File(this.path)); builder.redirectErrorStream(true); this.process = builder.start(); @@ -417,6 +422,64 @@ public class Server { }, 10, 500, TimeUnit.MILLISECONDS); } + /** + * Generates the process arguments required for starting this Minecraft server + * + * @return

The process arguments required for starting this Minecraft server

+ */ + private List generateServerProcessArguments() { + //Decide the path of the .jar file to be executed + String serverPath; + if (type.getName().equalsIgnoreCase("Custom")) { + serverPath = this.path + File.separator + serverVersion; + } else { + serverPath = jarDirectory + this.type.getName() + serverVersion + ".jar"; + } + + List processArguments = new ArrayList<>(20); + processArguments.add(getJavaCommand()); + processArguments.add("-Xmx" + this.maxRam); + processArguments.add("-Xms512M"); + if (versionAtLeast("1.8")) { + addGarbageCollectorFlags(processArguments); + } + processArguments.add("-Djline.terminal=jline.UnsupportedTerminal"); + processArguments.add("-Dcom.mojang.eula.agree=true"); + processArguments.add("-jar"); + processArguments.add(serverPath); + processArguments.add("nogui"); + return processArguments; + } + + /** + * Adds garbage collector flags to the server process arguments to improve performance + * + * @param processArguments

The process arguments to add the flags to

+ */ + private void addGarbageCollectorFlags(List processArguments) { + processArguments.add("-XX:+UseG1GC"); + processArguments.add("-XX:+ParallelRefProcEnabled"); + processArguments.add("-XX:MaxGCPauseMillis=200"); + processArguments.add("-XX:+UnlockExperimentalVMOptions"); + processArguments.add("-XX:+DisableExplicitGC"); + processArguments.add("-XX:+AlwaysPreTouch"); + boolean excessiveRam = Integer.parseInt(this.maxRam.substring(0, this.maxRam.length() - 1)) > 11; + processArguments.add("-XX:G1NewSizePercent=" + (excessiveRam ? 40 : 30)); + processArguments.add("-XX:G1MaxNewSizePercent=" + (excessiveRam ? 50 : 40)); + processArguments.add("-XX:G1HeapRegionSize=" + (excessiveRam ? "16M" : "8M")); + processArguments.add("-XX:G1ReservePercent=" + (excessiveRam ? 15 : 20)); + processArguments.add("-XX:G1HeapWastePercent=5"); + processArguments.add("-XX:G1MixedGCCountTarget=4"); + processArguments.add("-XX:InitiatingHeapOccupancyPercent=" + (excessiveRam ? 20 : 15)); + processArguments.add("-XX:G1MixedGCLiveThresholdPercent=90"); + processArguments.add("-XX:G1RSetUpdatingPauseTimePercent=5"); + processArguments.add("-XX:SurvivorRatio=32"); + processArguments.add("-XX:+PerfDisableSharedMem"); + processArguments.add("-XX:MaxTenuringThreshold=1"); + processArguments.add("-Dusing.aikars.flags=https://mcflags.emc.gs"); + processArguments.add("-Daikars.new.flags=true"); + } + /** * Updates a single console with process output * @@ -580,4 +643,5 @@ public class Server { this.getMaxRam() ); } + } \ No newline at end of file