Created
April 9, 2018 12:56
-
-
Save DarkSeraphim/9ecc148e635beded4957ae8cc1bb533f to your computer and use it in GitHub Desktop.
Configuration file
This file contains hidden or 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
| public class Config extends YamlConfiguration { | |
| private final File file; | |
| public Config(String name) | |
| throws IOException, InvalidConfigurationException { | |
| this.file = new File(name); | |
| load(file); | |
| loadDefaults(file.toPath()); | |
| } | |
| public Config(Plugin plugin, File file) | |
| throws IOException, InvalidConfigurationException { | |
| this.file = file; | |
| load(file); | |
| Path base = plugin.getDataFolder().toPath(); | |
| loadDefaults(base.relativize(file.toPath()).normalize()); | |
| } | |
| private void loadDefaults(Path path) throws IOException { | |
| Path rel = path.normalize(); | |
| ClassLoader cl = getClass().getClassLoader(); | |
| try (InputStream defaultFile = cl.getResourceAsStream(rel.toString()); | |
| Reader reader = fromInput(defaultFile, StandardCharsets.UTF_8)) { | |
| if (reader == null) { | |
| return; | |
| } | |
| setDefaults(YamlConfiguration.loadConfiguration(reader)); | |
| } | |
| } | |
| private static Reader fromInput(InputStream in, Charset encoding) { | |
| return in == null ? null : new InputStreamReader(in, encoding); | |
| } | |
| public void reload() throws IOException, InvalidConfigurationException { | |
| load(file); | |
| } | |
| public void save() throws IOException { | |
| save(file); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment