Adds various fixes to make the two java versions work as expected
Makes it possible to load a controller without generating a GUI, for better testing Makes sure not to try and parse empty profile lines Saves controller settings in a more readable and appendable format Adds code for using the correct java version for the occasion Adds a new function for writing to files
This commit is contained in:
@ -8,7 +8,7 @@ import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -230,7 +230,9 @@ public class Profile {
|
||||
*/
|
||||
public static Profile fromString(String profileString) throws ConfigurationException {
|
||||
Profile profile;
|
||||
if (profileString.contains("?")) {
|
||||
if (profileString.equals("")) {
|
||||
return null;
|
||||
} else if (profileString.contains("?")) {
|
||||
String[] data = profileString.split("\\?");
|
||||
String[] profileData = data[0].split(";", -1);
|
||||
profile = parseProfile(profileData);
|
||||
|
@ -9,16 +9,20 @@ import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
|
||||
import net.knarcraft.minecraftserverlauncher.utility.JarDownloader;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* This class handles profiles, GUI creation and session restoration
|
||||
*/
|
||||
public class Controller {
|
||||
public class ServerLauncherController {
|
||||
|
||||
private final String workingDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
|
||||
private final String profilesFile = workingDirectory + File.separator + "Profiles.txt";
|
||||
@ -28,12 +32,14 @@ public class Controller {
|
||||
private ServerLauncherGUI serverLauncherGUI;
|
||||
private Profile currentProfile;
|
||||
private boolean downloadAllJars;
|
||||
private static Controller controller;
|
||||
private static ServerLauncherController controller;
|
||||
private String javaCommand = "java";
|
||||
private String oldJavaCommand = "java";
|
||||
|
||||
/**
|
||||
* Instantiates a new controller
|
||||
*/
|
||||
private Controller() {
|
||||
private ServerLauncherController() {
|
||||
this.profileList = new ArrayList<>();
|
||||
}
|
||||
|
||||
@ -42,9 +48,9 @@ public class Controller {
|
||||
*
|
||||
* @return <p>An instance of the controller</p>
|
||||
*/
|
||||
public static Controller getInstance() {
|
||||
public static ServerLauncherController getInstance() {
|
||||
if (controller == null) {
|
||||
controller = new Controller();
|
||||
controller = new ServerLauncherController();
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
@ -85,6 +91,29 @@ public class Controller {
|
||||
return currentProfile.getDelayStartup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the command for running java
|
||||
*
|
||||
* <p>To play on the newest version of Minecraft, this needs to be JDK 16. Can be just "java" or a file path.</p>
|
||||
*
|
||||
* @return <p>The command for running java</p>
|
||||
*/
|
||||
public String getJavaCommand() {
|
||||
return javaCommand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the command for running older versions of java
|
||||
*
|
||||
* <p>The command used to run older minecraft server versions. JRE 8 should probably work.
|
||||
* Can be just "java" or a file path.</p>
|
||||
*
|
||||
* @return <p>The command for running older versions of java</p>
|
||||
*/
|
||||
public String getOldJavaCommand() {
|
||||
return oldJavaCommand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
int guiWidth;
|
||||
@ -96,7 +125,8 @@ public class Controller {
|
||||
guiWidth = serverLauncherGUI.getSize().width;
|
||||
guiHeight = serverLauncherGUI.getSize().height;
|
||||
}
|
||||
return String.format("%s;%d;%d;%b", currentProfile.getName(), guiWidth, guiHeight, downloadAllJars);
|
||||
return String.format("selectedProfile;%s\nguiWidth;%d\nguiHeight;%d\ndownloadAllJars;%b\njavaCommand;%s\noldJavaCommand;%s",
|
||||
currentProfile.getName(), guiWidth, guiHeight, downloadAllJars, javaCommand, oldJavaCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -171,37 +201,122 @@ public class Controller {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data about the controller from a save string
|
||||
* Sets the download all jars option
|
||||
*
|
||||
* @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>
|
||||
* @param downloadAllJars <p>Whether to download all jars</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];
|
||||
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();
|
||||
CommonFunctions.writeFile(mainFile, this.toString());
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Profile profile : profileList) {
|
||||
builder.append(profile.toString()).append("\n");
|
||||
}
|
||||
CommonFunctions.writeFile(profilesFile, builder.toString());
|
||||
} catch (IOException e) {
|
||||
serverLauncherGUI.showError("Unable to save data.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads profiles and servers from a text file
|
||||
*/
|
||||
public void loadState() throws ConfigurationException, IOException {
|
||||
loadState(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads profiles and servers from a text file
|
||||
*
|
||||
* @param silent <p>If silent, no GUI will be created</p>
|
||||
*/
|
||||
public void loadState(boolean silent) throws ConfigurationException, IOException {
|
||||
//Loads data regarding this controller
|
||||
String currentProfile = null;
|
||||
if (new File(mainFile).exists()) {
|
||||
String controllerData = CommonFunctions.readFile(mainFile);
|
||||
currentProfile = this.fromString(controllerData);
|
||||
} else {
|
||||
currentProfile = this.fromString(silent);
|
||||
} else if (!silent) {
|
||||
this.serverLauncherGUI = new ServerLauncherGUI();
|
||||
}
|
||||
if (silent) {
|
||||
return;
|
||||
}
|
||||
//Loads all saved profiles
|
||||
loadProfiles(currentProfile);
|
||||
//Makes the GUI show the loaded data
|
||||
executeGUILoadingTasks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data about the controller from a save string
|
||||
*
|
||||
* @param silent <p>If silent, no GUI will be created</p>
|
||||
* @return <p>The currently selected profile</p>
|
||||
*/
|
||||
private String fromString(boolean silent) {
|
||||
Map<String, String> loadedData = new HashMap<>();
|
||||
|
||||
try {
|
||||
String currentData = CommonFunctions.readFile(mainFile);
|
||||
for (String line : currentData.split("\n")) {
|
||||
parseSaveLine(loadedData, line);
|
||||
}
|
||||
this.downloadAllJars = Boolean.parseBoolean(loadedData.get("downloadAllJars"));
|
||||
|
||||
if (!silent) {
|
||||
this.serverLauncherGUI = new ServerLauncherGUI(Integer.parseInt(loadedData.get("guiWidth")),
|
||||
Integer.parseInt(loadedData.get("guiHeight")));
|
||||
} else {
|
||||
this.serverLauncherGUI = new ServerLauncherGUI(true);
|
||||
}
|
||||
|
||||
if (loadedData.get("javaCommand") != null) {
|
||||
this.javaCommand = loadedData.get("javaCommand");
|
||||
}
|
||||
if (loadedData.get("oldJavaCommand") != null) {
|
||||
this.oldJavaCommand = loadedData.get("oldJavaCommand");
|
||||
}
|
||||
return loadedData.get("selectedProfile");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves one config line to the provided map
|
||||
*
|
||||
* @param loadedData <p>The map to save the loaded configuration to</p>
|
||||
* @param line <p>The line to read</p>
|
||||
*/
|
||||
private void parseSaveLine(Map<String, String> loadedData, String line) {
|
||||
String[] lineData = line.split(";");
|
||||
if (lineData.length != 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
loadedData.put(lineData[0], lineData[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all saved profiles
|
||||
*
|
||||
@ -213,6 +328,9 @@ public class Controller {
|
||||
while (in.hasNextLine()) {
|
||||
String profileData = in.nextLine();
|
||||
Profile loadedProfile = Profile.fromString(profileData);
|
||||
if (loadedProfile == null) {
|
||||
continue;
|
||||
}
|
||||
profileList.add(loadedProfile);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
@ -248,45 +366,6 @@ public class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
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
|
||||
*/
|
Reference in New Issue
Block a user