Integrated icon, motd & properties support for the Server.java entity

This commit is contained in:
Mathias Wagner 2024-02-14 14:53:08 +01:00
parent 0ed7fdb2f8
commit 449c5142a7
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,8 +1,10 @@
package de.gnmyt.mcdash.entities;
import de.gnmyt.mcdash.api.Logger;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Base64;
public class Server {
@ -12,6 +14,9 @@ public class Server {
private final String name;
private ServerStatus status;
private final ServerConfiguration configuration;
private final ServerProperties properties;
private final String base64Icon;
private final int dashPort;
private Process process;
/**
@ -19,12 +24,62 @@ public class Server {
*
* @param name The name of the server
* @param status The status of the server
* @param configurationFile The configuration file of the server
* @param serverDirectory The directory of the server
*/
public Server(String name, ServerStatus status, File configurationFile) {
public Server(String name, ServerStatus status, File serverDirectory) {
this.name = name;
this.status = status;
this.configuration = ServerConfiguration.load(configurationFile);
this.configuration = ServerConfiguration.load(new File(serverDirectory, "mcdash.json"));
this.properties = new ServerProperties(new File(serverDirectory, "server.properties"));
File configFile = new File(serverDirectory, "plugins/MinecraftDashboard/config.yml");
this.dashPort = loadPortFromConfig(configFile);
this.base64Icon = loadIcon(serverDirectory);
}
/**
* Loads the port from the config file
* @param configFile The config file
*/
public int loadPortFromConfig(File configFile) {
if (configFile.exists()) {
try {
String content = FileUtils.readFileToString(configFile, "UTF-8");
String[] lines = content.split("\n");
for (String line : lines) {
if (line.startsWith("port:")) {
return Integer.parseInt(line.split(":")[1].trim());
}
}
} catch (Exception e) {
LOG.error("An error occurred while reading the config file", e.getMessage());
}
}
return 0;
}
/**
* Loads the icon of the server
* @param serverDirectory The directory of the server
* @return the base64 icon of the server
*/
public String loadIcon(File serverDirectory) {
File iconFile = new File(serverDirectory, "server-icon.png");
if (iconFile.exists()) {
try {
return Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(iconFile));
} catch (Exception e) {
LOG.error("An error occurred while reading the icon file", e.getMessage());
}
} else {
try {
return Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(new File(Server.class.getResource("/server-icon.png").getFile())));
} catch (Exception e) {
LOG.error("An error occurred while reading the icon file", e.getMessage());
}
}
return null;
}
/**
@ -105,12 +160,41 @@ public class Server {
keepAliveThread.start();
}
/**
* Gets the dashboard port of the server
*
* @return the dashboard port of the server
*/
public int getDashPort() {
return dashPort;
}
/**
* Gets the base64 icon of the server
* @return the base64 icon of the server
*/
public String getBase64Icon() {
return base64Icon;
}
/**
* Gets the properties of the server
*
* @return the properties of the server
*/
public ServerProperties getProperties() {
return properties;
}
@Override
public String toString() {
return "Server{" +
"name='" + name + '\'' +
", status=" + status +
", configuration=" + configuration +
", port=" + dashPort +
", icon=" + base64Icon +
", properties=" + properties +
'}';
}
}