Adds missing comments, simplifies proxy distinction, moves updating to own class and fixes formatting
This commit is contained in:
@ -1,16 +1,20 @@
|
||||
package net.knarcraft.minecraftserverlauncher.profile;
|
||||
|
||||
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.Server;
|
||||
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 javax.naming.ConfigurationException;
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
@ -19,26 +23,22 @@ import java.util.concurrent.Executors;
|
||||
/**
|
||||
* Contains all user settings, and a list of servers.
|
||||
*
|
||||
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Profile {
|
||||
public class Profile implements java.io.Serializable {
|
||||
private static final ArrayList<Profile> profiles = new ArrayList<>();
|
||||
private static Profile current;
|
||||
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 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;
|
||||
|
||||
private static Profile current;
|
||||
private static transient ServerLauncherGUI serverLauncherGui;
|
||||
private final ArrayList<Collection> collections;
|
||||
private final String name;
|
||||
private boolean runInBackground;
|
||||
private int delayStartup;
|
||||
private boolean downloadAllAvailableJARFiles;
|
||||
private static String vanillaVersion;
|
||||
private static String snapshotVersion;
|
||||
private static String bungeeVersion;
|
||||
|
||||
private Profile(String name) {
|
||||
this.collections = new ArrayList<>();
|
||||
@ -68,43 +68,22 @@ public class Profile {
|
||||
return serverLauncherGui;
|
||||
}
|
||||
|
||||
public boolean getRunInBackground() {
|
||||
return this.runInBackground;
|
||||
}
|
||||
|
||||
public int getDelayStartup() {
|
||||
return this.delayStartup;
|
||||
}
|
||||
|
||||
public boolean getDownloadAllAvailableJARFiles() {
|
||||
return this.downloadAllAvailableJARFiles;
|
||||
}
|
||||
|
||||
public static Profile getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public ArrayList<Collection> getCollections() {
|
||||
return this.collections;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Collection object by name.
|
||||
* Set the current profile to the profile with a certain name.
|
||||
*
|
||||
* @param name The name of the collection.
|
||||
* @return A collection object.
|
||||
* @param name The name of the profile
|
||||
*/
|
||||
public Collection getCollection(String name) {
|
||||
for (Collection collection : this.collections) {
|
||||
if (collection.getName().equals(name)) {
|
||||
return collection;
|
||||
public static void setCurrent(String name) {
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
current = profile;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Profile getProfile(String name) {
|
||||
@ -120,38 +99,144 @@ public class Profile {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a profile, and creates a profile with the data.
|
||||
*
|
||||
* @param profileData <p>The data of the new profile</p>
|
||||
* @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])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a server, and creates a new collection.
|
||||
*
|
||||
* @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) 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;
|
||||
}
|
||||
|
||||
public void setRunInBackground(boolean value) {
|
||||
this.runInBackground = value;
|
||||
}
|
||||
|
||||
public int getDelayStartup() {
|
||||
return this.delayStartup;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current profile to the profile with a certain name.
|
||||
* Gets all collections stored as part of this profile
|
||||
*
|
||||
* @param name The name of the profile
|
||||
* @return <p>All collections stored by this profile</p>
|
||||
*/
|
||||
public static void setCurrent(String name) {
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
current = profile;
|
||||
break;
|
||||
public ArrayList<Collection> getCollections() {
|
||||
return this.collections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of this profile
|
||||
*
|
||||
* @return <p>The name of this profile</p>
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Collection object by name.
|
||||
*
|
||||
* @param name The name of the collection.
|
||||
* @return A collection object.
|
||||
*/
|
||||
public Collection getCollection(String name) {
|
||||
for (Collection collection : this.collections) {
|
||||
if (collection.getName().equals(name)) {
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a collection to the profile if the name is valid.
|
||||
*
|
||||
* @param name The name of the collection and its elements.
|
||||
* @param name The name of the collection and its elements.
|
||||
*/
|
||||
public void addCollection(String name) throws ConfigurationException {
|
||||
if (name == null) { //If a user cancels or crosses out window
|
||||
@ -171,28 +256,6 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
public void removeCollection(String name) {
|
||||
for (int i = 0; i < collections.size(); i++) {
|
||||
if (collections.get(i).getName().equals(name)) {
|
||||
@ -203,16 +266,6 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a profile with the given name from the list of profiles, if such a profile exists
|
||||
* @param name <p>The name of the profile to rempve</p>
|
||||
*/
|
||||
public static void removeProfile(String name) {
|
||||
if (profiles.size() > 1) {
|
||||
profiles.removeIf(profile -> profile.name.equals(name));
|
||||
}
|
||||
}
|
||||
|
||||
public void updateConsoles() {
|
||||
JTabbedPane consolesTab = ServerConsoles.getTabbedPane();
|
||||
consolesTab.removeAll();
|
||||
@ -224,8 +277,8 @@ public class Profile {
|
||||
/**
|
||||
* Sends a command to a server, or all servers
|
||||
*
|
||||
* @param serverName The target server
|
||||
* @param command The command to send.
|
||||
* @param serverName The target server
|
||||
* @param command The command to send.
|
||||
*/
|
||||
public void sendCommand(String serverName, String command) {
|
||||
if (serverName.equals("All")) {
|
||||
@ -256,6 +309,18 @@ public class Profile {
|
||||
* 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();
|
||||
}*/
|
||||
|
||||
for (Collection collection : this.collections) {
|
||||
Server server = collection.getServer();
|
||||
ServerTab serverTab = collection.getServerTab();
|
||||
@ -267,7 +332,7 @@ public class Profile {
|
||||
} catch (IllegalArgumentException e) {
|
||||
serverLauncherGui.showError("Invalid server version for " + server.getName());
|
||||
}
|
||||
server.toggle(serverTab.enabled());
|
||||
server.setEnabled(serverTab.enabled());
|
||||
}
|
||||
if (!new File(profilesDir).exists() && !new File(profilesDir).mkdirs()) {
|
||||
serverLauncherGui.showError("Unable to create the folder " + profilesDir);
|
||||
@ -284,11 +349,8 @@ public class Profile {
|
||||
height = serverLauncherGui.getSize().height;
|
||||
}
|
||||
file.println(String.format(
|
||||
"%s;%s;%s;%s;%d;%d",
|
||||
"%s;%s;%s",
|
||||
current.name,
|
||||
vanillaVersion,
|
||||
snapshotVersion,
|
||||
bungeeVersion,
|
||||
width,
|
||||
height
|
||||
));
|
||||
@ -304,17 +366,13 @@ public class Profile {
|
||||
for (Collection collection : profile.collections) {
|
||||
Server server = collection.getServer();
|
||||
saveString.append(String.format(
|
||||
"%s;%s;%b;%s;%s;%s;%s;%s;%s;%s!",
|
||||
"%s;%s;%b;%s;%s;%s!",
|
||||
server.getName(),
|
||||
server.getPath(),
|
||||
server.isEnabled(),
|
||||
server.getTypeName(),
|
||||
server.getServerVersion(),
|
||||
server.getMaxRam(),
|
||||
server.getVanillaVersion(),
|
||||
server.getSnapshotVersion(),
|
||||
server.getSpongeVanillaVersion(),
|
||||
server.getBungeeVersion()
|
||||
server.getMaxRam()
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -344,16 +402,32 @@ public class Profile {
|
||||
/**
|
||||
* Reads profiles and servers from a text file.
|
||||
*/
|
||||
public static void load() throws ConfigurationException {
|
||||
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];
|
||||
vanillaVersion = staticData[1];
|
||||
snapshotVersion = staticData[2];
|
||||
bungeeVersion = staticData[3];
|
||||
int guiWidth = Integer.parseInt(staticData[4]);
|
||||
int guiHeight = Integer.parseInt(staticData[5]);
|
||||
int guiWidth = Integer.parseInt(staticData[1]);
|
||||
int guiHeight = Integer.parseInt(staticData[2]);
|
||||
serverLauncherGui = new ServerLauncherGUI(guiWidth, guiHeight);
|
||||
while (in.hasNextLine()) {
|
||||
String line = in.nextLine();
|
||||
@ -379,6 +453,7 @@ public class Profile {
|
||||
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);
|
||||
@ -392,7 +467,6 @@ public class Profile {
|
||||
serverLauncherGui.showMessage("A profiles file was not found. Default profile was created.");
|
||||
try {
|
||||
serverLauncherGui = new ServerLauncherGUI();
|
||||
} catch (FileNotFoundException ex) {
|
||||
serverLauncherGui.showMessage("Failed to load ServerLauncherGUI messages. The ServerLauncherGUI can't be shown.");
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
@ -417,38 +491,4 @@ public class Profile {
|
||||
serverLauncherGui.hide();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a profile, and creates a profile with the data.
|
||||
* @param profileData <p>The data of the new profile</p>
|
||||
* @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])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a server, and creates a new collection.
|
||||
* @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) throws ConfigurationException {
|
||||
profile.collections.add(new Collection(
|
||||
serverData[0],
|
||||
serverData[1],
|
||||
Boolean.parseBoolean(serverData[2]),
|
||||
serverData[3],
|
||||
serverData[4],
|
||||
serverData[5],
|
||||
serverData[6],
|
||||
serverData[7],
|
||||
serverData[8],
|
||||
serverData[9])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user