Improves output logging and fixes some bugs
Fixes an error caused by the BuildTools directory not being created Creates functions for writing to files in CommonFunctions Adds some more error info to the log when BuildTools fails to download Fixes some typos Makes sure all visible text is logged
This commit is contained in:
parent
28291cf4c3
commit
e47b34a472
@ -5,12 +5,12 @@ import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
|
||||
import net.knarcraft.minecraftserverlauncher.server.Server;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.Updater;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@ -35,11 +35,7 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Updater.checkForUpdate(updateURL, updateChannel);
|
||||
try (PrintWriter file = new PrintWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log")) {
|
||||
file.print("");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
CommonFunctions.writeFile(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", "");
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
ServerConsoles.instantiate();
|
||||
|
@ -181,7 +181,7 @@ public class Server {
|
||||
int serverNum = 0;
|
||||
for (Collection collection : controller.getCurrentProfile().getCollections()) {
|
||||
if (!collection.getServer().runServer(serverNum++ == 0)) {
|
||||
controller.getGUI().setStatus("An error occurred. Start aborted");
|
||||
controller.getGUI().showError("An error occurred. Start aborted. Please check the BuildTools log.");
|
||||
try {
|
||||
Server.stop();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
|
@ -44,6 +44,20 @@ public interface GUI {
|
||||
*/
|
||||
void showMessage(String message);
|
||||
|
||||
/**
|
||||
* Logs a message to the logfile
|
||||
*
|
||||
* @param message <p>The message to log</p>
|
||||
*/
|
||||
void logMessage(String message);
|
||||
|
||||
/**
|
||||
* Logs an error to the logfile
|
||||
*
|
||||
* @param error <p>The error to log</p>
|
||||
*/
|
||||
void logError(String error);
|
||||
|
||||
/**
|
||||
* Asks the user for a directory as a file object
|
||||
*
|
||||
|
@ -1,7 +1,11 @@
|
||||
package net.knarcraft.minecraftserverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
@ -12,6 +16,7 @@ public abstract class MessageHandler implements GUI {
|
||||
|
||||
private final boolean silent;
|
||||
private final BufferedWriter writer;
|
||||
private final String logFile = Main.getApplicationWorkDirectory() + File.separator + "latestrun.log";
|
||||
|
||||
/***
|
||||
* Initializes a new message handler
|
||||
@ -23,11 +28,29 @@ public abstract class MessageHandler implements GUI {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logMessage(String message) {
|
||||
try {
|
||||
CommonFunctions.appendFile(logFile, "[Info]: " + message);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logError(String message) {
|
||||
try {
|
||||
CommonFunctions.appendFile(logFile, "[Error]: " + message);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showError(String title, String message) {
|
||||
if (silent) {
|
||||
try {
|
||||
writer.write("Error: ");
|
||||
writer.write("[Error]: ");
|
||||
writer.write(message);
|
||||
writer.newLine();
|
||||
writer.flush();
|
||||
@ -37,6 +60,7 @@ public abstract class MessageHandler implements GUI {
|
||||
} else {
|
||||
showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
logError(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -48,7 +72,7 @@ public abstract class MessageHandler implements GUI {
|
||||
public void showMessage(String title, String message) {
|
||||
if (silent) {
|
||||
try {
|
||||
writer.write(message);
|
||||
writer.write("[Info]: " + message);
|
||||
writer.newLine();
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
@ -57,6 +81,7 @@ public abstract class MessageHandler implements GUI {
|
||||
} else {
|
||||
showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
logMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -14,9 +14,7 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
@ -104,12 +102,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
|
||||
@Override
|
||||
public void setStatus(String text) {
|
||||
this.lblStatuslabel.setText(text);
|
||||
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() +
|
||||
File.separator + "latestrun.log", true))) {
|
||||
file.println(text);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.logMessage(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -115,7 +115,7 @@ public class ServerLauncherMenu implements ActionListener {
|
||||
} else if (actionSource == oldJavaCommandInfoMenuItem) {
|
||||
serverLauncherGUI.showMessage("Old Java command", serverLauncherGUI.getMessage("oldJavaCommandText"));
|
||||
} else if (actionSource == deleteBuiltJarsInfoMenuItem) {
|
||||
serverLauncherGUI.showMessage("Delete build jar files", serverLauncherGUI.getMessage("deleteBuiltJarFilesText"));
|
||||
serverLauncherGUI.showMessage("Delete built jar files", serverLauncherGUI.getMessage("deleteBuiltJarFilesText"));
|
||||
} else if (actionSource == aboutMenuItem) {
|
||||
serverLauncherGUI.showMessage("About", serverLauncherGUI.getMessage("aboutText"));
|
||||
} else if (actionSource == storyMenuItem) {
|
||||
@ -134,7 +134,7 @@ public class ServerLauncherMenu implements ActionListener {
|
||||
downloadJarsCheckBoxMenuItem = createCheckBoxMenuItem("Download jars", mnOptions);
|
||||
javaCommandMenuItem = createMenuItem("Java command", mnOptions);
|
||||
oldJavaCommandMenuItem = createMenuItem("Old Java command", mnOptions);
|
||||
deleteBuiltJarsMenuItem = createMenuItem("Delete build jar files", mnOptions);
|
||||
deleteBuiltJarsMenuItem = createMenuItem("Delete built jar files", mnOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -151,7 +151,7 @@ public class ServerLauncherMenu implements ActionListener {
|
||||
downloadJarsMenuItem = createMenuItem("Download jars", mnOptionsInfo);
|
||||
javaCommandInfoMenuItem = createMenuItem("Java command", mnOptionsInfo);
|
||||
oldJavaCommandInfoMenuItem = createMenuItem("Old Java command", mnOptionsInfo);
|
||||
deleteBuiltJarsInfoMenuItem = createMenuItem("Delete build jar files", mnOptionsInfo);
|
||||
deleteBuiltJarsInfoMenuItem = createMenuItem("Delete built jar files", mnOptionsInfo);
|
||||
|
||||
JMenu mnAbout = new JMenu("About");
|
||||
mnInfo.add(mnAbout);
|
||||
@ -210,14 +210,14 @@ public class ServerLauncherMenu implements ActionListener {
|
||||
* Deletes build Spigot and Bukkit .jar files if the user accepts
|
||||
*/
|
||||
private void deleteBuiltJars() {
|
||||
int answer = JOptionPane.showConfirmDialog(null, "This will delete build .jar files, causing them " +
|
||||
"to be rebuilt on the next run. Do you want to continue?", "Delete build .jar files",
|
||||
int answer = JOptionPane.showConfirmDialog(null, "This will delete built .jar files, causing them " +
|
||||
"to be rebuilt on the next run. Do you want to continue?", "Delete built .jar files",
|
||||
JOptionPane.YES_NO_OPTION
|
||||
);
|
||||
if (answer == JOptionPane.YES_NO_OPTION) {
|
||||
String jarDirectory = controller.getJarDirectory();
|
||||
File spigotFile = new File(jarDirectory + "SpigotLatest");
|
||||
File bukkitFile = new File(jarDirectory + "BukkitLatest");
|
||||
File spigotFile = new File(jarDirectory + "SpigotLatest.jar");
|
||||
File bukkitFile = new File(jarDirectory + "BukkitLatest.jar");
|
||||
boolean success = true;
|
||||
if (spigotFile.exists() && !spigotFile.delete()) {
|
||||
serverLauncherGUI.showError("Unable to delete latest spigot .jar");
|
||||
@ -228,7 +228,7 @@ public class ServerLauncherMenu implements ActionListener {
|
||||
success = false;
|
||||
}
|
||||
if (success) {
|
||||
serverLauncherGUI.showMessage("Deletion successful", "Deleted build .jar files");
|
||||
serverLauncherGUI.showMessage("Deletion successful", "Deleted built .jar files");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
@ -44,7 +45,6 @@ public final class CommonFunctions {
|
||||
public static void createAllFolders() throws FileNotFoundException {
|
||||
createFolder(new File(filesDirectory));
|
||||
createFolder(new File(filesDirectory + File.separator + "Jars"));
|
||||
createFolder(new File(filesDirectory + File.separator + "testjars"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,7 +53,7 @@ public final class CommonFunctions {
|
||||
* @param folder <p>The folder to create</p>
|
||||
* @throws FileNotFoundException <p>If unable to create the folder</p>
|
||||
*/
|
||||
private static void createFolder(File folder) throws FileNotFoundException {
|
||||
protected static void createFolder(File folder) throws FileNotFoundException {
|
||||
if (!folder.exists()) {
|
||||
if (!folder.mkdirs()) {
|
||||
throw new FileNotFoundException("Cannot create necessary directory.");
|
||||
@ -128,6 +128,16 @@ public final class CommonFunctions {
|
||||
return new BufferedReader(new InputStreamReader(new FileInputStream(path)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a buffered writer for writing to a given file
|
||||
* @param path <p>The path to the file to write to</p>
|
||||
* @return <p>A buffered writer for writing to the file</p>
|
||||
* @throws FileNotFoundException <p>If the file does not exist</p>
|
||||
*/
|
||||
public static BufferedWriter getFileWriter(String path) throws FileNotFoundException {
|
||||
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file from disk
|
||||
*
|
||||
@ -139,6 +149,14 @@ public final class CommonFunctions {
|
||||
return CommonFunctions.readBufferedReader(getFileReader(path));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes text to a file and adds a trailing newline
|
||||
*
|
||||
* @param path <p>The path of the file to write to</p>
|
||||
* @param text <p>The text to write</p>
|
||||
* @throws IOException <p>If unable to write to the file</p>
|
||||
*/
|
||||
public static void writeFile(String path, String text) throws IOException {
|
||||
writeFile(path, text, true);
|
||||
}
|
||||
@ -152,7 +170,7 @@ public final class CommonFunctions {
|
||||
* @throws IOException <p>If unable to write to the file</p>
|
||||
*/
|
||||
public static void writeFile(String path, String text, Boolean addTrailingNewline) throws IOException {
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
|
||||
BufferedWriter writer = getFileWriter(path);
|
||||
writer.write(text);
|
||||
if (addTrailingNewline) {
|
||||
writer.newLine();
|
||||
@ -160,6 +178,20 @@ public final class CommonFunctions {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends text to a file
|
||||
*
|
||||
* @param path <p>The path to the file to append to</p>
|
||||
* @param text <p>The text to append</p>
|
||||
* @throws IOException <p>If unable to append to the file</p>
|
||||
*/
|
||||
public static void appendFile(String path, String text) throws IOException {
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
|
||||
writer.write(text);
|
||||
writer.newLine();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a file from a website and replaces the target file
|
||||
*
|
||||
|
@ -4,10 +4,12 @@ import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
|
||||
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
@ -37,6 +39,11 @@ public class JarBuilder {
|
||||
this.gui = gui;
|
||||
this.versionContainer = ServerVersionContainer.getInstance();
|
||||
this.javaCommand = ServerLauncherController.getInstance().getJavaCommand();
|
||||
try {
|
||||
CommonFunctions.createFolder(new File(buildDirectory));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,6 +82,7 @@ public class JarBuilder {
|
||||
* Downloads the latest BuildTools version
|
||||
*/
|
||||
public void downloadBuildTools() {
|
||||
ServerLauncherGUI gui = Main.getController().getGUI();
|
||||
try {
|
||||
String latestVersion = getLatestBuildToolsVersion();
|
||||
boolean exists = new File(buildDirectory + "BuildTools.jar").exists();
|
||||
@ -84,13 +92,16 @@ public class JarBuilder {
|
||||
String buildToolsURL = "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar";
|
||||
boolean success = CommonFunctions.downloadFile(buildToolsURL, Paths.get(buildDirectory + "BuildTools.jar"));
|
||||
if (!success) {
|
||||
Main.getController().getGUI().setStatus("Unable to download the latest BuildTools");
|
||||
gui.setStatus("Unable to download the latest BuildTools");
|
||||
gui.logError("Target: " + buildDirectory + "BuildTools.jar");
|
||||
gui.logError("URL: " + buildToolsURL);
|
||||
} else {
|
||||
versionContainer.setDownloadedBuildToolsVersion(latestVersion);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Main.getController().getGUI().setStatus("Unable to download the latest BuildTools");
|
||||
gui.setStatus("Unable to download the latest BuildTools");
|
||||
gui.logMessage(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@ -21,8 +22,13 @@ public class JarDownloaderTest {
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
|
||||
"testjars" + File.separator;
|
||||
String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
|
||||
try {
|
||||
CommonFunctions.createFolder(new File(filesDirectory + File.separator + "testjars"));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
targetDirectory = filesDirectory + File.separator + "testjars" + File.separator;
|
||||
removeDownloadedFiles();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user