88
99public class ForgeServerLauncher {
1010
11+ // ANSI颜色代码
12+ private static final String RESET = "\u001B [0m" ;
13+ private static final String BLACK = "\u001B [30m" ;
14+ private static final String RED = "\u001B [31m" ;
15+ private static final String GREEN = "\u001B [32m" ;
16+ private static final String YELLOW = "\u001B [33m" ;
17+ private static final String BLUE = "\u001B [34m" ;
18+ private static final String PURPLE = "\u001B [35m" ;
19+ private static final String CYAN = "\u001B [36m" ;
20+ private static final String WHITE = "\u001B [37m" ;
21+
1122 /**
1223 * 主程序入口
1324 * 该程序主要用于查找并启动最新的Forge或NeoForge版本
@@ -16,7 +27,7 @@ public class ForgeServerLauncher {
1627 */
1728 public static void main (String [] args ) {
1829 // 打印当前工作目录,用于调试和确认程序运行环境
19- System .out .println ("Current working directory: " + new File ("." ).getAbsolutePath ());
30+ System .out .println (GREEN + "Current working directory: " + new File ("." ).getAbsolutePath () + RESET );
2031
2132 // 定义Forge和NeoForge的目录路径
2233 String neoforgePath = "libraries/net/neoforged/neoforge" ;
@@ -31,23 +42,23 @@ public static void main(String[] args) {
3142
3243 // 检查NeoForge目录是否存在并添加符合条件的版本目录
3344 if (neoforgeDir .exists () && neoforgeDir .isDirectory ()) {
34- System .out .println ("Found NeoForge directory: " + neoforgeDir .getAbsolutePath ());
45+ System .out .println (CYAN + "Found NeoForge directory: " + neoforgeDir .getAbsolutePath () + RESET );
3546 versionDirs .addAll (findVersionDirectories (neoforgeDir ));
3647 } else {
37- System .out .println ("NeoForge directory not found or is not a directory: " + neoforgeDir .getAbsolutePath ());
48+ System .out .println (YELLOW + "NeoForge directory not found or is not a directory: " + neoforgeDir .getAbsolutePath () + RESET );
3849 }
3950
4051 // 检查Forge目录是否存在并添加符合条件的版本目录
4152 if (forgeDir .exists () && forgeDir .isDirectory ()) {
42- System .out .println ("Found Forge directory: " + forgeDir .getAbsolutePath ());
53+ System .out .println (CYAN + "Found Forge directory: " + forgeDir .getAbsolutePath () + RESET );
4354 versionDirs .addAll (findVersionDirectories (forgeDir ));
4455 } else {
45- System .out .println ("Forge directory not found or is not a directory: " + forgeDir .getAbsolutePath ());
56+ System .out .println (YELLOW + "Forge directory not found or is not a directory: " + forgeDir .getAbsolutePath () + RESET );
4657 }
4758
4859 // 如果没有找到任何有效的版本目录,打印提示并退出程序
4960 if (versionDirs .isEmpty ()) {
50- System .out .println ("No valid version directories found." );
61+ System .out .println (RED + "No valid version directories found." + RESET );
5162 return ;
5263 }
5364
@@ -57,14 +68,14 @@ public static void main(String[] args) {
5768 // 定位并读取unix_args.txt文件
5869 File unixArgsFile = new File (latestVersionDir , "unix_args.txt" );
5970 if (!unixArgsFile .exists ()) {
60- System .out .println ("unix_args.txt not found in the latest version directory." );
71+ System .out .println (RED + "unix_args.txt not found in the latest version directory." + RESET );
6172 return ;
6273 }
6374
6475 // 从文件中读取启动参数
6576 List <String > launchArguments = readArgumentsFromFile (unixArgsFile .getAbsolutePath ());
6677 if (launchArguments .isEmpty ()) {
67- System .out .println ("Failed to read arguments from unix_args.txt." );
78+ System .out .println (RED + "Failed to read arguments from unix_args.txt." + RESET );
6879 return ;
6980 }
7081
@@ -79,19 +90,19 @@ public static void main(String[] args) {
7990 String javaExecutable ;
8091
8192 if (fullCommandLine .isEmpty ()) {
82- System .err .println ("Failed to retrieve command line arguments." );
93+ System .err .println (RED + "Failed to retrieve command line arguments." + RESET );
8394 return ;
8495 }
8596
8697 if (info .command ().isPresent ()) {
8798 // 直接使用当前运行的 Java 可执行路径(如 /usr/bin/java 或 C:\...\java.exe)
8899 javaExecutable = info .command ().get ();
89- System .out .println ("Using Java from current process: " + javaExecutable );
100+ System .out .println (GREEN + "Using Java from current process: " + javaExecutable + RESET );
90101 } else {
91102 // 回退到 Java Home
92103 String javaHome = System .getProperty ("java.home" );
93104 javaExecutable = javaHome + File .separator + "bin" + File .separator + "java" ;
94- System .out .println ("Falling back to JAVA_HOME: " + javaExecutable );
105+ System .out .println (YELLOW + "Falling back to JAVA_HOME: " + javaExecutable + RESET );
95106 }
96107
97108 // 提取 JVM 参数(直到 -jar 为止)
@@ -173,7 +184,7 @@ public static void main(String[] args) {
173184 try {
174185 // 获取JAR文件所在目录并设置为工作目录
175186 File jarDir = new File (ForgeServerLauncher .class .getProtectionDomain ().getCodeSource ().getLocation ().toURI ()).getParentFile ();
176- System .out .println ("Working directory for subprocess: " + jarDir .getAbsolutePath ());
187+ System .out .println (CYAN + "Working directory for subprocess: " + jarDir .getAbsolutePath () + RESET );
177188
178189 // 使用ProcessBuilder启动子进程
179190 ProcessBuilder processBuilder = new ProcessBuilder (launchArguments );
@@ -198,7 +209,7 @@ public static void main(String[] args) {
198209 try (BufferedReader errorReader = new BufferedReader (new InputStreamReader (process .getErrorStream ()))) {
199210 String line ;
200211 while ((line = errorReader .readLine ()) != null ) {
201- System .err .println ("ERROR: " + line );
212+ System .err .println (RED + "ERROR: " + line + RESET );
202213 }
203214 } catch (IOException e ) {
204215 e .printStackTrace ();
@@ -227,7 +238,7 @@ public static void main(String[] args) {
227238
228239 // 等待子进程结束并获取退出码然后强制结束进程
229240 int exitCode = process .waitFor ();
230- System .out .println ("Process exited with code: " + exitCode );
241+ System .out .println (GREEN + "Process exited with code: " + exitCode + RESET );
231242 System .exit (0 );
232243
233244 } catch (Exception e ) {
@@ -271,12 +282,12 @@ private static List<File> findVersionDirectories(File parentDir) {
271282 List <File > versionDirs = new ArrayList <>();
272283 File [] files = parentDir .listFiles ();
273284 if (files == null ) {
274- System .err .println ("Unable to list files in directory: " + parentDir .getAbsolutePath ());
285+ System .err .println (RED + "Unable to list files in directory: " + parentDir .getAbsolutePath () + RESET );
275286 return versionDirs ; // 返回空列表
276287 }
277288 for (File dir : files ) {
278289 if (dir .isDirectory ()) {
279- System .out .println ("Checking version directory candidate: " + dir .getAbsolutePath ());
290+ System .out .println (CYAN + "Checking version directory candidate: " + dir .getAbsolutePath () + RESET );
280291 if (isValidVersionDirectory (dir )) {
281292 versionDirs .add (dir );
282293 }
@@ -315,7 +326,7 @@ private static int[] extractVersionNumberParts(File dir) {
315326 */
316327 private static File getLatestVersionDirectory (List <File > versionDirs ) {
317328 if (versionDirs .isEmpty ()) {
318- throw new IllegalArgumentException ("Version directory list is empty." );
329+ throw new IllegalArgumentException (RED + "Version directory list is empty." + RESET );
319330 }
320331
321332 File latestVersionDir = null ;
@@ -373,7 +384,7 @@ private static List<String> readUserJvmArgs(String filePath) {
373384 File file = new File (filePath );
374385
375386 if (!file .exists ()) {
376- System .out .println ("user_jvm_args.txt not found, using default JVM arguments." );
387+ System .out .println (YELLOW + "user_jvm_args.txt not found, using default JVM arguments." + RESET );
377388 return jvmArgs ;
378389 }
379390
@@ -397,7 +408,7 @@ private static List<String> readUserJvmArgs(String filePath) {
397408 }
398409 }
399410 } catch (IOException e ) {
400- System .err .println ("Error reading user_jvm_args.txt: " + e .getMessage ());
411+ System .err .println (RED + "Error reading user_jvm_args.txt: " + e .getMessage () + RESET );
401412 }
402413
403414 return jvmArgs ;
@@ -424,7 +435,7 @@ private static List<String> readArgumentsFromFile(String filePath) {
424435 if (argFile .exists ()) {
425436 arguments .addAll (readArgumentsFromFile (argFile .getAbsolutePath ()));
426437 } else {
427- System .err .println ("Arg file not found: " + argFilePath );
438+ System .err .println (RED + "Arg file not found: " + argFilePath + RESET );
428439 }
429440 } else {
430441 arguments .add (token );
0 commit comments