Skip to content

Commit

Permalink
- Fixed nogui option causing crashes or ignoring jvm arguments
Browse files Browse the repository at this point in the history
- updated dependency
You can now start the server like the following
  • Loading branch information
vaporvee committed Sep 28, 2024
1 parent ab06ba0 commit b581c65
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
4 changes: 2 additions & 2 deletions boundless-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.vaporvee"
version = "1.0"
version = "1.1.0"

repositories {
mavenCentral()
Expand All @@ -13,7 +13,7 @@ repositories {
dependencies {
implementation("org.apache.logging.log4j:log4j-api:2.24.0")
implementation("org.apache.logging.log4j:log4j-core:2.24.0")
implementation("com.google.code.gson:gson:2.8.8")
implementation("com.google.code.gson:gson:2.8.9")

testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.google.gson.JsonParser;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -15,7 +17,6 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand All @@ -24,6 +25,7 @@ public class BoundlessServer {
private static final String currentDir = System.getProperty("user.dir");
private static final String installerFileName = "installer.jar";
private static final String serverConfigFileName = "boundless-server.json";
private static boolean nogui = false;

public static void main(String[] args) {
writeJvmArgsToFile(args);
Expand Down Expand Up @@ -54,23 +56,6 @@ public static void main(String[] args) {
launchServer();
}


private static List<String> readJvmArgsFromFile() {
Path jvmArgsFile = Path.of(currentDir, "user_jvm_args.txt");
List<String> jvmArgs = new ArrayList<>();

try (BufferedReader reader = Files.newBufferedReader(jvmArgsFile)) {
String line;
while ((line = reader.readLine()) != null) {
jvmArgs.add(line);
}
} catch (IOException e) {
logger.error("Error reading JVM arguments from file: {}", e.getMessage());
}

return jvmArgs;
}

public static String getLatestModpackUrl(String modpackSlug, String channel) {
String apiUrl = "https://api.modrinth.com/v2/project/" + modpackSlug + "/version";

Expand Down Expand Up @@ -107,11 +92,16 @@ public static String getLatestModpackUrl(String modpackSlug, String channel) {
return null;
}

private static void writeJvmArgsToFile(String[] jvmArgs) {
private static void writeJvmArgsToFile(String[] args) {
Path jvmArgsFile = Path.of(currentDir, "user_jvm_args.txt");

logger.warn(args);
try (BufferedWriter writer = Files.newBufferedWriter(jvmArgsFile)) {
for (String arg : jvmArgs) {
for (String arg : args) {
if (arg.equalsIgnoreCase("nogui") || arg.equalsIgnoreCase("--nogui")) {
writeJvmArgsFromRuntime();
nogui = true;
break;
}
writer.write(arg);
writer.newLine();
}
Expand All @@ -121,6 +111,21 @@ private static void writeJvmArgsToFile(String[] jvmArgs) {
}
}

private static void writeJvmArgsFromRuntime() {
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> jvmArgs = runtimeMxBean.getInputArguments();

try (BufferedWriter writer = new BufferedWriter(new FileWriter("user_jvm_args.txt", true))) {
for (String arg : jvmArgs) {
writer.write(arg);
writer.newLine();
}
System.out.println("JVM arguments written to user_jvm_args.txt");
} catch (IOException e) {
e.printStackTrace();
}
}

private static boolean isServerUpdated() {
Path configFilePath = Path.of(currentDir, serverConfigFileName);
if (Files.exists(configFilePath)) {
Expand Down Expand Up @@ -189,7 +194,7 @@ private static void downloadAndExecuteInstaller(String fileURL) {
}

private static void executeInstaller(String[] jarArgs) {
Process installerProcess = executeJarFile(jarArgs, false); // Use false to handle the installer output manually
Process installerProcess = executeJarFile(jarArgs, false);
if (installerProcess == null) {
logger.error("Failed to start the installer.jar process. Exiting program.");
System.exit(1);
Expand Down Expand Up @@ -225,8 +230,6 @@ private static void executeInstaller(String[] jarArgs) {
}

private static void launchServer() {
List<String> jvmArgs = readJvmArgsFromFile();

String neoForgeVersion = "21.1.62";
String osSpecificArgs = System.getProperty("os.name").startsWith("Windows")
? "@libraries/net/neoforged/neoforge/" + neoForgeVersion + "/win_args.txt"
Expand All @@ -235,17 +238,17 @@ private static void launchServer() {
logger.info("Starting Boundless Horizons Server...");

List<String> commandArgs = new ArrayList<>();
commandArgs.addAll(jvmArgs);
commandArgs.add(osSpecificArgs);

String argSuffix = System.getProperty("os.name").startsWith("Windows") ? "%*" : "\"$@\"";
commandArgs.add(argSuffix);
if(nogui){
commandArgs.add("nogui");
}

// Start the server process with inheritIO to take over the terminal
Process serverProcess = executeJarFile(commandArgs.toArray(new String[0]), true);

try {
// Wait for the server process to finish
int exitCode = serverProcess.waitFor();
logger.info("Server process exited with code: {}", exitCode);
} catch (InterruptedException e) {
Expand All @@ -263,7 +266,7 @@ private static Process executeJarFile(String[] javaArgs, boolean inheritIO) {
.directory(new File(currentDir));

if (inheritIO) {
processBuilder.redirectErrorStream(true).inheritIO(); // Inherit I/O for the server
processBuilder.redirectErrorStream(true).inheritIO();
}

Process process = processBuilder.start();
Expand Down

0 comments on commit b581c65

Please sign in to comment.