173 lines
6.2 KiB
Java
Raw Normal View History

2018-01-24 12:18:06 +01:00
package net.knarcraft.serverlauncher.server;
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. */
public class Server {
/** Necessary urls for downloading from knarcraft.net */
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/";
/** 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 String name;
private String path;
private boolean enabled;
private ArrayList<String> playerList;
private AdvancedServerType type;
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 {
File file = new File(this.path + "\\" + this.getType());
Path filePath = Paths.get(this.path + "\\" + this.getType());
switch (this.type.getName()) {
case "Custom":
if (!file.isFile()) {
throw new FileNotFoundException("Specified custom jar was not found.");
}
case "Spigot":
if (!file.isFile()) {
staticJar(SPIGOTURL);
}
break;
case "Craftbukkit":
staticJar(BUKKITURL);
break;
case "MCPCplus":
staticJar(MCPCURL);
break;
case "Vanilla":
case "Snapshot":
if (this.serverVersion.equals("Latest")) {
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.");
}
} else {
downloadFile(type.getDownloadURL() + this.serverVersion + type.getDownloadURLPart() + this.serverVersion + ".jar", filePath);
}
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.
}
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()));
}
/**
* 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.
* @return True if successfull. False otherwise.
*/
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;
}
}
/**
* 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.
*/
private String stringBetween(String string, String start, String end) {
return string.substring(string.indexOf(start) + 1 + start.length(), string.indexOf(end));
}
}