diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java index e873fc2..6980739 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java @@ -1,6 +1,9 @@ package net.knarcraft.minecraftserverlauncher.userinterface; import javax.swing.*; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; /** * This class handles displaying messages to the user @@ -8,6 +11,7 @@ import javax.swing.*; public abstract class MessageHandler implements GUI { private final boolean silent; + private final BufferedWriter writer; /*** * Initializes a new message handler @@ -16,12 +20,18 @@ public abstract class MessageHandler implements GUI { */ public MessageHandler(boolean silent) { this.silent = silent; + this.writer = new BufferedWriter(new OutputStreamWriter(System.out)); } @Override public void showError(String title, String message) { if (silent) { - System.out.println(message); + try { + writer.write(message); + writer.flush(); + } catch (IOException e) { + System.out.println(message); + } } else { showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE); } @@ -35,7 +45,12 @@ public abstract class MessageHandler implements GUI { @Override public void showMessage(String title, String message) { if (silent) { - System.out.println(message); + try { + writer.write(message); + writer.flush(); + } catch (IOException e) { + System.out.println(message); + } } else { showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE); } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java index ea3e57f..74bdb0d 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java @@ -2,6 +2,7 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; +import net.knarcraft.minecraftserverlauncher.userinterface.GUI; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -11,16 +12,27 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.file.Paths; +/** + * The jar builder is responsible for building the newest Spigot and CraftBukkit .jar files using BuildTools + */ public class JarBuilder { private final String buildDirectory; private final String jarDirectory; private final ServerVersionContainer versionContainer; + private final GUI gui; - public JarBuilder(String buildDirectory, String jarDirectory, ServerVersionContainer versionContainer) { + /** + * Instantiates a new jar builder + * @param buildDirectory
The directory containing BuildTool files
+ * @param jarDirectoryThe directory containing downloaded .jar files
+ * @param guiThe GUI to write messages to
+ */ + public JarBuilder(String buildDirectory, String jarDirectory, GUI gui) { this.buildDirectory = buildDirectory; this.jarDirectory = jarDirectory; - this.versionContainer = versionContainer; + this.gui = gui; + this.versionContainer = ServerVersionContainer.getInstance(); } /** @@ -31,9 +43,9 @@ public class JarBuilder { ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev", "latest", "--output-dir", jarDirectory); executeBuildProcess(processBuilder); - System.out.println("Finished building spigot .jar. Moving it to the correct location"); + gui.setStatus("Finished building spigot .jar. Moving it to the correct location"); moveBuiltJar("spigot-", "SpigotLatest.jar"); - System.out.println("Finished moving spigot .jar"); + gui.setStatus("Finished moving spigot .jar"); } /** @@ -44,9 +56,9 @@ public class JarBuilder { ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--compile", "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory); executeBuildProcess(processBuilder); - System.out.println("Finished building craftbukkit .jar. Moving it to the correct location"); + gui.setStatus("Finished building craftbukkit .jar. Moving it to the correct location"); moveBuiltJar("craftbukkit-", "BukkitLatest.jar"); - System.out.println("Finished moving craftbukkit .jar"); + gui.setStatus("Finished moving craftbukkit .jar"); } /** @@ -111,7 +123,7 @@ public class JarBuilder { writer.flush(); if (process.exitValue() == 0) { - System.out.println("Jar building process finished successfully."); + gui.setStatus("Jar building process finished successfully."); } else { throw new RuntimeException("Jar building process failed with exit code " + process.exitValue() + "."); } diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java b/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java index b77787e..ae436db 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/userinterface/FakeGUI.java @@ -1,19 +1,30 @@ package net.knarcraft.minecraftserverlauncher.userinterface; +import java.io.BufferedWriter; import java.io.File; +import java.io.IOException; +import java.io.OutputStreamWriter; public class FakeGUI extends MessageHandler implements GUI { + private final BufferedWriter writer; + /*** * Initializes a new fake gui */ public FakeGUI() { super(true); + this.writer = new BufferedWriter(new OutputStreamWriter(System.out)); } @Override public void setStatus(String message) { - System.out.println(message); + try { + writer.write(message); + writer.flush(); + } catch (IOException e) { + System.out.println(message); + } } @Override diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java index 42f3e2e..ccc29b9 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java @@ -1,7 +1,7 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; -import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; +import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; @@ -27,7 +27,7 @@ public class JarBuilderTest { "BuildTools" + File.separator; jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "testjars" + File.separator; - jarBuilder = new JarBuilder(targetDirectory, jarDirectory, ServerVersionContainer.getInstance()); + jarBuilder = new JarBuilder(targetDirectory, jarDirectory, new FakeGUI()); removeBuildToolsFiles(); }