Does some much needed cleanup, adds the Paper server type and fixes broken downloads
Some checks failed
KnarCraft/Minecraft-Server-Launcher/pipeline/head There was a failure building this commit

This commit is contained in:
2020-08-06 19:55:27 +02:00
parent 9ef314b178
commit 26cc5370e2
30 changed files with 1722 additions and 1557 deletions

View File

@ -1,65 +0,0 @@
package net.knarcraft.minecraftserverlauncher.server;
/**
* A more advanced servertype for particularly tricky jar downloads.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
*/
public class AdvancedServerType extends ServerType {
private final String versionURL;
private final String downloadURLPart;
private final String srcStart;
private final String srcEnd;
/**
* Instantiates a new Advanced server type
* @param name <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param versionURL <p>The URL for checking last version for server type</p>
* @param srcStart <p>The string in the version file marking the start of the newest version entry</p>
* @param srcEnd <p>The string in the version file marking the end of the newest version entry</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
* @param downloadURLPart <p>An extra part for the download URL</p>
*/
AdvancedServerType(String name, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart) {
super(name, versions, downloadURL);
this.srcStart = srcStart;
this.srcEnd = srcEnd;
this.versionURL = versionURL;
this.downloadURLPart = downloadURLPart;
}
/**
* Gets the URL used for downloading latest version information
* @return <p>The latest version URL</p>
*/
public String getVersionURL() {
return this.versionURL;
}
/**
* Gets an additional part of the download URL
* @return <p>Additional download URL part</p>
*/
public String getDownloadURLPart() {
return this.downloadURLPart;
}
/**
* Gets the string marking the start of the latest server version in the version document
* @return <p>A string marking the start of the latest version</p>
*/
public String getSrcStart() {
return this.srcStart;
}
/**
* Gets the string marking the end of the latest server version in the version document
* @return <p>A string marking the end of the latest version</p>
*/
public String getSrcEnd() {
return this.srcEnd;
}
}

View File

@ -1,16 +1,17 @@
package net.knarcraft.minecraftserverlauncher.server;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.knarcraft.minecraftserverlauncher.Shared;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.profile.Collection;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
@ -18,14 +19,16 @@ import java.util.concurrent.TimeUnit;
/**
* Contains all necessary information to create, run and manage a Minecraft server.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
*/
public class Server {
/** Available ram sizes. For GUI dropdown */
/**
* Available ram sizes. For ServerLauncherGUI dropdown
*/
private static final String[] ramList = {
"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G","11G", "12G", "13G", "14G", "15G", "16G"
"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"
};
private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator;
@ -39,10 +42,10 @@ public class Server {
private Process process;
private BufferedWriter writer;
private BufferedReader reader;
private String vanillaVersion;
private String snapshotVersion;
private String spongeVanillaVersion;
private String bungeeVersion;
private final String vanillaVersion;
private final String snapshotVersion;
private final String spongeVanillaVersion;
private final String bungeeVersion;
private boolean started;
public Server(String name) {
@ -62,22 +65,12 @@ public class Server {
this.bungeeVersion = "";
}
public Server(
String name,
String path,
boolean enabled,
String typeName,
String serverVersion,
String maxRam,
String vanillaVersion,
String snapshotVersion,
String spongeVanillaVersion,
String bungeeVersion
) {
public Server(String name, String path, boolean enabled, String typeName, String serverVersion, String maxRam,
String vanillaVersion, String snapshotVersion, String spongeVanillaVersion, String bungeeVersion) {
this.name = name;
this.path = path;
this.enabled = enabled;
this.type = ServerType.getByName(typeName);
this.type = ServerTypeHandler.getByName(typeName);
this.serverVersion = serverVersion;
this.maxRam = maxRam;
this.vanillaVersion = vanillaVersion;
@ -147,13 +140,13 @@ public class Server {
}
/**
* @return A representation of the name of a jarfile.
* @return A representation of the name of a jarfile.
*/
private String getType() {
if (this.type.getName().equals("Custom")) {
return this.serverVersion;
} else {
return this.type.getName() + this.serverVersion + ".jar";
return "";
}
}
@ -182,7 +175,7 @@ public class Server {
/**
* Removes a player with the selected name from the playerlist.
*
* @param name The name of the player to remove
* @param name The name of the player to remove
*/
public void removePlayer(String name) {
playerList.removeIf(player -> player.equals(name));
@ -224,7 +217,7 @@ public class Server {
/**
* Tries to stop all enabled servers.
*
* @throws IOException If a writer's process is already closed but not null.
* @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()) {
@ -272,7 +265,7 @@ public class Server {
Profile.getGUI().setStatus("Downloading jar...");
this.downloadJar();
Profile.getGUI().setStatus("File downloaded");
} catch (FileNotFoundException e) {
} catch (IOException e) {
System.out.println(e.getMessage());
Profile.getGUI().setStatus("Error: Jar file not found");
e.printStackTrace();
@ -293,7 +286,7 @@ public class Server {
String serverPath;
if (Profile.getCurrent().getDownloadAllAvailableJARFiles() && !type.getName().equals("Custom")) {
serverPath = jarDirectory + this.getType();
} else {
} else {
serverPath = this.path + File.separator + this.getType();
}
builder = new ProcessBuilder(
@ -310,7 +303,7 @@ public class Server {
builder.redirectErrorStream(true);
this.process = builder.start();
this.writer = new BufferedWriter(new OutputStreamWriter(this.process.getOutputStream()));
this.reader = new BufferedReader (new InputStreamReader(this.process.getInputStream()));
this.reader = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
Profile.getGUI().setStatus("Servers are running");
this.started = true;
return true;
@ -328,8 +321,8 @@ public class Server {
/**
* Reads all available output from the server process.
*
* @return The server output
* @throws IOException If reading from the reader fails
* @return The server output
* @throws IOException If reading from the reader fails
*/
public String read() throws IOException {
String line;
@ -343,139 +336,20 @@ public class Server {
/**
* Downloads necessary .jar file for the server.
* This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
*
* @throws FileNotFoundException if the file was not found and could not be acquired.
*/
private void downloadJar() throws FileNotFoundException {
AdvancedServerType type;
File file = new File(this.path + File.separator + this.getType());
Path filePath = Paths.get(this.path + File.separator + this.getType());
String versionText, newestVersion, url = this.type.getDownloadURL(), name = this.type.getName(), ver = this.serverVersion;
switch (this.type.getName()) {
case "Custom":
if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found.");
}
break;
case "Spigot":
case "Craftbukkit":
case "MCPCplus":
if (!(file.isFile() || Shared.downloadFile(url + name + ver + ".jar", filePath))) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
break;
case "Vanilla":
case "Snapshot":
type = (AdvancedServerType) this.type;
if (this.serverVersion.equals("Latest")) {
try {
versionText = Shared.readFile(type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString();
JsonElement verElem = jsonObject.getAsJsonArray("versions").get(0);
String versionFile = verElem.getAsJsonObject().get("url").getAsString();
try {
versionText = Shared.readFile(versionFile);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
if (!file.isFile() || !latest.equals(this.getVersion(name))) {
this.setVersion(name, latest);
jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
if (!Shared.downloadFile(jarFile, filePath)) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
} else {
if (!(file.isFile() || Shared.downloadFile(url + ver + type.getDownloadURLPart() + ver + ".jar", filePath))) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
break;
case "SpongeVanilla":
type = (AdvancedServerType) this.type;
try {
versionText = Shared.readFile(type.getVersionURL() + this.serverVersion);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = Shared.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
this.setVersion(name, newestVersion);
if (!Shared.downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath)) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
break;
case "Bungee":
type = (AdvancedServerType) this.type;
try {
versionText = Shared.readFile(type.getVersionURL());
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = Shared.stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
this.setVersion(name, newestVersion);
if (!Shared.downloadFile(url, filePath)) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
}
}
/**
* Returns the current version of a type
* @param type <p>The version type</p>
* @return <p>The version string</p>
*/
private String getVersion(String type) {
switch (type) {
case "Vanilla":
return this.vanillaVersion;
case "Snapshot":
return this.snapshotVersion;
case "SpongeVanilla":
return this.spongeVanillaVersion;
case "Bungee":
return this.bungeeVersion;
default:
return "";
}
}
/**
* Sets a server type's last downloaded version.
* @param type <p>The version type</p>
* @param version <p>The version string</p>
*/
private void setVersion(String type, String version) {
if (!type.equals("")) {
switch (type) {
case "Vanilla":
this.vanillaVersion = version;
break;
case "Snapshot":
this.snapshotVersion = version;
break;
case "SpongeVanilla":
this.spongeVanillaVersion = version;
break;
case "Bungee":
this.bungeeVersion = version;
}
private void downloadJar() throws IOException {
String path = this.path + File.separator;
File file = new File(path + this.getType());
if (!(file.isFile() || type.downloadJar(path, this.serverVersion))) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}
/**
* Sends a command to this server through its writer.
*
* @param command <p>Command to send to the server</p>
* @throws IOException <p>If write fails</p>
*/

View File

@ -1,134 +0,0 @@
package net.knarcraft.minecraftserverlauncher.server;
import javax.naming.ConfigurationException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import static net.knarcraft.minecraftserverlauncher.Shared.getResourceAsScanner;
/**
* Contains the bare minimum to be a functional server type.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
*/
public class ServerType {
private final String name;
private final String[] versions;
private final String downloadURL;
private static final ArrayList<ServerType> serverTypes = new ArrayList<>();
/**
* Instantiates a new server type
* @param name <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
*/
ServerType(String name, String[] versions, String downloadURL) {
this.name = name;
this.versions = versions;
this.downloadURL = downloadURL;
serverTypes.add(this);
}
/**
* Gets the name of the server type
* @return <p>Server type name</p>
*/
public String getName() {
return this.name;
}
/**
* Gets a list of versions available for the server type
* @return <p>A list of server versions</p>
*/
public String[] getVersions() {
return this.versions;
}
/**
* Gets the url used for downloading JAR files
* @return <p>A download URL</p>
*/
public String getDownloadURL() {
return this.downloadURL;
}
/**
* Gets all instantiated server types
* @return <p>A list of server types</p>
*/
public static ArrayList<ServerType> getServerTypes() {
return serverTypes;
}
/**
* Gets a list of all server types' names.
* @return <p>A list of strings</p>
*/
public static String[] getTypeNames() {
ArrayList<ServerType> types = ServerType.getServerTypes();
String[] serverTypeNames = new String[types.size()];
for (int i = 0; i < types.size(); i++) {
serverTypeNames[i] = types.get(i).getName();
}
return serverTypeNames;
}
/**
* Gets a server type by the given name
* @param name <p>Then name of the server type</p>
* @return <p>A ServerType</p>
*/
public static ServerType getByName(String name) {
for (ServerType serverType : serverTypes) {
if (serverType.getName().equals(name)) {
return serverType;
}
}
return null;
}
/**
* Reads valid server types and version from a file, and creates their objects.
* @throws ConfigurationException <p>If anything goes wrong</p>
*/
public static void loadServerTypes() throws ConfigurationException {
if (serverTypes.isEmpty()) {
Scanner file;
try {
file = getResourceAsScanner("servertypes.csv");
} catch (FileNotFoundException e) {
throw new ConfigurationException("Server type configuration file is missing.");
}
while (file.hasNextLine()) {
//Splits the next file line into arguments
String[] fileLine = file.nextLine().split(";", -1);
int lineLength = fileLine.length;
//Gets list of server versions from file line
String[] serverVersion;
if (fileLine[1].contains(",")) {
serverVersion = fileLine[1].split(",", -1);
} else {
serverVersion = new String[]{fileLine[1]};
}
switch (lineLength) {
case 7:
new AdvancedServerType(fileLine[0], serverVersion, fileLine[2], fileLine[3], fileLine[4], fileLine[5], fileLine[6]);
break;
case 3:
new ServerType(fileLine[0], serverVersion, fileLine[2]);
break;
default:
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} else {
throw new ConfigurationException("Error: Configuration already loaded.");
}
}
}

View File

@ -0,0 +1,136 @@
package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.server.servertypes.BungeeCord;
import net.knarcraft.minecraftserverlauncher.server.servertypes.CraftBukkit;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Custom;
import net.knarcraft.minecraftserverlauncher.server.servertypes.MCPCPlus;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Paper;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Snapshot;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Spigot;
import net.knarcraft.minecraftserverlauncher.server.servertypes.SpongeVanilla;
import net.knarcraft.minecraftserverlauncher.server.servertypes.Vanilla;
import javax.naming.ConfigurationException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsScanner;
/**
* A class which keeps track of available server types
*/
public class ServerTypeHandler {
private static final ArrayList<ServerType> serverTypes = new ArrayList<>();
/**
* Gets a list of all server types' names.
* @return <p>A list of strings</p>
*/
public static String[] getTypeNames() throws ConfigurationException {
ArrayList<ServerType> types = getServerTypes();
String[] serverTypeNames = new String[types.size()];
for (int i = 0; i < types.size(); i++) {
serverTypeNames[i] = types.get(i).getName();
}
return serverTypeNames;
}
/**
* Gets all instantiated server types
* @return <p>A list of server types</p>
*/
public static ArrayList<ServerType> getServerTypes() throws ConfigurationException {
if (serverTypes.isEmpty()) {
loadServerTypes();
}
return serverTypes;
}
/**
* Gets a server type by the given name
* @param name <p>Then name of the server type</p>
* @return <p>A AbstractServerType</p>
*/
public static ServerType getByName(String name) {
for (ServerType serverType : serverTypes) {
if (serverType.getName().equals(name)) {
return serverType;
}
}
return null;
}
/**
* Reads valid server types and version from a file, and creates their objects.
* @throws ConfigurationException <p>If anything goes wrong</p>
*/
private static void loadServerTypes() throws ConfigurationException {
Scanner file;
try {
file = getResourceAsScanner("servertypes.csv");
} catch (FileNotFoundException e) {
throw new ConfigurationException("Server type configuration file is missing.");
}
while (file.hasNextLine()) {
//Splits the next file line into arguments
String[] serverTypeInfo = file.nextLine().split(";", -1);
//Gets list of server versions from file line
String[] serverVersions;
if (serverTypeInfo[1].contains(",")) {
serverVersions = serverTypeInfo[1].split(",", -1);
} else {
serverVersions = new String[]{serverTypeInfo[1]};
}
addServerType(serverTypeInfo, serverVersions);
}
}
/**
* Adds a new server type
*
* @param serverTypeInfo <p>A list containing necessary information for initializing the server type</p>
* @param serverVersions <p>A list of all server versions for the server type</p>
*/
private static void addServerType(String[] serverTypeInfo, String[] serverVersions) {
ServerType newType;
switch (serverTypeInfo[0]) {
case "Craftbukkit":
newType = new CraftBukkit("Bukkit", serverVersions, serverTypeInfo[2], serverTypeInfo[3]);
break;
case "Spigot":
newType = new Spigot("Spigot", serverVersions, serverTypeInfo[2], serverTypeInfo[3]);
break;
case "Vanilla":
newType = new Vanilla("Vanilla", serverVersions, serverTypeInfo[2], serverTypeInfo[3]);
break;
case "Snapshot":
newType = new Snapshot("Snapshot", serverVersions, serverTypeInfo[2], serverTypeInfo[3]);
break;
case "MCPCplus":
newType = new MCPCPlus("MCPCplus", serverVersions, serverTypeInfo[2], serverTypeInfo[3]);
break;
case "Paper":
newType = new Paper("Paper", serverVersions, serverTypeInfo[2], serverTypeInfo[3]);
break;
case "Bungee":
newType = new BungeeCord("Bungee", serverVersions, serverTypeInfo[2], serverTypeInfo[3],
serverTypeInfo[4], serverTypeInfo[5]);
break;
case "SpongeVanilla":
newType = new SpongeVanilla("SpongeVanilla", serverVersions, serverTypeInfo[2], serverTypeInfo[3],
serverTypeInfo[4], serverTypeInfo[5], serverTypeInfo[6]);
break;
case "Custom":
newType = new Custom("Custom", serverVersions, serverTypeInfo[2]);
break;
default:
throw new IllegalArgumentException("Unknown server type defined in config file.");
}
serverTypes.add(newType);
}
}

View File

@ -0,0 +1,51 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
/**
* Contains the bare minimum to be a functional server type.
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
*/
public abstract class AbstractServerType implements ServerType {
private final String typeName;
private final String[] versions;
final String downloadURL;
/**
* Instantiates a new server type
* @param typeName <p>The typeName of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
*/
AbstractServerType(String typeName, String[] versions, String downloadURL) {
this.typeName = typeName;
this.versions = versions;
this.downloadURL = downloadURL;
}
/**
* Gets the name of the server type
* @return <p>Server type typeName</p>
*/
public String getName() {
return this.typeName;
}
/**
* Gets a list of versions available for the server type
* @return <p>A list of server versions</p>
*/
public String[] getVersions() {
return this.versions;
}
/**
* Gets the url used for downloading JAR files
* @return <p>A download URL</p>
*/
public String getDownloadURL() {
return this.downloadURL;
}
}

View File

@ -0,0 +1,41 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween;
public class BungeeCord extends AbstractServerType {
private String versionURL;
private String lastVersion;
private String srcStart;
private String srcEnd;
/**
* Instantiates a new server type
*
* @param typeName <p>The typeName of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
*/
public BungeeCord(String typeName, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL) {
super(typeName, versions, downloadURL);
this.versionURL = versionURL;
this.srcStart = srcStart;
this.srcEnd = srcEnd;
}
@Override
public boolean downloadJar(String folder, String version) throws IOException {
String file = this.getName() + version + ".jar";
File filePath = new File(folder + file);
String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd);
String oldVersion = lastVersion;
lastVersion = newestVersion;
return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(downloadURL, Paths.get(filePath.toURI()));
}
}

View File

@ -0,0 +1,33 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import java.io.File;
import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
public class CraftBukkit extends AbstractServerType {
private String downloadURLPart;
/**
* Instantiates a new server type
*
* @param typeName <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
* @param downloadURLPart <p>A string used after the download url as an additional part of the URL</p>
*/
public CraftBukkit(String typeName, String[] versions, String downloadURL, String downloadURLPart) {
super(typeName, versions, downloadURL);
this.downloadURLPart = downloadURLPart;
}
@Override
public boolean downloadJar(String folder, String version) {
String targetFile = this.getName() + version + ".jar";
String file = downloadURLPart + version + ".jar";
File filePath = new File(folder + targetFile);
return filePath.isFile() || downloadFile(downloadURL + file, Paths.get(filePath.toURI()));
}
}

View File

@ -0,0 +1,23 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import java.io.File;
import java.io.IOException;
public class Custom extends AbstractServerType {
/**
* Instantiates a new server type
*
* @param typeName <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
*/
public Custom(String typeName, String[] versions, String downloadURL) {
super(typeName, versions, downloadURL);
}
@Override
public boolean downloadJar(String folder, String version) throws IOException {
File filePath = new File(folder + version);
return filePath.isFile();
}
}

View File

@ -0,0 +1,17 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class MCPCPlus extends CraftBukkit {
/**
* Instantiates a new server type
*
* @param typeName <p>The name of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
* @param downloadURLPart <p>A string used after the download url as an additional part of the URL</p>
*/
public MCPCPlus(String typeName, String[] versions, String downloadURL, String downloadURLPart) {
super(typeName, versions, downloadURL, downloadURLPart);
}
}

View File

@ -0,0 +1,17 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class Paper extends Spigot {
/**
* Instantiates a new server type
*
* @param typeName <p>The typeName of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
* @param downloadURLPart <p>A string used after the download url as an additional part of the URL</p>
*/
public Paper(String typeName, String[] versions, String downloadURL, String downloadURLPart) {
super(typeName, versions, downloadURL, downloadURLPart);
}
}

View File

@ -0,0 +1,30 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import java.io.IOException;
/**
* Describes a server type
*/
public interface ServerType {
/**
* Gets the name of the server type
* @return <p>Server type name</p>
*/
String getName();
/**
* Gets a list of versions available for the server type
* @return <p>A list of server versions</p>
*/
String[] getVersions();
/**
* Downloads a .jar file for this server type
*
* @param folder <p>The folder to save the downloaded file to</p>
* @param version <p>The server type version to use</p>
* @return <p>True if the file exists or was downloaded</p>
*/
boolean downloadJar(String folder, String version) throws IOException;
}

View File

@ -0,0 +1,18 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class Snapshot extends Vanilla {
/**
* Instantiates a snapshot server type
*
* @param typeName <p>The name of this server type</p>
* @param versions <p>Available versions for this server type</p>
* @param versionURL <p>The URL used for downloading the version document</p>
* @param downloadURL <p>The URL used for downloading the new file</p>
*/
public Snapshot(String typeName, String[] versions, String versionURL, String downloadURL) {
super(typeName, versions, versionURL, downloadURL);
this.releaseType = "snapshot";
}
}

View File

@ -0,0 +1,17 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class Spigot extends CraftBukkit {
/**
* Instantiates a new server type
*
* @param typeName <p>The typeName of the server type</p>
* @param versions <p>A list of one or more server versions for the type</p>
* @param downloadURL <p>The URL used for downloading .jar files</p>
* @param downloadURLPart <p>A string used after the download url as an additional part of the URL</p>
*/
public Spigot(String typeName, String[] versions, String downloadURL, String downloadURLPart) {
super(typeName, versions, downloadURL, downloadURLPart);
}
}

View File

@ -0,0 +1,53 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
public class SpongeVanilla extends AbstractServerType {
private String versionURL;
private String lastVersion;
private String srcStart;
private String srcEnd;
private String downloadURLPart;
/**
* Instantiates a new SpongeVanilla server type
*
* @param typeName <p>The name of this server type</p>
* @param versions <p>Available versions for this server type</p>
* @param versionURL <p>The URL used for downloading the version document</p>
* @param srcStart <p>The string to search for to determine newest version</p>
* @param srcEnd <p>The string marking the end of the newest version statement</p>
* @param downloadURL <p>The URL used for downloading the new file</p>
* @param downloadURLPart <p>A string used after the download url as an additional part of the URL</p>
*/
public SpongeVanilla(String typeName, String[] versions, String versionURL, String srcStart, String srcEnd,
String downloadURL, String downloadURLPart) {
super(typeName, versions, downloadURL);
this.versionURL = versionURL;
this.srcStart = srcStart;
this.srcEnd = srcEnd;
this.downloadURLPart = downloadURLPart;
}
@Override
public boolean downloadJar(String folder, String version) throws IOException {
String file = this.getName() + version + ".jar";
File filePath = new File(folder + file);
String versionText = CommonFunctions.readFile(versionURL + version);
String newestVersion = CommonFunctions.stringBetween(versionText, srcStart, srcEnd);
String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar";
String oldVersion = lastVersion;
lastVersion = newestVersion;
return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(jarURL, Paths.get(filePath.toURI()));
}
}

View File

@ -0,0 +1,4 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class Travertine {
}

View File

@ -0,0 +1,102 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
public class Vanilla extends AbstractServerType {
private final String versionURL;
private String lastVersion;
String releaseType;
/**
* Instantiates a vanilla server type
*
* @param typeName <p>The name of this server type to display</p>
* @param versions <p>Available versions for this server type</p>
* @param versionURL <p>The URL used for downloading the version document</p>
* @param downloadURL <p>The URL used for downloading the new file</p>
*/
public Vanilla(String typeName, String[] versions, String versionURL, String downloadURL) {
super(typeName, versions, downloadURL);
this.versionURL = versionURL;
this.releaseType = "release";
}
@Override
public boolean downloadJar(String folder, String version) throws IOException {
String file = this.getName() + version + ".jar";
File filePath = new File(folder + file);
if (version.equals("Latest")) {
String[] latestData = getLatestFile();
String latest = latestData[0];
String jarFile = latestData[1];
String currentVersion = lastVersion;
lastVersion = latest;
return (filePath.isFile() && latest.equals(currentVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI()));
} else {
String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version));
return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI()));
}
}
/**
* Gets the URL to the .jar file for the newest version
*
* @return <p>An array containing the latest version and a link to its file</p>
* @throws IOException <p>If the remote resource cannot be read</p>
*/
private String[] getLatestFile() throws IOException {
String versionText = readFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get(releaseType).getAsString();
String versionURL = getServerFileVersionURL(latest);
String jarURL = getVanillaDownloadURL(versionURL);
return new String[]{latest, jarURL};
}
/**
* Gets the URL necessary for downloading a given minecraft .jar file
*
* @param versionURL <p>The URL to the version document describing the .jar file</p>
* @return <p>The URL necessary do download the .jar file</p>
* @throws IOException <p>If the remote resource cannot be read</p>
*/
private String getVanillaDownloadURL(String versionURL) throws IOException {
String versionText = readFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
return jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
}
/**
* Gets the version URL from the Minecraft vanilla version document
*
* @param targetVersion <p>The version to download</p>
* @return <p>The URL to the file</p>
* @throws IOException <p>If the file cannot be downloaded</p>
*/
private String getServerFileVersionURL(String targetVersion) throws IOException {
String versionText = readFile(versionURL);
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
JsonArray availableVersions = jsonObject.getAsJsonArray("versions");
for (JsonElement availableVersion : availableVersions) {
JsonObject versionObject = availableVersion.getAsJsonObject();
if (!versionObject.get("id").getAsString().equals(targetVersion)) {
continue;
}
return versionObject.get("url").getAsString();
}
throw new FileNotFoundException("Unable to find the requested Minecraft vanilla .jar file.");
}
}

View File

@ -0,0 +1,4 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes;
public class Waterfall {
}