Does some much needed cleanup, adds the Paper server type and fixes broken downloads
Some checks failed
KnarCraft/Minecraft-Server-Launcher/pipeline/head There was a failure building this commit
Some checks failed
KnarCraft/Minecraft-Server-Launcher/pipeline/head There was a failure building this commit
This commit is contained in:
@ -1,30 +1,21 @@
|
||||
package net.knarcraft.minecraftserverlauncher.profile;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
|
||||
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerTab;
|
||||
import net.knarcraft.minecraftserverlauncher.server.AdvancedServerType;
|
||||
import net.knarcraft.minecraftserverlauncher.server.Server;
|
||||
import net.knarcraft.minecraftserverlauncher.server.ServerType;
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.JarDownloader;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.downloadFile;
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.readFile;
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.stringBetween;
|
||||
|
||||
/**
|
||||
* Contains all user settings, and a list of servers.
|
||||
*
|
||||
@ -35,7 +26,7 @@ import static net.knarcraft.minecraftserverlauncher.Shared.stringBetween;
|
||||
public class Profile {
|
||||
private static final ArrayList<Profile> profiles = new ArrayList<>();
|
||||
private static Profile current;
|
||||
private static GUI gui;
|
||||
private static ServerLauncherGUI serverLauncherGui;
|
||||
private static final String profilesDir = Main.getApplicationWorkDirectory() + File.separator + "files";
|
||||
private static final String profilesFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Profiles.txt";
|
||||
private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator;
|
||||
@ -73,8 +64,8 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
|
||||
public static GUI getGUI() {
|
||||
return gui;
|
||||
public static ServerLauncherGUI getGUI() {
|
||||
return serverLauncherGui;
|
||||
}
|
||||
|
||||
public boolean getRunInBackground() {
|
||||
@ -162,7 +153,7 @@ public class Profile {
|
||||
*
|
||||
* @param name The name of the collection and its elements.
|
||||
*/
|
||||
public void addCollection(String name) {
|
||||
public void addCollection(String name) throws ConfigurationException {
|
||||
if (name == null) { //If a user cancels or crosses out window
|
||||
return;
|
||||
}
|
||||
@ -175,7 +166,7 @@ public class Profile {
|
||||
) {
|
||||
collections.add(new Collection(name));
|
||||
} else {
|
||||
showError("A server name must my unique and not empty or \"All\"." +
|
||||
serverLauncherGui.showError("A server name must my unique and not empty or \"All\"." +
|
||||
"It can't contain any of the characters \"!\", \"?\" or \";\".");
|
||||
}
|
||||
}
|
||||
@ -190,12 +181,12 @@ public class Profile {
|
||||
return;
|
||||
}
|
||||
if (name.equals("") && !name.matches("^[!?;]+$")) {
|
||||
showError("Profile name can't be blank.");
|
||||
serverLauncherGui.showError("Profile name can't be blank.");
|
||||
return;
|
||||
}
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
showError("There is already a profile with this name.");
|
||||
serverLauncherGui.showError("There is already a profile with this name.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -206,7 +197,7 @@ public class Profile {
|
||||
for (int i = 0; i < collections.size(); i++) {
|
||||
if (collections.get(i).getName().equals(name)) {
|
||||
this.collections.remove(i);
|
||||
gui.update();
|
||||
serverLauncherGui.update();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -242,7 +233,7 @@ public class Profile {
|
||||
try {
|
||||
collection.getServer().sendCommand(command);
|
||||
} catch (IOException e) {
|
||||
showError("Server " + collection.getName() + " caused an exception.");
|
||||
serverLauncherGui.showError("Server " + collection.getName() + " caused an exception.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -252,10 +243,10 @@ public class Profile {
|
||||
try {
|
||||
target.sendCommand(command);
|
||||
} catch (IOException e) {
|
||||
showError("Server " + target.getName() + " caused an exception.");
|
||||
serverLauncherGui.showError("Server " + target.getName() + " caused an exception.");
|
||||
}
|
||||
} else {
|
||||
showError("Server " + serverName + " is invalid.");
|
||||
serverLauncherGui.showError("Server " + serverName + " is invalid.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -270,27 +261,27 @@ public class Profile {
|
||||
ServerTab serverTab = collection.getServerTab();
|
||||
server.setPath(serverTab.getPath());
|
||||
server.setMaxRam(serverTab.getMaxRam());
|
||||
server.setType(ServerType.getByName(serverTab.getType()));
|
||||
server.setType(ServerTypeHandler.getByName(serverTab.getType()));
|
||||
try {
|
||||
server.setServerVersion(serverTab.getVersion());
|
||||
} catch (IllegalArgumentException e) {
|
||||
showError("Invalid server version for " + server.getName());
|
||||
serverLauncherGui.showError("Invalid server version for " + server.getName());
|
||||
}
|
||||
server.toggle(serverTab.enabled());
|
||||
}
|
||||
if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) {
|
||||
showError("Unable to create the folder " + profilesDir);
|
||||
serverLauncherGui.showError("Unable to create the folder " + profilesDir);
|
||||
throw new FileNotFoundException("Unable to create the profiles folder: " + profilesDir);
|
||||
}
|
||||
try (PrintWriter file = new PrintWriter(profilesFile)) {
|
||||
int width;
|
||||
int height;
|
||||
if (gui == null) {
|
||||
if (serverLauncherGui == null) {
|
||||
width = 440;
|
||||
height = 170;
|
||||
} else {
|
||||
width = gui.getSize().width;
|
||||
height = gui.getSize().height;
|
||||
width = serverLauncherGui.getSize().width;
|
||||
height = serverLauncherGui.getSize().height;
|
||||
}
|
||||
file.println(String.format(
|
||||
"%s;%s;%s;%s;%d;%d",
|
||||
@ -334,8 +325,8 @@ public class Profile {
|
||||
))) {
|
||||
fileAppend.println(saveString);
|
||||
} catch (IOException e) {
|
||||
if (gui != null) {
|
||||
showError("Unable to save to file. Try running the software as an administrator.");
|
||||
if (serverLauncherGui != null) {
|
||||
serverLauncherGui.showError("Unable to save to file. Try running the software as an administrator.");
|
||||
} else {
|
||||
System.out.println("Unable to save to file. Try running the software as an administrator.");
|
||||
}
|
||||
@ -343,8 +334,8 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (gui != null) {
|
||||
showError("Unable to save to file. Try running the software as an administrator.");
|
||||
if (serverLauncherGui != null) {
|
||||
serverLauncherGui.showError("Unable to save to file. Try running the software as an administrator.");
|
||||
}
|
||||
throw new FileNotFoundException("Unable to create the profiles file");
|
||||
}
|
||||
@ -353,7 +344,7 @@ public class Profile {
|
||||
/**
|
||||
* Reads profiles and servers from a text file.
|
||||
*/
|
||||
public static void load() {
|
||||
public static void load() throws ConfigurationException {
|
||||
try (Scanner in = new Scanner(new File(profilesFile))) {
|
||||
try {
|
||||
String[] staticData = in.nextLine().split(";", -1);
|
||||
@ -363,7 +354,7 @@ public class Profile {
|
||||
bungeeVersion = staticData[3];
|
||||
int guiWidth = Integer.parseInt(staticData[4]);
|
||||
int guiHeight = Integer.parseInt(staticData[5]);
|
||||
gui = new GUI(guiWidth, guiHeight);
|
||||
serverLauncherGui = new ServerLauncherGUI(guiWidth, guiHeight);
|
||||
while (in.hasNextLine()) {
|
||||
String line = in.nextLine();
|
||||
if (line.contains("?")) {
|
||||
@ -388,7 +379,7 @@ public class Profile {
|
||||
current = getProfile(profileName);
|
||||
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
showError("Invalid Profile.txt file. Profiles could not be loaded. If this error persists, please " +
|
||||
serverLauncherGui.showError("Invalid Profile.txt file. Profiles could not be loaded. If this error persists, please " +
|
||||
"manually delete the file.");
|
||||
System.exit(1);
|
||||
} catch (IOException e) {
|
||||
@ -398,23 +389,24 @@ public class Profile {
|
||||
addProfile("Default");
|
||||
}
|
||||
} catch (FileNotFoundException | NoSuchElementException e) {
|
||||
showMessage("A profiles file was not found. Default profile was created.");
|
||||
serverLauncherGui.showMessage("A profiles file was not found. Default profile was created.");
|
||||
try {
|
||||
gui = new GUI();
|
||||
serverLauncherGui = new ServerLauncherGUI();
|
||||
} catch (FileNotFoundException ex) {
|
||||
showMessage("Failed to load GUI messages. The GUI can't be shown.");
|
||||
serverLauncherGui.showMessage("Failed to load ServerLauncherGUI messages. The ServerLauncherGUI can't be shown.");
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
addProfile("Default");
|
||||
}
|
||||
gui.update();
|
||||
gui.updateProfiles();
|
||||
serverLauncherGui.update();
|
||||
serverLauncherGui.updateProfiles();
|
||||
current.updateConsoles();
|
||||
if (current.downloadAllAvailableJARFiles) {
|
||||
Executors.newSingleThreadExecutor().execute(() -> {
|
||||
JarDownloader downloader = new JarDownloader(serverLauncherGui, jarDirectory);
|
||||
try {
|
||||
downloadJars();
|
||||
downloader.downloadJars();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -422,7 +414,7 @@ public class Profile {
|
||||
}
|
||||
if (current.runInBackground) {
|
||||
Executors.newSingleThreadExecutor().execute(Server::startServers);
|
||||
gui.hide();
|
||||
serverLauncherGui.hide();
|
||||
}
|
||||
}
|
||||
|
||||
@ -445,7 +437,7 @@ public class Profile {
|
||||
* @param profile <p>The profile which to add the collection</p>
|
||||
* @param serverData <p>The data to parse</p>
|
||||
*/
|
||||
private static void parseServer(Profile profile, String[] serverData) {
|
||||
private static void parseServer(Profile profile, String[] serverData) throws ConfigurationException {
|
||||
profile.collections.add(new Collection(
|
||||
serverData[0],
|
||||
serverData[1],
|
||||
@ -459,249 +451,4 @@ public class Profile {
|
||||
serverData[9])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads all jars to the program directory.
|
||||
*
|
||||
* @throws IOException On version file failure or folder creation failure
|
||||
*/
|
||||
public static void downloadJars() throws IOException {
|
||||
if (!new File(jarDirectory).exists() && !new File(jarDirectory).mkdirs()) {
|
||||
showError("Could not create the Jars folder. Please run the program with admin permissions, or move it to " +
|
||||
"a writable directory.");
|
||||
throw new FileNotFoundException("Unable to create jars folder");
|
||||
}
|
||||
try {
|
||||
downloadAll();
|
||||
printToGui("Finished downloading jars");
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new FileNotFoundException("One or more downloads failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints something to the gui status field if the gui exists
|
||||
* Otherwise it prints to the console
|
||||
* @param message <p>The string to show the user</p>
|
||||
*/
|
||||
private static void printToGui(String message) {
|
||||
if (gui != null) {
|
||||
gui.setStatus(message);
|
||||
} else {
|
||||
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
|
||||
* @throws IOException <p>If a jar fails to download</p>
|
||||
*/
|
||||
private static void downloadAll() throws IOException {
|
||||
for (ServerType type : ServerType.getServerTypes()) {
|
||||
String downloadURL = Objects.requireNonNull(type).getDownloadURL();
|
||||
String typeName = type.getName();
|
||||
AdvancedServerType advancedServerType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
|
||||
for (String version : type.getVersions()) {
|
||||
boolean success;
|
||||
printToGui("Downloading: " + typeName + version + ".jar");
|
||||
File file = new File(jarDirectory + type.getName() + version + ".jar");
|
||||
Path filePath = Paths.get(jarDirectory + type.getName() + version + ".jar");
|
||||
switch (type.getName()) {
|
||||
case "Vanilla":
|
||||
case "Snapshot":
|
||||
success = downloadVanillaJar(advancedServerType, file, downloadURL, filePath, typeName, version);
|
||||
break;
|
||||
case "Spigot":
|
||||
case "Craftbukkit":
|
||||
case "MCPCplus":
|
||||
success = downloadSpigotJar(file, downloadURL, typeName, version, filePath);
|
||||
break;
|
||||
case "SpongeVanilla":
|
||||
success = downloadSpongeVanillaJar(advancedServerType, file, downloadURL, filePath, version);
|
||||
break;
|
||||
case "Bungee":
|
||||
success = downloadBungeeJar(advancedServerType, file, downloadURL, filePath, typeName);
|
||||
break;
|
||||
default:
|
||||
success = true;
|
||||
}
|
||||
if (!success) {
|
||||
printToGui("Error downloading: " + typeName + 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
|
||||
* @param type <p>The version type</p>
|
||||
* @return <p>The version string</p>
|
||||
*/
|
||||
private static String getVersion(String type) {
|
||||
switch (type) {
|
||||
case "Vanilla":
|
||||
return vanillaVersion;
|
||||
case "Snapshot":
|
||||
return snapshotVersion;
|
||||
case "Bungee":
|
||||
return bungeeVersion;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a server type's last downloaded version
|
||||
* @param type <p>The version type</p>
|
||||
* @param version <p>The version string</p>
|
||||
*/
|
||||
private static void setVersion(String type, String version) {
|
||||
if (!type.equals("")) {
|
||||
switch (type) {
|
||||
case "Vanilla":
|
||||
vanillaVersion = version;
|
||||
break;
|
||||
case "Snapshot":
|
||||
snapshotVersion = version;
|
||||
break;
|
||||
case "Bungee":
|
||||
bungeeVersion = version;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user