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:
@ -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