mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2026-02-19 02:12:58 +01:00
Revert "Disabling mcMMO when the config breaks is dumb" This reverts commit86e7bfbf89. Revert "Config validation rewrite part 1" This reverts commit16e90da8fd. Revert "Update changelog" This reverts commit0ccd89fad4.
105 lines
3.3 KiB
Java
105 lines
3.3 KiB
Java
package com.gmail.nossr50.config;
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.io.File;
|
|
import java.util.List;
|
|
|
|
public abstract class ConfigLoader {
|
|
protected final File configFile;
|
|
protected final @NotNull File dataFolder;
|
|
protected String fileName;
|
|
protected YamlConfiguration config;
|
|
|
|
public ConfigLoader(String relativePath, String fileName, @NotNull File dataFolder) {
|
|
this.fileName = fileName;
|
|
this.dataFolder = dataFolder;
|
|
configFile = new File(dataFolder, relativePath + File.separator + fileName);
|
|
loadFile();
|
|
}
|
|
|
|
public ConfigLoader(String fileName, @NotNull File dataFolder) {
|
|
this.fileName = fileName;
|
|
this.dataFolder = dataFolder;
|
|
configFile = new File(dataFolder, fileName);
|
|
loadFile();
|
|
}
|
|
|
|
@Deprecated
|
|
public ConfigLoader(String relativePath, String fileName) {
|
|
this.fileName = fileName;
|
|
configFile = new File(mcMMO.p.getDataFolder(), relativePath + File.separator + fileName);
|
|
this.dataFolder = mcMMO.p.getDataFolder();
|
|
loadFile();
|
|
}
|
|
|
|
@Deprecated
|
|
public ConfigLoader(String fileName) {
|
|
this.fileName = fileName;
|
|
configFile = new File(mcMMO.p.getDataFolder(), fileName);
|
|
this.dataFolder = mcMMO.p.getDataFolder();
|
|
loadFile();
|
|
}
|
|
|
|
protected void loadFile() {
|
|
if (!configFile.exists()) {
|
|
mcMMO.p.getLogger().info("Creating mcMMO " + fileName + " File...");
|
|
|
|
try {
|
|
mcMMO.p.saveResource(fileName, false); // Normal files
|
|
} catch (IllegalArgumentException ex) {
|
|
mcMMO.p.saveResource(configFile.getParentFile().getName() + File.separator + fileName, false); // Mod files
|
|
}
|
|
} else {
|
|
mcMMO.p.getLogger().info("Loading mcMMO " + fileName + " File...");
|
|
}
|
|
|
|
config = YamlConfiguration.loadConfiguration(configFile);
|
|
}
|
|
|
|
protected abstract void loadKeys();
|
|
|
|
protected boolean validateKeys() {
|
|
return true;
|
|
}
|
|
|
|
protected boolean noErrorsInConfig(List<String> issues) {
|
|
for (String issue : issues) {
|
|
mcMMO.p.getLogger().warning(issue);
|
|
}
|
|
|
|
return issues.isEmpty();
|
|
}
|
|
|
|
protected void validate() {
|
|
if (validateKeys()) {
|
|
mcMMO.p.debug("No errors found in " + fileName + "!");
|
|
} else {
|
|
mcMMO.p.getLogger().warning("Errors were found in " + fileName + "! mcMMO was disabled!");
|
|
mcMMO.p.getServer().getPluginManager().disablePlugin(mcMMO.p);
|
|
mcMMO.p.noErrorsInConfigFiles = false;
|
|
}
|
|
}
|
|
|
|
public File getFile() {
|
|
return configFile;
|
|
}
|
|
|
|
public void backup() {
|
|
mcMMO.p.getLogger().warning("You are using an old version of the " + fileName + " file.");
|
|
mcMMO.p.getLogger().warning("Your old file has been renamed to " + fileName + ".old and has been replaced by an updated version.");
|
|
|
|
configFile.renameTo(new File(configFile.getPath() + ".old"));
|
|
|
|
if (mcMMO.p.getResource(fileName) != null) {
|
|
mcMMO.p.saveResource(fileName, true);
|
|
}
|
|
|
|
mcMMO.p.getLogger().warning("Reloading " + fileName + " with new values...");
|
|
loadFile();
|
|
loadKeys();
|
|
}
|
|
}
|