2018-01-24 12:18:06 +01:00
|
|
|
package net.knarcraft.serverlauncher.server;
|
2018-01-23 23:49:48 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.io.FileNotFoundException;
|
2018-01-24 12:18:06 +01:00
|
|
|
/* Contains all necessary information to create, run and manage a Minecraft server. */
|
2018-01-23 23:49:48 +01:00
|
|
|
public class Server {
|
2018-01-24 22:31:48 +01:00
|
|
|
/** Necessary urls for downloading from knarcraft.net */
|
2018-01-24 21:33:14 +01:00
|
|
|
private static final String BASEURL = "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars"; //The url we download jar files from.
|
|
|
|
private static final String BUKKITURL = BASEURL + "/Bukkit/";
|
|
|
|
private static final String MCPCURL = BASEURL + "/MCPC+/";
|
|
|
|
private static final String SPIGOTURL = BASEURL + "/Spigot/";
|
2018-01-24 22:31:48 +01:00
|
|
|
/** 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"};
|
|
|
|
|
2018-01-23 23:49:48 +01:00
|
|
|
private String name;
|
|
|
|
private String path;
|
|
|
|
private boolean enabled;
|
|
|
|
private ArrayList<String> playerList;
|
2018-01-24 21:33:14 +01:00
|
|
|
private AdvancedServerType type;
|
2018-01-23 23:49:48 +01:00
|
|
|
private String serverVersion;
|
|
|
|
private String maxRam;
|
|
|
|
private long pid;
|
|
|
|
|
|
|
|
public Server(String name) {
|
|
|
|
this.name = name;
|
|
|
|
this.path = "";
|
|
|
|
this.enabled = false;
|
|
|
|
this.playerList = new ArrayList<>();
|
|
|
|
this.type = null;
|
|
|
|
this.serverVersion = null;
|
|
|
|
this.maxRam = ramList[0];
|
|
|
|
this.pid = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPath() {
|
|
|
|
return this.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getType() {
|
|
|
|
return this.type.getName() + this.serverVersion + ".jar";
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return this.enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String maxRam() {
|
|
|
|
return this.maxRam;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updatePid(long pid) {
|
|
|
|
this.pid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads necessary .jar file for the server.
|
|
|
|
* All server versions are downloaded if missing.
|
|
|
|
* Newest server versions (snapshot, vanilla and bungee) need to be checked against an online json file and downloaded if outdated.
|
|
|
|
* Custom files must exist, or trigger a message.
|
|
|
|
*/
|
|
|
|
public void downloadJar() throws FileNotFoundException {
|
2018-01-24 22:31:48 +01:00
|
|
|
File file = new File(this.path + "\\" + this.getType());
|
|
|
|
Path filePath = Paths.get(this.path + "\\" + this.getType());
|
2018-01-24 21:33:14 +01:00
|
|
|
switch (this.type.getName()) {
|
|
|
|
case "Custom":
|
2018-01-24 22:31:48 +01:00
|
|
|
if (!file.isFile()) {
|
|
|
|
throw new FileNotFoundException("Specified custom jar was not found.");
|
|
|
|
}
|
2018-01-24 21:33:14 +01:00
|
|
|
case "Spigot":
|
2018-01-24 22:31:48 +01:00
|
|
|
if (!file.isFile()) {
|
|
|
|
staticJar(SPIGOTURL);
|
|
|
|
}
|
2018-01-24 21:33:14 +01:00
|
|
|
break;
|
|
|
|
case "Craftbukkit":
|
|
|
|
staticJar(BUKKITURL);
|
|
|
|
break;
|
|
|
|
case "MCPCplus":
|
|
|
|
staticJar(MCPCURL);
|
|
|
|
break;
|
|
|
|
case "Vanilla":
|
2018-01-24 22:31:48 +01:00
|
|
|
case "Snapshot":
|
2018-01-24 21:33:14 +01:00
|
|
|
if (this.serverVersion.equals("Latest")) {
|
2018-01-24 22:31:48 +01:00
|
|
|
String versionText;
|
|
|
|
try {
|
|
|
|
versionText = readFile(this.type.getVersionURL());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new FileNotFoundException("Version file could not be downloaded.");
|
|
|
|
}
|
|
|
|
String newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
|
|
|
boolean success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath);
|
|
|
|
if (!success) {
|
|
|
|
throw new FileNotFoundException("Jar file could not be downloaded.");
|
|
|
|
}
|
2018-01-24 21:33:14 +01:00
|
|
|
} else {
|
2018-01-24 22:31:48 +01:00
|
|
|
downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
|
2018-01-24 21:33:14 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Sponge":
|
|
|
|
staticJar(type.getVersionURL(), type.getSrcStart(), type.getSrcEnd(), type.getDownloadURL(), type.getDownloadURLPart(), this.getType());
|
|
|
|
}
|
|
|
|
//TODO: Download a jar file based on version and type. Throw an error if the file could not be found or fetched.
|
2018-01-23 23:49:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 21:33:14 +01:00
|
|
|
private boolean staticJar(String url) {
|
|
|
|
return downloadFile(url + this.getType(), Paths.get(this.path + this.getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean staticJar(String versionURL, String srcStart, String srcEnd, String downloadURL, String downloadURLPart, String outfile) {
|
|
|
|
return downloadFile(versionURL + this.getType(), Paths.get(this.path + this.getType()));
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:49:48 +01:00
|
|
|
/**
|
2018-01-24 22:31:48 +01:00
|
|
|
* Reads a file from a website.
|
2018-01-23 23:49:48 +01:00
|
|
|
* This is used to find the newest version of the software.
|
|
|
|
*
|
|
|
|
* @param path The full url of the file to read.
|
|
|
|
* @return True if successfull. False otherwise.
|
|
|
|
*/
|
2018-01-24 22:31:48 +01:00
|
|
|
private static String readFile(String path) throws IOException {
|
|
|
|
URL url = new URL(path);
|
|
|
|
return new Scanner(url.openStream()).useDelimiter("\\Z").next();
|
2018-01-23 23:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-01-24 21:33:14 +01:00
|
|
|
private static boolean downloadFile(String path, Path outfile) {
|
2018-01-23 23:49:48 +01:00
|
|
|
try {
|
2018-01-24 22:31:48 +01:00
|
|
|
URL url = new URL(path);
|
2018-01-23 23:49:48 +01:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @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 21:33:14 +01:00
|
|
|
private String stringBetween(String string, String start, String end) {
|
|
|
|
return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end));
|
|
|
|
}
|
2018-01-23 23:49:48 +01:00
|
|
|
}
|