Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created April 9, 2018 12:56
Show Gist options
  • Save DarkSeraphim/9ecc148e635beded4957ae8cc1bb533f to your computer and use it in GitHub Desktop.
Save DarkSeraphim/9ecc148e635beded4957ae8cc1bb533f to your computer and use it in GitHub Desktop.
Configuration file
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