Skip to content

Instantly share code, notes, and snippets.

@bm
Last active September 23, 2019 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bm/8392170 to your computer and use it in GitHub Desktop.
Save bm/8392170 to your computer and use it in GitHub Desktop.
package com.brysonm;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
public class YMLFactory {
public static YML buildYML(String name, JavaPlugin main) {
YML yml = new YML(name, main);
return yml;
}
public static class YML {
private FileConfiguration config;
private File file;
private String name;
private JavaPlugin main;
private String path;
public YML(String name, JavaPlugin main) {
this.name = name;
this.main = main;
this.path = this.name + ".yml";
this.file = new File(main.getDataFolder(), path);
}
public FileConfiguration getConfig() {
if(config == null) {
reloadConfig();
}
return config;
}
public void saveConfig() {
if(config == null || file == null) {
return;
}
try {
getConfig().save(file);
} catch(IOException ex) {
main.getLogger().log(Level.SEVERE, "Could not save " + name + " to file", ex);
}
}
public void reloadConfig() {
if(file == null) {
file = new File(main.getDataFolder(), path);
}
config = YamlConfiguration.loadConfiguration(file);
InputStream fileStream = main.getResource(path);
if(fileStream != null) {
YamlConfiguration configStream = YamlConfiguration.loadConfiguration(fileStream);
config.setDefaults(configStream);
}
}
public void saveDefaultConfig() {
if(file == null) {
file = new File(main.getDataFolder(), path);
}
if(!file.exists()) {
main.saveResource(path, false);
}
}
}
}
@HyperPlay
Copy link

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment