Advertisement
Timtower

CustomConfig

Mar 6th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.logging.Level;
  5.  
  6. import org.bukkit.configuration.file.FileConfiguration;
  7. import org.bukkit.configuration.file.YamlConfiguration;
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. public class CustomConfig {
  11.  
  12. private final String fileName;
  13. private final JavaPlugin plugin;
  14. private File configFile;
  15. private FileConfiguration fileConfiguration;
  16.  
  17. public CustomConfig(JavaPlugin plugin, String fileName) {
  18. if (plugin == null)
  19. throw new IllegalArgumentException("plugin cannot be null");
  20. if (!plugin.isInitialized())
  21. throw new IllegalArgumentException("plugin must be initiaized");
  22. this.plugin = plugin;
  23. this.fileName = fileName;
  24. File dataFolder = plugin.getDataFolder();
  25. if (dataFolder == null)
  26. throw new IllegalStateException();
  27. this.configFile = new File(plugin.getDataFolder(), fileName);
  28. }
  29.  
  30. public void reloadConfig() {
  31. fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
  32.  
  33. // Look for defaults in the jar
  34. InputStream defConfigStream = plugin.getResource(fileName);
  35. if (defConfigStream != null) {
  36. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  37. fileConfiguration.setDefaults(defConfig);
  38. }
  39. }
  40.  
  41. public FileConfiguration getConfig() {
  42. if (fileConfiguration == null) {
  43. this.reloadConfig();
  44. }
  45. return fileConfiguration;
  46. }
  47.  
  48. public void saveConfig() {
  49. if (fileConfiguration == null || configFile == null) {
  50. return;
  51. } else {
  52. try {
  53. getConfig().save(configFile);
  54. } catch (IOException ex) {
  55. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
  56. }
  57. }
  58. }
  59. public void saveDefaultConfig() {
  60. if (!configFile.exists()) {
  61. this.plugin.saveResource(fileName, false);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement