[UTIL] AuxFileConfiguration Implementation

Discussion in 'Resources' started by Gopaintman, Dec 2, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Gopaintman

    I've often found out that I needed multiple file that used Bukkit's YAML configuration API. Thus I created this. It is essentially a class to remove the redundancy of creating many separate classes for different files.

    To use (Replace "this" with the name of the class that extends JavaPlugin):
    Code:java
    1. @Override
    2. public void onEnable() {
    3.  
    4. AuxFileUtil util = new AuxFileUtil(this, "example");
    5.  
    6. }


    Class itself:
    Code:java
    1. /**
    2. *
    3. */
    4. package com.katsaroucraft.gopaintman.customwhitelists;
    5.  
    6. import java.io.File;
    7. import java.io.FileInputStream;
    8. import java.io.FileNotFoundException;
    9. import java.io.IOException;
    10. import java.io.InputStream;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. /**
    16. * @author Gopaintman
    17. *
    18. */
    19. public class AuxFileUtil {
    20. JavaPlugin plugin;
    21. FileConfiguration fileConfig;
    22. File file;
    23. String fileName;
    24.  
    25. public AuxFileUtil(JavaPlugin instance, String fileName) {
    26. this.plugin = instance;
    27. this.fileName = fileName;
    28. file = new File(plugin.getDataFolder(), fileName.toLowerCase() + ".yml");
    29. fileConfig = YamlConfiguration.loadConfiguration(file);
    30. //On declaration (should be in onEnable) checks to make sure that files are still there
    31. if(!file.exists()){
    32. try {
    33. file.createNewFile();
    34. } catch (IOException e) {
    35. // TODO Auto-generated catch block
    36. e.printStackTrace();
    37. }
    38. }
    39. }
    40. //For those who are too lazy to write blah.getConfig().set(x,y);
    41. public void setValue(String path, Object value){
    42. fileConfig.set(path, value);
    43. saveConfig();
    44. }
    45. //Method to create new file. Really just there for simplicity
    46. public void createFile(){
    47. if(file.exists()){
    48. return;
    49. }
    50. try {
    51. file.createNewFile();
    52. } catch (IOException e) {
    53. // TODO Auto-generated catch block
    54. e.printStackTrace();
    55. }
    56.  
    57. }
    58. //Reloads config from disk
    59. public void reloadConfig() {
    60. if (file == null) {
    61. file = new File(plugin.getDataFolder(), fileName.toLowerCase()
    62. + ".yml");
    63. this.createFile();
    64. }
    65. fileConfig = YamlConfiguration.loadConfiguration(file);
    66.  
    67. try {
    68. InputStream defConfigStream = new FileInputStream(file);
    69. if (defConfigStream != null) {
    70. YamlConfiguration defConfig = YamlConfiguration
    71. .loadConfiguration(file);
    72. fileConfig.setDefaults(defConfig);
    73. defConfigStream.close();
    74. }
    75. } catch (FileNotFoundException e) {
    76.  
    77. e.printStackTrace();
    78. } catch (IOException e) {
    79.  
    80. e.printStackTrace();
    81. }
    82.  
    83. }
    84. //Saves config and file
    85. public void saveConfig() {
    86. if(fileConfig == null || file == null){
    87. return;
    88. }
    89. try {
    90. fileConfig.save(file);
    91. } catch (IOException e) {
    92. plugin.getLogger().severe("Could not save file: " + fileName.toLowerCase() + ".yml");
    93. e.printStackTrace();
    94. }
    95. }
    96.  
    97. /**
    98.   * @return the fileConfig
    99.   */
    100. public FileConfiguration getConfig() {
    101.  
    102. return YamlConfiguration.loadConfiguration(file);
    103.  
    104. }
    105.  
    106. /**
    107.   * @return the file
    108.   */
    109. public File getFile() {
    110. return new File(this.plugin.getDataFolder(), fileName.toLowerCase() + ".yml");
    111. }
    112.  
    113. /**
    114.   * @return the fileName currently held in RAM. May not be real filename (may not be in lowercase)
    115.   */
    116. public String getFileName() {
    117. //Should be the name of the file on the disk just not necessarily in lowercase
    118. return fileName;
    119. }
    120. }
    121.  
     
  2. Offline

    Cirno

    Make sure you set "plugin" to null after being disabled...
     
Thread Status:
Not open for further replies.

Share This Page