From 0845438297d742379f5054daf9d5e6dc8f1ce2e2 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 28 Oct 2018 09:45:36 -0400 Subject: [PATCH] Fixed potential error in ServerConfiguration and added ability to programmatically modify server configuration --- .../simon987/server/ServerConfiguration.java | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Server/src/main/java/net/simon987/server/ServerConfiguration.java b/Server/src/main/java/net/simon987/server/ServerConfiguration.java index 8cff8b6..bb8966d 100644 --- a/Server/src/main/java/net/simon987/server/ServerConfiguration.java +++ b/Server/src/main/java/net/simon987/server/ServerConfiguration.java @@ -3,25 +3,24 @@ package net.simon987.server; import net.simon987.server.logging.LogManager; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.Properties; /** - * Wrapper for Java Property + * Wrapper for Java Properties class */ public class ServerConfiguration { - /** - * Properties - */ private Properties properties; + private String fileName; + + public ServerConfiguration(String fileName) { + + this.fileName = fileName; - public ServerConfiguration(String file) { try { properties = new Properties(); - InputStream is = new FileInputStream("config.properties"); + InputStream is = new FileInputStream(this.fileName); properties.load(is); } catch (IOException e) { @@ -29,6 +28,17 @@ public class ServerConfiguration { } } + private void saveConfig() { + + try { + OutputStream os = new FileOutputStream(this.fileName); + properties.store(os, ""); + + } catch (IOException e) { + LogManager.LOGGER.severe("Problem saving server configuration: " + e.getMessage()); + } + } + public int getInt(String key) { return Integer.valueOf((String) properties.get(key)); @@ -39,4 +49,14 @@ public class ServerConfiguration { return (String) properties.get(key); } + public void setInt(String key, int value) { + properties.setProperty(key, String.valueOf(value)); + saveConfig(); + } + + public void setString(String key, String value) { + properties.setProperty(key, value); + saveConfig(); + } + }