Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CustomConfig by rsod, I've made it for myself
- * Usage:
- * CustomConfig cnf = new CustomConfig("directoryname", "filename", this); //directory can be null,
- * then config will be put in plugin's root folder
- * String a = cnf.get().getString("SomeValue"); //read values like with default config
- * cnf.get().set("SomeAnotherValue", a); //set values the same way,
- * cnf.save(); //and don't forget to save it!
- */
- import java.io.File;
- import java.io.IOException;
- import java.util.logging.Level;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- public class CustomConfig {
- private JavaPlugin plugin;
- private String name;
- private String dir;
- public CustomConfig(String dir, String nm, JavaPlugin pl){
- this.plugin = pl;
- this.name = nm + ".yml";
- this.dir = dir;
- if(dir != null) this.name = dir + "/" + this.name;
- }
- private FileConfiguration config = null;
- private File configfile = null;
- public void reload() {
- if (configfile == null) {
- if(dir != null && !new File(dir).exists()){
- new File(dir).mkdir();
- }
- configfile = new File(plugin.getDataFolder(), name);
- }
- config = YamlConfiguration.loadConfiguration(configfile);
- }
- public FileConfiguration get() {
- if (config == null) {
- this.reload();
- }
- return config;
- }
- public void save() {
- if (config == null || configfile == null) {
- return;
- }
- try {
- this.get().save(configfile);
- plugin.getLogger().info("Custom config saved: " + this.name);
- } catch (IOException ex) {
- plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configfile, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement