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

View File

@ -1,7 +1,31 @@
package com.Acrobot.Breeze.Configuration; package com.Acrobot.Breeze.Configuration;
import java.lang.reflect.Field;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class FieldParser { 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; package com.Acrobot.Breeze.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class ValueParser { 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 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 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 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 String CANNOT_ACCESS_THE_CHEST = "You don't have permissions to access this chest!";
public static byte NEWLINE_9; /////////////////////////////////////////////////// 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 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; return prefix + message;
} }
} }

View File

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