Skip to content

Commit

Permalink
Merge pull request #6 from taggernation/UPDATE
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
toonystank authored Oct 13, 2022
2 parents a517245 + f98c739 commit 4123339
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<dependency>
<groupId>dev.triumphteam</groupId>
<artifactId>triumph-gui</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
Expand Down Expand Up @@ -188,7 +188,7 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.1.1</version>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>io.papermc</groupId>
Expand Down Expand Up @@ -217,7 +217,7 @@
<dependency>
<groupId>dev.dejvokep</groupId>
<artifactId>boosted-yaml</artifactId>
<version>1.2</version>
<version>1.3</version>
</dependency>
<dependency>
<groupId>io.github.alenalex.adventurelib.spigot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -202,17 +228,23 @@ 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;
}

/**
* Get the given path as a list.
* @param path String
* @return List
*/
public List<String> getStringList(String path) {
return config.getStringList(path);
public @Nullable List<String> getStringList(String path) {
if (config.contains(path)) {
return config.getStringList(path);
}
return null;
}

/**
Expand All @@ -221,8 +253,14 @@ public List<String> 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<String> 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.
Expand Down Expand Up @@ -259,5 +297,4 @@ public double getDouble(String path) {
public String getString(String path) {
return config.getString(path);
}

}

0 comments on commit 4123339

Please sign in to comment.