Improves writing speed when not using a GUI
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

Uses buffered writers instead of System.out
Uses a GUI for writing JarBuilder messages
Adds some missing comments to the jar builder
This commit is contained in:
Kristian Knarvik 2021-08-02 16:49:35 +02:00
parent 52eacb9980
commit 32c17c6005
4 changed files with 50 additions and 12 deletions

View File

@ -1,6 +1,9 @@
package net.knarcraft.minecraftserverlauncher.userinterface; package net.knarcraft.minecraftserverlauncher.userinterface;
import javax.swing.*; import javax.swing.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/** /**
* This class handles displaying messages to the user * This class handles displaying messages to the user
@ -8,6 +11,7 @@ import javax.swing.*;
public abstract class MessageHandler implements GUI { public abstract class MessageHandler implements GUI {
private final boolean silent; private final boolean silent;
private final BufferedWriter writer;
/*** /***
* Initializes a new message handler * Initializes a new message handler
@ -16,12 +20,18 @@ public abstract class MessageHandler implements GUI {
*/ */
public MessageHandler(boolean silent) { public MessageHandler(boolean silent) {
this.silent = silent; this.silent = silent;
this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
} }
@Override @Override
public void showError(String title, String message) { public void showError(String title, String message) {
if (silent) { if (silent) {
try {
writer.write(message);
writer.flush();
} catch (IOException e) {
System.out.println(message); System.out.println(message);
}
} else { } else {
showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE); showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE);
} }
@ -35,7 +45,12 @@ public abstract class MessageHandler implements GUI {
@Override @Override
public void showMessage(String title, String message) { public void showMessage(String title, String message) {
if (silent) { if (silent) {
try {
writer.write(message);
writer.flush();
} catch (IOException e) {
System.out.println(message); System.out.println(message);
}
} else { } else {
showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE); showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE);
} }

View File

@ -2,6 +2,7 @@ package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -11,16 +12,27 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.file.Paths; import java.nio.file.Paths;
/**
* The jar builder is responsible for building the newest Spigot and CraftBukkit .jar files using BuildTools
*/
public class JarBuilder { public class JarBuilder {
private final String buildDirectory; private final String buildDirectory;
private final String jarDirectory; private final String jarDirectory;
private final ServerVersionContainer versionContainer; private final ServerVersionContainer versionContainer;
private final GUI gui;
public JarBuilder(String buildDirectory, String jarDirectory, ServerVersionContainer versionContainer) { /**
* Instantiates a new jar builder
* @param buildDirectory <p>The directory containing BuildTool files</p>
* @param jarDirectory <p>The directory containing downloaded .jar files</p>
* @param gui <p>The GUI to write messages to</p>
*/
public JarBuilder(String buildDirectory, String jarDirectory, GUI gui) {
this.buildDirectory = buildDirectory; this.buildDirectory = buildDirectory;
this.jarDirectory = jarDirectory; 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", ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev",
"latest", "--output-dir", jarDirectory); "latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder); 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"); 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", ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--compile",
"craftbukkit", "--rev", "latest", "--output-dir", jarDirectory); "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder); 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"); 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(); writer.flush();
if (process.exitValue() == 0) { if (process.exitValue() == 0) {
System.out.println("Jar building process finished successfully."); gui.setStatus("Jar building process finished successfully.");
} else { } else {
throw new RuntimeException("Jar building process failed with exit code " + process.exitValue() + "."); throw new RuntimeException("Jar building process failed with exit code " + process.exitValue() + ".");
} }

View File

@ -1,20 +1,31 @@
package net.knarcraft.minecraftserverlauncher.userinterface; package net.knarcraft.minecraftserverlauncher.userinterface;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class FakeGUI extends MessageHandler implements GUI { public class FakeGUI extends MessageHandler implements GUI {
private final BufferedWriter writer;
/*** /***
* Initializes a new fake gui * Initializes a new fake gui
*/ */
public FakeGUI() { public FakeGUI() {
super(true); super(true);
this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
} }
@Override @Override
public void setStatus(String message) { public void setStatus(String message) {
try {
writer.write(message);
writer.flush();
} catch (IOException e) {
System.out.println(message); System.out.println(message);
} }
}
@Override @Override
public File askForDirectory(String prompt) { public File askForDirectory(String prompt) {

View File

@ -1,7 +1,7 @@
package net.knarcraft.minecraftserverlauncher.utility; package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main; 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.BeforeEach;
import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
@ -27,7 +27,7 @@ public class JarBuilderTest {
"BuildTools" + File.separator; "BuildTools" + File.separator;
jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
"testjars" + File.separator; "testjars" + File.separator;
jarBuilder = new JarBuilder(targetDirectory, jarDirectory, ServerVersionContainer.getInstance()); jarBuilder = new JarBuilder(targetDirectory, jarDirectory, new FakeGUI());
removeBuildToolsFiles(); removeBuildToolsFiles();
} }