| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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 <T extends ConfigurationSerializable> void saveObject(
- String path,
- String node,
- T object
- ) {
- YamlConfiguration cfg = safeLoadConfig(path);
- cfg.set(node, object);
- saveConfig(cfg, path);
- }
- public <T extends ConfigurationSerializable> T loadObject(
- String path,
- String node,
- Class<T> 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 <T extends ConfigurationSerializable> void saveCollection(
- String path,
- String node,
- Collection<T> collection
- ) {
- YamlConfiguration cfg = safeLoadConfig(path);
- List<T> list = new ArrayList<>(collection);
- cfg.set(node, list);
- saveConfig(cfg, path);
- }
- public <T extends ConfigurationSerializable> List<T> loadCollection(
- String path,
- String node,
- Class<T> type
- ) {
- YamlConfiguration cfg = safeLoadConfig(path);
- List<?> rawList = cfg.getList(node);
- if (rawList == null) return new ArrayList<>();
- List<T> result = new ArrayList<>();
- for (Object obj : rawList) {
- if (type.isInstance(obj)) {
- result.add(type.cast(obj));
- }
- }
- return result;
- }
- /* =========================================================
- =============== MAP STORAGE ==============================
- ========================================================= */
- public <K, V extends ConfigurationSerializable> void saveMap(
- String path,
- String node,
- Map<K, V> map,
- Function<K, String> keySerializer
- ) {
- YamlConfiguration cfg = safeLoadConfig(path);
- ConfigurationSection section = cfg.createSection(node);
- for (Map.Entry<K, V> entry : map.entrySet()) {
- String key = keySerializer.apply(entry.getKey());
- section.set(key, entry.getValue());
- }
- saveConfig(cfg, path);
- }
- public <K, V extends ConfigurationSerializable> Map<K, V> loadMap(
- String path,
- String node,
- Function<String, K> keyDeserializer,
- Class<V> type
- ) {
- YamlConfiguration cfg = safeLoadConfig(path);
- ConfigurationSection section = cfg.getConfigurationSection(node);
- if (section == null) return new HashMap<>();
- Map<K, V> 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;
- }
- }
|