Skip to content

Instantly share code, notes, and snippets.

@AlexProgrammerDE
Last active February 19, 2021 19:29
Show Gist options
  • Save AlexProgrammerDE/c1eee25e031b9c9ceef8c1cb7e51b5e6 to your computer and use it in GitHub Desktop.
Save AlexProgrammerDE/c1eee25e031b9c9ceef8c1cb7e51b5e6 to your computer and use it in GitHub Desktop.
package net.pistonmaster.spigotplugin.config;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import com.amihaiemil.eoyaml.extensions.MergedYamlMapping;
import net.pistonmaster.spigotplugin.Main;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
public class ConfigManager {
private final Main plugin;
private MergedYamlMapping mergedYamlMapping;
public ConfigManager(Main plugin) {
this.plugin = plugin;
}
public void create() throws IOException {
createIfAbsent();
mergedYamlMapping = new MergedYamlMapping(getConfig(), getDefaultConfig());
saveConfig(mergedYamlMapping);
}
public YamlMapping getMapping() {
return mergedYamlMapping;
}
private YamlMapping getDefaultConfig() throws IOException {
return Yaml.createYamlInput(getDefaultInput()).readYamlMapping();
}
private YamlMapping getConfig() throws IOException {
return Yaml.createYamlInput(getConfigFile()).readYamlMapping();
}
private void saveConfig(YamlMapping mapping) throws IOException {
Yaml.createYamlPrinter(new FileWriter(getConfigFile())).print(mapping);
}
private void createIfAbsent() throws IOException {
File configFile = getConfigFile();
if (!plugin.getDataFolder().exists() && !plugin.getDataFolder().mkdir()) {
throw new IOException("Could not create data folder!");
}
if (!configFile.exists()) {
Files.copy(getDefaultInput(), configFile.toPath());
}
}
private File getConfigFile() {
return new File(plugin.getDataFolder(), "config.yml");
}
private InputStream getDefaultInput() {
return plugin.getResource("config.yml");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment