Solved Multiple Configuration Files

Discussion in 'Plugin Development' started by JPG2000, Oct 13, 2013.

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

    JPG2000

    Hello! In bukkit, theres one thing that always confusses me: Multiple Configuration Files. Now, I tried alot of tutorials. The one on the wiki, did not work. Now I tried one from pogostick29dev's channel, and its not working. Its going into the IOexception every time. I have modified the code a little bit, because before, It didn't even generate:

    My Code:
    Code:java
    1. package me.JPG.Administration.Utills;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import me.JPG.Administration.Core.Main;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.plugin.Plugin;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13.  
    14. public class ConfigurationManager {
    15.  
    16. private Main main;
    17.  
    18. public ConfigurationManager(Main x) {
    19. main = x;
    20. }
    21.  
    22. Plugin p;
    23.  
    24. FileConfiguration monitour;
    25. File mfile;
    26.  
    27. FileConfiguration report;
    28. File rfile;
    29.  
    30. public void setup(Plugin p) {
    31. mfile = new File(p.getDataFolder(), "monitour.yml");
    32. if (!mfile.exists()) {
    33. try {
    34. mfile.createNewFile();
    35. }
    36. catch (IOException e) {
    37. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create monitour.yml!");
    38. }
    39. }
    40.  
    41. monitour = YamlConfiguration.loadConfiguration(mfile);
    42.  
    43. rfile = new File(p.getDataFolder(), "report.yml");
    44.  
    45. if (!rfile.exists()) {
    46. try {
    47. rfile.createNewFile();
    48. }
    49. catch (IOException e) {
    50. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create report.yml!");
    51. }
    52. }
    53.  
    54. report = YamlConfiguration.loadConfiguration(rfile);
    55. }
    56.  
    57. public FileConfiguration getReport() {
    58. return report;
    59. }
    60.  
    61. public void saveReport() {
    62. try {
    63. report.save(rfile);
    64. }
    65. catch (IOException e) {
    66. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save report.yml!"); //REPORT == data
    67. }
    68. }
    69.  
    70. public void reloadReport() {
    71. report = YamlConfiguration.loadConfiguration(rfile);
    72. }
    73.  
    74. public FileConfiguration getMonitour() {
    75. return monitour;
    76. }
    77.  
    78. public void saveMonitour() {
    79. try {
    80. monitour.save(mfile);
    81. }
    82. catch (IOException e) {
    83. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
    84. }
    85. }
    86.  
    87. public void reloadMonitour() {
    88. monitour = YamlConfiguration.loadConfiguration(mfile);
    89. }
    90.  
    91. public PluginDescriptionFile getDesc() {
    92. return p.getDescription();
    93. }
    94. }



    Example Code (No worky):
    Code:java
    1. package me.pogostick29.teleport;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10. import org.bukkit.plugin.Plugin;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12.  
    13. public class SettingsManager {
    14.  
    15. private SettingsManager() { }
    16.  
    17. static SettingsManager instance = new SettingsManager();
    18.  
    19. public static SettingsManager getInstance() {
    20. return instance;
    21. }
    22.  
    23. Plugin p;
    24.  
    25. FileConfiguration config;
    26. File cfile;
    27.  
    28. FileConfiguration data;
    29. File dfile;
    30.  
    31. public void setup(Plugin p) {
    32. cfile = new File(p.getDataFolder(), "config.yml");
    33. config = p.getConfig();
    34. //config.options().copyDefaults(true);
    35. //saveConfig();
    36.  
    37. if (!p.getDataFolder().exists()) {
    38. p.getDataFolder().mkdir();
    39. }
    40.  
    41. dfile = new File(p.getDataFolder(), "data.yml");
    42.  
    43. if (!dfile.exists()) {
    44. try {
    45. dfile.createNewFile();
    46. }
    47. catch (IOException e) {
    48. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
    49. }
    50. }
    51.  
    52. data = YamlConfiguration.loadConfiguration(dfile);
    53. }
    54.  
    55. public FileConfiguration getData() {
    56. return data;
    57. }
    58.  
    59. public void saveData() {
    60. try {
    61. data.save(dfile);
    62. }
    63. catch (IOException e) {
    64. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
    65. }
    66. }
    67.  
    68. public void reloadData() {
    69. data = YamlConfiguration.loadConfiguration(dfile);
    70. }
    71.  
    72. public FileConfiguration getConfig() {
    73. return config;
    74. }
    75.  
    76. public void saveConfig() {
    77. try {
    78. config.save(cfile);
    79. }
    80. catch (IOException e) {
    81. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
    82. }
    83. }
    84.  
    85. public void reloadConfig() {
    86. config = YamlConfiguration.loadConfiguration(cfile);
    87. }
    88.  
    89. public PluginDescriptionFile getDesc() {
    90. return p.getDescription();
    91. }
    92. }



    Now, in my code, like I said, the console prints out the Can't save files for both of them. Heres my on enable:
    Code:java
    1. public ConfigurationManager cm = new ConfigurationManager(this);
    2.  
    3. @Override
    4. public void onEnable() {
    5. p = this;
    6.  
    7. this.cm.setup(p);
    8. }
     
  2. Offline

    MrSparkzz

  3. Offline

    JPG2000

    MrSparkzz No stack trace. Like I said, it does the IOE exception, and prints out "Cant load file blabla".
     
  4. Offline

    MrSparkzz

    JPG2000
    Add this above line 31 in ConfigurationManager
    Code:java
    1.  
    2. if (!plugin.getDataFolder().exists()) {
    3. plugin.getDataFolder().mkdir();
    4. }
    5.  


    JPG2000
    Please set this thread to Solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    Ultimate_n00b and JPG2000 like this.
  5. Offline

    JPG2000

    MrSparkzz It works (generates) thanks!!!!!!! :D
     
Thread Status:
Not open for further replies.

Share This Page