Changes were done
This commit is contained in:
Kristian Knarvik 2018-02-17 22:06:26 +01:00
commit 1ec620f87a
8 changed files with 256 additions and 164 deletions

View File

@ -9,12 +9,13 @@ import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static net.knarcraft.serverlauncher.Shared.stringBetween;
//Java 8 required. //Java 8 required.
/** /**
@ -28,13 +29,14 @@ import java.util.concurrent.TimeUnit;
public class Main { public class Main {
private static GUI gui; private static GUI gui;
@SuppressWarnings("CanBeFinal") @SuppressWarnings("CanBeFinal")
public static String jarDir; public static String appDir;
static { static {
try { try {
jarDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile()); appDir = 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);
} }
} }
@ -71,9 +73,8 @@ public class Main {
* Reads from server processes, and writes the output to our custom consoles. * Reads from server processes, and writes the output to our custom consoles.
*/ */
private static void updateConsoles() { private static void updateConsoles() {
Server server;
for (Collection collection : Profile.getCurrent().getCollections()) { for (Collection collection : Profile.getCurrent().getCollections()) {
server = collection.getServer(); Server server = collection.getServer();
if (server.isEnabled() && server.getProcess() != null) { if (server.isEnabled() && server.getProcess() != null) {
try { try {
String readText = server.read(); String readText = server.read();
@ -126,20 +127,4 @@ public class Main {
} }
} }
} }
/**
* Finds a substring between two substrings in a string.
*
* @param string The string containing the substrings
* @param start The substring before the wanted substring
* @param end The substring after the wanted substring
* @return The wanted substring.
*/
public static String stringBetween(String string, String start, String end) {
int startPos = string.indexOf(start) + start.length();
if (!string.contains(start) || string.indexOf(end, startPos) < startPos) {
return "";
}
return string.substring(startPos, string.indexOf(end, startPos));
}
} }

View File

@ -0,0 +1,57 @@
package net.knarcraft.serverlauncher;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
public class Shared {
/**
* Finds a substring between two substrings in a string.
*
* @param string The string containing the substrings
* @param start The substring before the wanted substring
* @param end The substring after the wanted substring
* @return The wanted substring.
*/
public static String stringBetween(String string, String start, String end) {
int startPos = string.indexOf(start) + start.length();
if (!string.contains(start) || string.indexOf(end, startPos) < startPos) {
return "";
}
return string.substring(startPos, string.indexOf(end, startPos));
}
/**
* Reads a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read
* @return True if successful. False otherwise
*/
public static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to
* @return True if successful. False otherwise
*/
public static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
}
}

View File

@ -9,16 +9,17 @@ import net.knarcraft.serverlauncher.Main;
import javax.swing.*; import javax.swing.*;
import java.io.*; import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static net.knarcraft.serverlauncher.Shared.downloadFile;
import static net.knarcraft.serverlauncher.Shared.readFile;
import static net.knarcraft.serverlauncher.Shared.stringBetween;
/** /**
* Contains all user settings, and a list of servers. * Contains all user settings, and a list of servers.
* *
@ -29,9 +30,9 @@ import java.util.concurrent.Executors;
public class Profile { 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 final String profilesDir = Main.jarDir + File.separator + "files"; private static final String profilesDir = Main.appDir + File.separator + "files";
private static final String profilesFile = Main.jarDir + File.separator + "files" + File.separator + "Profiles.txt"; private static final String profilesFile = Main.appDir + File.separator + "files" + File.separator + "Profiles.txt";
private static final String jarDir = Main.jarDir + File.separator + "files" + File.separator + "Jars" + File.separator; private static final String jarDir = Main.appDir + 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;
@ -86,6 +87,10 @@ public class Profile {
return this.collections; return this.collections;
} }
public String getName() {
return this.name;
}
/** /**
* Gets a Collection object by name. * Gets a Collection object by name.
* *
@ -110,6 +115,10 @@ public class Profile {
return null; return null;
} }
public static ArrayList<Profile> getProfiles() {
return profiles;
}
public void setRunInBackground(boolean value) { public void setRunInBackground(boolean value) {
this.runInBackground = value; this.runInBackground = value;
} }
@ -191,7 +200,6 @@ public class Profile {
} }
} }
new Profile(name); new Profile(name);
Main.gui().addProfile(name);
} }
public void removeCollection(String name) { public void removeCollection(String name) {
@ -209,7 +217,6 @@ public class Profile {
for (int i = 0; i < profiles.size(); i++) { for (int i = 0; i < profiles.size(); i++) {
if (profiles.get(i).name.equals((name))) { if (profiles.get(i).name.equals((name))) {
profiles.remove(i); profiles.remove(i);
Main.gui().removeProfile(i);
} }
} }
} }
@ -271,7 +278,7 @@ public class Profile {
* Reads all server tabs, and saves it to the variables of the corresponding servers. * Reads all server tabs, and saves it to the variables of the corresponding servers.
* Saves all profiles and servers to a text file. * Saves all profiles and servers to a text file.
*/ */
public void save() { public void save() throws FileNotFoundException {
for (Collection collection : this.collections) { for (Collection collection : this.collections) {
Server server = collection.getServer(); Server server = collection.getServer();
ServerTab serverTab = collection.getServerTab(); ServerTab serverTab = collection.getServerTab();
@ -290,16 +297,14 @@ public class Profile {
} }
server.toggle(serverTab.enabled()); server.toggle(serverTab.enabled());
} }
if (!new File(profilesFile).exists()) { if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) {
if (!new File(profilesDir).mkdirs()) { JOptionPane.showMessageDialog(
JOptionPane.showMessageDialog( null,
null, "Unable to create the folder " + profilesDir,
"Unable to create the file " + profilesFile, "Error",
"Error", JOptionPane.ERROR_MESSAGE
JOptionPane.ERROR_MESSAGE );
); throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir);
return;
}
} }
try (PrintWriter file = new PrintWriter(profilesFile)) { try (PrintWriter file = new PrintWriter(profilesFile)) {
file.println(String.format("%s;%s;%s;%s", current.name, vanillaVersion, snapshotVersion, bungeeVersion)); file.println(String.format("%s;%s;%s;%s", current.name, vanillaVersion, snapshotVersion, bungeeVersion));
@ -325,7 +330,8 @@ public class Profile {
server.getVanillaVersion(), server.getVanillaVersion(),
server.getSnapshotVersion(), server.getSnapshotVersion(),
server.getSpongeVanillaVersion(), server.getSpongeVanillaVersion(),
server.getBungeeVersion()) server.getBungeeVersion()
)
); );
} }
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1)); saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
@ -335,21 +341,27 @@ public class Profile {
))) { ))) {
fileAppend.println(saveString); fileAppend.println(saveString);
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog( if (Main.gui() != null) {
null, JOptionPane.showMessageDialog(
"Unable to save to file. Try running the software as an administrator.", null,
"Error", "Unable to save to file. Try running the software as an administrator.",
JOptionPane.ERROR_MESSAGE "Error",
); JOptionPane.ERROR_MESSAGE
);
throw new FileNotFoundException("Unable to save to the profiles file.");
}
} }
} }
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog( if (Main.gui() != null) {
null, JOptionPane.showMessageDialog(
"Unable to save to file. Try running the software as an administrator.", null,
"Error", "Unable to save to file. Try running the software as an administrator.",
JOptionPane.ERROR_MESSAGE "Error",
); JOptionPane.ERROR_MESSAGE
);
}
throw new FileNotFoundException("Unable to create the profiles file");
} }
} }
@ -370,7 +382,6 @@ public class Profile {
String[] data = line.split("\\?"); String[] data = line.split("\\?");
String[] profileData = data[0].split(";", -1); String[] profileData = data[0].split(";", -1);
Profile profile = parseProfile(profileData); Profile profile = parseProfile(profileData);
Main.gui().addProfile(profileData[0]);
if (data[1].contains("!")) { if (data[1].contains("!")) {
String[] servers = data[1].split("!", -1); String[] servers = data[1].split("!", -1);
for (String server : servers) { for (String server : servers) {
@ -384,7 +395,6 @@ public class Profile {
} else { } else {
String[] profileData = line.split(";", -1); String[] profileData = line.split(";", -1);
parseProfile(profileData); parseProfile(profileData);
Main.gui().addProfile(profileData[0]);
} }
} }
current = getProfile(profileName); current = getProfile(profileName);
@ -411,13 +421,20 @@ public class Profile {
addProfile("Default"); addProfile("Default");
} }
Main.gui().update(); Main.gui().update();
Main.gui().updateProfiles();
current.updateConsoles(); current.updateConsoles();
if (current.runInBackground) { if (current.runInBackground) {
Main.gui().hide(); Main.gui().hide();
Executors.newSingleThreadExecutor().execute(Server::startServers); Executors.newSingleThreadExecutor().execute(Server::startServers);
} }
if (current.downloadJars) { if (current.downloadJars) {
Executors.newSingleThreadExecutor().execute(Profile::downloadJars); Executors.newSingleThreadExecutor().execute(() -> {
try {
downloadJars();
} catch (IOException e) {
e.printStackTrace();
}
});
} }
} }
@ -445,7 +462,7 @@ public class Profile {
); );
} }
private static void downloadJars() { public static void downloadJars() throws IOException {
if (!new File(jarDir).exists() && !new File(jarDir).mkdirs()) { if (!new File(jarDir).exists() && !new File(jarDir).mkdirs()) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
@ -453,6 +470,7 @@ public class Profile {
"Error", "Error",
JOptionPane.ERROR_MESSAGE JOptionPane.ERROR_MESSAGE
); );
throw new FileNotFoundException("Unable to create jars folder");
} }
downloadSimple("Spigot"); downloadSimple("Spigot");
downloadSimple("Craftbukkit"); downloadSimple("Craftbukkit");
@ -464,43 +482,49 @@ public class Profile {
Main.gui().setStatus("Finished downloading jars"); Main.gui().setStatus("Finished downloading jars");
} }
private static void downloadSimple(String typeName) { private static void downloadSimple(String typeName) throws FileNotFoundException {
ServerType type = ServerType.getByName(typeName); ServerType type = ServerType.getByName(typeName);
String url = Objects.requireNonNull(type).getDownloadURL(); String url = Objects.requireNonNull(type).getDownloadURL();
String name = type.getName(); String name = type.getName();
Boolean success; Boolean success;
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + type.getName() + version + ".jar");
if (!file.isFile()) { if (!file.isFile()) {
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
Main.gui().setStatus("Downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Downloading: " + name + version + ".jar");
}
success = downloadFile(url + name + version + ".jar", filePath); success = downloadFile(url + name + version + ".jar", filePath);
if (!success) { if (!success) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar");
}
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
} }
} }
} }
private static void downloadMixed(String typeName) { private static void downloadMixed(String typeName) throws IOException {
AdvancedServerType type = (AdvancedServerType) ServerType.getByName(typeName); AdvancedServerType type = (AdvancedServerType) ServerType.getByName(typeName);
String url = Objects.requireNonNull(type).getDownloadURL(); String url = Objects.requireNonNull(type).getDownloadURL();
String name = type.getName(); String name = type.getName();
String versionText = ""; String versionText;
String newestVersion; String newestVersion;
Boolean success; Boolean success;
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + type.getName() + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
Main.gui().setStatus("Downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Downloading: " + name + version + ".jar");
}
if (version.equals("Latest")) { if (version.equals("Latest")) {
try { try {
versionText = readFile(type.getVersionURL()); versionText = readFile(type.getVersionURL());
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error reading: " + type.getVersionURL()); throw new IOException("Error reading: " + type.getVersionURL());
} }
newestVersion = Main.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(getVersion(name))) { if (!file.isFile() || !newestVersion.equals(getVersion(name))) {
success = downloadFile( success = downloadFile(
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
@ -508,104 +532,90 @@ public class Profile {
); );
setVersion(name, newestVersion); setVersion(name, newestVersion);
if (!success) { if (!success) {
System.out.println("Error downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar");
}
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
} }
} else { } else {
if (!file.isFile()) { if (!file.isFile()) {
success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath); success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath);
if (!success) { if (!success) {
System.out.println("Error downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar");
}
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
} }
} }
} }
} }
private static void downloadSponge() { private static void downloadSponge() throws IOException {
AdvancedServerType type = (AdvancedServerType) ServerType.getByName("SpongeVanilla"); AdvancedServerType type = (AdvancedServerType) ServerType.getByName("SpongeVanilla");
String url = Objects.requireNonNull(type).getDownloadURL(); String url = Objects.requireNonNull(type).getDownloadURL();
String name = type.getName(); String name = type.getName();
String versionText = ""; String versionText;
String newestVersion; String newestVersion;
Boolean success; Boolean success;
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + name + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + name + version + ".jar");
Main.gui().setStatus("Downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Downloading: " + name + version + ".jar");
}
try { try {
versionText = readFile(type.getVersionURL() + version); versionText = readFile(type.getVersionURL() + version);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error reading: " + type.getVersionURL()); throw new IOException("Error reading: " + type.getVersionURL());
} }
newestVersion = Main.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile()) { if (!file.isFile()) {
success = downloadFile( success = downloadFile(
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
filePath filePath
); );
if (!success) { if (!success) {
System.out.println("Error downloading: " + name + version + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Error downloading: " + name + version + ".jar");
}
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
} }
} }
} }
private static void downloadBungee() { private static void downloadBungee() throws IOException {
AdvancedServerType type = (AdvancedServerType) ServerType.getByName("Bungee"); AdvancedServerType type = (AdvancedServerType) ServerType.getByName("Bungee");
String url = Objects.requireNonNull(type).getDownloadURL(); String url = Objects.requireNonNull(type).getDownloadURL();
String name = type.getName(); String name = type.getName();
String versionText = ""; String versionText;
String newestVersion; String newestVersion;
Boolean success; Boolean success;
File file = new File(jarDir + type.getName() + ".jar"); File file = new File(jarDir + type.getName() + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + ".jar");
Main.gui().setStatus("Downloading: " + name + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Downloading: " + name + ".jar");
}
try { try {
versionText = readFile(type.getVersionURL()); versionText = readFile(type.getVersionURL());
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error reading: " + type.getVersionURL()); throw new IOException("Error reading: " + type.getVersionURL());
} }
newestVersion = Main.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(getVersion(name))) { if (!file.isFile() || !newestVersion.equals(getVersion(name))) {
success = downloadFile(url, filePath); success = downloadFile(url, filePath);
setVersion(name, newestVersion); setVersion(name, newestVersion);
if (!success) { if (!success) {
System.out.println("Error downloading: " + name + ".jar"); if (Main.gui() != null) {
Main.gui().setStatus("Error downloading: " + name + ".jar");
}
throw new FileNotFoundException("Error downloading: " + name + ".jar");
} }
} }
} }
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to
* @return True if successful. False otherwise
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
}
/**
* Reads a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read
* @return True if successful. False otherwise
*/
private static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
/** /**
* Returns the current version of a type * Returns the current version of a type
* *

View File

@ -4,11 +4,13 @@ import net.knarcraft.serverlauncher.Main;
import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.profile.Profile;
import java.io.*; import java.io.*;
import java.net.URL;
import java.util.Scanner;
import java.nio.file.*; import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
import static net.knarcraft.serverlauncher.Shared.downloadFile;
import static net.knarcraft.serverlauncher.Shared.readFile;
import static net.knarcraft.serverlauncher.Shared.stringBetween;
/** /**
* Contains all necessary information to create, run and manage a Minecraft server. * Contains all necessary information to create, run and manage a Minecraft server.
@ -22,7 +24,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.jarDir + File.separator + "files" + File.separator + "Jars" + File.separator; private static final String jarDir = Main.appDir + File.separator + "files" + File.separator + "Jars" + File.separator;
private final String name; private final String name;
private String path; private String path;
@ -247,6 +249,7 @@ public class Server {
Main.gui().setStatus("File downloaded"); Main.gui().setStatus("File downloaded");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Main.gui().setStatus("Error: Jar file not found"); Main.gui().setStatus("Error: Jar file not found");
e.printStackTrace();
return false; return false;
} }
} }
@ -343,7 +346,7 @@ public class Server {
} catch (IOException e) { } catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded."); throw new FileNotFoundException("Version file could not be downloaded.");
} }
newestVersion = Main.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) { if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
success = downloadFile( success = downloadFile(
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
@ -370,7 +373,7 @@ public class Server {
} catch (IOException e) { } catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded."); throw new FileNotFoundException("Version file could not be downloaded.");
} }
newestVersion = Main.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) { if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
success = downloadFile( success = downloadFile(
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
@ -389,7 +392,7 @@ public class Server {
} catch (IOException e) { } catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded."); throw new FileNotFoundException("Version file could not be downloaded.");
} }
newestVersion = Main.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) { if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
success = downloadFile(url, filePath); success = downloadFile(url, filePath);
this.setVersion(name, newestVersion); this.setVersion(name, newestVersion);
@ -445,36 +448,6 @@ public class Server {
} }
} }
/**
* Reads a file from a website.
* This is used to find the newest version of the software.
*
* @param path The full url of the file to read
* @return True if successful. False otherwise
*/
private static String readFile(String path) throws IOException {
URL url = new URL(path);
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
}
/**
* Downloads a file from a website.
*
* @param path The full url of the file to download.
* @param outfile The file to save to
* @return True if successful. False otherwise
*/
private static boolean downloadFile(String path, Path outfile) {
try {
URL url = new URL(path);
InputStream in = url.openStream();
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
}
/** /**
* Sends a command to this server through its writer. * Sends a command to this server through its writer.
* *

View File

@ -89,12 +89,11 @@ public class GUI implements ActionListener {
this.updatePlayers(); this.updatePlayers();
} }
public void addProfile(String name) { public void updateProfiles() {
this.profiles.addItem(name); this.profiles.removeAllItems();
} for (Profile profile : Profile.getProfiles()) {
this.profiles.addItem(profile.getName());
public void removeProfile(int index) { }
this.profiles.removeItemAt(index);
} }
/** /**
@ -400,7 +399,11 @@ public class GUI implements ActionListener {
trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup); trayIcon = new TrayIcon(trayImage, "Minecraft Server Launcher", popup);
trayIcon.setImageAutoSize(true); trayIcon.setImageAutoSize(true);
ActionListener exitListener= e -> { ActionListener exitListener= e -> {
Profile.getCurrent().save(); try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.exit(0); System.exit(0);
}; };
@ -432,7 +435,11 @@ public class GUI implements ActionListener {
e1.printStackTrace(); e1.printStackTrace();
} }
} else { } else {
Profile.getCurrent().save(); try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
stop(); stop();
System.exit(0); System.exit(0);
} }
@ -453,7 +460,11 @@ public class GUI implements ActionListener {
frame.addWindowListener(new WindowAdapter() { frame.addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
Profile.getCurrent().save(); try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
stop(); stop();
System.exit(0); System.exit(0);
} }
@ -532,7 +543,11 @@ public class GUI implements ActionListener {
} else if (e.getSource() == mntmStory) { } else if (e.getSource() == mntmStory) {
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/"); goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/");
} else if (e.getSource() == btnStartServer) { } else if (e.getSource() == btnStartServer) {
Profile.getCurrent().save(); try {
Profile.getCurrent().save();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Executors.newSingleThreadExecutor().execute(Server::startServers); Executors.newSingleThreadExecutor().execute(Server::startServers);
} else if (e.getSource() == btnStopServer) { } else if (e.getSource() == btnStopServer) {
stop(); stop();
@ -545,13 +560,19 @@ public class GUI implements ActionListener {
backup(); backup();
} else if (e.getSource() == addProfile) { } else if (e.getSource() == addProfile) {
Profile.addProfile(JOptionPane.showInputDialog("Profile name: ")); Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
updateProfiles();
} else if (e.getSource() == delProfile) { } else if (e.getSource() == delProfile) {
Object selected = profiles.getSelectedItem(); Object selected = profiles.getSelectedItem();
if (selected != null) { if (selected != null) {
Profile.removeProfile(selected.toString()); Profile.removeProfile(selected.toString());
updateProfiles();
} }
} else if (e.getSource() == profiles) { } else if (e.getSource() == profiles) {
changeProfile(); try {
changeProfile();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == btnKick) { } else if (e.getSource() == btnKick) {
if (selectedServerValue != null && selectedPlayerValue != null) { if (selectedServerValue != null && selectedPlayerValue != null) {
Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue); Profile.getCurrent().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
@ -589,7 +610,7 @@ public class GUI implements ActionListener {
/** /**
* Saves the previous profile and loads data from the new profile. * Saves the previous profile and loads data from the new profile.
*/ */
private void changeProfile() { private void changeProfile() throws FileNotFoundException {
Profile.getCurrent().save(); Profile.getCurrent().save();
Object current = this.profiles.getSelectedItem(); Object current = this.profiles.getSelectedItem();
if (current != null) { if (current != null) {

View File

@ -1,8 +1,5 @@
package net.knarcraft.serverlauncher.userinterface; package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import java.awt.BorderLayout; import java.awt.BorderLayout;

15
test/DownloadTests.java Normal file
View File

@ -0,0 +1,15 @@
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.ServerType;
import org.junit.Test;
import javax.naming.ConfigurationException;
import java.io.IOException;
public class DownloadTests {
@Test
public void downloadJarsTest() throws IOException, ConfigurationException {
// Will currently always fail since knarcraft.net is down.
ServerType.loadServerTypes();
Profile.downloadJars();
}
}

34
test/Tests.java Normal file
View File

@ -0,0 +1,34 @@
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.ServerType;
import org.junit.Test;
import javax.naming.ConfigurationException;
import java.io.FileNotFoundException;
import static net.knarcraft.serverlauncher.Shared.stringBetween;
import static org.junit.Assert.assertEquals;
public class Tests {
@Test
public void loadServerVersions() throws ConfigurationException { //Make sure the server versions file has correct syntax
ServerType.loadServerTypes();
}
@Test
public void saveProfile() throws FileNotFoundException { //Make sure we can write profiles to disk
Profile.addProfile("Test");
Profile.getCurrent().save();
}
@Test
public void stringBetweenTest() { //Make sure stringBetween is not creating exceptions
String substring = stringBetween("fish'nchips", "f", "'");
assertEquals("ish", substring);
substring = stringBetween("something", "whale", "fish");
assertEquals("", substring);
substring = stringBetween("something", "so", "fish");
assertEquals("", substring);
substring = stringBetween("something", "asd", "ing");
assertEquals("", substring);
}
}