package me.lethunderhawk.fluxapi.util.config; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; import java.util.*; import java.util.function.Function; public final class ConfigLoader { private final JavaPlugin plugin; private final File dataFolder; public ConfigLoader(JavaPlugin plugin) { this.plugin = plugin; this.dataFolder = plugin.getDataFolder(); if (!dataFolder.exists()) { dataFolder.mkdirs(); } } /* ========================================================= =============== BASIC FILE HANDLING ===================== ========================================================= */ public File getFile(String path) { File file = new File(dataFolder, path.endsWith(".yml") ? path : path + ".yml"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } return file; } /** * @Deprecated since 1.2.5 * @param path * @return */ public YamlConfiguration loadConfig(String path) { File file = getFile(path); return YamlConfiguration.loadConfiguration(file); } public YamlConfiguration safeLoadConfig(String path) { File file = getFile(path); if(!file.exists()) return new YamlConfiguration(); try { return YamlConfiguration.loadConfiguration(file); } catch(Exception e) { plugin.getLogger().warning("Failed to load config " + file.getName() + ", creating new one."); return new YamlConfiguration(); } } public void saveConfig(YamlConfiguration cfg, String path) { File file = getFile(path); try { cfg.save(file); } catch (IOException e) { throw new RuntimeException("Could not save file: " + file.getName(), e); } } /* ========================================================= =============== SINGLE OBJECT ============================ ========================================================= */ public void saveObject( String path, String node, T object ) { YamlConfiguration cfg = safeLoadConfig(path); cfg.set(node, object); saveConfig(cfg, path); } public T loadObject( String path, String node, Class type ) { YamlConfiguration cfg = safeLoadConfig(path); Object obj = cfg.get(node); if(obj == null){ System.out.println("Node '" + node + "' not found."); return null; } if (!type.isInstance(obj)) { throw new IllegalStateException( "Expected type " + type.getName() + " but got " + obj.getClass().getName() ); } return type.cast(obj); } /* ========================================================= =============== COLLECTION =============================== ========================================================= */ public void saveCollection( String path, String node, Collection collection ) { YamlConfiguration cfg = safeLoadConfig(path); List list = new ArrayList<>(collection); cfg.set(node, list); saveConfig(cfg, path); } public List loadCollection( String path, String node, Class type ) { YamlConfiguration cfg = safeLoadConfig(path); List rawList = cfg.getList(node); if (rawList == null) return new ArrayList<>(); List result = new ArrayList<>(); for (Object obj : rawList) { if (type.isInstance(obj)) { result.add(type.cast(obj)); } } return result; } /* ========================================================= =============== MAP STORAGE ============================== ========================================================= */ public void saveMap( String path, String node, Map map, Function keySerializer ) { YamlConfiguration cfg = safeLoadConfig(path); ConfigurationSection section = cfg.createSection(node); for (Map.Entry entry : map.entrySet()) { String key = keySerializer.apply(entry.getKey()); section.set(key, entry.getValue()); } saveConfig(cfg, path); } public Map loadMap( String path, String node, Function keyDeserializer, Class type ) { YamlConfiguration cfg = safeLoadConfig(path); ConfigurationSection section = cfg.getConfigurationSection(node); if (section == null) return new HashMap<>(); Map result = new HashMap<>(); for (String key : section.getKeys(false)) { Object obj = section.get(key); if (type.isInstance(obj)) { result.put( keyDeserializer.apply(key), type.cast(obj) ); } } return result; } }