Switched settings to config.yml.

This commit is contained in:
Pascal Koenig
2011-08-27 19:03:09 +02:00
parent 7e8a50a0dc
commit 1918dcb483
6 changed files with 60 additions and 208 deletions

View File

@@ -3,6 +3,10 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/Stats.jar"/>
<classpathentry kind="lib" path="lib/bukkit-0.0.1-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="lib/bukkit-0.0.1-SNAPSHOT.jar">
<attributes>
<attribute name="javadoc_location" value="http://jd.bukkit.org/apidocs/"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

2
MANIFEST.MF Normal file
View File

@@ -0,0 +1,2 @@
Manifest-Version: 1.0

View File

@@ -0,0 +1,46 @@
package de.sockenklaus.XmlStats;
import java.io.File;
import org.bukkit.util.config.Configuration;
public class Settings {
private static final String configFilename = "config.yml";
private Configuration conf;
public Settings(XmlStats xmlStats){
File f = new File(xmlStats.getDataFolder(), configFilename);
conf = new Configuration(f);
if(f.exists()){
conf.load();
}
else {
conf.setProperty("options.webserver-enabled", false);
conf.setProperty("options.webserver-port", 9123);
conf.setProperty("plugins.stats", true);
conf.setProperty("plugins.users", true);
conf.setProperty("plugins.achievements", false);
conf.setProperty("plugins.economy", false);
conf.save();
}
}
public int getInt(String path){
return conf.getInt(path, -1);
}
public boolean getBoolean(String path){
return conf.getBoolean(path, false);
}
public String getString(String path){
return conf.getString(path, "");
}
public void setProperty(String path, Object value){
conf.setProperty(path, value);
}
public String getSettingsFilename(){
return configFilename;
}
}

View File

@@ -1,24 +0,0 @@
package de.sockenklaus.XmlStats.Settings;
import java.io.File;
import de.sockenklaus.XmlStats.XmlStats;
public class Settings {
public static final String settingsFilename = "XmlStats.conf";
public static int xmlStatsPort;
public static boolean xmlStatsEnabled;
public static void load(XmlStats plugin) {
SettingsFile properties = new SettingsFile(new File(plugin.getDataFolder(), settingsFilename));
xmlStatsPort = properties.getInt("xmlstats-port", 8080, "port of the webserver for xml-access");
xmlStatsEnabled = properties.getBoolean("xmlstats-enabled", false, "disabled per default to avoid unwanted port-mappings");
properties.save();
}
}

View File

@@ -1,174 +0,0 @@
package de.sockenklaus.XmlStats.Settings;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import de.sockenklaus.XmlStats.XmlStats;
public class SettingsFile {
private HashMap<String, PropertiesEntry> map;
private File file;
private boolean modified;
public SettingsFile(File file) {
this.file = file;
map = new HashMap<String, PropertiesEntry>();
Scanner scan;
try {
if (!file.exists())
file.createNewFile();
scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (!line.contains("="))
continue;
if (line.length() == 0)
continue;
if (line.trim().charAt(0) == '#')
continue;
int equals = line.indexOf("=");
int commentIndex = line.length();
if (line.contains("#")) {
commentIndex = line.indexOf("#");
}
String key = line.substring(0, equals).trim();
if (key.equals(""))
continue;
String value = line.substring(equals + 1, commentIndex).trim();
String comment = "";
if (commentIndex < line.length() - 1) {
comment = line.substring(commentIndex + 1, line.length()).trim();
}
map.put(key, new PropertiesEntry(value, comment));
}
} catch (FileNotFoundException e) {
XmlStats.LogError("Cannot read file " + file.getName());
} catch (IOException e) {
XmlStats.LogError("Cannot create file " + file.getName());
}
}
public boolean getBoolean(String key, Boolean defaultValue, String defaultComment) {
if (map.containsKey(key)) {
return Boolean.parseBoolean(map.get(key).value);
} else {
map.put(key, new PropertiesEntry(defaultValue.toString(), defaultComment));
modified = true;
return defaultValue;
}
}
public boolean getBoolean(String key) {
if (map.containsKey(key)) {
return Boolean.parseBoolean(map.get(key).value);
}
return false;
}
public void remove(String key) {
map.remove(key);
}
public String getString(String key, String defaultValue, String defaultComment) {
if (map.containsKey(key)) {
return map.get(key).value;
} else {
map.put(key, new PropertiesEntry(defaultValue.toString(), defaultComment));
modified = true;
return defaultValue;
}
}
public int getInt(String key, Integer defaultValue, String defaultComment) {
if (map.containsKey(key)) {
try {
return Integer.parseInt(map.get(key).value);
} catch (Exception e) {
XmlStats.LogWarn("Trying to get Integer from " + key + ": " + map.get(key).value);
return 0;
}
} else {
map.put(key, new PropertiesEntry(defaultValue.toString(), defaultComment));
modified = true;
return defaultValue;
}
}
public double getDouble(String key, Double defaultValue, String defaultComment) {
if (map.containsKey(key)) {
try {
return Double.parseDouble(map.get(key).value);
} catch (Exception e) {
XmlStats.LogWarn("Trying to get Double from " + key + ": " + map.get(key).value);
return 0;
}
} else {
map.put(key, new PropertiesEntry(defaultValue.toString(), defaultComment));
modified = true;
return defaultValue;
}
}
public void save() {
if (!modified)
return;
BufferedWriter bwriter = null;
FileWriter fwriter = null;
try {
if (!file.exists())
file.createNewFile();
fwriter = new FileWriter(file);
bwriter = new BufferedWriter(fwriter);
SortedSet<Map.Entry<String, PropertiesEntry>> results = new TreeSet<Map.Entry<String, PropertiesEntry>>(new Comparator<Map.Entry<String, PropertiesEntry>>() {
@Override
public int compare(Map.Entry<String, PropertiesEntry> a, Map.Entry<String, PropertiesEntry> b) {
// int d = a.getValue().compareTo(b.getValue());
int d = a.getKey().compareTo(b.getKey());
return d;
}
});
results.addAll(map.entrySet());
for (Entry<String, PropertiesEntry> entry : results) {
StringBuilder builder = new StringBuilder();
builder.append(entry.getKey());
builder.append(" = ");
builder.append(entry.getValue().value);
if (!entry.getValue().comment.equals("")) {
builder.append(" #");
builder.append(entry.getValue().comment);
}
bwriter.write(builder.toString());
bwriter.newLine();
}
bwriter.flush();
} catch (IOException e) {
XmlStats.LogError("IO Exception with file " + file.getName());
} finally {
try {
if (bwriter != null) {
bwriter.flush();
bwriter.close();
}
if (fwriter != null) {
fwriter.close();
}
} catch (IOException e) {
XmlStats.LogError("IO Exception with file " + file.getName() + " (on close)");
}
}
}
private class PropertiesEntry {
public String value;
public String comment;
public PropertiesEntry(String value, String comment) {
this.value = value;
this.comment = comment;
}
}
}

View File

@@ -8,18 +8,16 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.nidefawl.Stats.Stats;
import de.sockenklaus.XmlStats.Settings.Settings;
public class XmlStats extends JavaPlugin {
public final static Logger log = Logger.getLogger("Minecraft");
public final static double version = 0.01;
public final static String logprefix = "[XmlStats-" + version + "]";
public final static String logprefix = "[XmlStats]";
public boolean enabled = false;
private static Stats statsPlugin;
private static Server serverRef;
public WebServer xmlQueryServer;
private WebServer xmlQueryServer;
private Settings settings;
@Override
public void onDisable() {
@@ -41,12 +39,12 @@ public class XmlStats extends JavaPlugin {
statsPlugin = (Stats)getServer().getPluginManager().getPlugin("Stats");
serverRef = getServer();
Settings.load(this);
settings = new Settings(this);
if (Settings.xmlStatsEnabled){
if (settings.getBoolean("options.webserver-enabled")){
if (getServer().getPluginManager().isPluginEnabled("Stats")){
try {
xmlQueryServer = new WebServer(Settings.xmlStatsPort);
xmlQueryServer = new WebServer(settings.getInt("options.webserver-port"));
enabled = true;
LogInfo("Plugin Enabled");
@@ -62,7 +60,7 @@ public class XmlStats extends JavaPlugin {
}
}
else {
LogError("Plugin ist derzeit in der "+getDataFolder().getPath()+"/"+Settings.settingsFilename+" deaktiviert.");
LogWarn("Webserver ist derzeit in der "+getDataFolder().getPath()+"/"+settings.getSettingsFilename()+" deaktiviert.");
}