2018-01-24 12:18:06 +01:00
|
|
|
package net.knarcraft.serverlauncher.server;
|
2018-01-23 23:49:48 +01:00
|
|
|
|
2018-01-31 17:40:28 +01:00
|
|
|
import net.knarcraft.serverlauncher.profile.Collection;
|
2018-01-30 13:39:09 +01:00
|
|
|
import net.knarcraft.serverlauncher.profile.Profile;
|
2018-01-28 16:17:31 +01:00
|
|
|
import net.knarcraft.serverlauncher.userinterface.GUI;
|
2018-01-31 17:40:28 +01:00
|
|
|
import net.knarcraft.serverlauncher.userinterface.ServerTab;
|
2018-01-28 16:17:31 +01:00
|
|
|
|
2018-01-25 17:34:45 +01:00
|
|
|
import java.io.*;
|
2018-01-23 23:49:48 +01:00
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.util.ArrayList;
|
2018-01-25 10:53:51 +01:00
|
|
|
|
2018-01-25 21:17:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains all necessary information to create, run and manage a Minecraft server.
|
|
|
|
*
|
2018-01-25 21:21:06 +01:00
|
|
|
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
|
|
|
* @version 0.0.0.1
|
|
|
|
* @since 0.0.0.1
|
2018-01-25 21:17:02 +01:00
|
|
|
*/
|
2018-01-23 23:49:48 +01:00
|
|
|
public class Server {
|
2018-01-26 20:26:16 +01:00
|
|
|
/** Available ram sizes. For GUI dropdown */
|
2018-01-30 13:39:09 +01:00
|
|
|
private static final String[] ramList = {
|
|
|
|
"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G","11G", "12G", "13G", "14G", "15G", "16G"
|
|
|
|
};
|
2018-01-25 10:53:51 +01:00
|
|
|
|
2018-01-26 20:36:10 +01:00
|
|
|
private final String name;
|
2018-01-25 10:53:51 +01:00
|
|
|
private String path;
|
|
|
|
private boolean enabled;
|
2018-01-26 20:34:15 +01:00
|
|
|
private final ArrayList<String> playerList;
|
2018-01-25 19:55:28 +01:00
|
|
|
private ServerType type;
|
2018-01-25 10:53:51 +01:00
|
|
|
private String serverVersion;
|
|
|
|
private String maxRam;
|
2018-01-26 15:59:49 +01:00
|
|
|
private Process process;
|
2018-01-28 16:17:31 +01:00
|
|
|
private BufferedWriter writer;
|
2018-01-31 21:24:54 +01:00
|
|
|
private BufferedReader reader;
|
2018-01-25 10:53:51 +01:00
|
|
|
|
2018-01-31 17:40:28 +01:00
|
|
|
public Server(String name, GUI window, ServerTab serverTab) {
|
2018-01-25 10:53:51 +01:00
|
|
|
this.name = name;
|
|
|
|
this.path = "";
|
|
|
|
this.enabled = false;
|
|
|
|
this.playerList = new ArrayList<>();
|
|
|
|
this.type = null;
|
|
|
|
this.serverVersion = null;
|
|
|
|
this.maxRam = ramList[0];
|
2018-01-26 15:59:49 +01:00
|
|
|
this.process = null;
|
2018-01-31 17:40:28 +01:00
|
|
|
window.addServer(serverTab);
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addPlayer(String name) {
|
|
|
|
this.playerList.add(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removePlayer(String name) {
|
|
|
|
for (int i = 0; i < playerList.size(); i++) {
|
|
|
|
if (name.equals(playerList.get(i))) {
|
|
|
|
playerList.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 20:26:16 +01:00
|
|
|
/**
|
|
|
|
* @return A representation of the name of a jarfile.
|
|
|
|
*/
|
2018-01-25 21:21:06 +01:00
|
|
|
private String getType() {
|
2018-01-30 13:39:09 +01:00
|
|
|
if (this.type.getName().equals("Custom")) {
|
|
|
|
return this.serverVersion;
|
|
|
|
} else {
|
|
|
|
return this.type.getName() + this.serverVersion + ".jar";
|
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 19:06:50 +01:00
|
|
|
public String getName() {
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPath() {
|
|
|
|
return this.path;
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-26 15:59:49 +01:00
|
|
|
public Process getProcess() {
|
|
|
|
return this.process;
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:53:51 +01:00
|
|
|
public void toggle() {
|
|
|
|
this.enabled = !this.enabled;
|
|
|
|
}
|
2018-01-30 17:14:29 +01:00
|
|
|
public void toggle(boolean value) {
|
|
|
|
this.enabled = value;
|
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
|
|
|
|
public void setPath(String path) {
|
|
|
|
this.path = path;
|
|
|
|
}
|
|
|
|
|
2018-01-25 19:55:28 +01:00
|
|
|
public void setType(ServerType type) {
|
2018-01-25 10:53:51 +01:00
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
2018-01-28 16:17:31 +01:00
|
|
|
public static String[] getRamList() {
|
|
|
|
return ramList;
|
|
|
|
}
|
|
|
|
|
2018-01-28 19:06:50 +01:00
|
|
|
public boolean isEnabled() {
|
|
|
|
return this.enabled;
|
|
|
|
}
|
|
|
|
|
2018-01-30 00:44:03 +01:00
|
|
|
public void setMaxRam(String ram) {
|
|
|
|
this.maxRam = ram;
|
|
|
|
}
|
|
|
|
|
2018-01-28 16:17:31 +01:00
|
|
|
public static void stop() throws IOException {
|
2018-01-31 17:40:28 +01:00
|
|
|
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
|
|
|
|
if (collection.getServer().writer != null) {
|
|
|
|
if (collection.getServer().type.getName().equals("Bungee")) {
|
|
|
|
collection.getServer().writer.write("end\n");
|
2018-01-28 16:17:31 +01:00
|
|
|
} else {
|
2018-01-31 17:40:28 +01:00
|
|
|
collection.getServer().writer.write("stop\n");
|
2018-01-28 16:17:31 +01:00
|
|
|
}
|
2018-01-31 17:40:28 +01:00
|
|
|
collection.getServer().writer.flush();
|
|
|
|
collection.getServer().writer = null;
|
2018-01-28 16:17:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:34:45 +01:00
|
|
|
/**
|
|
|
|
* Sets the server's server version to a valid version, or ignores the request.
|
|
|
|
*
|
2018-01-25 21:17:02 +01:00
|
|
|
* @param serverVersion Version number.
|
2018-01-25 17:34:45 +01:00
|
|
|
*/
|
2018-01-25 21:17:02 +01:00
|
|
|
public void setServerVersion(String serverVersion) throws IllegalArgumentException {
|
2018-01-25 11:49:01 +01:00
|
|
|
String[] versions = this.type.getVersions();
|
|
|
|
for (String version : versions) {
|
|
|
|
if (version.equals(serverVersion)) {
|
|
|
|
this.serverVersion = serverVersion;
|
2018-01-25 21:17:02 +01:00
|
|
|
return;
|
2018-01-25 11:49:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 21:17:02 +01:00
|
|
|
throw new IllegalArgumentException("Invalid server version.");
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 21:17:02 +01:00
|
|
|
/**
|
|
|
|
* Runs all enabled servers with their settings.
|
|
|
|
*/
|
|
|
|
public static void startServers() {
|
2018-01-30 17:14:29 +01:00
|
|
|
GUI.getGUI().setStatus("Starting servers");
|
2018-01-31 17:40:28 +01:00
|
|
|
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
|
|
|
|
if (!collection.getServer().run()) {
|
2018-01-30 00:44:03 +01:00
|
|
|
GUI.getGUI().setStatus("An error occurred. Start aborted");
|
2018-01-28 16:23:15 +01:00
|
|
|
}
|
2018-01-25 21:17:02 +01:00
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
2018-01-23 23:49:48 +01:00
|
|
|
|
2018-01-25 17:34:45 +01:00
|
|
|
/**
|
|
|
|
* Runs the Minecraft server.
|
|
|
|
*/
|
2018-01-28 16:17:31 +01:00
|
|
|
private boolean run() {
|
2018-01-25 21:17:02 +01:00
|
|
|
if (this.enabled) {
|
2018-01-25 17:34:45 +01:00
|
|
|
try {
|
2018-01-30 00:44:03 +01:00
|
|
|
GUI.getGUI().setStatus("Downloading jar...");
|
2018-01-25 17:34:45 +01:00
|
|
|
this.downloadJar();
|
2018-01-30 00:44:03 +01:00
|
|
|
GUI.getGUI().setStatus("File downloaded");
|
2018-01-25 17:34:45 +01:00
|
|
|
} catch (FileNotFoundException e) {
|
2018-01-30 00:44:03 +01:00
|
|
|
GUI.getGUI().setStatus("Error: Jar file not found");
|
2018-01-28 16:17:31 +01:00
|
|
|
return false;
|
2018-01-25 17:34:45 +01:00
|
|
|
}
|
|
|
|
try {
|
2018-01-30 13:39:09 +01:00
|
|
|
ProcessBuilder builder = new ProcessBuilder(
|
|
|
|
"java",
|
|
|
|
"-Xmx" + this.maxRam,
|
|
|
|
"-Xms512M",
|
|
|
|
"-Djline.terminal=jline.UnsupportedTerminal",
|
|
|
|
"-Dcom.mojang.eula.agree=true",
|
|
|
|
"-jar",
|
2018-01-30 17:14:29 +01:00
|
|
|
"\"" + this.path + "\\" + this.getType() + "\"",
|
|
|
|
"nogui"
|
2018-01-30 13:39:09 +01:00
|
|
|
);
|
2018-01-26 15:59:49 +01:00
|
|
|
builder.directory(new File(this.path));
|
|
|
|
builder.redirectErrorStream(true);
|
2018-01-26 20:34:15 +01:00
|
|
|
this.process = builder.start();
|
2018-01-31 21:24:54 +01:00
|
|
|
OutputStream stdin = this.process.getOutputStream();
|
2018-01-28 16:17:31 +01:00
|
|
|
this.writer = new BufferedWriter(new OutputStreamWriter(stdin));
|
2018-01-31 21:24:54 +01:00
|
|
|
InputStream stdout = this.process.getInputStream();
|
|
|
|
this.reader = new BufferedReader (new InputStreamReader(stdout));
|
2018-01-30 00:44:03 +01:00
|
|
|
GUI.getGUI().setStatus("Servers are running");
|
2018-01-28 16:17:31 +01:00
|
|
|
return true;
|
2018-01-25 17:34:45 +01:00
|
|
|
} catch (IOException e) {
|
2018-01-30 00:44:03 +01:00
|
|
|
GUI.getGUI().setStatus("Could not start server");
|
2018-01-28 16:17:31 +01:00
|
|
|
return false;
|
2018-01-25 17:34:45 +01:00
|
|
|
}
|
2018-01-28 16:17:31 +01:00
|
|
|
} else {
|
|
|
|
return true;
|
2018-01-25 17:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:24:54 +01:00
|
|
|
public String read() throws IOException {
|
|
|
|
String line;
|
|
|
|
if ((line = reader.readLine()) != null) {
|
|
|
|
return line;
|
|
|
|
} else {return "";}
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:49:48 +01:00
|
|
|
/**
|
|
|
|
* Downloads necessary .jar file for the server.
|
2018-01-25 10:53:51 +01:00
|
|
|
* 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.
|
2018-01-23 23:49:48 +01:00
|
|
|
*/
|
2018-01-25 21:21:06 +01:00
|
|
|
private void downloadJar() throws FileNotFoundException {
|
2018-01-25 19:55:28 +01:00
|
|
|
AdvancedServerType type;
|
2018-01-25 10:53:51 +01:00
|
|
|
File file = new File(this.path + "\\" + this.getType());
|
2018-01-24 22:31:48 +01:00
|
|
|
Path filePath = Paths.get(this.path + "\\" + this.getType());
|
2018-01-30 13:39:09 +01:00
|
|
|
Profile currentProfile = GUI.getGUI().currentProfile();
|
2018-01-25 10:53:51 +01:00
|
|
|
String versionText;
|
|
|
|
String newestVersion;
|
2018-01-30 13:39:09 +01:00
|
|
|
String url = this.type.getDownloadURL();
|
|
|
|
String name = this.type.getName();
|
|
|
|
String ver = this.serverVersion;
|
2018-01-25 10:53:51 +01:00
|
|
|
boolean success;
|
|
|
|
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()) {
|
2018-01-30 13:39:09 +01:00
|
|
|
success = downloadFile(url + name + ver + ".jar", filePath);
|
|
|
|
System.out.println(url + name + ver + ".jar");
|
2018-01-25 10:53:51 +01:00
|
|
|
if (!success) {
|
|
|
|
throw new FileNotFoundException("Jar file could not be downloaded.");
|
2018-01-24 22:31:48 +01:00
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Vanilla":
|
|
|
|
case "Snapshot":
|
2018-01-25 19:55:28 +01:00
|
|
|
type = (AdvancedServerType) this.type;
|
2018-01-25 10:53:51 +01:00
|
|
|
if (this.serverVersion.equals("Latest")) {
|
|
|
|
try {
|
2018-01-25 19:55:28 +01:00
|
|
|
versionText = readFile(type.getVersionURL());
|
2018-01-25 10:53:51 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new FileNotFoundException("Version file could not be downloaded.");
|
2018-01-24 22:31:48 +01:00
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
2018-01-30 13:39:09 +01:00
|
|
|
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
|
|
|
|
success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
|
|
|
|
GUI.getGUI().currentProfile().setVersion(this.type.getName(), newestVersion);
|
|
|
|
if (!success) {
|
|
|
|
throw new FileNotFoundException("Jar file could not be downloaded.");
|
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!file.isFile()) {
|
2018-01-30 13:39:09 +01:00
|
|
|
success = downloadFile(url + ver + type.getDownloadURLPart() + ver + ".jar", filePath);
|
2018-01-24 22:31:48 +01:00
|
|
|
if (!success) {
|
|
|
|
throw new FileNotFoundException("Jar file could not be downloaded.");
|
|
|
|
}
|
2018-01-24 21:33:14 +01:00
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
break;
|
2018-01-25 17:34:45 +01:00
|
|
|
case "SpongeVanilla":
|
2018-01-25 19:55:28 +01:00
|
|
|
type = (AdvancedServerType) this.type;
|
2018-01-25 10:53:51 +01:00
|
|
|
try {
|
2018-01-25 19:55:28 +01:00
|
|
|
versionText = readFile(type.getVersionURL() + this.serverVersion);
|
2018-01-25 10:53:51 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new FileNotFoundException("Version file could not be downloaded.");
|
|
|
|
}
|
|
|
|
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
2018-01-30 13:39:09 +01:00
|
|
|
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
|
|
|
|
success = downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
|
|
|
|
currentProfile.setVersion(name, newestVersion);
|
|
|
|
if (!success) {
|
|
|
|
throw new FileNotFoundException("Jar file could not be downloaded.");
|
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Bungee":
|
2018-01-30 13:39:09 +01:00
|
|
|
type = (AdvancedServerType) this.type;
|
2018-01-25 19:55:28 +01:00
|
|
|
try {
|
2018-01-30 13:39:09 +01:00
|
|
|
versionText = readFile(type.getVersionURL());
|
2018-01-25 10:53:51 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new FileNotFoundException("Version file could not be downloaded.");
|
|
|
|
}
|
2018-01-30 13:39:09 +01:00
|
|
|
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
|
|
|
if (!file.isFile() || !newestVersion.equals(currentProfile.getVersion(name))) {
|
|
|
|
success = downloadFile(url, filePath);
|
|
|
|
currentProfile.setVersion(name, newestVersion);
|
|
|
|
if (!success) {
|
|
|
|
throw new FileNotFoundException("Jar file could not be downloaded.");
|
|
|
|
}
|
2018-01-25 10:53:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a file from a website.
|
|
|
|
* This is used to find the newest version of the software.
|
|
|
|
*
|
|
|
|
* @param path The full url of the file to read.
|
2018-01-26 20:34:15 +01:00
|
|
|
* @return True if successful. False otherwise.
|
2018-01-25 10:53:51 +01:00
|
|
|
*/
|
|
|
|
private static String readFile(String path) throws IOException {
|
|
|
|
URL url = new URL(path);
|
|
|
|
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads a file from a website.
|
|
|
|
*
|
|
|
|
* @param path The full url of the file to download.
|
|
|
|
* @param outfile The file to save to.
|
|
|
|
* @return True if successful. False otherwise.
|
|
|
|
*/
|
|
|
|
private static boolean downloadFile(String path, Path outfile) {
|
|
|
|
try {
|
|
|
|
URL url = new URL(path);
|
|
|
|
InputStream in = url.openStream();
|
|
|
|
Files.copy(in, outfile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 21:33:14 +01:00
|
|
|
|
2018-01-24 22:31:48 +01:00
|
|
|
/**
|
|
|
|
* Finds a substring between two substrings in a string.
|
|
|
|
*
|
2018-01-25 10:53:51 +01:00
|
|
|
* @param string The string containing the substrings.
|
|
|
|
* @param start The substring before the wanted substring.
|
|
|
|
* @param end The substring after the wanted substring.
|
|
|
|
* @return The wanted substring.
|
2018-01-24 22:31:48 +01:00
|
|
|
*/
|
2018-01-25 10:53:51 +01:00
|
|
|
private String stringBetween(String string, String start, String end) {
|
2018-01-25 11:38:47 +01:00
|
|
|
int startPos = string.indexOf(start) + start.length();
|
|
|
|
return string.substring(startPos, string.indexOf(end, startPos));
|
2018-01-24 21:33:14 +01:00
|
|
|
}
|
2018-01-30 00:44:03 +01:00
|
|
|
|
|
|
|
public void sendCommand(String command) throws IOException {
|
2018-01-31 21:24:54 +01:00
|
|
|
if (this.process != null && this.writer != null) {
|
|
|
|
this.writer.write(command + "\n");
|
2018-01-30 00:44:03 +01:00
|
|
|
this.writer.flush();
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 23:49:48 +01:00
|
|
|
}
|