Fixed potential error in ServerConfiguration and added ability to programmatically modify server configuration

This commit is contained in:
Simon 2018-10-28 09:45:36 -04:00
parent 564a692c2e
commit 0845438297

View File

@ -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();
}
}