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

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:
2020-08-17 21:41:38 +02:00
parent 2ed6b8bedb
commit a3de8a9f6c
24 changed files with 603 additions and 537 deletions

View File

@ -2,7 +2,7 @@ package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.profile.Controller;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import javax.naming.ConfigurationException;
@ -25,7 +25,7 @@ import java.util.concurrent.TimeUnit;
* @version 1.0.0
* @since 1.0.0
*/
public class Server implements java.io.Serializable {
public class Server {
/**
* Available ram sizes. For ServerLauncherGUI dropdown
*/
@ -108,7 +108,7 @@ public class Server implements java.io.Serializable {
* @throws IOException If a writer's process is already closed but not null.
*/
public static void stop() throws IOException {
for (Collection collection : Profile.getCurrent().getCollections()) {
for (Collection collection : Main.getController().getCurrentProfile().getCollections()) {
Server server = collection.getServer();
if (server.writer != null) {
if (server.type.isProxy()) {
@ -127,16 +127,17 @@ public class Server implements java.io.Serializable {
* Runs all enabled servers with their settings
*/
public static void startServers() {
Profile.getGUI().setStatus("Starting servers");
for (Collection collection : Profile.getCurrent().getCollections()) {
Controller controller = Main.getController();
controller.getGUI().setStatus("Starting servers");
for (Collection collection : controller.getCurrentProfile().getCollections()) {
if (!collection.getServer().runServer()) {
Profile.getGUI().setStatus("An error occurred. Start aborted");
controller.getGUI().setStatus("An error occurred. Start aborted");
try {
Server.stop();
} catch (IOException e) {
e.printStackTrace();
}
Profile.getGUI().updateRunning(false);
controller.getGUI().updateRunning(false);
return;
}
}
@ -322,7 +323,7 @@ public class Server implements java.io.Serializable {
*/
public void addPlayer(String name) {
this.playerList.add(name);
Profile.getGUI().addPlayer(name);
Main.getController().getGUI().addPlayer(name);
}
/**
@ -332,7 +333,7 @@ public class Server implements java.io.Serializable {
*/
public void removePlayer(String name) {
playerList.removeIf(player -> player.equals(name));
Profile.getGUI().removePlayer(name);
Main.getController().getGUI().removePlayer(name);
}
/**
@ -350,22 +351,24 @@ public class Server implements java.io.Serializable {
* @return <p>True if nothing went wrong</p>
*/
private boolean runServer() {
//Ignore a disabled server
if (!this.enabled) {
this.started = false;
return true;
}
this.started = true;
//Tries to do necessary pre-start work
if (!initializeJarDownload() || !delayStartup()) {
this.started = false;
return false;
}
//Starts the server if possible
try {
startServerProcess();
Profile.getGUI().setStatus("Servers are running");
Main.getController().getGUI().setStatus("Servers are running");
this.started = true;
return true;
} catch (IOException e) {
Profile.getGUI().setStatus("Could not start server");
Main.getController().getGUI().setStatus("Could not start server");
this.started = false;
return false;
}
@ -379,16 +382,11 @@ public class Server implements java.io.Serializable {
private void startServerProcess() throws IOException {
ProcessBuilder builder;
String serverPath;
String serverFile;
//Decide the path of the .jar file to be executed
if (type.getName().equals("Custom")) {
serverFile = serverVersion;
serverPath = this.path + File.separator + serverVersion;
} else {
serverFile = this.type.getName() + serverVersion + ".jar";
}
if (!type.getName().equals("Custom")) {
serverPath = jarDirectory + serverFile;
} else {
serverPath = this.path + File.separator + serverFile;
serverPath = jarDirectory + this.type.getName() + serverVersion + ".jar";
}
builder = new ProcessBuilder("java", "-Xmx" + this.maxRam, "-Xms512M",
"-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", serverPath,
@ -407,8 +405,8 @@ public class Server implements java.io.Serializable {
*/
private boolean delayStartup() {
try {
Profile.getGUI().setStatus("Delaying startup");
TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup());
Main.getController().getGUI().setStatus("Delaying startup");
TimeUnit.SECONDS.sleep(Main.getController().getCurrentProfile().getDelayStartup());
return true;
} catch (InterruptedException e) {
e.printStackTrace();
@ -423,13 +421,14 @@ public class Server implements java.io.Serializable {
* @return <p>True if nothing went wrong</p>
*/
private boolean initializeJarDownload() {
if (!Profile.getCurrent().getDownloadAllAvailableJARFiles()) {
Controller controller = Main.getController();
if (!controller.getDownloadAllJars()) {
try {
Profile.getGUI().setStatus("Downloading jar...");
controller.getGUI().setStatus("Downloading jar...");
this.downloadJar();
Profile.getGUI().setStatus("File downloaded");
controller.getGUI().setStatus("File downloaded");
} catch (IOException e) {
Profile.getGUI().setStatus("Error: Jar file not found");
controller.getGUI().setStatus("Error: Jar file not found");
e.printStackTrace();
this.started = false;
return false;

View File

@ -10,10 +10,10 @@ import java.util.Map;
/**
* This class acts as a container for all "latest" server versions
*/
public class ServerVersionContainer implements java.io.Serializable {
public class ServerVersionContainer {
private static ServerVersionContainer serverVersionContainer;
private String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv";
private final String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv";
private String vanillaVersion;
private String snapshotVersion;
private String bungeeVersion;
@ -273,15 +273,6 @@ public class ServerVersionContainer implements java.io.Serializable {
saveState();
}
/**
* Gets the entire sponge vanilla version map
*
* @return <p>The entire sponge vanilla version map</p>
*/
public Map<String, String> getSpongeVanillaVersions() {
return this.spongeVanillaVersions;
}
/**
* Gets a specific sponge vanilla version
*
@ -292,16 +283,6 @@ public class ServerVersionContainer implements java.io.Serializable {
return spongeVanillaVersions.get(versionKey);
}
/**
* Replaces the entire sponge vanilla version map
*
* @param newVersions <p>The new version map to use</p>
*/
public void setSpongeVanillaVersion(Map<String, String> newVersions) {
this.spongeVanillaVersions = newVersions;
saveState();
}
/**
* Sets the current version for a given sponge vanilla version
*

View File

@ -12,9 +12,9 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stri
public class BungeeCord extends AbstractServerType {
private String versionURL;
private String srcStart;
private String srcEnd;
private final String versionURL;
private final String srcStart;
private final String srcEnd;
/**
* Instantiates a new BungeeCord server type

View File

@ -10,7 +10,7 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.down
*/
public class CraftBukkit extends AbstractServerType {
private String downloadURLPart;
private final String downloadURLPart;
/**
* Instantiates a new server type

View File

@ -1,7 +1,6 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import java.io.File;
import java.io.IOException;
public class Custom extends AbstractServerType {
/**
@ -14,7 +13,7 @@ public class Custom extends AbstractServerType {
}
@Override
public boolean downloadJar(String folder, String version) throws IOException {
public boolean downloadJar(String folder, String version) {
File filePath = new File(folder + version);
return filePath.isFile();
}

View File

@ -11,10 +11,10 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.down
public class SpongeVanilla extends AbstractServerType {
private String versionURL;
private String srcStart;
private String srcEnd;
private String downloadURLPart;
private final String versionURL;
private final String srcStart;
private final String srcEnd;
private final String downloadURLPart;
/**
* Instantiates a new SpongeVanilla server type

View File

@ -19,7 +19,7 @@ public class Travertine extends Waterfall {
String srcEnd, String downloadURL) {
super(typeName, isProxy, versions, versionURL, srcStart, srcEnd, downloadURL);
this.oldVersion = ServerVersionContainer.getInstance().getTravertineVersion();
this.versionUpdateFunction = (newVersion) -> serverVersionContainer.setTravertineVersion(newVersion);
this.versionUpdateFunction = serverVersionContainer::setTravertineVersion;
}
}

View File

@ -6,7 +6,6 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.function.Function;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
@ -14,12 +13,12 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stri
public class Waterfall extends AbstractServerType {
private String srcStart;
private String srcEnd;
private String versionURL;
private final String srcStart;
private final String srcEnd;
private final String versionURL;
String oldVersion;
Consumer<String> versionUpdateFunction;
ServerVersionContainer serverVersionContainer;
final ServerVersionContainer serverVersionContainer;
/**
* Instantiates a new Waterfall server type
@ -40,7 +39,7 @@ public class Waterfall extends AbstractServerType {
this.srcEnd = srcEnd;
this.versionURL = versionURL;
this.oldVersion = serverVersionContainer.getWaterfallVersion();
this.versionUpdateFunction = (newVersion) -> serverVersionContainer.setWaterfallVersion(newVersion);
this.versionUpdateFunction = serverVersionContainer::setWaterfallVersion;
}
@Override