Makes the project into a Maven project
Some checks failed
KnarCraft/Minecraft-Server-Launcher/master There was a failure building this commit

Moves stuff around
Adds Jenkinsfile
Changes some package names
Replaces library with Maven dependency
This commit is contained in:
2020-02-12 21:30:36 +01:00
parent 4901ea0627
commit 194686b9d8
17 changed files with 96 additions and 69 deletions

View File

@ -0,0 +1,39 @@
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;
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;
}
public String getVersionURL() {
return this.versionURL;
}
public String getDownloadURLPart() {
return this.downloadURLPart;
}
public String getSrcStart() {
return this.srcStart;
}
public String getSrcEnd() {
return this.srcEnd;
}
}

View File

@ -0,0 +1,496 @@
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.profile.Collection;
import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.Main;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
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
*/
public class Server {
/** Available ram sizes. For GUI dropdown */
private static final String[] ramList = {
"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G","11G", "12G", "13G", "14G", "15G", "16G"
};
private static final String jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator;
private final String name;
private String path;
private boolean enabled;
private final ArrayList<String> playerList;
private ServerType type;
private String serverVersion;
private String maxRam;
private Process process;
private BufferedWriter writer;
private BufferedReader reader;
private String vanillaVersion;
private String snapshotVersion;
private String spongeVanillaVersion;
private String bungeeVersion;
private boolean started;
public Server(String name) {
this.name = name;
this.path = "";
this.enabled = false;
this.playerList = new ArrayList<>();
this.type = null;
this.serverVersion = "";
this.maxRam = ramList[0];
this.process = null;
this.writer = null;
this.reader = null;
this.vanillaVersion = "";
this.snapshotVersion = "";
this.spongeVanillaVersion = "";
this.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.serverVersion = serverVersion;
this.maxRam = maxRam;
this.vanillaVersion = vanillaVersion;
this.snapshotVersion = snapshotVersion;
this.spongeVanillaVersion = spongeVanillaVersion;
this.bungeeVersion = bungeeVersion;
this.playerList = new ArrayList<>();
}
public String getName() {
return this.name;
}
public boolean isStarted() {
return started;
}
public String getTypeName() {
return this.type.getName();
}
public String getServerVersion() {
return this.serverVersion;
}
public String getPath() {
return this.path;
}
public Process getProcess() {
return this.process;
}
public String getMaxRam() {
return this.maxRam;
}
public static String[] getRamList() {
return ramList;
}
public String getVanillaVersion() {
return this.vanillaVersion;
}
public String getSnapshotVersion() {
return this.snapshotVersion;
}
public String getSpongeVanillaVersion() {
return this.spongeVanillaVersion;
}
public String getBungeeVersion() {
return this.bungeeVersion;
}
public ArrayList<String> getPlayers() {
return this.playerList;
}
public void stopped() {
process = null;
writer = null;
reader = null;
started = false;
}
/**
* @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";
}
}
public boolean isEnabled() {
return this.enabled;
}
public void toggle(boolean value) {
this.enabled = value;
}
public boolean hasPlayer(String name) {
for (String player : this.playerList) {
if (player.equals(name)) {
return true;
}
}
return false;
}
public void addPlayer(String name) {
this.playerList.add(name);
Profile.getGUI().addPlayer(name);
}
/**
* Removes a player with the selected name from the playerlist.
*
* @param name The name of the player to remove
*/
public void removePlayer(String name) {
for (int i = 0; i < playerList.size(); i++) {
if (name.equals(playerList.get(i))) {
playerList.remove(i);
}
}
Profile.getGUI().removePlayer(name);
}
public void setPath(String path) {
this.path = path;
}
public void setType(ServerType type) {
this.type = type;
}
public void setMaxRam(String ram) {
this.maxRam = ram;
}
/**
* Sets the server's server version to a valid version, or ignores the request.
*
* @param serverVersion Version number.
*/
public void setServerVersion(String serverVersion) throws IllegalArgumentException {
if (this.type.getName().equals("Custom")) {
this.serverVersion = serverVersion;
} else {
String[] versions = this.type.getVersions();
for (String version : versions) {
if (version.equals(serverVersion)) {
this.serverVersion = serverVersion;
return;
}
}
throw new IllegalArgumentException("Invalid server version.");
}
}
/**
* Tries to stop all enabled servers.
*
* @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()) {
Server server = collection.getServer();
if (server.writer != null) {
if (server.type.getName().equals("Bungee")) {
server.writer.write("end\n");
} else {
server.writer.write("stop\n");
}
server.writer.flush();
server.writer = null;
server.started = false;
}
}
}
/**
* Runs all enabled servers with their settings.
*/
public static void startServers() {
Profile.getGUI().setStatus("Starting servers");
for (Collection collection : Profile.getCurrent().getCollections()) {
if (!collection.getServer().run()) {
Profile.getGUI().setStatus("An error occurred. Start aborted");
try {
Server.stop();
} catch (IOException e) {
e.printStackTrace();
}
Profile.getGUI().updateRunning(false);
return;
}
}
}
/**
* Runs the Minecraft server.
*/
private boolean run() {
if (this.enabled) {
this.started = true;
if (!Profile.getCurrent().getDownloadJars()) {
try {
Profile.getGUI().setStatus("Downloading jar...");
this.downloadJar();
Profile.getGUI().setStatus("File downloaded");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
Profile.getGUI().setStatus("Error: Jar file not found");
e.printStackTrace();
this.started = false;
return false;
}
}
try {
Profile.getGUI().setStatus("Delaying startup");
TimeUnit.SECONDS.sleep(Profile.getCurrent().getDelayStartup());
} catch (InterruptedException e) {
e.printStackTrace();
this.started = false;
return false;
}
try {
ProcessBuilder builder;
String serverPath;
if (Profile.getCurrent().getDownloadJars() && !type.getName().equals("Custom")) {
serverPath = jarDir + this.getType();
} else {
serverPath = this.path + File.separator + this.getType();
}
builder = new ProcessBuilder(
"java",
"-Xmx" + this.maxRam,
"-Xms512M",
"-Djline.terminal=jline.UnsupportedTerminal",
"-Dcom.mojang.eula.agree=true",
"-jar",
serverPath,
"nogui"
);
builder.directory(new File(this.path));
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()));
Profile.getGUI().setStatus("Servers are running");
this.started = true;
return true;
} catch (IOException e) {
Profile.getGUI().setStatus("Could not start server");
this.started = false;
return false;
}
} else {
this.started = false;
return true;
}
}
/**
* Reads all available output from the server process.
*
* @return The server output
* @throws IOException If reading from the reader fails
*/
public String read() throws IOException {
String line;
StringBuilder text = new StringBuilder();
while (reader.ready() && (line = reader.readLine()) != null) {
text.append(line).append("\n");
}
return text.toString().trim();
}
/**
* 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 The version type
* @return The version string
*/
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 The version type
* @param version The version string
*/
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;
}
}
}
/**
* Sends a command to this server through its writer.
*
* @param command Command to send to the server
* @throws IOException If write fails
*/
public void sendCommand(String command) throws IOException {
if (this.process != null && this.writer != null) {
this.writer.write(command + "\n");
this.writer.flush();
}
}
}

View File

@ -0,0 +1,112 @@
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;
/**
* 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<>();
ServerType(String name, String[] versions, String downloadURL) {
this.name = name;
this.versions = versions;
this.downloadURL = downloadURL;
serverTypes.add(this);
}
public String getName() {
return this.name;
}
public String[] getVersions() {
return this.versions;
}
public String getDownloadURL() {
return this.downloadURL;
}
public static ArrayList<ServerType> getServerTypes() {
return serverTypes;
}
/**
* Gets a list of all server types' names.
*
* @return A list of strings
*/
public static String[] getTypeNames() {
ArrayList<ServerType> types = ServerType.getServerTypes();
String[] serverTypes = new String[types.size()];
for (int i = 0; i < types.size(); i++) {
serverTypes[i] = types.get(i).getName();
}
return serverTypes;
}
/**
* Gets a server type by name.
*
* @param name Then name of the server type
* @return A ServerType
*/
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 if anything goes wrong.
*/
public static void loadServerTypes() throws ConfigurationException {
if (serverTypes.isEmpty()) {
Scanner file;
try {
file = new Scanner(new File("config/servertypes.csv"));
} catch (FileNotFoundException e) {
file = new Scanner(ServerType.class.getResourceAsStream("/config/servertypes.csv"));
}
while (file.hasNextLine()) {
String[] str = file.nextLine().split(";", -1);
int len = str.length;
String[] ver;
if (str[1].contains(",")) {
ver = str[1].split(",", -1);
} else {
ver = new String[]{str[1]};
}
switch (len) {
case 7:
new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6]);
break;
case 3:
new ServerType(str[0], ver, str[2]);
break;
default:
throw new ConfigurationException("Error: Configuration file invalid.");
}
}
} else {
throw new ConfigurationException("Error: Configuration already loaded.");
}
}
}