Advertisement
Guest User

UtilConfig

a guest
Apr 26th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. package me.FinestDev.Project;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.lang.annotation.ElementType;
  6. import java.lang.annotation.Retention;
  7. import java.lang.annotation.RetentionPolicy;
  8. import java.lang.annotation.Target;
  9. import java.lang.reflect.Field;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.lang.reflect.Modifier;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Map.Entry;
  17.  
  18. import org.bukkit.Bukkit;
  19. import org.bukkit.Location;
  20. import org.bukkit.configuration.ConfigurationSection;
  21. import org.bukkit.configuration.InvalidConfigurationException;
  22. import org.bukkit.configuration.file.YamlConfiguration;
  23. import org.bukkit.util.Vector;
  24. import org.json.simple.JSONObject;
  25. import org.json.simple.parser.JSONParser;
  26. import org.json.simple.parser.ParseException;
  27.  
  28. import com.google.common.base.Joiner;
  29.  
  30.  
  31. public class UtilConfig {
  32.  
  33. private static final transient char DEFAULT_SEPARATOR = '_';
  34. private static final transient String LINE_SEPARATOR = System.lineSeparator();
  35. private static final transient String TEMP_CONFIG_SECTION = "temp";
  36. public static final transient HashMap<Class<?>, Class<?>> PRIMITIVES_CLASS = new HashMap<Class<?>, Class<?>>() {
  37. private static final long serialVersionUID = 1L; {
  38. put(int.class, Integer.class);
  39. put(long.class, Long.class);
  40. put(double.class, Double.class);
  41. put(float.class, Float.class);
  42. put(boolean.class, Boolean.class);
  43. put(byte.class, Byte.class);
  44. put(void.class, Void.class);
  45. put(short.class, Short.class);
  46. }
  47. };
  48.  
  49. private transient File configFile;
  50. private transient List<String> header;
  51.  
  52. protected UtilConfig(final File configFile) {
  53. this.configFile = configFile;
  54. }
  55.  
  56. protected UtilConfig(final File configFile, final List<String> header) {
  57. this.configFile = configFile;
  58. this.header = header;
  59. }
  60.  
  61. public final void load() throws InvalidConfigurationException {
  62. try {
  63. final YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
  64. for(final Field field : getClass().getFields()) {
  65. loadField(field, getFieldName(field), config);
  66. }
  67. saveConfig(config);
  68. }
  69. catch(final Exception ex) {
  70. throw new InvalidConfigurationException(ex);
  71. }
  72. }
  73.  
  74. public final void save() throws InvalidConfigurationException {
  75. try {
  76. final YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
  77. for(final Field field : getClass().getFields()) {
  78. saveField(field, getFieldName(field), config);
  79. }
  80. saveConfig(config);
  81. }
  82. catch(final Exception ex) {
  83. throw new InvalidConfigurationException(ex);
  84. }
  85. }
  86.  
  87. private final String getFieldName(final Field field) {
  88. final ConfigOptions options = field.getAnnotation(ConfigOptions.class);
  89. return (options == null ? field.getName().replace(DEFAULT_SEPARATOR, '.') : options.name());
  90. }
  91.  
  92. private final void saveConfig(final YamlConfiguration config) throws IOException {
  93. if(header != null && header.size() > 0) {
  94. config.options().header(Joiner.on(LINE_SEPARATOR).join(header));
  95. }
  96. config.save(configFile);
  97. }
  98.  
  99. private final void loadField(final Field field, final String name, final YamlConfiguration config) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, ParseException {
  100. if(Modifier.isTransient(field.getModifiers())) {
  101. return;
  102. }
  103. final Object configValue = config.get(getFieldName(field));
  104. if(configValue == null) {
  105. saveField(field, name, config);
  106. }
  107. else {
  108. field.set(this, deserializeObject(field.getType(), configValue));
  109. }
  110. }
  111.  
  112.  
  113. private final void saveField(final Field field, final String name, final YamlConfiguration config) throws IllegalAccessException {
  114. if(Modifier.isTransient(field.getModifiers())) {
  115. return;
  116. }
  117. config.set(name, serializeObject(field.get(this), config));
  118. }
  119.  
  120.  
  121. @SuppressWarnings({"unchecked", "rawtypes"})
  122. private final Object deserializeObject(final Class<?> clazz, final Object object) throws ParseException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  123. if(PRIMITIVES_CLASS.containsValue(clazz) || clazz.isPrimitive()) {
  124. return PRIMITIVES_CLASS.get(clazz).getMethod("valueOf", String.class).invoke(this, object.toString());
  125. }
  126. if(clazz.isEnum() || object instanceof Enum<?>) {
  127. return Enum.valueOf((Class<? extends Enum>)clazz, object.toString());
  128. }
  129. if(Map.class.isAssignableFrom(clazz) || object instanceof Map) {
  130. final ConfigurationSection section = (ConfigurationSection)object;
  131. final Map<Object, Object> map = new HashMap<Object, Object>();
  132. for(final String key : section.getKeys(false)) {
  133. final Object value = section.get(key);
  134. map.put(key, deserializeObject(value.getClass(), value));
  135. }
  136. return map;
  137. }
  138. if(List.class.isAssignableFrom(clazz) || object instanceof List) {
  139. final List<Object> result = new ArrayList<Object>();
  140. for(final Object value : (List<?>)object) {
  141. result.add(deserializeObject(value.getClass(), value));
  142. }
  143. return result;
  144. }
  145. if(Location.class.isAssignableFrom(clazz) || object instanceof Location) {
  146. final JSONObject jsonObject = (JSONObject)new JSONParser().parse(object.toString());
  147. return new Location(Bukkit.getWorld(jsonObject.get("world").toString()), Double.parseDouble(jsonObject.get("x").toString()), Double.parseDouble(jsonObject.get("y").toString()), Double.parseDouble(jsonObject.get("z").toString()), Float.parseFloat(jsonObject.get("yaw").toString()), Float.parseFloat(jsonObject.get("pitch").toString()));
  148. }
  149. if(Vector.class.isAssignableFrom(clazz) || object instanceof Vector) {
  150. final JSONObject jsonObject = (JSONObject)new JSONParser().parse(object.toString());
  151. return new Vector(Double.parseDouble(jsonObject.get("x").toString()), Double.parseDouble(jsonObject.get("y").toString()), Double.parseDouble(jsonObject.get("z").toString()));
  152. }
  153. return object.toString();
  154. }
  155.  
  156. @SuppressWarnings("unchecked")
  157. private final Object serializeObject(final Object object, final YamlConfiguration config) {
  158. if(object instanceof Enum) {
  159. return ((Enum<?>)object).name();
  160. }
  161. if(object instanceof Map) {
  162. final ConfigurationSection section = config.createSection(TEMP_CONFIG_SECTION);
  163. for(final Entry<?, ?> entry : ((Map<?, ?>)object).entrySet()) {
  164. section.set(entry.getKey().toString(), serializeObject(entry.getValue(), config));
  165. }
  166. config.set(TEMP_CONFIG_SECTION, null);
  167. return section;
  168. }
  169. if(object instanceof List) {
  170. final List<Object> result = new ArrayList<Object>();
  171. for(final Object value : (List<?>)object) {
  172. result.add(serializeObject(value, config));
  173. }
  174. return result;
  175. }
  176. if(object instanceof Location) {
  177. final Location location = (Location)object;
  178. final JSONObject jsonObject = new JSONObject();
  179. jsonObject.put("x", location.getX());
  180. jsonObject.put("y", location.getY());
  181. jsonObject.put("z", location.getZ());
  182. jsonObject.put("yaw", location.getYaw());
  183. jsonObject.put("pitch", location.getPitch());
  184. return jsonObject.toJSONString();
  185. }
  186. if(object instanceof Vector) {
  187. final Vector vector = (Vector)object;
  188. final JSONObject jsonObject = new JSONObject();
  189. jsonObject.put("x", vector.getX());
  190. jsonObject.put("y", vector.getY());
  191. jsonObject.put("z", vector.getZ());
  192. return jsonObject.toJSONString();
  193. }
  194. return object.toString();
  195. }
  196.  
  197.  
  198. public final List<String> getHeader() {
  199. return header;
  200. }
  201.  
  202. public final File getFile() {
  203. return configFile;
  204. }
  205. public final void setHeader(final List<String> header) {
  206. this.header = header;
  207. }
  208. public final void setFile(final File configFile) {
  209. this.configFile = configFile;
  210. }
  211.  
  212. @Retention(RetentionPolicy.RUNTIME)
  213. @Target(ElementType.FIELD)
  214. protected @interface ConfigOptions {
  215.  
  216.  
  217. public String name();
  218.  
  219. }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement