Files

Discussion in 'Plugin Development' started by messageofdeath, Dec 8, 2013.

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

    messageofdeath

    I have been working for files for the longest time now, and this extremely weird bug has come up.

    1. I copy the file from jar into the "Plugin Folder"
    2. I load my FileConfiguration with YamlConfiguration
    3. I check if anything is in it, It says there's nothing in it by using this.config.getSection("")
    4. I checked the file (The entire configuration is there.
    5. I set a value with set("Test", "test");
    6. I go back and check and the entire file was erased except the Test:test
    7. I check if anything is in it, It says the only value now is what I have set.

    I have no idea what is going on at all.

    My YamlDatabase: http://pastebin.com/EmH8t87J
    My Configuration: http://pastebin.com/5bBMNfPZ

    Thanks in advance.
     
  2. Offline

    Gopaintman

    You may be overriding the current default values somehow.
    When you set the value, what do you use to save it?

    From what I understand you want to have a class manage on each new instance a new config file. I've got something similar.

    Code:java
    1. /**
    2. *
    3. */
    4. package com.katsaroucraft.gopaintman;
    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. private JavaPlugin plugin;
    21. private FileConfiguration fileConfig;
    22. private File file;
    23. private 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. if(!file.exists()){
    31. try {
    32. file.createNewFile();
    33. } catch (IOException e) {
    34. // TODO Auto-generated catch block
    35. e.printStackTrace();
    36. }
    37. }
    38. }
    39. public void setValue(String path, Object value){
    40. fileConfig.set(path, value);
    41. saveConfig();
    42. }
    43. public void setValueNoSave(String path, Object value){
    44. fileConfig.set(path, value);
    45. }
    46. public void createFile(){
    47. if(file.exists()){
    48. return;
    49. }
    50. try {
    51. file.createNewFile();
    52. fileConfig.addDefaults(fileConfig);
    53. } catch (IOException e) {
    54. // TODO Auto-generated catch block
    55. e.printStackTrace();
    56. }
    57.  
    58. }
    59.  
    60. public void reloadConfig() {
    61. if (file == null) {
    62. file = new File(plugin.getDataFolder(), fileName.toLowerCase()
    63. + ".yml");
    64. this.createFile();
    65. }
    66. fileConfig = YamlConfiguration.loadConfiguration(file);
    67.  
    68. try {
    69. InputStream defConfigStream = new FileInputStream(file);
    70. if (defConfigStream != null) {
    71. YamlConfiguration defConfig = YamlConfiguration
    72. .loadConfiguration(file);
    73. fileConfig.setDefaults(defConfig);
    74. defConfigStream.close();
    75. }
    76. } catch (FileNotFoundException e) {
    77.  
    78. e.printStackTrace();
    79. } catch (IOException e) {
    80.  
    81. e.printStackTrace();
    82. }
    83.  
    84. }
    85.  
    86. public void saveConfig() {
    87. if(fileConfig == null || file == null){
    88. return;
    89. }
    90. try {
    91. fileConfig.save(file);
    92. } catch (IOException e) {
    93. plugin.getLogger().severe("Could not save file: " + fileName + ".yml");
    94. e.printStackTrace();
    95. }
    96. }
    97.  
    98. /**
    99.   * @return the fileConfig
    100.   */
    101. public FileConfiguration getConfig() {
    102. return fileConfig;
    103. }
    104.  
    105. /**
    106.   * @return the file
    107.   */
    108. public File getFile() {
    109. if(file.exists()){
    110. return file;
    111. }else{
    112. createFile();
    113. return file;
    114. }
    115.  
    116.  
    117. }
    118.  
    119. /**
    120.   * @return the fileName currently held in RAM. May not be real filename
    121.   */
    122. public String getFileName() {
    123. return fileName;
    124. }
    125. }
    126.  
     
  3. Offline

    messageofdeath

    Gopaintman Hey dude all I did was remove these two methods and it's works fine.

    But I don't get how it did because these methods were never called in the class or the instance of the class.

    /*public void changeFile(String fileName) {
    this.changeFile(fileName, this.plugin.getDataFolder().getPath());
    }

    public void changeFile(String fileName, String fileLocation) {
    this.fileName = fileName;
    this.fileLocation = fileLocation;
    this.onStartUp();
    }*/
     
  4. Offline

    Gopaintman

    So if you just remove them they work? Try adding them back, maybe you did something else and it fixed it.
     
  5. Offline

    messageofdeath

    It worked, Thank you. I must of did something to fix it.
     
Thread Status:
Not open for further replies.

Share This Page