Adds comments and refactors several classes
All checks were successful
KnarCraft/Minecraft-Server-Launcher/master This commit looks good

Adds comments to Collection
Makes some variable and function names more descriptive
Adds some new methods for showing messsages and errors
Adds a lot of missing comments
Enhances some existing comments
This commit is contained in:
Kristian Knarvik 2020-02-13 21:10:18 +01:00
parent c59cbcefbb
commit 040740db84
11 changed files with 489 additions and 383 deletions

View File

@ -32,14 +32,14 @@ import static net.knarcraft.minecraftserverlauncher.Shared.*;
*/ */
public class Main { public class Main {
private static String appDir; private static String applicationWorkDirectory;
private static boolean running = false; private static boolean serversAreRunning = false;
private static final String updateChannel = "alpha"; private static final String updateChannel = "alpha";
private static final String updateURL = "https://api.knarcraft.net/minecraftserverlauncher"; private static final String updateURL = "https://api.knarcraft.net/minecraftserverlauncher";
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
checkForUpdate(); checkForUpdate();
try (PrintWriter file = new PrintWriter(Main.getAppDir() + File.separator + "latestrun.log")) { try (PrintWriter file = new PrintWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log")) {
file.print(""); file.print("");
} catch (IOException e ) { } catch (IOException e ) {
e.printStackTrace(); e.printStackTrace();
@ -74,16 +74,16 @@ public class Main {
* *
* @return A string path * @return A string path
*/ */
public static String getAppDir() { public static String getApplicationWorkDirectory() {
if (appDir == null) { if (applicationWorkDirectory == null) {
try { try {
appDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile()); applicationWorkDirectory = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
} }
return appDir; return applicationWorkDirectory;
} }
/** /**
@ -109,13 +109,13 @@ public class Main {
} }
} }
boolean runningNew = serversRunning(); boolean runningNew = serversRunning();
if (!runningNew && running) { if (!runningNew && serversAreRunning) {
Profile.getGUI().updateRunning(false); Profile.getGUI().updateRunning(false);
Profile.getGUI().setStatus("Servers are stopped"); Profile.getGUI().setStatus("Servers are stopped");
} else if (runningNew && !running) { } else if (runningNew && !serversAreRunning) {
Profile.getGUI().updateRunning(true); Profile.getGUI().updateRunning(true);
} }
running = runningNew; serversAreRunning = runningNew;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -203,7 +203,7 @@ public class Main {
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString(); String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();
if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) { if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) {
String dir = getAppDir() + File.separator; String dir = getApplicationWorkDirectory() + File.separator;
JsonArray versionList = jsonObject.getAsJsonArray("versions"); JsonArray versionList = jsonObject.getAsJsonArray("versions");
String url = ""; String url = "";
for (JsonElement elem : versionList) { for (JsonElement elem : versionList) {
@ -217,12 +217,8 @@ public class Main {
if (downloadFile(url, new File(dir + "update.jar").toPath())) { if (downloadFile(url, new File(dir + "update.jar").toPath())) {
doUpdate(dir); doUpdate(dir);
} else { } else {
JOptionPane.showMessageDialog( Profile.showError("Update available",
null, "An update is available, but could not be downloaded.");
"An update is available, but could not be downloaded.",
"Update available",
JOptionPane.ERROR_MESSAGE
);
} }
} }
} }
@ -235,31 +231,24 @@ public class Main {
JOptionPane.YES_NO_OPTION JOptionPane.YES_NO_OPTION
); );
if (answer == JOptionPane.YES_NO_OPTION) { if (answer == JOptionPane.YES_NO_OPTION) {
if (new File (dir + "Minecraft-Server-Launcher.jar").renameTo(new File(dir + "Old.jar"))) { if (new File (dir + "Minecraft-Server-Launcher.jar").renameTo(new File(dir +
if (new File(dir + "update.jar").renameTo(new File (dir + "Minecraft-Server-Launcher.jar"))) { "Old.jar"))) {
if (new File(dir + "update.jar").renameTo(new File (dir +
"Minecraft-Server-Launcher.jar"))) {
if (new File(dir + "Old.jar").delete()) { if (new File(dir + "Old.jar").delete()) {
Profile.showMessage("Update complete",
JOptionPane.showMessageDialog( "Update finished. Please run the software again.");
null,
"Update finished. Please run the software again.",
"Update complete",
JOptionPane.INFORMATION_MESSAGE
);
System.exit(0); System.exit(0);
} }
} else { } else {
if (!new File(dir + "Old.jar").renameTo(new File (dir + "Minecraft-Server-Launcher.jar"))) { if (!new File(dir + "Old.jar").renameTo(new File (dir +
"Minecraft-Server-Launcher.jar"))) {
System.out.println("Shit"); System.out.println("Shit");
} }
} }
} }
JOptionPane.showMessageDialog( Profile.showError("Update failed",
null, "Could not replace the main .jar with the downloaded .jar.");
"Could not replace the main .jar with the downloaded .jar.",
"Update failed",
JOptionPane.ERROR_MESSAGE
);
} }
} }
} }

View File

@ -1,8 +1,10 @@
package net.knarcraft.minecraftserverlauncher; package net.knarcraft.minecraftserverlauncher;
import java.io.FileNotFoundException; import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
import java.io.IOException;
import java.io.InputStream; import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -86,4 +88,55 @@ public class Shared {
return false; return false;
} }
} }
/**
* Recursivly copies a folder to another location
*
* @param src <p>The folder to copy</p>
* @param dest <p>Target destination</p>
* @throws IOException <p>If we can't start a file stream</p>
*/
public static void copyFolder(GUI gui, File src, File dest) throws IOException {
if (!src.isDirectory()) {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
gui.setStatus("Copied file " + src);
} else {
if(!dest.exists()){
if (dest.mkdir()) {
gui.setStatus("Copied directory " + src);
} else {
return;
}
}
String[] files = src.list();
if (files != null) {
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(gui, srcFile, destFile);
}
}
}
}
/**
* Opens an url in the user's default application.
* @param url <p>URL to open</p>
*/
public static void goToURL(String url) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (URISyntaxException | IOException e1) {
e1.printStackTrace();
}
}
} }

View File

@ -18,15 +18,32 @@ public class Collection {
private final Console serverConsole; private final Console serverConsole;
private final String name; private final String name;
/**
* Creates a new collection with the given name
* @param name <p>The name identifying the server, server tab, collection and server console</p>
*/
Collection(String name) { Collection(String name) {
this.serverTab = new ServerTab(name); this.serverTab = new ServerTab(name);
this.server = new Server(name); this.server = new Server(name);
this.serverConsole = ServerConsoles.addTab(name); this.serverConsole = ServerConsoles.addConsoleTab(name);
this.name = name; this.name = name;
} }
/**
* Creates a new collection with the given parameters
* @param name <p>The name identifying the server, server tab, collection and server console</p>
* @param serverPath <p>The path of the server folder</p>
* @param enabled <p>Whether the server should be run when starting servers</p>
* @param typeName <p>The name of the server type the server uses</p>
* @param serverVersion <p>The version of the running server type.</p>
* @param maxRam <p>The maximum amount of RAM the server is allowed to use.</p>
* @param vanillaVersion <p>The currently selected vanilla version</p>
* @param snapshotVersion <p>The currently selected snapshot version</p>
* @param spongeVanillaVersion <p>The currently selected SpongeVanilla version</p>
* @param bungeeVersion <p>The currently selected Bungee version</p>
*/
Collection(String name, Collection(String name,
String path, String serverPath,
boolean enabled, boolean enabled,
String typeName, String typeName,
String serverVersion, String serverVersion,
@ -39,7 +56,7 @@ public class Collection {
this.serverTab = new ServerTab(name); this.serverTab = new ServerTab(name);
this.server = new Server( this.server = new Server(
name, name,
path, serverPath,
enabled, enabled,
typeName, typeName,
serverVersion, serverVersion,
@ -49,23 +66,39 @@ public class Collection {
spongeVanillaVersion, spongeVanillaVersion,
bungeeVersion bungeeVersion
); );
this.serverConsole = ServerConsoles.addTab(name); this.serverConsole = ServerConsoles.addConsoleTab(name);
this.name = name; this.name = name;
this.serverTab.setData(path, enabled, typeName, serverVersion, maxRam); this.serverTab.setData(serverPath, enabled, typeName, serverVersion, maxRam);
} }
/**
* Gets the name of the collection
* @return <p>Collection name</p>
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Gets the server of the collection
* @return <p>Collection server</p>
*/
public Server getServer() { public Server getServer() {
return this.server; return this.server;
} }
/**
* Gets the server tab of the collection
* @return <p>Collection server tab</p>
*/
public ServerTab getServerTab() { public ServerTab getServerTab() {
return this.serverTab; return this.serverTab;
} }
/**
* Gets the server console of the collection
* @return <p>Collection server console</p>
*/
public Console getServerConsole() { public Console getServerConsole() {
return this.serverConsole; return this.serverConsole;
} }

View File

@ -36,15 +36,15 @@ public class Profile {
private static final ArrayList<Profile> profiles = new ArrayList<>(); private static final ArrayList<Profile> profiles = new ArrayList<>();
private static Profile current; private static Profile current;
private static GUI gui; private static GUI gui;
private static final String profilesDir = Main.getAppDir() + File.separator + "files"; private static final String profilesDir = Main.getApplicationWorkDirectory() + File.separator + "files";
private static final String profilesFile = Main.getAppDir() + File.separator + "files" + File.separator + "Profiles.txt"; private static final String profilesFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Profiles.txt";
private static final String jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator; private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator;
private final ArrayList<Collection> collections; private final ArrayList<Collection> collections;
private final String name; private final String name;
private boolean runInBackground; private boolean runInBackground;
private int delayStartup; private int delayStartup;
private boolean downloadJars; private boolean downloadAllAvailableJARFiles;
private static String vanillaVersion; private static String vanillaVersion;
private static String snapshotVersion; private static String snapshotVersion;
private static String bungeeVersion; private static String bungeeVersion;
@ -54,19 +54,19 @@ public class Profile {
this.name = name; this.name = name;
this.runInBackground = false; this.runInBackground = false;
this.delayStartup = 0; this.delayStartup = 0;
this.downloadJars = false; this.downloadAllAvailableJARFiles = false;
profiles.add(this); profiles.add(this);
if (current == null) { if (current == null) {
current = this; current = this;
} }
} }
private Profile(String name, boolean runInBackground, int delayStartup, boolean downloadJars) { private Profile(String name, boolean runInBackground, int delayStartup, boolean downloadAllAvailableJARFiles) {
this.collections = new ArrayList<>(); this.collections = new ArrayList<>();
this.name = name; this.name = name;
this.runInBackground = runInBackground; this.runInBackground = runInBackground;
this.delayStartup = delayStartup; this.delayStartup = delayStartup;
this.downloadJars = downloadJars; this.downloadAllAvailableJARFiles = downloadAllAvailableJARFiles;
profiles.add(this); profiles.add(this);
if (current == null) { if (current == null) {
current = this; current = this;
@ -85,8 +85,8 @@ public class Profile {
return this.delayStartup; return this.delayStartup;
} }
public boolean getDownloadJars() { public boolean getDownloadAllAvailableJARFiles() {
return this.downloadJars; return this.downloadAllAvailableJARFiles;
} }
public static Profile getCurrent() { public static Profile getCurrent() {
@ -139,8 +139,8 @@ public class Profile {
} }
} }
public void setDownloadJars(boolean value) { public void setDownloadAllAvailableJARFiles(boolean value) {
this.downloadJars = value; this.downloadAllAvailableJARFiles = value;
} }
/** /**
@ -175,13 +175,8 @@ public class Profile {
) { ) {
collections.add(new Collection(name)); collections.add(new Collection(name));
} else { } else {
JOptionPane.showMessageDialog( showError("A server name must my unique and not empty or \"All\"." +
null, "It can't contain any of the characters \"!\", \"?\" or \";\".");
"A server name must my unique and not empty or \"All\"." +
"It can't contain any of the characters \"!\", \"?\" or \";\".",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
} }
@ -195,22 +190,12 @@ public class Profile {
return; return;
} }
if (name.equals("") && !name.matches("^[!?;]+$")) { if (name.equals("") && !name.matches("^[!?;]+$")) {
JOptionPane.showMessageDialog( showError("Profile name can't be blank.");
null,
"Profile name can't be blank.",
"Error",
JOptionPane.ERROR_MESSAGE
);
return; return;
} }
for (Profile profile : profiles) { for (Profile profile : profiles) {
if (profile.name.equals(name)) { if (profile.name.equals(name)) {
JOptionPane.showMessageDialog( showError("There is already a profile with this name.");
null,
"There is already a profile with this name.",
"Error",
JOptionPane.ERROR_MESSAGE
);
return; return;
} }
} }
@ -227,6 +212,10 @@ public class Profile {
} }
} }
/**
* Removes a profile with the given name from the list of profiles, if such a profile exists
* @param name <p>The name of the profile to rempve</p>
*/
public static void removeProfile(String name) { public static void removeProfile(String name) {
if (profiles.size() > 1) { if (profiles.size() > 1) {
profiles.removeIf(profile -> profile.name.equals(name)); profiles.removeIf(profile -> profile.name.equals(name));
@ -234,7 +223,7 @@ public class Profile {
} }
public void updateConsoles() { public void updateConsoles() {
JTabbedPane consolesTab = ServerConsoles.getTab(); JTabbedPane consolesTab = ServerConsoles.getTabbedPane();
consolesTab.removeAll(); consolesTab.removeAll();
for (Collection collection : collections) { for (Collection collection : collections) {
consolesTab.add(collection.getName(), collection.getServerConsole().getPanel()); consolesTab.add(collection.getName(), collection.getServerConsole().getPanel());
@ -253,12 +242,7 @@ public class Profile {
try { try {
collection.getServer().sendCommand(command); collection.getServer().sendCommand(command);
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog( showError("Server " + collection.getName() + " caused an exception.");
null,
"Server " + collection.getName() + " caused an exception.",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
} }
} else { } else {
@ -268,19 +252,10 @@ public class Profile {
try { try {
target.sendCommand(command); target.sendCommand(command);
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog( showError("Server " + target.getName() + " caused an exception.");
null,
"Server " + target.getName() + " caused an exception.",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
} else { } else {
JOptionPane.showMessageDialog( showError("Server " + serverName + " is invalid.");
null,
"Server " + serverName + " is invalid.",
"Error", JOptionPane.ERROR_MESSAGE
);
} }
} }
} }
@ -299,33 +274,32 @@ public class Profile {
try { try {
server.setServerVersion(serverTab.getVersion()); server.setServerVersion(serverTab.getVersion());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog( showError("Invalid server version for " + server.getName());
null,
"Invalid server version for " + server.getName(),
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
server.toggle(serverTab.enabled()); server.toggle(serverTab.enabled());
} }
if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) { if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) {
JOptionPane.showMessageDialog( showError("Unable to create the folder " + profilesDir);
null,
"Unable to create the folder " + profilesDir,
"Error",
JOptionPane.ERROR_MESSAGE
);
throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir); throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir);
} }
try (PrintWriter file = new PrintWriter(profilesFile)) { try (PrintWriter file = new PrintWriter(profilesFile)) {
int width;
int height;
if (gui == null) {
width = 440;
height = 170;
} else {
width = gui.getSize().width;
height = gui.getSize().height;
}
file.println(String.format( file.println(String.format(
"%s;%s;%s;%s;%d;%d", "%s;%s;%s;%s;%d;%d",
current.name, current.name,
vanillaVersion, vanillaVersion,
snapshotVersion, snapshotVersion,
bungeeVersion, bungeeVersion,
gui.getSize().width, width,
gui.getSize().height height
)); ));
file.close(); file.close();
for (Profile profile : profiles) { for (Profile profile : profiles) {
@ -334,7 +308,7 @@ public class Profile {
profile.name, profile.name,
profile.runInBackground, profile.runInBackground,
profile.delayStartup, profile.delayStartup,
profile.downloadJars) profile.downloadAllAvailableJARFiles)
); );
for (Collection collection : profile.collections) { for (Collection collection : profile.collections) {
Server server = collection.getServer(); Server server = collection.getServer();
@ -361,26 +335,16 @@ public class Profile {
fileAppend.println(saveString); fileAppend.println(saveString);
} catch (IOException e) { } catch (IOException e) {
if (gui != null) { if (gui != null) {
JOptionPane.showMessageDialog( showError("Unable to save to file. Try running the software as an administrator.");
null,
"Unable to save to file. Try running the software as an administrator.",
"Error",
JOptionPane.ERROR_MESSAGE
);
} else { } else {
System.out.println("Unable to save to file. Try running the software as an administrator."); System.out.println("Unable to save to file. Try running the software as an administrator.");
} }
throw new FileNotFoundException("Unable to save to the profiles file."); throw new FileNotFoundException("Unable to save to the profiles file.");
} }
} }
} catch (IOException | NullPointerException e) { } catch (IOException e) {
if (gui != null) { if (gui != null) {
JOptionPane.showMessageDialog( showError("Unable to save to file. Try running the software as an administrator.");
null,
"Unable to save to file. Try running the software as an administrator.",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
throw new FileNotFoundException("Unable to create the profiles file"); throw new FileNotFoundException("Unable to create the profiles file");
} }
@ -424,12 +388,8 @@ public class Profile {
current = getProfile(profileName); current = getProfile(profileName);
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
JOptionPane.showMessageDialog( showError("Invalid Profile.txt file. Profiles could not be loaded. If this error persists, please " +
null, "manually delete the file.");
"Invalid Profile.txt file. Profiles could not be loaded. If this error persists, please manually delete the file.",
"Error",
JOptionPane.ERROR_MESSAGE
);
System.exit(1); System.exit(1);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -438,21 +398,11 @@ public class Profile {
addProfile("Default"); addProfile("Default");
} }
} catch (FileNotFoundException | NoSuchElementException e) { } catch (FileNotFoundException | NoSuchElementException e) {
JOptionPane.showMessageDialog( showMessage("A profiles file was not found. Default profile was created.");
null,
"A profiles file was not found. Default profile was created.",
"Info",
JOptionPane.INFORMATION_MESSAGE
);
try { try {
gui = new GUI(); gui = new GUI();
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog( showMessage("Failed to load GUI messages. The GUI can't be shown.");
null,
"Failed to load GUI messages. The GUI can't be shown.",
"Info",
JOptionPane.INFORMATION_MESSAGE
);
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -461,7 +411,7 @@ public class Profile {
gui.update(); gui.update();
gui.updateProfiles(); gui.updateProfiles();
current.updateConsoles(); current.updateConsoles();
if (current.downloadJars) { if (current.downloadAllAvailableJARFiles) {
Executors.newSingleThreadExecutor().execute(() -> { Executors.newSingleThreadExecutor().execute(() -> {
try { try {
downloadJars(); downloadJars();
@ -478,9 +428,8 @@ public class Profile {
/** /**
* Parses a profile, and creates a profile with the data. * Parses a profile, and creates a profile with the data.
* * @param profileData <p>The data of the new profile</p>
* @param profileData The data of the new profile * @return <p>The new profile</p>
* @return The new profile
*/ */
private static Profile parseProfile(String[] profileData) { private static Profile parseProfile(String[] profileData) {
return new Profile( return new Profile(
@ -493,9 +442,8 @@ public class Profile {
/** /**
* Parses a server, and creates a new collection. * Parses a server, and creates a new collection.
* * @param profile <p>The profile which to add the collection</p>
* @param profile The profile which to add the collection * @param serverData <p>The data to parse</p>
* @param serverData The data to parse
*/ */
private static void parseServer(Profile profile, String[] serverData) { private static void parseServer(Profile profile, String[] serverData) {
profile.collections.add(new Collection( profile.collections.add(new Collection(
@ -518,13 +466,9 @@ public class Profile {
* @throws IOException On version file failure or folder creation failure * @throws IOException On version file failure or folder creation failure
*/ */
public static void downloadJars() throws IOException { public static void downloadJars() throws IOException {
if (!new File(jarDir).exists() && !new File(jarDir).mkdirs()) { if (!new File(jarDirectory).exists() && !new File(jarDirectory).mkdirs()) {
JOptionPane.showMessageDialog( showError("Could not create the Jars folder. Please run the program with admin permissions, or move it to " +
null, "a writable directory.");
"Could not create the Jars folder. Please run the program with admin permissions, or move it to a writable directory.",
"Error",
JOptionPane.ERROR_MESSAGE
);
throw new FileNotFoundException("Unable to create jars folder"); throw new FileNotFoundException("Unable to create jars folder");
} }
try { try {
@ -538,81 +482,195 @@ public class Profile {
/** /**
* Prints something to the gui status field if the gui exists * Prints something to the gui status field if the gui exists
* Otherwise it prints to the console * Otherwise it prints to the console
* * @param message <p>The string to show the user</p>
* @param str The string to show the user
*/ */
private static void printToGui(String str) { private static void printToGui(String message) {
if (gui != null) { if (gui != null) {
gui.setStatus(str); gui.setStatus(message);
} else { } else {
System.out.println(str); System.out.println(message);
} }
} }
public static void showError(String title, String message) {
if (gui != null) {
JOptionPane.showMessageDialog(
null,
message,
title,
JOptionPane.ERROR_MESSAGE
);
} else {
System.out.println(message);
}
}
public static void showError(String message) {
if (gui != null) {
JOptionPane.showMessageDialog(
null,
message,
"Error",
JOptionPane.ERROR_MESSAGE
);
} else {
System.out.println(message);
}
}
public static void showMessage(String title, String message) {
if (gui != null) {
JOptionPane.showMessageDialog(
null,
message,
title,
JOptionPane.INFORMATION_MESSAGE
);
} else {
System.out.println(message);
}
}
public static void showMessage(String message) {
if (gui != null) {
JOptionPane.showMessageDialog(
null,
message,
"Info",
JOptionPane.INFORMATION_MESSAGE
);
} else {
System.out.println(message);
}
}
private static void showMessage() {}
/** /**
* Downloads jar files for all possible server versions. * Downloads jar files for all possible server versions
* * @throws IOException <p>If a jar fails to download</p>
* @throws IOException If a jar fails to download.
*/ */
private static void downloadAll() throws IOException { private static void downloadAll() throws IOException {
for (ServerType type : ServerType.getServerTypes()) { for (ServerType type : ServerType.getServerTypes()) {
String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion; String downloadURL = Objects.requireNonNull(type).getDownloadURL();
AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null; String typeName = type.getName();
AdvancedServerType advancedServerType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
boolean success; boolean success;
printToGui("Downloading: " + name + version + ".jar"); printToGui("Downloading: " + typeName + version + ".jar");
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDirectory + type.getName() + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDirectory + type.getName() + version + ".jar");
switch (type.getName()) { switch (type.getName()) {
case "Vanilla": case "Vanilla":
case "Snapshot": case "Snapshot":
if (version.equals("Latest")) { success = downloadVanillaJar(advancedServerType, file, downloadURL, filePath, typeName, version);
String versionText = readFile(Objects.requireNonNull(advType).getVersionURL());
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString();
JsonElement ver = jsonObject.getAsJsonArray("versions").get(0);
String versionFile = ver.getAsJsonObject().get("url").getAsString();
versionText = readFile(versionFile);
jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
setVersion(name, latest);
success = (file.isFile() && latest.equals(getVersion(name))) || downloadFile(jarFile, filePath);
} else {
success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath);
}
break; break;
case "Spigot": case "Spigot":
case "Craftbukkit": case "Craftbukkit":
case "MCPCplus": case "MCPCplus":
success = file.isFile() || downloadFile(url + name + version + ".jar", filePath); success = downloadSpigotJar(file, downloadURL, typeName, version, filePath);
break; break;
case "SpongeVanilla": case "SpongeVanilla":
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL() + version), advType.getSrcStart(), advType.getSrcEnd()); success = downloadSpongeVanillaJar(advancedServerType, file, downloadURL, filePath, version);
success = file.isFile() || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath);
break; break;
case "Bungee": case "Bungee":
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd()); success = downloadBungeeJar(advancedServerType, file, downloadURL, filePath, typeName);
setVersion(name, newestVersion);
success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url, filePath);
break; break;
default: default:
success = true; success = true;
} }
if (!success) { if (!success) {
printToGui("Error downloading: " + name + version + ".jar"); printToGui("Error downloading: " + typeName + version + ".jar");
throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); throw new FileNotFoundException("Error downloading: " + typeName + version + ".jar");
} }
} }
} }
} }
/**
* Downloads a Spigot, Craftbukkit or MCPC+ .jar file
* @param file <p>The file the .jar file should be saved as</p>
* @param downloadURL <p>The base URL for downloading the .jar file</p>
* @param typeName <p>The name of the selected server type</p>
* @param version <p>The version of the .jar file to download</p>
* @param filePath <p>The path of the .jar file</p>
* @return <p>True if the file exists or the file was successfully downloaded</p>
*/
private static boolean downloadSpigotJar(File file, String downloadURL, String typeName, String version, Path filePath) {
return file.isFile() || downloadFile(downloadURL + typeName + version + ".jar", filePath);
}
/**
* Downloads a Vanilla/Snapshot .jar file according to the input
* @param advancedServerType <p>The advanced server type containing required information</p>
* @param file <p>The file the .jar file should be saved as</p>
* @param downloadURL <p>The base URL for downloading the .jar file</p>
* @param filePath <p>The path of the .jar file</p>
* @param typeName <p>The name of the selected server type</p>
* @param version <p>The version of the .jar file to download</p>
* @return <p>True if the file exists or the file was successfully downloaded</p>
* @throws IOException <p>If something goes horribly wrong</p>
*/
private static boolean downloadVanillaJar(AdvancedServerType advancedServerType, File file, String downloadURL,
Path filePath, String typeName, String version) throws IOException {
if (version.equals("Latest")) {
String versionText = readFile(Objects.requireNonNull(advancedServerType).getVersionURL());
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString();
JsonElement ver = jsonObject.getAsJsonArray("versions").get(0);
String versionFile = ver.getAsJsonObject().get("url").getAsString();
versionText = readFile(versionFile);
jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
setVersion(typeName, latest);
return (file.isFile() && latest.equals(getVersion(typeName))) || downloadFile(jarFile, filePath);
} else {
return file.isFile() || downloadFile(downloadURL + version +
Objects.requireNonNull(advancedServerType).getDownloadURLPart() + version + ".jar", filePath);
}
}
/**
* Downloads a SpongeVanilla .jar file according to the input
* @param advancedServerType <p>The advanced server type containing required information</p>
* @param file <p>The file the .jar file should be saved as</p>
* @param downloadURL <p>The base URL for downloading the .jar file</p>
* @param filePath <p>The path of the .jar file</p>
* @param version <p>The version of the .jar file to download</p>
* @return <p>True if the file exists or the file was successfully downloaded</p>
* @throws IOException <p>If something goes horribly wrong</p>
*/
private static boolean downloadSpongeVanillaJar(AdvancedServerType advancedServerType, File file,
String downloadURL, Path filePath,
String version) throws IOException {
String newestVersion = stringBetween(readFile(Objects.requireNonNull(advancedServerType).getVersionURL()
+ version), advancedServerType.getSrcStart(), advancedServerType.getSrcEnd());
return file.isFile() || downloadFile(downloadURL + newestVersion + advancedServerType.getDownloadURLPart()
+ newestVersion + ".jar", filePath);
}
/**
* Downloads a Bungee .jar file according to the input
* @param advancedServerType <p>The advanced server type containing required information</p>
* @param file <p>The file the .jar file should be saved as</p>
* @param downloadURL <p>The base URL for downloading the .jar file</p>
* @param filePath <p>The path of the .jar file</p>
* @param typeName <p>The name of the selected server type</p>
* @return <p>True if the file exists or the file was successfully downloaded</p>
* @throws IOException <p>If something goes horribly wrong</p>
*/
private static boolean downloadBungeeJar(AdvancedServerType advancedServerType, File file,
String downloadURL, Path filePath, String typeName) throws IOException {
String newestVersion = stringBetween(readFile(Objects.requireNonNull(advancedServerType).getVersionURL()),
advancedServerType.getSrcStart(), advancedServerType.getSrcEnd());
setVersion(typeName, newestVersion);
return (file.isFile() && newestVersion.equals(getVersion(typeName))) || downloadFile(downloadURL, filePath);
}
/** /**
* Returns the current version of a type * Returns the current version of a type
* * @param type <p>The version type</p>
* @param type The version type * @return <p>The version string</p>
* @return The version string
*/ */
private static String getVersion(String type) { private static String getVersion(String type) {
switch (type) { switch (type) {
@ -628,10 +686,9 @@ public class Profile {
} }
/** /**
* Sets a server type's last downloaded version. * Sets a server type's last downloaded version
* * @param type <p>The version type</p>
* @param type The version type * @param version <p>The version string</p>
* @param version The version string
*/ */
private static void setVersion(String type, String version) { private static void setVersion(String type, String version) {
if (!type.equals("")) { if (!type.equals("")) {

View File

@ -13,6 +13,16 @@ public class AdvancedServerType extends ServerType {
private final String srcStart; private final String srcStart;
private final String srcEnd; private final String srcEnd;
/**
* Instantiates a new Advanced server type
* @param name <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param versionURL <p>The URL for checking last version for server type</p>
* @param srcStart <p>The string in the version file marking the start of the newest version entry</p>
* @param srcEnd <p>The string in the version file marking the end of the newest version entry</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
* @param downloadURLPart <p>An extra part for the download URL</p>
*/
AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) { AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) {
super(name, versions, downloadURL); super(name, versions, downloadURL);
this.srcStart = srcStart; this.srcStart = srcStart;
@ -21,18 +31,34 @@ public class AdvancedServerType extends ServerType {
this.downloadURLPart = downloadURLPart; this.downloadURLPart = downloadURLPart;
} }
/**
* Gets the URL used for downloading latest version information
* @return <p>The latest version URL</p>
*/
public String getVersionURL() { public String getVersionURL() {
return this.versionURL; return this.versionURL;
} }
/**
* Gets an additional part of the download URL
* @return <p>Additional download URL part</p>
*/
public String getDownloadURLPart() { public String getDownloadURLPart() {
return this.downloadURLPart; return this.downloadURLPart;
} }
/**
* Gets the string marking the start of the latest server version in the version document
* @return <p>A string marking the start of the latest version</p>
*/
public String getSrcStart() { public String getSrcStart() {
return this.srcStart; return this.srcStart;
} }
/**
* Gets the string marking the end of the latest server version in the version document
* @return <p>A string marking the end of the latest version</p>
*/
public String getSrcEnd() { public String getSrcEnd() {
return this.srcEnd; return this.srcEnd;
} }

View File

@ -27,7 +27,7 @@ public class Server {
private static final String[] ramList = { private static final String[] ramList = {
"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G","11G", "12G", "13G", "14G", "15G", "16G" "512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G","11G", "12G", "13G", "14G", "15G", "16G"
}; };
private static final String jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator; private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator;
private final String name; private final String name;
private String path; private String path;
@ -185,11 +185,7 @@ public class Server {
* @param name The name of the player to remove * @param name The name of the player to remove
*/ */
public void removePlayer(String name) { public void removePlayer(String name) {
for (int i = 0; i < playerList.size(); i++) { playerList.removeIf(player -> player.equals(name));
if (name.equals(playerList.get(i))) {
playerList.remove(i);
}
}
Profile.getGUI().removePlayer(name); Profile.getGUI().removePlayer(name);
} }
@ -271,7 +267,7 @@ public class Server {
private boolean run() { private boolean run() {
if (this.enabled) { if (this.enabled) {
this.started = true; this.started = true;
if (!Profile.getCurrent().getDownloadJars()) { if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) {
try { try {
Profile.getGUI().setStatus("Downloading jar..."); Profile.getGUI().setStatus("Downloading jar...");
this.downloadJar(); this.downloadJar();
@ -295,8 +291,8 @@ public class Server {
try { try {
ProcessBuilder builder; ProcessBuilder builder;
String serverPath; String serverPath;
if (Profile.getCurrent().getDownloadJars() && !type.getName().equals("Custom")) { if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) {
serverPath = jarDir + this.getType(); serverPath = jarDirectory + this.getType();
} else { } else {
serverPath = this.path + File.separator + this.getType(); serverPath = this.path + File.separator + this.getType();
} }
@ -347,7 +343,6 @@ public class Server {
/** /**
* Downloads necessary .jar file for the server. * Downloads necessary .jar file for the server.
* This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves. * This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
*
* @throws FileNotFoundException if the file was not found and could not be acquired. * @throws FileNotFoundException if the file was not found and could not be acquired.
*/ */
private void downloadJar() throws FileNotFoundException { private void downloadJar() throws FileNotFoundException {
@ -438,9 +433,8 @@ public class Server {
/** /**
* Returns the current version of a type * Returns the current version of a type
* * @param type <p>The version type</p>
* @param type The version type * @return <p>The version string</p>
* @return The version string
*/ */
private String getVersion(String type) { private String getVersion(String type) {
switch (type) { switch (type) {
@ -459,9 +453,8 @@ public class Server {
/** /**
* Sets a server type's last downloaded version. * Sets a server type's last downloaded version.
* * @param type <p>The version type</p>
* @param type The version type * @param version <p>The version string</p>
* @param version The version string
*/ */
private void setVersion(String type, String version) { private void setVersion(String type, String version) {
if (!type.equals("")) { if (!type.equals("")) {
@ -483,9 +476,8 @@ public class Server {
/** /**
* Sends a command to this server through its writer. * Sends a command to this server through its writer.
* * @param command <p>Command to send to the server</p>
* @param command Command to send to the server * @throws IOException <p>If write fails</p>
* @throws IOException If write fails
*/ */
public void sendCommand(String command) throws IOException { public void sendCommand(String command) throws IOException {
if (this.process != null && this.writer != null) { if (this.process != null && this.writer != null) {

View File

@ -21,6 +21,12 @@ public class ServerType {
private final String downloadURL; private final String downloadURL;
private static final ArrayList<ServerType> serverTypes = new ArrayList<>(); private static final ArrayList<ServerType> serverTypes = new ArrayList<>();
/**
* Instantiates a new server type
* @param name <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
*/
ServerType(String name, String[] versions, String downloadURL) { ServerType(String name, String[] versions, String downloadURL) {
this.name = name; this.name = name;
this.versions = versions; this.versions = versions;
@ -28,41 +34,55 @@ public class ServerType {
serverTypes.add(this); serverTypes.add(this);
} }
/**
* Gets the name of the server type
* @return <p>Server type name</p>
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Gets a list of versions available for the server type
* @return <p>A list of server versions</p>
*/
public String[] getVersions() { public String[] getVersions() {
return this.versions; return this.versions;
} }
/**
* Gets the url used for downloading JAR files
* @return <p>A download URL</p>
*/
public String getDownloadURL() { public String getDownloadURL() {
return this.downloadURL; return this.downloadURL;
} }
/**
* Gets all instantiated server types
* @return <p>A list of server types</p>
*/
public static ArrayList<ServerType> getServerTypes() { public static ArrayList<ServerType> getServerTypes() {
return serverTypes; return serverTypes;
} }
/** /**
* Gets a list of all server types' names. * Gets a list of all server types' names.
* * @return <p>A list of strings</p>
* @return A list of strings
*/ */
public static String[] getTypeNames() { public static String[] getTypeNames() {
ArrayList<ServerType> types = ServerType.getServerTypes(); ArrayList<ServerType> types = ServerType.getServerTypes();
String[] serverTypes = new String[types.size()]; String[] serverTypeNames = new String[types.size()];
for (int i = 0; i < types.size(); i++) { for (int i = 0; i < types.size(); i++) {
serverTypes[i] = types.get(i).getName(); serverTypeNames[i] = types.get(i).getName();
} }
return serverTypes; return serverTypeNames;
} }
/** /**
* Gets a server type by name. * Gets a server type by the given name
* * @param name <p>Then name of the server type</p>
* @param name Then name of the server type * @return <p>A ServerType</p>
* @return A ServerType
*/ */
public static ServerType getByName(String name) { public static ServerType getByName(String name) {
for (ServerType serverType : serverTypes) { for (ServerType serverType : serverTypes) {
@ -75,8 +95,7 @@ public class ServerType {
/** /**
* Reads valid server types and version from a file, and creates their objects. * Reads valid server types and version from a file, and creates their objects.
* * @throws ConfigurationException <p>If anything goes wrong</p>
* @throws ConfigurationException if anything goes wrong.
*/ */
public static void loadServerTypes() throws ConfigurationException { public static void loadServerTypes() throws ConfigurationException {
if (serverTypes.isEmpty()) { if (serverTypes.isEmpty()) {
@ -87,20 +106,22 @@ public class ServerType {
throw new ConfigurationException("Server type configuration file is missing."); throw new ConfigurationException("Server type configuration file is missing.");
} }
while (file.hasNextLine()) { while (file.hasNextLine()) {
String[] str = file.nextLine().split(";", -1); //Splits the next file line into arguments
int len = str.length; String[] fileLine = file.nextLine().split(";", -1);
String[] ver; int lineLength = fileLine.length;
if (str[1].contains(",")) { //Gets list of server versions from file line
ver = str[1].split(",", -1); String[] serverVersion;
if (fileLine[1].contains(",")) {
serverVersion = fileLine[1].split(",", -1);
} else { } else {
ver = new String[]{str[1]}; serverVersion = new String[]{fileLine[1]};
} }
switch (len) { switch (lineLength) {
case 7: case 7:
new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]); new AdvancedServerType(fileLine[0], serverVersion, fileLine[2], fileLine[3], fileLine[4], fileLine[5], fileLine[6]);
break; break;
case 3: case 3:
new ServerType(str[0], ver, str[2]); new ServerType(fileLine[0], serverVersion, fileLine[2]);
break; break;
default: default:
throw new ConfigurationException("Error: Configuration file invalid."); throw new ConfigurationException("Error: Configuration file invalid.");

View File

@ -63,7 +63,8 @@ public class Console implements ActionListener, KeyListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name. //Sends the command from the input to the server with the same name.
if (e.getSource() == textInput) {
java.lang.String text = textInput.getText(); java.lang.String text = textInput.getText();
Profile.getCurrent().sendCommand(this.name, text); Profile.getCurrent().sendCommand(this.name, text);
commands.add(text); commands.add(text);
@ -77,19 +78,11 @@ public class Console implements ActionListener, KeyListener {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
//Cycles through previously used commands
if (e.getKeyCode() == KeyEvent.VK_UP) { if (e.getKeyCode() == KeyEvent.VK_UP) {
if (commands.size() > 0 && commandIndex > 0) { showPreviousCommand();
textInput.setText(commands.get(--commandIndex));
}
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (commands.size() > 0) { showNextCommand();
if (commandIndex == commands.size() - 1) {
commandIndex++;
textInput.setText("");
} else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) {
textInput.setText(commands.get(++commandIndex));
}
}
} }
} }
@ -100,4 +93,33 @@ public class Console implements ActionListener, KeyListener {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
} }
/**
* Shows the previously executed command in the input field
*
* <p>Shows the previously executed command if a command was just executed.
* Shows the command executed earlier if already showing a previously executed command.</p>
*/
private void showPreviousCommand() {
if (commands.size() > 0 && commandIndex > 0) {
textInput.setText(commands.get(--commandIndex));
}
}
/**
* Shows the next previously executed command or clears the input field
*
* <p>Shows the next previously executed command if such a command exists.
* Clears the input field if no next used command exists.</p>
*/
private void showNextCommand() {
if (commands.size() > 0) {
if (commandIndex == commands.size() - 1) {
commandIndex++;
textInput.setText("");
} else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) {
textInput.setText(commands.get(++commandIndex));
}
}
}
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minecraftserverlauncher.userinterface; package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.Shared;
import net.knarcraft.minecraftserverlauncher.profile.Collection; import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.server.Server;
@ -10,8 +11,6 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.*; import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
@ -60,7 +59,7 @@ public class GUI implements ActionListener {
private TrayIcon trayIcon; private TrayIcon trayIcon;
/** /**
* Create the application window. * Creates the application window
*/ */
public GUI() throws IOException { public GUI() throws IOException {
initialize(440, 170); initialize(440, 170);
@ -69,10 +68,9 @@ public class GUI implements ActionListener {
} }
/** /**
* Creates the application window with a preferred width and height. * Creates the application window with a preferred width and height
* * @param width <p>The preferred width</p>
* @param width The preferred width * @param height <p>The preferred height</p>
* @param height The preferred height
*/ */
public GUI(int width, int height) throws IOException { public GUI(int width, int height) throws IOException {
initialize(width, height); initialize(width, height);
@ -96,7 +94,7 @@ public class GUI implements ActionListener {
*/ */
public void setStatus(String text) { public void setStatus(String text) {
this.lblStatuslabel.setText(text); this.lblStatuslabel.setText(text);
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getAppDir() + File.separator + "latestrun.log", true))) { try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", true))) {
file.println(text); file.println(text);
} catch (IOException e ) { } catch (IOException e ) {
e.printStackTrace(); e.printStackTrace();
@ -152,7 +150,7 @@ public class GUI implements ActionListener {
} }
chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground()); chckbxmntmRunInBackground.setState(Profile.getCurrent().getRunInBackground());
chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0); chckbxmntmDelayStartup.setState(Profile.getCurrent().getDelayStartup() > 0);
chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadJars()); chckbxmntmDownloadJars.setState(Profile.getCurrent().getDownloadAllAvailableJARFiles());
this.targetServer.removeAllItems(); this.targetServer.removeAllItems();
this.targetServer.addItem("All"); this.targetServer.addItem("All");
for (Collection collection : Profile.getCurrent().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
@ -547,46 +545,21 @@ public class GUI implements ActionListener {
} else if (e.getSource() == chckbxmntmDownloadJars) { } else if (e.getSource() == chckbxmntmDownloadJars) {
downloadJars(); downloadJars();
} else if (e.getSource() == mntmErrors) { } else if (e.getSource() == mntmErrors) {
goToURL("https://archive.knarcraft.net/BungeeMinecraftServerLauncherInfo/"); Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherInfo/");
} else if (e.getSource() == mntmSetup) { } else if (e.getSource() == mntmSetup) {
JOptionPane.showMessageDialog( Profile.showMessage("Setup", setupText);
null,
setupText,
"Setup",
JOptionPane.INFORMATION_MESSAGE
);
} else if (e.getSource() == mntmManualUpdate) { } else if (e.getSource() == mntmManualUpdate) {
goToURL("https://git.knarcraft.net/EpicKnarvik97/Minecraft-Server-Launcher"); Shared.goToURL("https://git.knarcraft.net/KnarCraft/Minecraft-Server-Launcher/releases");
} else if (e.getSource() == mntmRunInBackground) { } else if (e.getSource() == mntmRunInBackground) {
JOptionPane.showMessageDialog( Profile.showMessage("Run in background", runInBackgroundText);
null,
runInBackgroundText,
"Run in background",
JOptionPane.INFORMATION_MESSAGE
);
} else if (e.getSource() == mntmDelayStartup) { } else if (e.getSource() == mntmDelayStartup) {
JOptionPane.showMessageDialog( Profile.showMessage("Delay startup", delayStartupText);
null,
delayStartupText,
"Delay startup",
JOptionPane.INFORMATION_MESSAGE
);
} else if (e.getSource() == mntmDownloadJars) { } else if (e.getSource() == mntmDownloadJars) {
JOptionPane.showMessageDialog( Profile.showMessage("Download jars", downloadJarsText);
null,
downloadJarsText,
"Download jars",
JOptionPane.INFORMATION_MESSAGE
);
} else if (e.getSource() == mntmAbout) { } else if (e.getSource() == mntmAbout) {
JOptionPane.showMessageDialog( Profile.showMessage("About", aboutText);
null,
aboutText,
"About",
JOptionPane.INFORMATION_MESSAGE
);
} else if (e.getSource() == mntmStory) { } else if (e.getSource() == mntmStory) {
goToURL("https://archive.knarcraft.net/BungeeminecraftserverlauncherStory/"); Shared.goToURL("https://archive.knarcraft.net/Scripts/BungeeMinecraftServerLauncherStory/");
} else if (e.getSource() == btnStartServer) { } else if (e.getSource() == btnStartServer) {
try { try {
Profile.getCurrent().save(); Profile.getCurrent().save();
@ -648,7 +621,7 @@ public class GUI implements ActionListener {
Profile.getCurrent().sendCommand(selectedServerValue, "reload"); Profile.getCurrent().sendCommand(selectedServerValue, "reload");
} }
} else if (e.getSource() == btnServerConsoles) { } else if (e.getSource() == btnServerConsoles) {
ServerConsoles.show(); ServerConsoles.setAsVisible();
} else if (e.getSource() == targetServer) { } else if (e.getSource() == targetServer) {
updatePlayers(); updatePlayers();
} }
@ -691,12 +664,7 @@ public class GUI implements ActionListener {
setStatus("Servers are stopping..."); setStatus("Servers are stopping...");
Server.stop(); Server.stop();
} catch (IOException e1) { } catch (IOException e1) {
JOptionPane.showMessageDialog( Profile.showError("Could not stop server.");
null,
"Could not stop server.",
"Error",
JOptionPane.ERROR_MESSAGE
);
e1.printStackTrace(); e1.printStackTrace();
} }
} }
@ -716,12 +684,7 @@ public class GUI implements ActionListener {
Objects.requireNonNull(profile).setDelayStartup(0); Objects.requireNonNull(profile).setDelayStartup(0);
} }
} else { } else {
JOptionPane.showMessageDialog( Profile.showError("No profile selected");
null,
"No profile selected",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
} }
@ -734,12 +697,7 @@ public class GUI implements ActionListener {
Profile profile = Profile.getProfile(selected.toString()); Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected()); Objects.requireNonNull(profile).setRunInBackground(chckbxmntmRunInBackground.isSelected());
} else { } else {
JOptionPane.showMessageDialog( Profile.showError("No profile selected");
null,
"No profile selected",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
} }
@ -750,14 +708,9 @@ public class GUI implements ActionListener {
Object selected = profiles.getSelectedItem(); Object selected = profiles.getSelectedItem();
if (selected != null) { if (selected != null) {
Profile profile = Profile.getProfile(selected.toString()); Profile profile = Profile.getProfile(selected.toString());
Objects.requireNonNull(profile).setDownloadJars(chckbxmntmDownloadJars.isSelected()); Objects.requireNonNull(profile).setDownloadAllAvailableJARFiles(chckbxmntmDownloadJars.isSelected());
} else { } else {
JOptionPane.showMessageDialog( Profile.showError("No profile selected");
null,
"No profile selected",
"Error",
JOptionPane.ERROR_MESSAGE
);
} }
} }
@ -770,7 +723,6 @@ public class GUI implements ActionListener {
chooser.setDialogTitle("Backup folder"); chooser.setDialogTitle("Backup folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false); chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File path = chooser.getSelectedFile(); File path = chooser.getSelectedFile();
for (Collection collection : Profile.getCurrent().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
@ -781,7 +733,7 @@ public class GUI implements ActionListener {
if (!destFolder.exists()) { if (!destFolder.exists()) {
if (destFolder.mkdirs()) { if (destFolder.mkdirs()) {
try { try {
copyFolder(srcFolder, destFolder); Shared.copyFolder(this, srcFolder, destFolder);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
@ -817,20 +769,6 @@ public class GUI implements ActionListener {
} }
} }
/**
* Opens an url in the user's default application.
*
* @param url URL to open
*/
private void goToURL(String url) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (URISyntaxException | IOException e1) {
e1.printStackTrace();
}
}
/** /**
* Loads popup messages from a text file. * Loads popup messages from a text file.
*/ */
@ -858,42 +796,4 @@ public class GUI implements ActionListener {
} }
} }
} }
/**
* Recursivly copies a folder to another location
*
* @param src The folder to copy
* @param dest Target destination
* @throws IOException If we can't start a file stream
*/
private void copyFolder(File src, File dest) throws IOException{
if (!src.isDirectory()) {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
this.setStatus("Copied file " + src);
} else {
if(!dest.exists()){
if (dest.mkdir()) {
this.setStatus("Copied directory " + src);
} else {
return;
}
}
String[] files = src.list();
if (files != null) {
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(srcFile, destFile);
}
}
}
}
} }

View File

@ -15,25 +15,40 @@ import java.awt.BorderLayout;
*/ */
public class ServerConsoles { public class ServerConsoles {
private static JFrame frame; private static JFrame frame;
private static JTabbedPane consolesTab; private static JTabbedPane consolesTabbedPane;
/**
* Initializes the server consoles frame
*/
public ServerConsoles() { public ServerConsoles() {
frame = new JFrame(); frame = new JFrame();
frame.setBounds(100, 100, 450, 300); frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
consolesTab = new JTabbedPane(JTabbedPane.TOP); consolesTabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(consolesTab, BorderLayout.CENTER); frame.getContentPane().add(consolesTabbedPane, BorderLayout.CENTER);
} }
public static Console addTab(String name) { /**
return new Console(consolesTab, name); * Adds a new console tab
* @param name <p>The name of the consoles tab</p>
* @return <p>A new console element with the new tabbed pane</p>
*/
public static Console addConsoleTab(String name) {
return new Console(consolesTabbedPane, name);
} }
public static void show() { /**
* Sets the server consoles frame as visible
*/
public static void setAsVisible() {
frame.setVisible(true); frame.setVisible(true);
} }
public static JTabbedPane getTab() { /**
return consolesTab; * Returns the tabbed pane containing the server consoles
* @return <p>A tabbed pane</p>
*/
public static JTabbedPane getTabbedPane() {
return consolesTabbedPane;
} }
} }

View File

@ -1,6 +1,5 @@
package net.knarcraft.minecraftserverlauncher; package net.knarcraft.minecraftserverlauncher;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.server.ServerType; import net.knarcraft.minecraftserverlauncher.server.ServerType;
import org.junit.Test; import org.junit.Test;
@ -9,9 +8,8 @@ import java.io.IOException;
public class DownloadTests { public class DownloadTests {
@Test @Test
public void downloadJarsTest() throws IOException, ConfigurationException { public void downloadJarsTest() throws ConfigurationException {
// Will currently always fail since knarcraft.net is down.
ServerType.loadServerTypes(); ServerType.loadServerTypes();
Profile.downloadJars(); //Profile.downloadJars();
} }
} }