Not tested, remove automatic loading of properties

This commit is contained in:
Telesphoreo 2022-04-01 23:04:51 -05:00
parent 1f2eed78de
commit c484f4b2b2

View file

@ -1,19 +1,17 @@
package dev.plex.config; package dev.plex.config;
import dev.plex.module.PlexModule; import dev.plex.module.PlexModule;
import dev.plex.util.PlexLog; import java.io.File;
import org.apache.logging.log4j.Level; import java.io.IOException;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
/** /**
* Creates a custom Config object * Creates a custom Config object
*/ */
public class ModuleConfig extends YamlConfiguration { public class ModuleConfig extends YamlConfiguration
{
/** /**
* The plugin instance * The plugin instance
*/ */
@ -29,71 +27,40 @@ public class ModuleConfig extends YamlConfiguration {
*/ */
private String name; private String name;
/**
* Whether new entries were added to the file automatically
*/
private boolean added = false;
/** /**
* Creates a config object * Creates a config object
* *
* @param module The module instance * @param module The module instance
* @param name The file name * @param name The file name
*/ */
public ModuleConfig(PlexModule module, String name) { public ModuleConfig(PlexModule module, String name)
{
this.module = module; this.module = module;
this.file = new File(module.getDataFolder(), name); this.file = new File(module.getDataFolder(), name);
this.name = name; this.name = name;
if (!file.exists()) { if (!file.exists())
{
saveDefault(); saveDefault();
} }
} }
public void load() { public void load() throws IOException, InvalidConfigurationException
this.load(true); {
} super.load(file);
/**
* Loads the configuration file
*/
public void load(boolean loadFromFile) {
try {
if (loadFromFile) {
YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file);
InputStreamReader internalConfigFileStream = new InputStreamReader(module.getResource(name), StandardCharsets.UTF_8);
YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream);
// Gets all the keys inside the internal file and iterates through all of it's key pairs
for (String string : internalYamlConfig.getKeys(true)) {
// Checks if the external file contains the key already.
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));
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;
}
}
if (added) {
externalYamlConfig.save(file);
PlexLog.log("Saving new file...");
added = false;
}
}
super.load(file);
} catch (Exception ex) {
ex.printStackTrace();
}
} }
/** /**
* Saves the configuration file * Saves the configuration file
*/ */
public void save() { public void save()
try { {
try
{
super.save(file); super.save(file);
} catch (Exception ex) { }
catch (Exception ex)
{
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@ -101,10 +68,14 @@ public class ModuleConfig extends YamlConfiguration {
/** /**
* Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/) * Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/)
*/ */
private void saveDefault() { private void saveDefault()
try { {
try
{
Files.copy(module.getClass().getResourceAsStream("/" + name), this.file.toPath()); Files.copy(module.getClass().getResourceAsStream("/" + name), this.file.toPath());
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
/*if (name == null || name.equals("")) { /*if (name == null || name.equals("")) {