Improves console and GUI messages
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

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
This commit is contained in:
Kristian Knarvik 2021-08-02 17:57:09 +02:00
parent d61239c417
commit 6ec44f1f92
3 changed files with 26 additions and 15 deletions

View File

@ -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);

View File

@ -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 <p>The start of the name of the built file used to find it</p>
* @param newName <p>The new name of the built file</p>
* @return <p>True if the .jar file was moved successfully</p>
*/
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 <p>The process builder to execute</p>
* @return <p>True if the process exited successfully</p>
*/
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 #", " [");
}
}
}

View File

@ -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);