Plex/server/src/main/java/dev/plex/config/Config.java

123 lines
3.3 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.config;
2020-10-28 03:49:56 +00:00
2021-01-03 07:21:15 +00:00
import dev.plex.Plex;
2022-02-24 04:55:04 +00:00
import dev.plex.util.PlexLog;
2023-03-08 20:26:10 +00:00
import org.bukkit.configuration.file.YamlConfiguration;
2022-01-04 03:04:39 +00:00
import java.io.File;
2022-02-24 04:55:04 +00:00
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
2022-01-04 03:04:39 +00:00
/**
* Creates a custom Config object
*/
2022-03-17 22:16:17 +00:00
public class Config extends YamlConfiguration
{
/**
* The plugin instance
*/
private Plex plugin;
2022-02-24 04:55:04 +00:00
/**
* The File instance
*/
private File file;
/**
* The file name
*/
private String name;
2020-10-28 03:49:56 +00:00
2022-02-24 04:55:04 +00:00
/**
* Whether new entries were added to the file automatically
*/
private boolean added = false;
/**
* Creates a config object
2022-02-22 09:30:23 +00:00
*
* @param plugin The plugin instance
2022-02-22 09:30:23 +00:00
* @param name The file name
*/
2022-03-17 22:16:17 +00:00
public Config(Plex plugin, String name)
{
2020-10-28 03:49:56 +00:00
this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), name);
this.name = name;
2020-10-28 03:49:56 +00:00
2022-03-17 22:16:17 +00:00
if (!file.exists())
{
2020-10-29 00:54:23 +00:00
saveDefault();
2020-10-28 03:49:56 +00:00
}
}
public void load()
{
this.load(true);
}
/**
* Loads the configuration file
*/
2022-03-17 22:16:17 +00:00
public void load(boolean loadFromFile)
{
try
{
if (loadFromFile)
{
YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file);
2022-04-09 00:05:20 +00:00
InputStreamReader internalConfigFileStream = new InputStreamReader(plugin.getResource(name), StandardCharsets.UTF_8);
YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream);
2022-02-24 04:55:04 +00:00
// Gets all the keys inside the internal file and iterates through all of it's key pairs
2022-03-17 22:16:17 +00:00
for (String string : internalYamlConfig.getKeys(true))
{
// Checks if the external file contains the key already.
2022-03-17 22:16:17 +00:00
if (!externalYamlConfig.contains(string))
{
// If it doesn't contain the key, we set the key based off what was found inside the plugin jar
externalYamlConfig.setComments(string, internalYamlConfig.getComments(string));
2022-04-09 00:05:20 +00:00
externalYamlConfig.setInlineComments(string, internalYamlConfig.getInlineComments(string));
externalYamlConfig.set(string, internalYamlConfig.get(string));
PlexLog.log("Setting key: " + string + " in " + this.name + " to the default value(s) since it does not exist!");
added = true;
}
}
2022-03-17 22:16:17 +00:00
if (added)
{
externalYamlConfig.save(file);
PlexLog.log("Saving new file...");
added = false;
2022-02-24 04:55:04 +00:00
}
}
2020-10-28 03:49:56 +00:00
super.load(file);
2022-03-17 22:16:17 +00:00
}
catch (Exception ex)
{
2020-10-28 03:49:56 +00:00
ex.printStackTrace();
}
}
/**
* Saves the configuration file
*/
2022-03-17 22:16:17 +00:00
public void save()
{
try
{
2020-10-28 03:49:56 +00:00
super.save(file);
2022-03-17 22:16:17 +00:00
}
catch (Exception ex)
{
2020-10-28 03:49:56 +00:00
ex.printStackTrace();
}
}
/**
* Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/)
*/
2022-03-17 22:16:17 +00:00
private void saveDefault()
{
plugin.saveResource(name, false);
2020-10-28 03:49:56 +00:00
}
}