mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-10 14:26:45 +00:00
Fixed potential error in ServerConfiguration and added ability to programmatically modify server configuration
This commit is contained in:
parent
564a692c2e
commit
0845438297
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user