ChestShop-3/src/main/java/com/Acrobot/Breeze/Configuration/Configuration.java

109 lines
3.1 KiB
Java
Raw Normal View History

2012-11-23 20:59:12 +01:00
package com.Acrobot.Breeze.Configuration;
2013-04-23 20:09:23 +02:00
import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
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;
2013-04-23 20:09:23 +02:00
import java.io.*;
2012-11-23 20:59:12 +01:00
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
2013-04-23 20:09:23 +02:00
import java.util.Scanner;
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 {
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
*/
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()) {
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)) {
2012-11-23 21:00:35 +01:00
field.set(null, ValueParser.parseToJava(config.get(path)));
2012-11-23 20:59:12 +01:00
} else {
2013-04-23 20:09:23 +02:00
if (field.isAnnotationPresent(PrecededBySpace.class)) {
writer.newLine();
}
2013-04-23 20:09:23 +02:00
writer.write(FieldParser.parse(field));
writer.newLine();
2012-11-23 20:59:12 +01:00
}
2012-11-23 21:00:35 +01:00
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
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-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) {
try {
Scanner scanner = new Scanner(file);
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) {
return ValueParser.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
}
}