More configuration stuff!

This commit is contained in:
Acrobot 2012-11-23 21:00:35 +01:00
parent 513aeef7b7
commit aa35a0aa84
5 changed files with 107 additions and 33 deletions

View File

@ -1,5 +1,6 @@
package com.Acrobot.Breeze.Configuration;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -11,14 +12,22 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* A class which can be used to make configs easier to load
*
* @author Acrobot
*/
public class Configuration {
public static void loadFileIntoClass(File file, Class clazz) {
/**
* Loads a YAML-formatted file into a class and modifies the file if some of class's fields are missing
*
* @param file File to load
* @param clazz Class to modify
*/
public static void pairFileAndClass(File file, Class clazz) {
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
for (Field field : clazz.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
@ -28,45 +37,46 @@ public class Configuration {
String path = field.getName();
try {
if (path.toLowerCase().replace("_", "").startsWith("newline")) {
writer.write('\n');
continue;
}
if (config.isSet(path)) {
field.set(null, config.get(path));
field.set(null, ValueParser.parseToJava(config.get(path)));
} else {
configureProperty(bw, field);
writer.write('\n' + FieldParser.parse(field));
}
} catch (IllegalAccessException ignored) {
}
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void configureProperty(BufferedWriter writer, Field field) {
try {
writer.write('\n' + field.getName() + ": " + retrieveValue(field.get(null)));
if (field.isAnnotationPresent(ConfigurationComment.class)) {
writer.write('\n' + "#" + field.getAnnotation(ConfigurationComment.class).value());
}
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String retrieveValue(Object value) {
StringBuilder toReturn = new StringBuilder(30);
writer.close();
if (value instanceof Number || value instanceof Boolean) {
toReturn.append(String.valueOf(value));
} else {
toReturn.append('\"').append(String.valueOf(value)).append('\"');
} catch (IOException e) {
e.printStackTrace();
}
}
return toReturn.toString();
/**
* Converts a java value to config-compatible value
*
* @param value Value to parse
* @return Parsed output
*/
public static String parseToConfig(Object value) {
return ValueParser.parseToYAML(value);
}
/**
* Colourises a string (using '&' character)
* @param string String to colourise
* @return Colourised string
*/
public static String getColoured(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}
}

View File

@ -1,7 +1,31 @@
package com.Acrobot.Breeze.Configuration;
import java.lang.reflect.Field;
/**
* @author Acrobot
*/
public class FieldParser {
/**
* Parses a field into a YAML-compatible string
*
* @param field Field to parse
* @return Parsed field
*/
public static String parse(Field field) {
StringBuilder builder = new StringBuilder(50);
try {
builder.append(field.getName()).append(": ").append(ValueParser.parseToYAML(field.get(null)));
} catch (IllegalAccessException e) {
e.printStackTrace();
return "";
}
if (field.isAnnotationPresent(ConfigurationComment.class)) {
builder.append('\n').append('#').append(field.getAnnotation(ConfigurationComment.class).value());
}
return builder.toString();
}
}

View File

@ -1,7 +1,48 @@
package com.Acrobot.Breeze.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Acrobot
*/
public class ValueParser {
/**
* Parses an object to a YAML-usable string
*
* @param object Object to parse
* @return YAML string
*/
public static String parseToYAML(Object object) {
if (object instanceof Number || object instanceof Boolean) {
return String.valueOf(object);
} else {
return '\"' + String.valueOf(object) + '\"';
}
}
/**
* Parses a YAML "object" to Java-compatible object
*
* @param object Object to parse
* @return Java-compatible object
*/
public static Object parseToJava(Object object) {
if (object instanceof ConfigurationSection) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (String message : ((ConfigurationSection) object).getKeys(false)) {
map.put(message, ((ConfigurationSection) object).getStringList(message));
}
return map;
} else if (object instanceof String) {
return Configuration.getColoured((String) object);
} else {
return object;
}
}
}

View File

@ -49,7 +49,6 @@ public class Messages {
public static String YOU_CANNOT_CREATE_SHOP = "You can't create this type of shop!";
public static String NO_CHEST_DETECTED = "Couldn't find a chest!";
public static String INVALID_SHOP_DETECTED = "The shop cannot be used! (It might lack a chest!)";
public static String ANOTHER_SHOP_DETECTED = "Another player's shop detected!";
public static String CANNOT_ACCESS_THE_CHEST = "You don't have permissions to access this chest!";
public static byte NEWLINE_9; ///////////////////////////////////////////////////
@ -72,7 +71,7 @@ public class Messages {
public static String CANNOT_CREATE_SHOP_HERE = "You can't create shop here!";
public static String getMessage(String message) {
public static String prefix(String message) {
return prefix + message;
}
}

View File

@ -12,7 +12,7 @@ public class Properties {
public static byte NEWLINE_1; ///////////////////////////////////////////////////
@ConfigurationComment("(In 1/1000th of a second) How often can a player use the shop sign?")
public static int SHOP_INTERACTION_INTERVAL = 100;
public static int SHOP_INTERACTION_INTERVAL = 250;
@ConfigurationComment("Do you want to allow using shops to people in creative mode?")
public static boolean IGNORE_CREATIVE_MODE = true;