From aa35a0aa8460cd5ef4aae4378c24f5398e0f600d Mon Sep 17 00:00:00 2001 From: Acrobot Date: Fri, 23 Nov 2012 21:00:35 +0100 Subject: [PATCH] More configuration stuff! --- .../Breeze/Configuration/Configuration.java | 70 +++++++++++-------- .../Breeze/Configuration/FieldParser.java | 24 +++++++ .../Breeze/Configuration/ValueParser.java | 41 +++++++++++ .../ChestShop/Configuration/Messages.java | 3 +- .../ChestShop/Configuration/Properties.java | 2 +- 5 files changed, 107 insertions(+), 33 deletions(-) diff --git a/com/Acrobot/Breeze/Configuration/Configuration.java b/com/Acrobot/Breeze/Configuration/Configuration.java index 8522465..23bf6d8 100644 --- a/com/Acrobot/Breeze/Configuration/Configuration.java +++ b/com/Acrobot/Breeze/Configuration/Configuration.java @@ -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 (config.isSet(path)) { - field.set(null, config.get(path)); - } else { - configureProperty(bw, field); + if (path.toLowerCase().replace("_", "").startsWith("newline")) { + writer.write('\n'); + continue; } - } 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) { 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(); - } + /** + * 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); } - private static String retrieveValue(Object value) { - StringBuilder toReturn = new StringBuilder(30); - - if (value instanceof Number || value instanceof Boolean) { - toReturn.append(String.valueOf(value)); - } else { - toReturn.append('\"').append(String.valueOf(value)).append('\"'); - } - - return toReturn.toString(); + /** + * Colourises a string (using '&' character) + * @param string String to colourise + * @return Colourised string + */ + public static String getColoured(String string) { + return ChatColor.translateAlternateColorCodes('&', string); } } diff --git a/com/Acrobot/Breeze/Configuration/FieldParser.java b/com/Acrobot/Breeze/Configuration/FieldParser.java index ac7f1c3..bb34473 100644 --- a/com/Acrobot/Breeze/Configuration/FieldParser.java +++ b/com/Acrobot/Breeze/Configuration/FieldParser.java @@ -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(); + } } diff --git a/com/Acrobot/Breeze/Configuration/ValueParser.java b/com/Acrobot/Breeze/Configuration/ValueParser.java index bf6af01..dbe8a77 100644 --- a/com/Acrobot/Breeze/Configuration/ValueParser.java +++ b/com/Acrobot/Breeze/Configuration/ValueParser.java @@ -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> map = new HashMap>(); + + 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; + } + } } diff --git a/com/Acrobot/ChestShop/Configuration/Messages.java b/com/Acrobot/ChestShop/Configuration/Messages.java index 97b4cf4..4d58c7d 100644 --- a/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/com/Acrobot/ChestShop/Configuration/Messages.java @@ -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; } } diff --git a/com/Acrobot/ChestShop/Configuration/Properties.java b/com/Acrobot/ChestShop/Configuration/Properties.java index a1e4741..87d34f8 100644 --- a/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/com/Acrobot/ChestShop/Configuration/Properties.java @@ -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;