Advertisement
rsod

CustomConfig

May 23rd, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /* CustomConfig by rsod, I've made it for myself
  2. * Usage:
  3. * CustomConfig cnf = new CustomConfig("directoryname", "filename", this); //directory can be null,
  4. * then config will be put in plugin's root folder
  5. * String a = cnf.get().getString("SomeValue"); //read values like with default config
  6. * cnf.get().set("SomeAnotherValue", a); //set values the same way,
  7. * cnf.save(); //and don't forget to save it!
  8. */
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.logging.Level;
  13.  
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17.  
  18. public class CustomConfig {
  19. private JavaPlugin plugin;
  20. private String name;
  21. private String dir;
  22.  
  23. public CustomConfig(String dir, String nm, JavaPlugin pl){
  24. this.plugin = pl;
  25. this.name = nm + ".yml";
  26. this.dir = dir;
  27. if(dir != null) this.name = dir + "/" + this.name;
  28. }
  29.  
  30. private FileConfiguration config = null;
  31. private File configfile = null;
  32. public void reload() {
  33. if (configfile == null) {
  34. if(dir != null && !new File(dir).exists()){
  35. new File(dir).mkdir();
  36. }
  37. configfile = new File(plugin.getDataFolder(), name);
  38. }
  39. config = YamlConfiguration.loadConfiguration(configfile);
  40. }
  41.  
  42. public FileConfiguration get() {
  43. if (config == null) {
  44. this.reload();
  45. }
  46. return config;
  47. }
  48.  
  49. public void save() {
  50. if (config == null || configfile == null) {
  51. return;
  52. }
  53. try {
  54. this.get().save(configfile);
  55. plugin.getLogger().info("Custom config saved: " + this.name);
  56. } catch (IOException ex) {
  57. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configfile, ex);
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement