Changes a lot of things to make everything cleaner. Closes #3
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
Drops the idea of using serializable Adds a new controller object which takes care of profiles and saving Moves profile independent settings to its own file Makes saving and loading from file a lot cleaner Fixes the bug preventing lastly used profile from loading Makes the profile object only do profile things Moves gui initialization to the controller object Updates vanilla version from 1.16.1 to 1.16.2 Moves backup to common functions
This commit is contained in:
@ -6,7 +6,6 @@ import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerTab;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Acts as a wrapper for objects necessary for each server.
|
||||
@ -15,9 +14,9 @@ import java.io.Serializable;
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Collection implements Serializable {
|
||||
public class Collection {
|
||||
private final Server server;
|
||||
private final transient ServerTab serverTab;
|
||||
private final ServerTab serverTab;
|
||||
private final Console serverConsole;
|
||||
private final String name;
|
||||
|
||||
@ -45,25 +44,8 @@ public class Collection implements Serializable {
|
||||
this.server = server;
|
||||
this.serverConsole = ServerConsoles.addConsoleTab(serverName);
|
||||
this.name = serverName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*/
|
||||
Collection(String name, String serverPath, boolean enabled, String typeName, String serverVersion, String maxRam)
|
||||
throws ConfigurationException {
|
||||
this.serverTab = new ServerTab(name);
|
||||
this.server = new Server(name, serverPath, enabled, typeName, serverVersion, maxRam);
|
||||
this.serverConsole = ServerConsoles.addConsoleTab(name);
|
||||
this.name = name;
|
||||
this.serverTab.setData(serverPath, enabled, typeName, serverVersion, maxRam);
|
||||
this.serverTab.setData(server.getPath(), server.isEnabled(), server.getTypeName(), server.getServerVersion(),
|
||||
server.getMaxRam());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,293 @@
|
||||
package net.knarcraft.minecraftserverlauncher.profile;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.server.Server;
|
||||
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerTab;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.JarDownloader;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* This class controls
|
||||
*/
|
||||
public class Controller {
|
||||
|
||||
private final String workingDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
|
||||
private final String profilesFile = workingDirectory + File.separator + "Profiles.txt";
|
||||
private final String mainFile = workingDirectory + File.separator + "Config.txt";
|
||||
private final String jarDirectory = workingDirectory + File.separator + "Jars" + File.separator;
|
||||
private final List<Profile> profileList;
|
||||
private ServerLauncherGUI serverLauncherGUI;
|
||||
private Profile currentProfile;
|
||||
private boolean downloadAllJars;
|
||||
private static Controller controller;
|
||||
|
||||
private Controller() {
|
||||
this.profileList = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an instance of the controller
|
||||
*
|
||||
* @return <p>An instance of the controller</p>
|
||||
*/
|
||||
public static Controller getInstance() {
|
||||
if (controller == null) {
|
||||
controller = new Controller();
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the GUI used by the software
|
||||
*
|
||||
* @return <p>The GUI used by the software</p>
|
||||
*/
|
||||
public ServerLauncherGUI getGUI() {
|
||||
return this.serverLauncherGUI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether to download all jar files
|
||||
*
|
||||
* @return <p>Whether to download all .jar files</p>
|
||||
*/
|
||||
public boolean getDownloadAllJars() {
|
||||
return downloadAllJars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether to run in background after closing
|
||||
*
|
||||
* @return <p>Whether to run in background after closing</p>
|
||||
*/
|
||||
public boolean getRunInBackground() {
|
||||
return currentProfile.getRunInBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether to delay server startup
|
||||
*
|
||||
* @return <p>Whether to delay server startup</p>
|
||||
*/
|
||||
public int getDelayStartup() {
|
||||
return currentProfile.getDelayStartup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
int guiWidth;
|
||||
int guiHeight;
|
||||
if (serverLauncherGUI == null) {
|
||||
guiWidth = 440;
|
||||
guiHeight = 170;
|
||||
} else {
|
||||
guiWidth = serverLauncherGUI.getSize().width;
|
||||
guiHeight = serverLauncherGUI.getSize().height;
|
||||
}
|
||||
return String.format("%s;%d;%d;%b", currentProfile.getName(), guiWidth, guiHeight, downloadAllJars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a profile by its name
|
||||
*
|
||||
* @param name <p>The name of the profile to get</p>
|
||||
* @return <p>The profile with the given name or null if not found</p>
|
||||
*/
|
||||
public Profile getProfileByName(String name) {
|
||||
for (Profile profile : profileList) {
|
||||
if (profile.getName().equals(name)) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the names of all available profiles
|
||||
*
|
||||
* @return <p>A list of all available profiles</p>
|
||||
*/
|
||||
public List<String> getProfileNames() {
|
||||
List<String> profileNames = new ArrayList<>();
|
||||
profileList.forEach((profile) -> profileNames.add(profile.getName()));
|
||||
return profileNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a profile if the name is valid and unique.
|
||||
*
|
||||
* @param name The name of the new profile.
|
||||
*/
|
||||
public void addProfile(String name) {
|
||||
if (!CommonFunctions.nameIsValid(name)) {
|
||||
serverLauncherGUI.showError("Profile name cannot be blank and cannot contain special characters.");
|
||||
return;
|
||||
}
|
||||
for (Profile profile : profileList) {
|
||||
if (profile.getName().equals(name)) {
|
||||
serverLauncherGUI.showError("There is already a profile with this name.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
Profile newProfile = new Profile(name);
|
||||
profileList.add(newProfile);
|
||||
if (currentProfile == null) {
|
||||
currentProfile = newProfile;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 void removeProfile(String name) {
|
||||
if (profileList.size() > 1) {
|
||||
profileList.removeIf(profile -> profile.getName().equals(name));
|
||||
} else {
|
||||
serverLauncherGUI.showError("This software requires the existence of at least one profile.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently selected profile
|
||||
*
|
||||
* @return <p>The currently selected profile</p>
|
||||
*/
|
||||
public Profile getCurrentProfile() {
|
||||
return this.currentProfile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data about the controller from a save string
|
||||
*
|
||||
* @param data <p>The save string to load</p>
|
||||
* @return <p>The currently selected profile</p>
|
||||
* @throws IOException <p>If unable to create the GUI</p>
|
||||
*/
|
||||
private String fromString(String data) throws IOException {
|
||||
String[] dataList = data.split(";");
|
||||
this.downloadAllJars = Boolean.parseBoolean(dataList[3]);
|
||||
this.serverLauncherGUI = new ServerLauncherGUI(Integer.parseInt(dataList[1]), Integer.parseInt(dataList[2]));
|
||||
return dataList[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads profiles and servers from a text file.
|
||||
*/
|
||||
public void loadState() throws ConfigurationException, IOException {
|
||||
//Loads data regarding this controller
|
||||
String currentProfile = null;
|
||||
if (new File(mainFile).exists()) {
|
||||
String controllerData = CommonFunctions.readBufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(mainFile))));
|
||||
currentProfile = this.fromString(controllerData);
|
||||
} else {
|
||||
this.serverLauncherGUI = new ServerLauncherGUI();
|
||||
}
|
||||
|
||||
try (Scanner in = new Scanner(new File(profilesFile))) {
|
||||
while (in.hasNextLine()) {
|
||||
String profileData = in.nextLine();
|
||||
Profile loadedProfile = Profile.fromString(profileData);
|
||||
profileList.add(loadedProfile);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
addProfile("Default");
|
||||
}
|
||||
|
||||
this.currentProfile = getProfileByName(currentProfile);
|
||||
if (this.currentProfile == null) {
|
||||
this.currentProfile = this.profileList.get(0);
|
||||
}
|
||||
|
||||
this.serverLauncherGUI.updateProfiles();
|
||||
this.serverLauncherGUI.update();
|
||||
this.currentProfile.updateConsoles();
|
||||
if (this.downloadAllJars) {
|
||||
Executors.newSingleThreadExecutor().execute(() -> {
|
||||
JarDownloader downloader = new JarDownloader(serverLauncherGUI, jarDirectory);
|
||||
try {
|
||||
downloader.downloadJars();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
if (this.currentProfile.getRunInBackground()) {
|
||||
Executors.newSingleThreadExecutor().execute(Server::startServers);
|
||||
this.serverLauncherGUI.hide();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the download all jars option
|
||||
*
|
||||
* @param downloadAllJars <p>Whether to download all jars</p>
|
||||
*/
|
||||
public void setDownloadAllJars(boolean downloadAllJars) {
|
||||
this.downloadAllJars = downloadAllJars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the currently selected profile
|
||||
*
|
||||
* @param profileName <p>The name of the currently selected profile</p>
|
||||
*/
|
||||
public void setCurrentProfile(String profileName) {
|
||||
this.currentProfile = getProfileByName(profileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the state of the entire application to disk
|
||||
*/
|
||||
public void saveState() {
|
||||
saveGUIStateToServer();
|
||||
try {
|
||||
CommonFunctions.createAllFolders();
|
||||
PrintWriter mFile = new PrintWriter(mainFile);
|
||||
mFile.println(this.toString());
|
||||
mFile.close();
|
||||
PrintWriter pFile = new PrintWriter(profilesFile);
|
||||
for (Profile profile : profileList) {
|
||||
pFile.println(profile.toString());
|
||||
}
|
||||
pFile.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
serverLauncherGUI.showError("Unable to save data.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the server object with the current state of the GUI server tab
|
||||
*/
|
||||
private void saveGUIStateToServer() {
|
||||
for (Collection collection : this.currentProfile.getCollections()) {
|
||||
Server server = collection.getServer();
|
||||
ServerTab serverTab = collection.getServerTab();
|
||||
server.setPath(serverTab.getPath());
|
||||
server.setMaxRam(serverTab.getMaxRam());
|
||||
try {
|
||||
server.setType(ServerTypeHandler.getByName(serverTab.getType()));
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
server.setServerVersion(serverTab.getVersion());
|
||||
} catch (IllegalArgumentException e) {
|
||||
serverLauncherGUI.showError("Invalid server version for " + server.getName());
|
||||
}
|
||||
server.setEnabled(serverTab.enabled());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -2,23 +2,14 @@ package net.knarcraft.minecraftserverlauncher.profile;
|
||||
|
||||
import net.knarcraft.minecraftserverlauncher.Main;
|
||||
import net.knarcraft.minecraftserverlauncher.server.Server;
|
||||
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
|
||||
import net.knarcraft.minecraftserverlauncher.userinterface.ServerTab;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.JarDownloader;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Contains all user settings, and a list of servers.
|
||||
@ -27,110 +18,38 @@ import java.util.concurrent.Executors;
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Profile implements java.io.Serializable {
|
||||
private static final ArrayList<Profile> profiles = new ArrayList<>();
|
||||
private static final String workingDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
|
||||
private static final String profilesDir = workingDirectory;
|
||||
private static final String profilesFile = workingDirectory + File.separator + "Profiles.txt";
|
||||
private static final String jarDirectory = workingDirectory + File.separator + "Jars" + File.separator;
|
||||
private static Profile current;
|
||||
private static transient ServerLauncherGUI serverLauncherGui;
|
||||
public class Profile {
|
||||
|
||||
private static final ServerLauncherGUI serverLauncherGui = Main.getController().getGUI();
|
||||
private final ArrayList<Collection> collections;
|
||||
private final String name;
|
||||
private boolean runInBackground;
|
||||
private int delayStartup;
|
||||
private boolean downloadAllAvailableJARFiles;
|
||||
|
||||
private Profile(String name) {
|
||||
/**
|
||||
* Instantiates a new default profile
|
||||
*
|
||||
* @param name <p>The name of the profile</p>
|
||||
*/
|
||||
public Profile(String name) {
|
||||
this.collections = new ArrayList<>();
|
||||
this.name = name;
|
||||
this.runInBackground = false;
|
||||
this.delayStartup = 0;
|
||||
this.downloadAllAvailableJARFiles = false;
|
||||
profiles.add(this);
|
||||
if (current == null) {
|
||||
current = this;
|
||||
}
|
||||
}
|
||||
|
||||
private Profile(String name, boolean runInBackground, int delayStartup, boolean downloadAllAvailableJARFiles) {
|
||||
/**
|
||||
* Instantiates a new profile
|
||||
*
|
||||
* @param name <p>The name of the profile</p>
|
||||
* @param runInBackground <p>Whether to run the software in the background the next time it starts</p>
|
||||
* @param delayStartup <p>Whether to delay the startup of servers</p>
|
||||
*/
|
||||
private Profile(String name, boolean runInBackground, int delayStartup) {
|
||||
this.collections = new ArrayList<>();
|
||||
this.name = name;
|
||||
this.runInBackground = runInBackground;
|
||||
this.delayStartup = delayStartup;
|
||||
this.downloadAllAvailableJARFiles = downloadAllAvailableJARFiles;
|
||||
profiles.add(this);
|
||||
if (current == null) {
|
||||
current = this;
|
||||
}
|
||||
}
|
||||
|
||||
public static ServerLauncherGUI getGUI() {
|
||||
return serverLauncherGui;
|
||||
}
|
||||
|
||||
public static Profile getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current profile to the profile with a certain name.
|
||||
*
|
||||
* @param name The name of the profile
|
||||
*/
|
||||
public static void setCurrent(String name) {
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
current = profile;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Profile getProfile(String name) {
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ArrayList<Profile> getProfiles() {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a profile if the name is valid and unique.
|
||||
*
|
||||
* @param name The name of the new profile.
|
||||
*/
|
||||
public static void addProfile(String name) {
|
||||
if (name == null) { //If a user cancels or crosses out window
|
||||
return;
|
||||
}
|
||||
if (name.equals("") && !name.matches("^[!?;]+$")) {
|
||||
serverLauncherGui.showError("Profile name can't be blank.");
|
||||
return;
|
||||
}
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
serverLauncherGui.showError("There is already a profile with this name.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
new Profile(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if (profiles.size() > 1) {
|
||||
profiles.removeIf(profile -> profile.name.equals(name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -140,67 +59,47 @@ public class Profile implements java.io.Serializable {
|
||||
* @return <p>The new profile</p>
|
||||
*/
|
||||
private static Profile parseProfile(String[] profileData) {
|
||||
return new Profile(
|
||||
profileData[0],
|
||||
Boolean.parseBoolean(profileData[1]),
|
||||
Integer.parseInt(profileData[2]),
|
||||
Boolean.parseBoolean(profileData[3])
|
||||
);
|
||||
return new Profile(profileData[0], Boolean.parseBoolean(profileData[1]), Integer.parseInt(profileData[2]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a server, and creates a new collection.
|
||||
* Gets whether the software should keep running in the background
|
||||
*
|
||||
* @param profile <p>The profile which to add the collection</p>
|
||||
* @param serverData <p>The data to parse</p>
|
||||
* @return <p>Whether the software should keep running in the backgound</p>
|
||||
*/
|
||||
private static void parseServer(Profile profile, String[] serverData) throws ConfigurationException {
|
||||
profile.collections.add(new Collection(
|
||||
serverData[0],
|
||||
serverData[1],
|
||||
Boolean.parseBoolean(serverData[2]),
|
||||
serverData[3],
|
||||
serverData[4],
|
||||
serverData[5])
|
||||
);
|
||||
}
|
||||
|
||||
public boolean getRunInBackground() {
|
||||
return this.runInBackground;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the software should keep running in the background
|
||||
*
|
||||
* @param value <p>Whether the software should keep running in the background</p>
|
||||
*/
|
||||
public void setRunInBackground(boolean value) {
|
||||
this.runInBackground = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of seconds to delay startup
|
||||
*
|
||||
* @return <p>The number of seconds to delay starup</p>
|
||||
*/
|
||||
public int getDelayStartup() {
|
||||
return this.delayStartup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the amount of time to delay startup
|
||||
*
|
||||
* @param value <p>The number of seconds to delay startup</p>
|
||||
*/
|
||||
public void setDelayStartup(int value) {
|
||||
if (value >= 0) {
|
||||
this.delayStartup = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state of the download all jar files setting
|
||||
*
|
||||
* @return <p>Whether to download all jars</p>
|
||||
*/
|
||||
public boolean getDownloadAllAvailableJARFiles() {
|
||||
return this.downloadAllAvailableJARFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the state of the download all jar files setting
|
||||
*
|
||||
* @param value <p>The new value of the download all jar files setting</p>
|
||||
*/
|
||||
public void setDownloadAllAvailableJARFiles(boolean value) {
|
||||
this.downloadAllAvailableJARFiles = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all collections stored as part of this profile
|
||||
*
|
||||
@ -243,13 +142,7 @@ public class Profile implements java.io.Serializable {
|
||||
if (name == null) { //If a user cancels or crosses out window
|
||||
return;
|
||||
}
|
||||
if (getCollection(name) == null &&
|
||||
!name.equals("") &&
|
||||
!name.equals("All") &&
|
||||
!name.contains("!") &&
|
||||
!name.contains("?") &&
|
||||
!name.contains(";")
|
||||
) {
|
||||
if (getCollection(name) == null && !name.equals("All") && CommonFunctions.nameIsValid(name)) {
|
||||
collections.add(new Collection(name));
|
||||
} else {
|
||||
serverLauncherGui.showError("A server name must my unique and not empty or \"All\"." +
|
||||
@ -257,6 +150,11 @@ public class Profile implements java.io.Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the collection with the given name
|
||||
*
|
||||
* @param name <p>The name of the collection to remove</p>
|
||||
*/
|
||||
public void removeCollection(String name) {
|
||||
for (int i = 0; i < collections.size(); i++) {
|
||||
if (collections.get(i).getName().equals(name)) {
|
||||
@ -267,6 +165,9 @@ public class Profile implements java.io.Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates console tabs with the current servers
|
||||
*/
|
||||
public void updateConsoles() {
|
||||
JTabbedPane consolesTab = ServerConsoles.getTabbedPane();
|
||||
consolesTab.removeAll();
|
||||
@ -278,8 +179,8 @@ public class Profile implements java.io.Serializable {
|
||||
/**
|
||||
* Sends a command to a server, or all servers
|
||||
*
|
||||
* @param serverName The target server
|
||||
* @param command The command to send.
|
||||
* @param serverName <p>The target server</p>
|
||||
* @param command <p>The command to send</p>
|
||||
*/
|
||||
public void sendCommand(String serverName, String command) {
|
||||
if (serverName.equals("All")) {
|
||||
@ -305,194 +206,45 @@ public class Profile implements java.io.Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() throws FileNotFoundException {
|
||||
/*try {
|
||||
FileOutputStream fileOut =
|
||||
new FileOutputStream(profilesFile);
|
||||
ObjectOutputStream out = new ObjectOutputStream(fileOut);
|
||||
out.writeObject(this);
|
||||
out.close();
|
||||
fileOut.close();
|
||||
} catch (IOException i) {
|
||||
System.out.println("Unable to serialize or write the profile settings file");
|
||||
i.printStackTrace();
|
||||
}*/
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder saveString = new StringBuilder(String.format(
|
||||
"%s;%b;%d?",
|
||||
this.name,
|
||||
this.runInBackground,
|
||||
this.delayStartup)
|
||||
);
|
||||
for (Collection collection : this.collections) {
|
||||
Server server = collection.getServer();
|
||||
ServerTab serverTab = collection.getServerTab();
|
||||
server.setPath(serverTab.getPath());
|
||||
server.setMaxRam(serverTab.getMaxRam());
|
||||
try {
|
||||
server.setType(ServerTypeHandler.getByName(serverTab.getType()));
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
server.setServerVersion(serverTab.getVersion());
|
||||
} catch (IllegalArgumentException e) {
|
||||
serverLauncherGui.showError("Invalid server version for " + server.getName());
|
||||
}
|
||||
server.setEnabled(serverTab.enabled());
|
||||
}
|
||||
if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) {
|
||||
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 (serverLauncherGui == null) {
|
||||
width = 440;
|
||||
height = 170;
|
||||
} else {
|
||||
width = serverLauncherGui.getSize().width;
|
||||
height = serverLauncherGui.getSize().height;
|
||||
}
|
||||
file.println(String.format(
|
||||
"%s;%s;%s",
|
||||
current.name,
|
||||
width,
|
||||
height
|
||||
));
|
||||
file.close();
|
||||
for (Profile profile : profiles) {
|
||||
StringBuilder saveString = new StringBuilder(String.format(
|
||||
"%s;%b;%d;%b?",
|
||||
profile.name,
|
||||
profile.runInBackground,
|
||||
profile.delayStartup,
|
||||
profile.downloadAllAvailableJARFiles)
|
||||
);
|
||||
for (Collection collection : profile.collections) {
|
||||
Server server = collection.getServer();
|
||||
saveString.append(String.format(
|
||||
"%s;%s;%b;%s;%s;%s!",
|
||||
server.getName(),
|
||||
server.getPath(),
|
||||
server.isEnabled(),
|
||||
server.getTypeName(),
|
||||
server.getServerVersion(),
|
||||
server.getMaxRam()
|
||||
)
|
||||
);
|
||||
}
|
||||
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
|
||||
try (PrintWriter fileAppend = new PrintWriter(new FileWriter(
|
||||
profilesFile,
|
||||
true
|
||||
))) {
|
||||
fileAppend.println(saveString);
|
||||
} catch (IOException e) {
|
||||
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.");
|
||||
}
|
||||
throw new FileNotFoundException("Unable to save to the profiles file.");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
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");
|
||||
saveString.append(collection.getServer().toString());
|
||||
}
|
||||
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
|
||||
return saveString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads profiles and servers from a text file.
|
||||
* Gets a profile given a saved string
|
||||
*
|
||||
* @param profileString <p>The string containing all profile data</p>
|
||||
* @return <p>A profile with the given data</p>
|
||||
* @throws ConfigurationException <p>If unable to load one of the profile's servers</p>
|
||||
*/
|
||||
public static void load() throws ConfigurationException, IOException {
|
||||
/*Profile p;
|
||||
if (!new File(profilesFile).exists()) {
|
||||
addProfile("Default");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
FileInputStream fileIn = new FileInputStream(profilesFile);
|
||||
ObjectInputStream in = new ObjectInputStream(fileIn);
|
||||
p = (Profile) in.readObject();
|
||||
in.close();
|
||||
fileIn.close();
|
||||
profiles.add(p);
|
||||
} catch (IOException i) {
|
||||
i.printStackTrace();
|
||||
} catch (ClassNotFoundException c) {
|
||||
System.out.println("Profile class not found");
|
||||
c.printStackTrace();
|
||||
}*/
|
||||
|
||||
try (Scanner in = new Scanner(new File(profilesFile))) {
|
||||
try {
|
||||
String[] staticData = in.nextLine().split(";", -1);
|
||||
String profileName = staticData[0];
|
||||
int guiWidth = Integer.parseInt(staticData[1]);
|
||||
int guiHeight = Integer.parseInt(staticData[2]);
|
||||
serverLauncherGui = new ServerLauncherGUI(guiWidth, guiHeight);
|
||||
while (in.hasNextLine()) {
|
||||
String line = in.nextLine();
|
||||
if (line.contains("?")) {
|
||||
String[] data = line.split("\\?");
|
||||
String[] profileData = data[0].split(";", -1);
|
||||
Profile profile = parseProfile(profileData);
|
||||
if (data[1].contains("!")) {
|
||||
String[] servers = data[1].split("!", -1);
|
||||
for (String server : servers) {
|
||||
String[] serverData = server.split(";", -1);
|
||||
parseServer(profile, serverData);
|
||||
}
|
||||
} else {
|
||||
String[] serverData = data[1].split(";", -1);
|
||||
parseServer(profile, serverData);
|
||||
}
|
||||
} else {
|
||||
String[] profileData = line.split(";", -1);
|
||||
parseProfile(profileData);
|
||||
}
|
||||
public static Profile fromString(String profileString) throws ConfigurationException {
|
||||
Profile profile;
|
||||
if (profileString.contains("?")) {
|
||||
String[] data = profileString.split("\\?");
|
||||
String[] profileData = data[0].split(";", -1);
|
||||
profile = parseProfile(profileData);
|
||||
if (data[1].contains("!")) {
|
||||
String[] servers = data[1].split("!", -1);
|
||||
for (String server : servers) {
|
||||
profile.collections.add(new Collection(Server.fromString(server)));
|
||||
}
|
||||
current = getProfile(profileName);
|
||||
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
serverLauncherGui = new ServerLauncherGUI();
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
} else {
|
||||
profile.collections.add(new Collection(Server.fromString(data[1])));
|
||||
}
|
||||
if (profiles.size() == 0) {
|
||||
addProfile("Default");
|
||||
}
|
||||
} catch (FileNotFoundException | NoSuchElementException e) {
|
||||
try {
|
||||
serverLauncherGui = new ServerLauncherGUI();
|
||||
serverLauncherGui.showMessage("A profiles file was not found. Default profile was created.");
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
addProfile("Default");
|
||||
}
|
||||
serverLauncherGui.update();
|
||||
serverLauncherGui.updateProfiles();
|
||||
current.updateConsoles();
|
||||
if (current.downloadAllAvailableJARFiles) {
|
||||
Executors.newSingleThreadExecutor().execute(() -> {
|
||||
JarDownloader downloader = new JarDownloader(serverLauncherGui, jarDirectory);
|
||||
try {
|
||||
downloader.downloadJars();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
if (current.runInBackground) {
|
||||
Executors.newSingleThreadExecutor().execute(Server::startServers);
|
||||
serverLauncherGui.hide();
|
||||
} else {
|
||||
profile = parseProfile(profileString.split(";", -1));
|
||||
}
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user