From 449c5142a74ed92a2f90c4e2e660e8cb77434a6c Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Wed, 14 Feb 2024 14:53:08 +0100 Subject: [PATCH] Integrated icon, motd & properties support for the Server.java entity --- .../java/de/gnmyt/mcdash/entities/Server.java | 94 ++++++++++++++++++- 1 file changed, 89 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/gnmyt/mcdash/entities/Server.java b/src/main/java/de/gnmyt/mcdash/entities/Server.java index 35fc101..102f325 100644 --- a/src/main/java/de/gnmyt/mcdash/entities/Server.java +++ b/src/main/java/de/gnmyt/mcdash/entities/Server.java @@ -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,19 +14,72 @@ 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; /** * Basic constructor of the server * - * @param name The name of the server - * @param status The status of the server - * @param configurationFile The configuration file of the server + * @param name The name of the server + * @param status The status 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 + '}'; } }