From 6ec44f1f926a6002310563bb34ea4c9bcf72a051 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 2 Aug 2021 17:57:09 +0200 Subject: [PATCH] Improves console and GUI messages Adds newlines when writing to console Add Error: on lines containing an error Makes it clearer when a jar build is successful Makes failures show errors rather than throwing exceptions --- .../userinterface/MessageHandler.java | 3 ++ .../utility/JarBuilder.java | 37 +++++++++++-------- .../userinterface/FakeGUI.java | 1 + 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java index 6980739..a2425c0 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java @@ -27,7 +27,9 @@ public abstract class MessageHandler implements GUI { public void showError(String title, String message) { if (silent) { try { + writer.write("Error: "); writer.write(message); + writer.newLine(); writer.flush(); } catch (IOException e) { System.out.println(message); @@ -47,6 +49,7 @@ public abstract class MessageHandler implements GUI { if (silent) { try { writer.write(message); + writer.newLine(); writer.flush(); } catch (IOException e) { System.out.println(message); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java index 74bdb0d..61e0c36 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java @@ -42,10 +42,9 @@ public class JarBuilder { downloadBuildTools(); ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev", "latest", "--output-dir", jarDirectory); - executeBuildProcess(processBuilder); - gui.setStatus("Finished building spigot .jar. Moving it to the correct location"); - moveBuiltJar("spigot-", "SpigotLatest.jar"); - gui.setStatus("Finished moving spigot .jar"); + if (executeBuildProcess(processBuilder) && moveBuiltJar("spigot-", "SpigotLatest.jar")) { + gui.setStatus("Finished moving spigot.jar"); + } } /** @@ -55,10 +54,9 @@ public class JarBuilder { downloadBuildTools(); ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--compile", "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory); - executeBuildProcess(processBuilder); - gui.setStatus("Finished building craftbukkit .jar. Moving it to the correct location"); - moveBuiltJar("craftbukkit-", "BukkitLatest.jar"); - gui.setStatus("Finished moving craftbukkit .jar"); + if (executeBuildProcess(processBuilder) && moveBuiltJar("craftbukkit-", "BukkitLatest.jar")) { + gui.setStatus("Finished moving craftbukkit.jar"); + } } /** @@ -88,27 +86,32 @@ public class JarBuilder { * Moves a built .jar file to its target file * @param searchString

The start of the name of the built file used to find it

* @param newName

The new name of the built file

+ * @return

True if the .jar file was moved successfully

*/ - private void moveBuiltJar(String searchString, String newName) { + private boolean moveBuiltJar(String searchString, String newName) { File[] foundFiles = new File(jarDirectory).listFiles((file) -> file.isFile() && file.getName().startsWith(searchString)); if (foundFiles != null && foundFiles.length == 1) { File newFileLocation = new File(jarDirectory + File.separator + newName); if (newFileLocation.exists()) { if (!newFileLocation.delete()) { - throw new IllegalArgumentException("Unable to delete previously built .jar"); + gui.showError("Unable to delete previously built .jar"); } } if (!foundFiles[0].renameTo(newFileLocation)) { - throw new IllegalArgumentException("Unable to move built .jar"); + gui.showError("Unable to move built .jar"); + } else { + return true; } } + return false; } /** * Starts the build process and initializes * @param processBuilder

The process builder to execute

+ * @return

True if the process exited successfully

*/ - private void executeBuildProcess(ProcessBuilder processBuilder) { + private boolean executeBuildProcess(ProcessBuilder processBuilder) { processBuilder.directory(new File(buildDirectory)); processBuilder.redirectErrorStream(true); try { @@ -120,16 +123,20 @@ public class JarBuilder { writer.write(CommonFunctions.readBufferedReader(reader)); } while (process.isAlive()); + writer.newLine(); writer.flush(); if (process.exitValue() == 0) { - gui.setStatus("Jar building process finished successfully."); + gui.showMessage("Jar building process finished successfully."); + return true; } else { - throw new RuntimeException("Jar building process failed with exit code " + process.exitValue() + "."); + gui.showError("Jar building process failed with exit code " + process.exitValue() + + ". Please check the BuildTools log for errors."); } } catch (IOException e) { e.printStackTrace(); } + return false; } /** @@ -143,4 +150,4 @@ public class JarBuilder { return CommonFunctions.stringBetween(versionDocument, "BuildTools #", " ["); } -} +} \ No newline at end of file diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java b/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java index ae436db..9360483 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java @@ -21,6 +21,7 @@ public class FakeGUI extends MessageHandler implements GUI { public void setStatus(String message) { try { writer.write(message); + writer.newLine(); writer.flush(); } catch (IOException e) { System.out.println(message);