Skip to content

Commit

Permalink
Implemented new annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
the-codeboy committed Oct 10, 2021
1 parent 8511047 commit d3db0d3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 24 additions & 23 deletions src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package ml.codeboy.bukkitbootstrap.config;

import org.bukkit.configuration.InvalidConfigurationException;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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));
Expand All @@ -109,18 +116,12 @@ public static void saveConfig(Class<?> saveFrom, Object instance, File file) {
}
}
}
ConfigUtil.addComments(config, saveFrom);
try {
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}

private static <T> T getValue(FileConfiguration config, String path) {
return (T) config.get(path);
}

private static <T> T getList(FileConfiguration config, String path) {
return (T) config.getList(path);
}
}
55 changes: 55 additions & 0 deletions src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -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> T getValue(FileConfiguration config, String path) {
return (T) config.get(path);
}

private static <T> T getList(FileConfiguration config, String path) {
return (T) config.getList(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}

0 comments on commit d3db0d3

Please sign in to comment.