Fixes profiles file creation
Adds some tests
This commit is contained in:
parent
5d471881fb
commit
920c3de0d4
@ -9,12 +9,13 @@ import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static net.knarcraft.serverlauncher.Shared.stringBetween;
|
||||
//Java 8 required.
|
||||
|
||||
/**
|
||||
@ -28,13 +29,14 @@ import java.util.concurrent.TimeUnit;
|
||||
public class Main {
|
||||
private static GUI gui;
|
||||
@SuppressWarnings("CanBeFinal")
|
||||
public static String jarDir;
|
||||
public static String appDir;
|
||||
|
||||
static {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,9 +73,8 @@ public class Main {
|
||||
* Reads from server processes, and writes the output to our custom consoles.
|
||||
*/
|
||||
private static void updateConsoles() {
|
||||
Server server;
|
||||
for (Collection collection : Profile.getCurrent().getCollections()) {
|
||||
server = collection.getServer();
|
||||
Server server = collection.getServer();
|
||||
if (server.isEnabled() && server.getProcess() != null) {
|
||||
try {
|
||||
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));
|
||||
}
|
||||
}
|
57
src/net/knarcraft/serverlauncher/Shared.java
Normal file
57
src/net/knarcraft/serverlauncher/Shared.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,16 +9,17 @@ import net.knarcraft.serverlauncher.Main;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
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.
|
||||
*
|
||||
@ -29,9 +30,9 @@ import java.util.concurrent.Executors;
|
||||
public class Profile {
|
||||
private static final ArrayList<Profile> profiles = new ArrayList<>();
|
||||
private static Profile current;
|
||||
private static final String profilesDir = Main.jarDir + File.separator + "files";
|
||||
private static final String profilesFile = Main.jarDir + 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 profilesDir = Main.appDir + File.separator + "files";
|
||||
private static final String profilesFile = Main.appDir + File.separator + "files" + File.separator + "Profiles.txt";
|
||||
private static final String jarDir = Main.appDir + File.separator + "files" + File.separator + "Jars" + File.separator;
|
||||
|
||||
private final ArrayList<Collection> collections;
|
||||
private final String name;
|
||||
@ -86,6 +87,10 @@ public class Profile {
|
||||
return this.collections;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Collection object by name.
|
||||
*
|
||||
@ -110,6 +115,10 @@ public class Profile {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ArrayList<Profile> getProfiles() {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
public void setRunInBackground(boolean value) {
|
||||
this.runInBackground = value;
|
||||
}
|
||||
@ -191,7 +200,6 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
new Profile(name);
|
||||
Main.gui().addProfile(name);
|
||||
}
|
||||
|
||||
public void removeCollection(String name) {
|
||||
@ -209,7 +217,6 @@ public class Profile {
|
||||
for (int i = 0; i < profiles.size(); i++) {
|
||||
if (profiles.get(i).name.equals((name))) {
|
||||
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.
|
||||
* Saves all profiles and servers to a text file.
|
||||
*/
|
||||
public void save() {
|
||||
public void save() throws FileNotFoundException {
|
||||
for (Collection collection : this.collections) {
|
||||
Server server = collection.getServer();
|
||||
ServerTab serverTab = collection.getServerTab();
|
||||
@ -290,16 +297,14 @@ public class Profile {
|
||||
}
|
||||
server.toggle(serverTab.enabled());
|
||||
}
|
||||
if (!new File(profilesFile).exists()) {
|
||||
if (!new File(profilesDir).mkdirs()) {
|
||||
if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"Unable to create the file " + profilesFile,
|
||||
"Unable to create the folder " + profilesDir,
|
||||
"Error",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
return;
|
||||
}
|
||||
throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir);
|
||||
}
|
||||
try (PrintWriter file = new PrintWriter(profilesFile)) {
|
||||
file.println(String.format("%s;%s;%s;%s", current.name, vanillaVersion, snapshotVersion, bungeeVersion));
|
||||
@ -325,7 +330,8 @@ public class Profile {
|
||||
server.getVanillaVersion(),
|
||||
server.getSnapshotVersion(),
|
||||
server.getSpongeVanillaVersion(),
|
||||
server.getBungeeVersion())
|
||||
server.getBungeeVersion()
|
||||
)
|
||||
);
|
||||
}
|
||||
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
|
||||
@ -335,15 +341,19 @@ public class Profile {
|
||||
))) {
|
||||
fileAppend.println(saveString);
|
||||
} catch (IOException e) {
|
||||
if (Main.gui() != null) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"Unable to save to file. Try running the software as an administrator.",
|
||||
"Error",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
throw new FileNotFoundException("Unable to save to the profiles file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (Main.gui() != null) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"Unable to save to file. Try running the software as an administrator.",
|
||||
@ -351,6 +361,8 @@ public class Profile {
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
}
|
||||
throw new FileNotFoundException("Unable to create the profiles file");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -370,7 +382,6 @@ public class Profile {
|
||||
String[] data = line.split("\\?");
|
||||
String[] profileData = data[0].split(";", -1);
|
||||
Profile profile = parseProfile(profileData);
|
||||
Main.gui().addProfile(profileData[0]);
|
||||
if (data[1].contains("!")) {
|
||||
String[] servers = data[1].split("!", -1);
|
||||
for (String server : servers) {
|
||||
@ -384,7 +395,6 @@ public class Profile {
|
||||
} else {
|
||||
String[] profileData = line.split(";", -1);
|
||||
parseProfile(profileData);
|
||||
Main.gui().addProfile(profileData[0]);
|
||||
}
|
||||
}
|
||||
current = getProfile(profileName);
|
||||
@ -411,13 +421,20 @@ public class Profile {
|
||||
addProfile("Default");
|
||||
}
|
||||
Main.gui().update();
|
||||
Main.gui().updateProfiles();
|
||||
current.updateConsoles();
|
||||
if (current.runInBackground) {
|
||||
Main.gui().hide();
|
||||
Executors.newSingleThreadExecutor().execute(Server::startServers);
|
||||
}
|
||||
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()) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
@ -453,6 +470,7 @@ public class Profile {
|
||||
"Error",
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
throw new FileNotFoundException("Unable to create jars folder");
|
||||
}
|
||||
downloadSimple("Spigot");
|
||||
downloadSimple("Craftbukkit");
|
||||
@ -464,43 +482,49 @@ public class Profile {
|
||||
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);
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
Boolean success;
|
||||
|
||||
for (String version : type.getVersions()) {
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
if (!file.isFile()) {
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
if (Main.gui() != null) {
|
||||
Main.gui().setStatus("Downloading: " + name + version + ".jar");
|
||||
}
|
||||
success = downloadFile(url + name + version + ".jar", filePath);
|
||||
if (!success) {
|
||||
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);
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
String versionText = "";
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
Boolean success;
|
||||
for (String version : type.getVersions()) {
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
if (Main.gui() != null) {
|
||||
Main.gui().setStatus("Downloading: " + name + version + ".jar");
|
||||
}
|
||||
if (version.equals("Latest")) {
|
||||
try {
|
||||
versionText = readFile(type.getVersionURL());
|
||||
} 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))) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
@ -508,104 +532,90 @@ public class Profile {
|
||||
);
|
||||
setVersion(name, newestVersion);
|
||||
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 {
|
||||
if (!file.isFile()) {
|
||||
success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath);
|
||||
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");
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
String versionText = "";
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
Boolean success;
|
||||
for (String version : type.getVersions()) {
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
File file = new File(jarDir + name + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + name + version + ".jar");
|
||||
if (Main.gui() != null) {
|
||||
Main.gui().setStatus("Downloading: " + name + version + ".jar");
|
||||
}
|
||||
try {
|
||||
versionText = readFile(type.getVersionURL() + version);
|
||||
} 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()) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
filePath
|
||||
);
|
||||
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");
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
String versionText = "";
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
Boolean success;
|
||||
File file = new File(jarDir + type.getName() + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + ".jar");
|
||||
if (Main.gui() != null) {
|
||||
Main.gui().setStatus("Downloading: " + name + ".jar");
|
||||
}
|
||||
try {
|
||||
versionText = readFile(type.getVersionURL());
|
||||
} 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))) {
|
||||
success = downloadFile(url, filePath);
|
||||
setVersion(name, newestVersion);
|
||||
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
|
||||
*
|
||||
|
@ -4,11 +4,13 @@ import net.knarcraft.serverlauncher.Main;
|
||||
import net.knarcraft.serverlauncher.profile.Collection;
|
||||
import net.knarcraft.serverlauncher.profile.Profile;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
import java.nio.file.*;
|
||||
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.
|
||||
@ -22,7 +24,7 @@ public class Server {
|
||||
private static final String[] ramList = {
|
||||
"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 String path;
|
||||
@ -247,6 +249,7 @@ public class Server {
|
||||
Main.gui().setStatus("File downloaded");
|
||||
} catch (FileNotFoundException e) {
|
||||
Main.gui().setStatus("Error: Jar file not found");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -343,7 +346,7 @@ public class Server {
|
||||
} catch (IOException e) {
|
||||
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))) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
@ -370,7 +373,7 @@ public class Server {
|
||||
} catch (IOException e) {
|
||||
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))) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
@ -389,7 +392,7 @@ public class Server {
|
||||
} catch (IOException e) {
|
||||
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))) {
|
||||
success = downloadFile(url, filePath);
|
||||
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.
|
||||
*
|
||||
|
@ -89,12 +89,11 @@ public class GUI implements ActionListener {
|
||||
this.updatePlayers();
|
||||
}
|
||||
|
||||
public void addProfile(String name) {
|
||||
this.profiles.addItem(name);
|
||||
public void updateProfiles() {
|
||||
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.setImageAutoSize(true);
|
||||
ActionListener exitListener= e -> {
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
System.exit(0);
|
||||
};
|
||||
|
||||
@ -432,7 +435,11 @@ public class GUI implements ActionListener {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
stop();
|
||||
System.exit(0);
|
||||
}
|
||||
@ -453,7 +460,11 @@ public class GUI implements ActionListener {
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
stop();
|
||||
System.exit(0);
|
||||
}
|
||||
@ -532,7 +543,11 @@ public class GUI implements ActionListener {
|
||||
} else if (e.getSource() == mntmStory) {
|
||||
goToURL("https://knarcraft.net/Bungeeminecraftserverlauncher/Story/");
|
||||
} else if (e.getSource() == btnStartServer) {
|
||||
try {
|
||||
Profile.getCurrent().save();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
Executors.newSingleThreadExecutor().execute(Server::startServers);
|
||||
} else if (e.getSource() == btnStopServer) {
|
||||
stop();
|
||||
@ -545,13 +560,19 @@ public class GUI implements ActionListener {
|
||||
backup();
|
||||
} else if (e.getSource() == addProfile) {
|
||||
Profile.addProfile(JOptionPane.showInputDialog("Profile name: "));
|
||||
updateProfiles();
|
||||
} else if (e.getSource() == delProfile) {
|
||||
Object selected = profiles.getSelectedItem();
|
||||
if (selected != null) {
|
||||
Profile.removeProfile(selected.toString());
|
||||
updateProfiles();
|
||||
}
|
||||
} else if (e.getSource() == profiles) {
|
||||
try {
|
||||
changeProfile();
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} else if (e.getSource() == btnKick) {
|
||||
if (selectedServerValue != null && selectedPlayerValue != null) {
|
||||
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.
|
||||
*/
|
||||
private void changeProfile() {
|
||||
private void changeProfile() throws FileNotFoundException {
|
||||
Profile.getCurrent().save();
|
||||
Object current = this.profiles.getSelectedItem();
|
||||
if (current != null) {
|
||||
|
@ -1,8 +1,5 @@
|
||||
package net.knarcraft.serverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.serverlauncher.profile.Collection;
|
||||
import net.knarcraft.serverlauncher.profile.Profile;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JTabbedPane;
|
||||
import java.awt.BorderLayout;
|
||||
|
15
test/DownloadTests.java
Normal file
15
test/DownloadTests.java
Normal 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
34
test/Tests.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user