212 lines
7.3 KiB
Java

package net.knarcraft.serverlauncher.profile;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.ServerTab;
import net.knarcraft.serverlauncher.Main;
import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* Contains all user settings, and a list of servers.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class Profile {
private static final ArrayList<Profile> profiles = new ArrayList<>();
private static Profile current;
private final ArrayList<Collection> collections;
private final String name;
private boolean runInBackground;
private int delayStartup;
private boolean downloadJars;
private Profile(String name) {
this.collections = new ArrayList<>();
this.name = name;
this.runInBackground = false;
this.delayStartup = 0;
this.downloadJars = false;
profiles.add(this);
if (current == null) {
current = this;
}
}
public void addCollection(String name) {
if (name == null) { //If a user cancels or crosses out window
return;
}
if (!collectionExists(name) && !name.equals("") && !name.equals("All")) {
collections.add(new Collection(name));
} else {
JOptionPane.showMessageDialog(null, "A server name must my unique and not empty or \"All\".", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void removeCollection(String name) {
for (int i = 0; i < collections.size(); i++) {
if (collections.get(i).getName().equals(name)) {
this.collections.remove(i);
Main.gui().updateSelectServers();
break;
}
}
}
public Collection getCollection(String name) {
for (Collection collection : this.collections) {
if (collection.getName().equals(name)) {
return collection;
}
}
return null;
}
private boolean collectionExists(String name) {
for (Collection collection : this.collections) {
if (collection.getName().equals(name)) {
return true;
}
}
return false;
}
public ArrayList<Collection> getCollections() {
return this.collections;
}
public static void addProfile(String name) {
if (name == null) { //If a user cancels or crosses out window
return;
}
if (name.equals("")) {
JOptionPane.showMessageDialog(null, "Profile name can't be blank.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
JOptionPane.showMessageDialog(null, "There is already a profile with this name.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
}
new Profile(name);
Main.gui().addProfile(name);
}
public static Profile getProfile(String name) {
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
return profile;
}
}
return null;
}
public static void deleteProfile(String name) {
if (profiles.size() > 1) {
for (int i = 0; i < profiles.size(); i++) {
if (profiles.get(i).name.equals((name))) {
profiles.remove(i);
Main.gui().removeProfile(i);
}
}
}
}
public static Profile getCurrent() {
return current;
}
public static void setCurrent(String name) {
for (Profile profile : profiles) {
if (profile.name.equals(name)) {
current = profile;
break;
}
}
}
public void setRunInBackground(boolean value) {
this.runInBackground = value;
}
public void setDelayStartup(int value) {
if (value >= 0) {
this.delayStartup = value;
}
}
public void setDownloadJars(boolean value) {
this.downloadJars = value;
}
/**
* Sends a command to a server, or all servers
*
* @param serverName The target server
* @param command The command to send.
*/
public void sendCommand(String serverName, String command) {
if (serverName.equals("All")) {
for (Collection collection : this.collections) {
try {
collection.getServer().sendCommand(command);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Server " + collection.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} else {
Collection collection = getCollection(serverName);
if (collection != null) {
Server target = collection.getServer();
try {
target.sendCommand(command);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Server " + target.getName() + " caused an exception.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "Server " + serverName + " is invalid.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* Reads all server tabs, and saves it to the variables of the corresponding servers.
*/
public void save() {
for (Collection collection : this.collections) {
Server server = collection.getServer();
ServerTab serverTab = collection.getServerTab();
server.setPath(serverTab.getPath());
server.setMaxRam(serverTab.getMaxRam());
server.setType(ServerType.getByName(serverTab.getType()));
try {
server.setServerVersion(serverTab.getVersion());
} catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog(null, "Invalid server version for " + server.getName(), "Error", JOptionPane.ERROR_MESSAGE);
}
server.toggle(serverTab.enabled());
}
StringBuilder saveString = new StringBuilder(String.format("%s;%b;%b;%b?", this.name, this.runInBackground, this.delayStartup, this.downloadJars));
for (Collection collection : this.collections) {
Server server = collection.getServer();
saveString.append(String.format("%s;%s;%b;%s;%s;%s;%s;%s;%s;%s!", server.getName(), server.getPath(), server.isEnabled(), server.typeName(), server.getServerVersion(), server.getMaxRam(), server.getVanillaVersion(), server.getSnapshotVersion(), server.getSpongeVanillaVersion(), server.getBungeeVersion()));
}
saveString = new StringBuilder(saveString.substring(0, saveString.length() - 1));
try (PrintWriter file = new PrintWriter("files/Profiles.txt")) {
file.println(saveString);
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Unable to save to file. Try running the software as an administrator.", "About", JOptionPane.INFORMATION_MESSAGE);
}
}
}