ConfigLoader.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package me.lethunderhawk.fluxapi.util.config;
  2. import org.bukkit.configuration.ConfigurationSection;
  3. import org.bukkit.configuration.file.YamlConfiguration;
  4. import org.bukkit.configuration.serialization.ConfigurationSerializable;
  5. import org.bukkit.plugin.java.JavaPlugin;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.*;
  9. import java.util.function.Function;
  10. public final class ConfigLoader {
  11. private final JavaPlugin plugin;
  12. private final File dataFolder;
  13. public ConfigLoader(JavaPlugin plugin) {
  14. this.plugin = plugin;
  15. this.dataFolder = plugin.getDataFolder();
  16. if (!dataFolder.exists()) {
  17. dataFolder.mkdirs();
  18. }
  19. }
  20. /* =========================================================
  21. =============== BASIC FILE HANDLING =====================
  22. ========================================================= */
  23. public File getFile(String path) {
  24. File file = new File(dataFolder, path.endsWith(".yml") ? path : path + ".yml");
  25. if (!file.getParentFile().exists()) {
  26. file.getParentFile().mkdirs();
  27. }
  28. return file;
  29. }
  30. /**
  31. * @Deprecated since 1.2.5
  32. * @param path
  33. * @return
  34. */
  35. public YamlConfiguration loadConfig(String path) {
  36. File file = getFile(path);
  37. return YamlConfiguration.loadConfiguration(file);
  38. }
  39. public YamlConfiguration safeLoadConfig(String path) {
  40. File file = getFile(path);
  41. if(!file.exists())
  42. return new YamlConfiguration();
  43. try {
  44. return YamlConfiguration.loadConfiguration(file);
  45. } catch(Exception e) {
  46. plugin.getLogger().warning("Failed to load config " + file.getName() + ", creating new one.");
  47. return new YamlConfiguration();
  48. }
  49. }
  50. public void saveConfig(YamlConfiguration cfg, String path) {
  51. File file = getFile(path);
  52. try {
  53. cfg.save(file);
  54. } catch (IOException e) {
  55. throw new RuntimeException("Could not save file: " + file.getName(), e);
  56. }
  57. }
  58. /* =========================================================
  59. =============== SINGLE OBJECT ============================
  60. ========================================================= */
  61. public <T extends ConfigurationSerializable> void saveObject(
  62. String path,
  63. String node,
  64. T object
  65. ) {
  66. YamlConfiguration cfg = safeLoadConfig(path);
  67. cfg.set(node, object);
  68. saveConfig(cfg, path);
  69. }
  70. public <T extends ConfigurationSerializable> T loadObject(
  71. String path,
  72. String node,
  73. Class<T> type
  74. ) {
  75. YamlConfiguration cfg = safeLoadConfig(path);
  76. Object obj = cfg.get(node);
  77. if(obj == null){
  78. System.out.println("Node '" + node + "' not found.");
  79. return null;
  80. }
  81. if (!type.isInstance(obj)) {
  82. throw new IllegalStateException(
  83. "Expected type " + type.getName() +
  84. " but got " + obj.getClass().getName()
  85. );
  86. }
  87. return type.cast(obj);
  88. }
  89. /* =========================================================
  90. =============== COLLECTION ===============================
  91. ========================================================= */
  92. public <T extends ConfigurationSerializable> void saveCollection(
  93. String path,
  94. String node,
  95. Collection<T> collection
  96. ) {
  97. YamlConfiguration cfg = safeLoadConfig(path);
  98. List<T> list = new ArrayList<>(collection);
  99. cfg.set(node, list);
  100. saveConfig(cfg, path);
  101. }
  102. public <T extends ConfigurationSerializable> List<T> loadCollection(
  103. String path,
  104. String node,
  105. Class<T> type
  106. ) {
  107. YamlConfiguration cfg = safeLoadConfig(path);
  108. List<?> rawList = cfg.getList(node);
  109. if (rawList == null) return new ArrayList<>();
  110. List<T> result = new ArrayList<>();
  111. for (Object obj : rawList) {
  112. if (type.isInstance(obj)) {
  113. result.add(type.cast(obj));
  114. }
  115. }
  116. return result;
  117. }
  118. /* =========================================================
  119. =============== MAP STORAGE ==============================
  120. ========================================================= */
  121. public <K, V extends ConfigurationSerializable> void saveMap(
  122. String path,
  123. String node,
  124. Map<K, V> map,
  125. Function<K, String> keySerializer
  126. ) {
  127. YamlConfiguration cfg = safeLoadConfig(path);
  128. ConfigurationSection section = cfg.createSection(node);
  129. for (Map.Entry<K, V> entry : map.entrySet()) {
  130. String key = keySerializer.apply(entry.getKey());
  131. section.set(key, entry.getValue());
  132. }
  133. saveConfig(cfg, path);
  134. }
  135. public <K, V extends ConfigurationSerializable> Map<K, V> loadMap(
  136. String path,
  137. String node,
  138. Function<String, K> keyDeserializer,
  139. Class<V> type
  140. ) {
  141. YamlConfiguration cfg = safeLoadConfig(path);
  142. ConfigurationSection section = cfg.getConfigurationSection(node);
  143. if (section == null) return new HashMap<>();
  144. Map<K, V> result = new HashMap<>();
  145. for (String key : section.getKeys(false)) {
  146. Object obj = section.get(key);
  147. if (type.isInstance(obj)) {
  148. result.put(
  149. keyDeserializer.apply(key),
  150. type.cast(obj)
  151. );
  152. }
  153. }
  154. return result;
  155. }
  156. }