EpicKnarvik97 60fdcf5ddc
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
Adds a proper GUI to display backup progress
Extracts backup code to its own class
Adds a new GUI to display progress and the file copied
2021-09-27 21:39:15 +02:00

256 lines
8.3 KiB
Java

package net.knarcraft.minecraftserverlauncher.profile;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.Server;
import net.knarcraft.minecraftserverlauncher.server.ServerHandler;
import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles;
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import javax.naming.ConfigurationException;
import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Keeps track of a set of servers and some user settings
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
*/
public class Profile {
private static final ServerLauncherGUI serverLauncherGui = Main.getController().getGUI();
private final List<Collection> collections;
private final String name;
private boolean runInBackground;
private int delayStartup;
/**
* 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;
}
/**
* 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;
}
/**
* 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]));
}
/**
* Gets whether the software should keep running in the background
*
* @return <p>Whether the software should keep running in the background</p>
*/
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 startup</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 all collections stored as part of this profile
*
* @return <p>All collections stored by this profile</p>
*/
public List<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 given its name
*
* @param name <p>The name of the collection to get</p>
* @return <p>A collection or null if no collection exists with the given name</p>
*/
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 <p>The name of the collection and its elements</p>
*/
public void addCollection(String name) throws ConfigurationException {
if (name == null) { //If a user cancels or crosses out window
return;
}
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\"." +
"It can't contain any of the characters \"!\", \"?\" or \";\".");
}
}
/**
* 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)) {
this.collections.remove(i);
serverLauncherGui.updateWithSavedProfileData();
break;
}
}
}
/**
* Updates console tabs with the current servers
*/
public void updateConsoles() {
JTabbedPane consolesTab = ServerConsoles.getTabbedPane();
consolesTab.removeAll();
for (Collection collection : collections) {
consolesTab.add(collection.getName(), collection.getServerConsole().getPanel());
}
}
/**
* Sends a command to a server, or all servers
*
* @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")) {
for (Collection collection : this.collections) {
try {
collection.getServer().sendCommand(command);
} catch (IOException e) {
serverLauncherGui.showError("Server " + collection.getName() + " caused an exception.");
}
}
} else {
Collection collection = getCollection(serverName);
if (collection != null) {
Server target = collection.getServer();
try {
target.sendCommand(command);
} catch (IOException e) {
serverLauncherGui.showError("Server " + target.getName() + " caused an exception.");
}
} else {
serverLauncherGui.showError("Server " + serverName + " is invalid.");
}
}
}
@Override
public String toString() {
StringBuilder saveString = new StringBuilder(String.format(
"%s;%b;%d?",
this.name,
this.runInBackground,
this.delayStartup)
);
for (Collection collection : this.collections) {
saveString.append(collection.getServer().toString());
}
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
return saveString.toString();
}
/**
* 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 Profile fromString(String profileString) throws ConfigurationException {
Profile profile;
if (profileString.equals("")) {
return null;
} else 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(ServerHandler.fromString(server)));
}
} else {
profile.collections.add(new Collection(ServerHandler.fromString(data[1])));
}
} else {
profile = parseProfile(profileString.split(";", -1));
}
return profile;
}
}