diff --git a/pom.xml b/pom.xml index 5a07031..17f447d 100644 --- a/pom.xml +++ b/pom.xml @@ -158,7 +158,7 @@ dev.triumphteam triumph-gui - 3.1.2 + 3.1.3 net.kyori @@ -188,7 +188,7 @@ net.kyori adventure-platform-bukkit - 4.1.1 + 4.1.2 io.papermc @@ -217,7 +217,7 @@ dev.dejvokep boosted-yaml - 1.2 + 1.3 io.github.alenalex.adventurelib.spigot diff --git a/src/main/java/com/taggernation/taggernationlib/config/ConfigManager.java b/src/main/java/com/taggernation/taggernationlib/config/ConfigManager.java index f8f6331..45c517c 100644 --- a/src/main/java/com/taggernation/taggernationlib/config/ConfigManager.java +++ b/src/main/java/com/taggernation/taggernationlib/config/ConfigManager.java @@ -18,11 +18,11 @@ package com.taggernation.taggernationlib.config; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -34,7 +34,9 @@ public class ConfigManager { private File file; private FileConfiguration config; private final Plugin plugin; + private final boolean isInFolder; private String configVersion; + private boolean consoleLogger = true; /** * Initializes the Config. @@ -45,6 +47,10 @@ public class ConfigManager { */ public ConfigManager(Plugin plugin, String fileName, boolean force, boolean copy) throws IOException { this.plugin = plugin; + process(plugin,fileName,force,copy); + isInFolder = false; + } + private void process(Plugin plugin, String fileName, boolean force, boolean copy) throws IOException { this.file = new File(plugin.getDataFolder(), fileName); if (copy) { try { @@ -55,7 +61,7 @@ public ConfigManager(Plugin plugin, String fileName, boolean force, boolean copy } if (!file.exists()) { if (file.createNewFile()) { - plugin.getLogger().info("Created new file: " + file.getName()); + if (consoleLogger) plugin.getLogger().info("Created new file: " + file.getName()); } } this.config = YamlConfiguration.loadConfiguration(this.file); @@ -72,20 +78,37 @@ public ConfigManager(Plugin plugin, String fileName, boolean force, boolean copy public ConfigManager(Plugin plugin, String fileName, String path, boolean force, boolean copy) throws IOException { this.plugin = plugin; String filePath = plugin.getDataFolder() + File.separator + path; - this.file = new File(plugin.getDataFolder() + File.separator + path, fileName); + process(plugin, fileName, filePath, force, copy); + isInFolder = true; + } + private void process(Plugin plugin, String fileName, String path, boolean force, boolean copy) throws IOException { + this.file = new File(path, fileName); if (!file.exists()) { - plugin.getLogger().info("creating new File"); - plugin.getLogger().info("Creating folder: " + new File(plugin.getDataFolder() + File.separator + path).mkdirs()); + File file = new File(path); + if (file.mkdirs()) { + if (consoleLogger) plugin.getLogger().info("Created new directory: " + file.getName()); + } if (copy) try { copy(force, path + File.separator + fileName); } catch (IllegalArgumentException e) { e.printStackTrace(); } - else plugin.getLogger().info("Creating file: " + file.createNewFile()); + else { + if (file.createNewFile()) if (consoleLogger) plugin.getLogger().info("Creating file: " + file.getName()); + } } this.config = YamlConfiguration.loadConfiguration(this.file); } + /** + * Set logger of config load events. by default it's enabled + * @param consoleLogger boolean + * @return ConfigManager + */ + public ConfigManager setConsoleLogger(boolean consoleLogger) { + this.consoleLogger = consoleLogger; + return this; + } /** * Set the version of the config. useful for updating the config. * @param configVersion Version of the config @@ -125,10 +148,13 @@ public void save() { /** * Reload the config. * @throws IOException IOException - * @throws InvalidConfigurationException InvalidConfigurationException */ - public void reload() throws IOException, InvalidConfigurationException { - config.load(file); + public void reload() throws IOException { + if (isInFolder) { + process(plugin, file.getName(), file.getParentFile().getName(), false, false); + return; + } + process(plugin, file.getName(), false, false); } /** @@ -202,8 +228,11 @@ public void set(String path, Object value) { * @param path String * @return Object */ - public Object get(String path) { - return config.get(path); + public @Nullable Object get(String path) { + if (config.contains(path)) { + return config.get(path); + } + return null; } /** @@ -211,8 +240,11 @@ public Object get(String path) { * @param path String * @return List */ - public List getStringList(String path) { - return config.getStringList(path); + public @Nullable List getStringList(String path) { + if (config.contains(path)) { + return config.getStringList(path); + } + return null; } /** @@ -221,8 +253,14 @@ public List getStringList(String path) { * @param value String to add to list */ public void addToStringList(String path, String value) { - set(path,getStringList(path).add(value)); - save(); + if (config.contains(path) && config.isList(path)) { + List list = config.getStringList(path); + list.add(value); + config.set(path, list); + save(); + }else { + throw new IllegalArgumentException(path + " does not exist" + " in " + file.getName() + " or is not a list"); + } } /** * Get the given path as a boolean. @@ -259,5 +297,4 @@ public double getDouble(String path) { public String getString(String path) { return config.getString(path); } - }