-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8511047
commit d3db0d3
Showing
4 changed files
with
89 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/ml/codeboy/bukkitbootstrap/config/ConfigUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters