Last active
February 19, 2021 19:29
-
-
Save AlexProgrammerDE/c1eee25e031b9c9ceef8c1cb7e51b5e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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