From 0ed7fdb2f85caefdb89f9071ff76bdf2c271e911 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Wed, 14 Feb 2024 14:52:45 +0100 Subject: [PATCH] Created the ServerProperties.java entity --- .../mcdash/entities/ServerProperties.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/de/gnmyt/mcdash/entities/ServerProperties.java diff --git a/src/main/java/de/gnmyt/mcdash/entities/ServerProperties.java b/src/main/java/de/gnmyt/mcdash/entities/ServerProperties.java new file mode 100644 index 0000000..11325b2 --- /dev/null +++ b/src/main/java/de/gnmyt/mcdash/entities/ServerProperties.java @@ -0,0 +1,54 @@ +package de.gnmyt.mcdash.entities; + +import de.gnmyt.mcdash.api.Logger; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.util.HashMap; +import java.util.Properties; + +public class ServerProperties { + + private final Logger LOG = new Logger(ServerProperties.class); + + Properties serverProperties = new Properties(); + + /** + * Basic constructor of the {@link ServerProperties} + * + * @param serverPropertiesFile The server properties file + */ + public ServerProperties(File serverPropertiesFile) { + try { + serverProperties.load(FileUtils.openInputStream(serverPropertiesFile)); + } catch (Exception e) { + LOG.error("An error occurred while loading the server properties: {}", e.getMessage()); + } + } + + /** + * Gets a specific property from the server properties + * + * @param key The key of the property + * @return the value of the property + */ + public String getServerProperty(String key) { + return serverProperties.getProperty(key); + } + + /** + * Gets all server properties + * + * @return all server properties + */ + public HashMap getServerProperties() { + HashMap properties = new HashMap<>(); + serverProperties.forEach((key, value) -> properties.put((String) key, (String) value)); + return properties; + } + + @Override + public String toString() { + return "ServerProperties{" + serverProperties + '}'; + } +}