diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..397e3ce
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigReader.java b/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigReader.java
index 4e63bdd..97b3a8f 100644
--- a/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigReader.java
+++ b/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigReader.java
@@ -1,4 +1,3 @@
-
package ml.codeboy.bukkitbootstrap.config;
import org.bukkit.configuration.InvalidConfigurationException;
@@ -13,8 +12,12 @@
public class ConfigReader {
public static void readConfig(Class> saveTo) {
- File file= new File(JavaPlugin.getProvidingPlugin(saveTo).getDataFolder(),saveTo.getSimpleName()+".yml");
- readConfig(saveTo,file);
+ readConfig(saveTo, ConfigUtil.getDefaultFileName(saveTo));
+ }
+
+ public static void readConfig(Class> saveTo, String fileName) {
+ File file = new File(JavaPlugin.getProvidingPlugin(saveTo).getDataFolder(), fileName);
+ readConfig(saveTo, file);
}
public static void readConfig(Class> saveTo, File file) {
@@ -39,19 +42,18 @@ public static void readConfig(Class> saveTo, Object instance, File file) {
boolean changed = false;
+ ConfigScope scope= ConfigUtil.getScope(saveTo);
+
for (Field field : saveTo.getDeclaredFields()) {
- if (field.isAnnotationPresent(ConfigValue.class)) {
+ if (ConfigUtil.shouldBeSerialized(field,scope)) {
field.setAccessible(true);
- String path = field.getAnnotation(ConfigValue.class).key();
- if (path.equals(""))
- path = field.getName();
+ String path = ConfigUtil.getPath(field);
try {
if (config.contains(path)) {
- field.set(instance, getValue(config, path));
- }
- else {
+ field.set(instance, ConfigUtil.getValue(config, path));
+ } else {
config.set(path, field.get(instance));
changed = true;
}
@@ -61,6 +63,7 @@ public static void readConfig(Class> saveTo, Object instance, File file) {
}
}
if (changed) {
+ ConfigUtil.addComments(config, saveTo);
try {
config.save(file);
} catch (IOException e) {
@@ -70,8 +73,12 @@ public static void readConfig(Class> saveTo, Object instance, File file) {
}
public static void saveConfig(Class> saveFrom) {
- File file= new File(JavaPlugin.getProvidingPlugin(saveFrom).getDataFolder(),saveFrom.getSimpleName()+".yml");
- saveConfig(saveFrom,file);
+ readConfig(saveFrom, ConfigUtil.getDefaultFileName(saveFrom));
+ }
+
+ public static void saveConfig(Class> saveFrom, String name) {
+ File file = new File(JavaPlugin.getProvidingPlugin(saveFrom).getDataFolder(), name);
+ saveConfig(saveFrom, file);
}
public static void saveConfig(Class> saveFrom, File file) {
@@ -94,13 +101,13 @@ public static void saveConfig(Class> saveFrom, Object instance, File file) {
e.printStackTrace();
}
+ ConfigScope scope= ConfigUtil.getScope(saveFrom);
+
for (Field field : saveFrom.getDeclaredFields()) {
- if (field.isAnnotationPresent(ConfigValue.class)) {
+ if (ConfigUtil.shouldBeSerialized(field,scope)) {
field.setAccessible(true);
- String path = field.getAnnotation(ConfigValue.class).key();
- if (path.equals(""))
- path = field.getName();
+ String path = ConfigUtil.getPath(field);
try {
config.set(path, field.get(instance));
@@ -109,6 +116,7 @@ public static void saveConfig(Class> saveFrom, Object instance, File file) {
}
}
}
+ ConfigUtil.addComments(config, saveFrom);
try {
config.save(file);
} catch (IOException e) {
@@ -116,11 +124,4 @@ public static void saveConfig(Class> saveFrom, Object instance, File file) {
}
}
- private static T getValue(FileConfiguration config, String path) {
- return (T) config.get(path);
- }
-
- private static T getList(FileConfiguration config, String path) {
- return (T) config.getList(path);
- }
}
diff --git a/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigUtil.java b/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigUtil.java
new file mode 100644
index 0000000..2eea263
--- /dev/null
+++ b/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigUtil.java
@@ -0,0 +1,55 @@
+package ml.codeboy.bukkitbootstrap.config;
+
+import org.bukkit.configuration.file.FileConfiguration;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+public class ConfigUtil {
+ static String getDefaultFileName(Class> config) {
+ if (config.isAnnotationPresent(Config.class)) {
+ String name = config.getAnnotation(Config.class).name();
+ if (name != null && name.length() > 0)
+ return name;
+ }
+ return config.getSimpleName() + ".yml";
+ }
+
+ static boolean shouldBeSerialized(Field field, ConfigScope scope){
+ int modifiers= field.getModifiers();
+ if(field.isAnnotationPresent(Ignore.class)||Modifier.isTransient(modifiers))
+ return false;
+ return scope==ConfigScope.ALL||
+ field.isAnnotationPresent(ConfigValue.class)||
+ Modifier.isPrivate(modifiers)&&scope==ConfigScope.PRIVATE||
+ Modifier.isPublic(modifiers)&&scope==ConfigScope.PUBLIC;
+ }
+
+ static ConfigScope getScope(Class> clazz){
+ if(clazz.isAnnotationPresent(Config.class))
+ return clazz.getAnnotation(Config.class).scope();
+ return ConfigScope.NONE;
+ }
+
+ static String getPath(Field field) {
+ String path = field.getAnnotation(ConfigValue.class).key();
+ if (path == null || path.equals(""))
+ path = field.getName();
+ return path;
+ }
+
+ static void addComments(FileConfiguration config, Class> clazz) {
+ if (clazz.isAnnotationPresent(Config.class)) {
+ String comments = clazz.getAnnotation(Config.class).comments();
+ config.options().header(comments);
+ }
+ }
+
+ static T getValue(FileConfiguration config, String path) {
+ return (T) config.get(path);
+ }
+
+ private static T getList(FileConfiguration config, String path) {
+ return (T) config.getList(path);
+ }
+}
diff --git a/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigValue.java b/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigValue.java
index 8fbd782..44a6558 100644
--- a/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigValue.java
+++ b/src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigValue.java
@@ -8,5 +8,8 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConfigValue {
+ /**
+ * @return the key to use when saving. If this is empty the name of the field will be used
+ */
String key() default "";
}