2012-11-23 20:59:12 +01:00
|
|
|
package com.Acrobot.Breeze.Configuration;
|
|
|
|
|
2016-03-06 20:50:15 +01:00
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Modifier;
|
2018-10-23 20:04:18 +02:00
|
|
|
import java.util.HashMap;
|
2020-01-29 16:35:04 +01:00
|
|
|
import java.util.Locale;
|
2018-10-23 20:04:18 +02:00
|
|
|
import java.util.Map;
|
2016-03-06 20:50:15 +01:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
import com.Acrobot.Breeze.Configuration.Annotations.Parser;
|
2012-11-23 21:00:35 +01:00
|
|
|
import org.bukkit.ChatColor;
|
2012-11-23 20:59:12 +01:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
2016-03-06 20:50:15 +01:00
|
|
|
import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
|
2012-11-23 20:59:12 +01:00
|
|
|
|
|
|
|
/**
|
2012-11-23 21:00:35 +01:00
|
|
|
* A class which can be used to make configs easier to load
|
|
|
|
*
|
2012-11-23 20:59:12 +01:00
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Configuration {
|
2018-10-23 20:04:18 +02:00
|
|
|
private static Map<String, ValueParser> parsers = new HashMap<>();
|
2020-01-15 22:55:40 +01:00
|
|
|
private static final ValueParser DEFAULT_PARSER = new ValueParser();
|
|
|
|
private static final ValueParser ENUM_PARSER = new ValueParser() {
|
|
|
|
@Override
|
|
|
|
public <T> Object parseToJava(Class<T> type, Object object) {
|
|
|
|
if (object instanceof String && type.isEnum()) {
|
2020-01-29 16:35:04 +01:00
|
|
|
return Enum.valueOf((Class<? extends Enum>) type, ((String) object).toUpperCase(Locale.ROOT));
|
2020-01-15 22:55:40 +01:00
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
};
|
2018-10-23 20:04:18 +02:00
|
|
|
|
2012-11-23 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-08-01 09:52:13 +02:00
|
|
|
public static void pairFileAndClass(File file, Class<?> clazz) {
|
2012-11-23 20:59:12 +01:00
|
|
|
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
|
|
|
|
|
|
|
try {
|
2012-11-23 21:00:35 +01:00
|
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
|
2012-11-23 20:59:12 +01:00
|
|
|
|
2013-04-23 20:09:23 +02:00
|
|
|
if (!endsWithSpace(file)) {
|
|
|
|
writer.newLine();
|
|
|
|
}
|
|
|
|
|
2012-11-23 20:59:12 +01:00
|
|
|
for (Field field : clazz.getDeclaredFields()) {
|
2012-11-25 23:34:21 +01:00
|
|
|
if (!Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || !Modifier.isPublic(field.getModifiers())) {
|
2012-11-23 20:59:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String path = field.getName();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (config.isSet(path)) {
|
2020-01-15 22:55:40 +01:00
|
|
|
field.set(null, getParser(field).parseToJava(field.getType(), config.get(path)));
|
2020-01-29 16:35:04 +01:00
|
|
|
} else if (config.isSet(path.toLowerCase(Locale.ROOT))) {
|
|
|
|
field.set(null, getParser(field).parseToJava(field.getType(), config.get(path.toLowerCase(Locale.ROOT))));
|
2012-11-23 20:59:12 +01:00
|
|
|
} else {
|
2013-04-23 20:09:23 +02:00
|
|
|
if (field.isAnnotationPresent(PrecededBySpace.class)) {
|
|
|
|
writer.newLine();
|
2012-12-31 02:28:39 +01:00
|
|
|
}
|
2013-04-23 20:09:23 +02:00
|
|
|
|
|
|
|
writer.write(FieldParser.parse(field));
|
|
|
|
writer.newLine();
|
2012-11-23 20:59:12 +01:00
|
|
|
}
|
2019-05-03 22:08:56 +02:00
|
|
|
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
|
2012-11-23 21:00:35 +01:00
|
|
|
e.printStackTrace();
|
2012-11-23 20:59:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-23 21:00:35 +01:00
|
|
|
writer.close();
|
2012-11-23 20:59:12 +01:00
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 20:09:23 +02:00
|
|
|
/**
|
|
|
|
* Checks if the file ends with space
|
2013-07-21 16:49:55 +02:00
|
|
|
*
|
2013-04-23 20:09:23 +02:00
|
|
|
* @param file File to check
|
|
|
|
* @return If the file ends with space
|
|
|
|
*/
|
|
|
|
public static boolean endsWithSpace(File file) {
|
2016-03-06 20:50:15 +01:00
|
|
|
try (Scanner scanner = new Scanner(file)) {
|
2013-04-23 20:09:23 +02:00
|
|
|
String lastLine = "";
|
|
|
|
|
|
|
|
while (scanner.hasNextLine()) {
|
|
|
|
lastLine = scanner.nextLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastLine.isEmpty();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-23 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Converts a java value to config-compatible value
|
|
|
|
*
|
|
|
|
* @param value Value to parse
|
|
|
|
* @return Parsed output
|
|
|
|
*/
|
|
|
|
public static String parseToConfig(Object value) {
|
2018-10-23 20:04:18 +02:00
|
|
|
return DEFAULT_PARSER.parseToYAML(value);
|
2012-11-23 20:59:12 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Colourises a string (using '&' character)
|
2012-11-29 20:28:16 +01:00
|
|
|
*
|
2012-11-23 21:00:35 +01:00
|
|
|
* @param string String to colourise
|
|
|
|
* @return Colourised string
|
|
|
|
*/
|
|
|
|
public static String getColoured(String string) {
|
|
|
|
return ChatColor.translateAlternateColorCodes('&', string);
|
2012-11-23 20:59:12 +01:00
|
|
|
}
|
2018-10-23 20:04:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a parser
|
|
|
|
* @param name The name of the parser
|
|
|
|
* @param valueParser The parser itself
|
|
|
|
*/
|
|
|
|
public static void registerParser(String name, ValueParser valueParser) {
|
2020-01-29 16:35:04 +01:00
|
|
|
parsers.put(name.toLowerCase(Locale.ROOT), valueParser);
|
2018-10-23 20:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a registered parser
|
|
|
|
* @param name The name of the parser
|
|
|
|
* @return The parser or null if it doesn't exist
|
|
|
|
*/
|
|
|
|
public static ValueParser getParser(String name) {
|
2020-01-29 16:35:04 +01:00
|
|
|
return parsers.get(name.toLowerCase(Locale.ROOT));
|
2018-10-23 20:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the parser that should be used for a field
|
|
|
|
* @param field The field
|
|
|
|
* @return The registered parser if the field has a Parser annotation or the default one
|
|
|
|
*/
|
|
|
|
public static ValueParser getParser(Field field) {
|
|
|
|
ValueParser parser = null;
|
|
|
|
if (field.isAnnotationPresent(Parser.class)) {
|
|
|
|
parser = Configuration.getParser(field.getAnnotation(Parser.class).value());
|
|
|
|
}
|
2019-04-25 19:44:22 +02:00
|
|
|
if (parser == null) {
|
|
|
|
parser = Configuration.getParser(field.getType().getSimpleName());
|
|
|
|
}
|
2020-01-15 22:55:40 +01:00
|
|
|
if (parser == null && field.getType().isEnum()) {
|
|
|
|
parser = Configuration.ENUM_PARSER;
|
|
|
|
}
|
2018-10-23 20:04:18 +02:00
|
|
|
if (parser == null) {
|
|
|
|
parser = Configuration.DEFAULT_PARSER;
|
|
|
|
}
|
|
|
|
return parser;
|
|
|
|
}
|
2012-11-23 20:59:12 +01:00
|
|
|
}
|