Compare commits
6 Commits
v1.0.0-alp
...
v1.0.2-alp
Author | SHA1 | Date | |
---|---|---|---|
5ac22babab | |||
46bb3a9e29 | |||
cf0d1f7fff | |||
6ba2397e40 | |||
2476e379e5 | |||
5ed0236888 |
@ -1,5 +1,5 @@
|
||||
# Minecraft-Server-Launcher
|
||||
I originally created this software in 2013 using AutoIt. Since I am now learning Java, and I have wanted to rewrite it for a long time, I am trying to recreate it in Java. It is currently missing mostly everything, but it's nothing a lot of work can't fix.
|
||||
I originally created this software in 2013 using AutoIt. Since I am now learning Java, I have recreated it in Java.
|
||||
The original version can be found at: https://knarcraft.net/Downloads/Bungeeminecraftserverlauncher/
|
||||
Its goal is to do everything the original does, just better. The original is 1595 lines of code repetition and dirty code. I had no prior programming experience when I first started working on the original, which is why it became such a mess.
|
||||
|
||||
|
@ -27,7 +27,7 @@ import static net.knarcraft.serverlauncher.Shared.stringBetween;
|
||||
|
||||
public class Main {
|
||||
@SuppressWarnings("CanBeFinal")
|
||||
public static String appDir;
|
||||
private static String appDir;
|
||||
private static boolean running = false;
|
||||
|
||||
static {
|
||||
@ -37,6 +37,11 @@ public class Main {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
try (PrintWriter file = new PrintWriter(Main.getAppDir() + File.separator + "latestrun.log")) {
|
||||
file.print("");
|
||||
} catch (IOException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@ -45,7 +50,6 @@ public class Main {
|
||||
setup();
|
||||
new ServerConsoles();
|
||||
Profile.load();
|
||||
|
||||
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
|
||||
exec.scheduleAtFixedRate(Main::updateConsoles, 10, 500, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
@ -63,6 +67,10 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAppDir() {
|
||||
return appDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from server processes, and writes the output to consoles.
|
||||
*/
|
||||
|
@ -32,9 +32,9 @@ public class Profile {
|
||||
private static final ArrayList<Profile> profiles = new ArrayList<>();
|
||||
private static Profile current;
|
||||
private static GUI gui;
|
||||
private static final String profilesDir = Main.appDir + File.separator + "files";
|
||||
private static final String profilesFile = Main.appDir + File.separator + "files" + File.separator + "Profiles.txt";
|
||||
private static final String jarDir = Main.appDir + File.separator + "files" + File.separator + "Jars" + File.separator;
|
||||
private static final String profilesDir = Main.getAppDir() + File.separator + "files";
|
||||
private static final String profilesFile = Main.getAppDir() + File.separator + "files" + File.separator + "Profiles.txt";
|
||||
private static final String jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator;
|
||||
|
||||
private final ArrayList<Collection> collections;
|
||||
private final String name;
|
||||
@ -139,6 +139,11 @@ public class Profile {
|
||||
this.downloadJars = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current profile to the profile with a certain name.
|
||||
*
|
||||
* @param name The name of the profile
|
||||
*/
|
||||
public static void setCurrent(String name) {
|
||||
for (Profile profile : profiles) {
|
||||
if (profile.name.equals(name)) {
|
||||
@ -456,6 +461,12 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a profile, and creates a profile with the data.
|
||||
*
|
||||
* @param profileData The data of the new profile
|
||||
* @return The new profile
|
||||
*/
|
||||
private static Profile parseProfile(String[] profileData) {
|
||||
return new Profile(
|
||||
profileData[0],
|
||||
@ -465,6 +476,12 @@ public class Profile {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a server, and creates a new collection.
|
||||
*
|
||||
* @param profile The profile which to add the collection
|
||||
* @param serverData The data to parse
|
||||
*/
|
||||
private static void parseServer(Profile profile, String[] serverData) {
|
||||
profile.collections.add(new Collection(
|
||||
serverData[0],
|
||||
@ -480,6 +497,11 @@ public class Profile {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads all jars to the program directory.
|
||||
*
|
||||
* @throws IOException On version file failure or folder creation failure
|
||||
*/
|
||||
public static void downloadJars() throws IOException {
|
||||
if (!new File(jarDir).exists() && !new File(jarDir).mkdirs()) {
|
||||
JOptionPane.showMessageDialog(
|
||||
@ -490,146 +512,64 @@ public class Profile {
|
||||
);
|
||||
throw new FileNotFoundException("Unable to create jars folder");
|
||||
}
|
||||
downloadSimple("Spigot");
|
||||
downloadSimple("Craftbukkit");
|
||||
downloadSimple("MCPCplus");
|
||||
downloadMixed("Vanilla");
|
||||
downloadMixed("Snapshot");
|
||||
downloadSponge();
|
||||
downloadBungee();
|
||||
try {
|
||||
downloadAll();
|
||||
gui.setStatus("Finished downloading jars");
|
||||
}
|
||||
|
||||
private static void downloadSimple(String typeName) throws FileNotFoundException {
|
||||
ServerType type = ServerType.getByName(typeName);
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
Boolean success;
|
||||
for (String version : type.getVersions()) {
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
if (!file.isFile()) {
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
if (gui != null) {
|
||||
gui.setStatus("Downloading: " + name + version + ".jar");
|
||||
}
|
||||
success = downloadFile(url + name + version + ".jar", filePath);
|
||||
if (!success) {
|
||||
if (gui != null) {
|
||||
gui.setStatus("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
gui.setStatus("One or more downloads failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadMixed(String typeName) throws IOException {
|
||||
AdvancedServerType type = (AdvancedServerType) ServerType.getByName(typeName);
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
Boolean success;
|
||||
/**
|
||||
* Downloads jar files for all possible server versions.
|
||||
*
|
||||
* @throws IOException If a jar fails to download.
|
||||
*/
|
||||
private static void downloadAll() throws IOException {
|
||||
for (ServerType type : ServerType.getServerTypes()) {
|
||||
String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion;
|
||||
AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
|
||||
for (String version : type.getVersions()) {
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
Boolean success;
|
||||
if (gui != null) {
|
||||
gui.setStatus("Downloading: " + name + version + ".jar");
|
||||
}
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
switch (type.getName()) {
|
||||
case "Vanilla":
|
||||
case "Snapshot":
|
||||
if (version.equals("Latest")) {
|
||||
try {
|
||||
versionText = readFile(type.getVersionURL());
|
||||
} catch (IOException e) {
|
||||
throw new IOException("Error reading: " + type.getVersionURL());
|
||||
}
|
||||
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
||||
if (!file.isFile() || !newestVersion.equals(getVersion(name))) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
filePath
|
||||
);
|
||||
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd());
|
||||
setVersion(name, newestVersion);
|
||||
if (!success) {
|
||||
if (gui != null) {
|
||||
gui.setStatus("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
}
|
||||
success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath);
|
||||
} else {
|
||||
if (!file.isFile()) {
|
||||
success = downloadFile(url + version + type.getDownloadURLPart() + version + ".jar", filePath);
|
||||
if (!success) {
|
||||
if (gui != null) {
|
||||
gui.setStatus("Error downloading: " + name + version + ".jar");
|
||||
success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath);
|
||||
}
|
||||
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadSponge() throws IOException {
|
||||
AdvancedServerType type = (AdvancedServerType) ServerType.getByName("SpongeVanilla");
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
Boolean success;
|
||||
for (String version : type.getVersions()) {
|
||||
File file = new File(jarDir + name + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + name + version + ".jar");
|
||||
if (gui != null) {
|
||||
gui.setStatus("Downloading: " + name + version + ".jar");
|
||||
}
|
||||
try {
|
||||
versionText = readFile(type.getVersionURL() + version);
|
||||
} catch (IOException e) {
|
||||
throw new IOException("Error reading: " + type.getVersionURL());
|
||||
}
|
||||
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
||||
if (!file.isFile()) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
filePath
|
||||
);
|
||||
if (!success) {
|
||||
if (gui != null) {
|
||||
gui.setStatus("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void downloadBungee() throws IOException {
|
||||
AdvancedServerType type = (AdvancedServerType) ServerType.getByName("Bungee");
|
||||
String url = Objects.requireNonNull(type).getDownloadURL();
|
||||
String name = type.getName();
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
Boolean success;
|
||||
File file = new File(jarDir + type.getName() + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + ".jar");
|
||||
if (gui != null) {
|
||||
gui.setStatus("Downloading: " + name + ".jar");
|
||||
}
|
||||
try {
|
||||
versionText = readFile(type.getVersionURL());
|
||||
} catch (IOException e) {
|
||||
throw new IOException("Error reading: " + type.getVersionURL());
|
||||
}
|
||||
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
||||
if (!file.isFile() || !newestVersion.equals(getVersion(name))) {
|
||||
success = downloadFile(url, filePath);
|
||||
break;
|
||||
case "Spigot":
|
||||
case "Craftbukkit":
|
||||
case "MCPCplus":
|
||||
success = file.isFile() || downloadFile(url + name + version + ".jar", filePath);
|
||||
break;
|
||||
case "SpongeVanilla":
|
||||
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL() + version), advType.getSrcStart(), advType.getSrcEnd());
|
||||
success = file.isFile() || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath);
|
||||
break;
|
||||
case "Bungee":
|
||||
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd());
|
||||
setVersion(name, newestVersion);
|
||||
success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url, filePath);
|
||||
break;
|
||||
default:
|
||||
success = true;
|
||||
}
|
||||
if (!success) {
|
||||
if (gui != null) {
|
||||
gui.setStatus("Error downloading: " + name + ".jar");
|
||||
gui.setStatus("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
|
||||
}
|
||||
throw new FileNotFoundException("Error downloading: " + name + ".jar");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ public class Server {
|
||||
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.appDir + File.separator + "files" + File.separator + "Jars" + File.separator;
|
||||
private static final String jarDir = Main.getAppDir() + File.separator + "files" + File.separator + "Jars" + File.separator;
|
||||
|
||||
private final String name;
|
||||
private String path;
|
||||
@ -293,9 +293,9 @@ public class Server {
|
||||
ProcessBuilder builder;
|
||||
String serverPath;
|
||||
if (Profile.getCurrent().getDownloadJars() && !type.getName().equals("Custom")) {
|
||||
serverPath = "\"" + jarDir + this.getType() + "\"";
|
||||
serverPath = jarDir + this.getType();
|
||||
} else {
|
||||
serverPath = "\"" + this.path + File.separator + this.getType() + "\"";
|
||||
serverPath = this.path + File.separator + this.getType();
|
||||
}
|
||||
builder = new ProcessBuilder(
|
||||
"java",
|
||||
@ -351,12 +351,7 @@ public class Server {
|
||||
AdvancedServerType type;
|
||||
File file = new File(this.path + File.separator + this.getType());
|
||||
Path filePath = Paths.get(this.path + File.separator + this.getType());
|
||||
String versionText;
|
||||
String newestVersion;
|
||||
String url = this.type.getDownloadURL();
|
||||
String name = this.type.getName();
|
||||
String ver = this.serverVersion;
|
||||
boolean success;
|
||||
String versionText, newestVersion, url = this.type.getDownloadURL(), name = this.type.getName(), ver = this.serverVersion;
|
||||
switch (this.type.getName()) {
|
||||
case "Custom":
|
||||
if (!file.isFile()) {
|
||||
@ -366,12 +361,9 @@ public class Server {
|
||||
case "Spigot":
|
||||
case "Craftbukkit":
|
||||
case "MCPCplus":
|
||||
if (!file.isFile()) {
|
||||
success = downloadFile(url + name + ver + ".jar", filePath);
|
||||
if (!success) {
|
||||
if (!(file.isFile() || downloadFile(url + name + ver + ".jar", filePath))) {
|
||||
throw new FileNotFoundException("Jar file could not be downloaded.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "Vanilla":
|
||||
case "Snapshot":
|
||||
@ -384,23 +376,16 @@ public class Server {
|
||||
}
|
||||
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
||||
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
filePath
|
||||
);
|
||||
this.setVersion(name, newestVersion);
|
||||
if (!success) {
|
||||
if (!downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath)) {
|
||||
throw new FileNotFoundException("Jar file could not be downloaded.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!file.isFile()) {
|
||||
success = downloadFile(url + ver + type.getDownloadURLPart() + ver + ".jar", filePath);
|
||||
if (!success) {
|
||||
if (!(file.isFile() || downloadFile(url + ver + type.getDownloadURLPart() + ver + ".jar", filePath))) {
|
||||
throw new FileNotFoundException("Jar file could not be downloaded.");
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "SpongeVanilla":
|
||||
type = (AdvancedServerType) this.type;
|
||||
@ -411,12 +396,8 @@ public class Server {
|
||||
}
|
||||
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
||||
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
|
||||
success = downloadFile(
|
||||
url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar",
|
||||
filePath
|
||||
);
|
||||
this.setVersion(name, newestVersion);
|
||||
if (!success) {
|
||||
if (!downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath)) {
|
||||
throw new FileNotFoundException("Jar file could not be downloaded.");
|
||||
}
|
||||
}
|
||||
@ -430,9 +411,8 @@ public class Server {
|
||||
}
|
||||
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
|
||||
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
|
||||
success = downloadFile(url, filePath);
|
||||
this.setVersion(name, newestVersion);
|
||||
if (!success) {
|
||||
if (!downloadFile(url, filePath)) {
|
||||
throw new FileNotFoundException("Jar file could not be downloaded.");
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,11 @@ public class ServerType {
|
||||
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()];
|
||||
@ -51,6 +56,12 @@ public class ServerType {
|
||||
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)) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.knarcraft.serverlauncher.userinterface;
|
||||
|
||||
import net.knarcraft.serverlauncher.Main;
|
||||
import net.knarcraft.serverlauncher.profile.Collection;
|
||||
import net.knarcraft.serverlauncher.server.Server;
|
||||
import net.knarcraft.serverlauncher.profile.Profile;
|
||||
@ -93,6 +94,11 @@ public class GUI implements ActionListener {
|
||||
*/
|
||||
public void setStatus(String text) {
|
||||
this.lblStatuslabel.setText(text);
|
||||
try (PrintWriter file = new PrintWriter(new FileWriter(Main.getAppDir() + File.separator + "latestrun.log", true))) {
|
||||
file.println(text);
|
||||
} catch (IOException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,7 +38,7 @@ public class ServerTab implements ActionListener {
|
||||
this.chckbxEnabled.setSelected(enabled);
|
||||
this.serverTypes.setSelectedItem(typeName);
|
||||
this.serverTypes();
|
||||
this.serverTypes.setSelectedItem(serverVersion);
|
||||
this.serverVersions.setSelectedItem(serverVersion);
|
||||
this.maxRam.setSelectedItem(maxRam);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user