Switched settings to config.yml.
This commit is contained in:
@@ -3,6 +3,10 @@
|
|||||||
<classpathentry kind="src" path="src"/>
|
<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="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/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"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|||||||
2
MANIFEST.MF
Normal file
2
MANIFEST.MF
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
|
||||||
46
src/de/sockenklaus/XmlStats/Settings.java
Normal file
46
src/de/sockenklaus/XmlStats/Settings.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,18 +8,16 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
|
|
||||||
import com.nidefawl.Stats.Stats;
|
import com.nidefawl.Stats.Stats;
|
||||||
|
|
||||||
import de.sockenklaus.XmlStats.Settings.Settings;
|
|
||||||
|
|
||||||
public class XmlStats extends JavaPlugin {
|
public class XmlStats extends JavaPlugin {
|
||||||
|
|
||||||
public final static Logger log = Logger.getLogger("Minecraft");
|
public final static Logger log = Logger.getLogger("Minecraft");
|
||||||
public final static double version = 0.01;
|
public final static double version = 0.01;
|
||||||
public final static String logprefix = "[XmlStats-" + version + "]";
|
public final static String logprefix = "[XmlStats]";
|
||||||
public boolean enabled = false;
|
public boolean enabled = false;
|
||||||
private static Stats statsPlugin;
|
private static Stats statsPlugin;
|
||||||
private static Server serverRef;
|
private static Server serverRef;
|
||||||
public WebServer xmlQueryServer;
|
private WebServer xmlQueryServer;
|
||||||
|
private Settings settings;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
@@ -41,12 +39,12 @@ public class XmlStats extends JavaPlugin {
|
|||||||
statsPlugin = (Stats)getServer().getPluginManager().getPlugin("Stats");
|
statsPlugin = (Stats)getServer().getPluginManager().getPlugin("Stats");
|
||||||
serverRef = getServer();
|
serverRef = getServer();
|
||||||
|
|
||||||
Settings.load(this);
|
settings = new Settings(this);
|
||||||
|
|
||||||
if (Settings.xmlStatsEnabled){
|
if (settings.getBoolean("options.webserver-enabled")){
|
||||||
if (getServer().getPluginManager().isPluginEnabled("Stats")){
|
if (getServer().getPluginManager().isPluginEnabled("Stats")){
|
||||||
try {
|
try {
|
||||||
xmlQueryServer = new WebServer(Settings.xmlStatsPort);
|
xmlQueryServer = new WebServer(settings.getInt("options.webserver-port"));
|
||||||
|
|
||||||
enabled = true;
|
enabled = true;
|
||||||
LogInfo("Plugin Enabled");
|
LogInfo("Plugin Enabled");
|
||||||
@@ -62,7 +60,7 @@ public class XmlStats extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogError("Plugin ist derzeit in der "+getDataFolder().getPath()+"/"+Settings.settingsFilename+" deaktiviert.");
|
LogWarn("Webserver ist derzeit in der "+getDataFolder().getPath()+"/"+settings.getSettingsFilename()+" deaktiviert.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user