Solved Config not being saved

Discussion in 'Plugin Development' started by mazentheamazin, Mar 1, 2014.

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

    mazentheamazin

    Hi,

    I've currently made a plugin, however when I try to edit the config and/or save it nothing happens with no errors in console. This is the code:
    Code:java
    1. public FileConfiguration getConfiguration() {
    2. File file = new File(getDataFolder(), "config.yml");
    3. return YamlConfiguration.loadConfiguration(file);
    4. }
    5.  
    6. public void reloadConfiguration() {
    7. File file = new File(getDataFolder(), "config.yml");
    8. YamlConfiguration getConfig = YamlConfiguration.loadConfiguration(file);
    9. try {
    10. getConfig.save(file);
    11. getConfig.load(file);
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. } catch (InvalidConfigurationException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18.  
    19. public void saveConfiguration() {
    20. File file = new File(getDataFolder(), "config.yml");
    21. YamlConfiguration getConfig = YamlConfiguration.loadConfiguration(file);
    22. try {
    23. getConfig.save(file);
    24. getConfig.load(file);
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. } catch (InvalidConfigurationException e) {
    28. e.printStackTrace();
    29. }
    30. }

    I've tried everything I could however it is still not working, please help ASAP.

    Thanks,
    Mazen
     
  2. Offline

    TopTobster5

    Have you tried
    Code:java
    1. this.saveConfig;
    ?

    And then to load it, use
    Code:java
    1. this.reloadConfig();


    On a side note, I think you are making life hard for yourself. Bukkit has a lot of built in functions for config files, you are acting like it is a custom one. check the api out for help.
     
  3. Offline

    mazentheamazin

    TopTobster5
    Yes, I have. It didn't work, so I decided to do it manually.
     
  4. Offline

    TopTobster5

    mazentheamazin Trust me, it is a lot easier, you are much likely to get help using it. What part of it are you having trouble with?
     
  5. Offline

    mazentheamazin

    TopTobster5
    when I used getConfig it did not do anything, if I were to get a string from it, it would throw a NPE. And saveConfig() and reloadConfig just did not work at all.
     
  6. Offline

    TopTobster5

    Hmm, did you definitely create the config file? and how were you using the functions? (saveConfig() and reloadConfig() should be this.saveConfig() and this.reloadConfig()).
     
  7. Offline

    mazentheamazin

    TopTobster5
    The config file exists, I used this.reloadConfig() and this.saveConfig() before I switched to this.saveConfiguration() and this.reloadConfiguration()
     
  8. Offline

    TopTobster5

    May I ask, are you using the config file to store information, or as a configuration file?
     
  9. Offline

    mazentheamazin

    TopTobster5
    I am using it for both reasons. Please tag me so I can see your message.
     
  10. Offline

    TopTobster5

    mazentheamazin
    Is there any chance you could give me any of the code? I may be able to spot any errors that way.
     
  11. Offline

    mazentheamazin

    TopTobster5
    i.e This is one of the examples of getting a string from the config:
    Code:java
    1. this.getConfiguration().getString("Prefix")
    2. // An Example of me modifying something in the config
    3. this.getConfiguration().set("users." + player.getName(), "Knight");
    4. this.saveConfiguration()
     
  12. Offline

    TopTobster5

    mazentheamazin
    I'm guessing your main issue is that your not setting a variable with what you get. Using the config functions built in, it would be:
    Code:java
    1. String prefix = getConfig().string("prefix");


    I'm not sure what wrong with your set part (Except that you probally should be using player.getDisplayName() ), but again, using the built in functions, it would just be:
    Code:java
    1. this.getConfig().set("user." + player.getDisplayName(), "Knight");
     
  13. Offline

    mazentheamazin

    TopTobster5
    Yes, before I created the variable when I was getting the string. About your second statement, when getting the display name it may contain colors and can be changed, so is not a good variable to use when saving data. The getConfig is exactly the same as getConfiguration in my case.
     
  14. Offline

    TopTobster5

    mazentheamazin Hmm. Try putting in a line like getLogger().info("Test"); and see if it appears in your console. That will tell you if the code is even running.
     
  15. Offline

    mazentheamazin

    TopTobster5
    After those lines are run I send a message to a player, and it still sends it. So, I know its running.
     
  16. Offline

    TopTobster5

    mazentheamazin Can I see your code for getConfiguration() please?
     
  17. Offline

    mazentheamazin

    TopTobster5
    Code:java
    1. public FileConfiguration getConfiguration() {
    2. File file = new File(getDataFolder(), "config.yml");
    3. return YamlConfiguration.loadConfiguration(file);
    4. }


    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  18. Offline

    Gater12

    I don't see any wrong in using the built in getConfig() . Maybe it's the way you are using it is causing an NPE to be thrown?
     
  19. Offline

    mazentheamazin

    Gater12
    That may be so, however it doesn't save the config neither edit it. However, neither does my current method, so I decided to post on here
     
  20. Offline

    TopTobster5

    mazentheamazin My guess is that you are just using it wrong. Try making another basic plugin using the built in config functions. Then you may be able to understand exactly how they work and what you are doing wrong.
     
  21. Offline

    mazentheamazin

    TopTobster5
    I've worked with custom configuration files as well as normal configuration files for a while now, and normally I can find the jest of the problem however all that I am trying is not working, so I posted here.
    EDIT: If I did not mention before, I am able to get information from the config, however I can't set information and save it.

    Final Edit: After looking through the Configuration API Reference page on the Wiki, I changed my methods to the ones used for creating a custom YML File, and it worked! This is my correct code for anybody with the same problem:
    Code:java
    1. private FileConfiguration customConfig = null;
    2. private File customConfigFile = null;
    3.  
    4. public FileConfiguration getConfiguration() {
    5. if (customConfig == null) {
    6. reloadConfiguration();
    7. }
    8. return customConfig;
    9. }
    10.  
    11. public void reloadConfiguration() {
    12. if (customConfigFile == null) {
    13. customConfigFile = new File(getDataFolder(), "config.yml");
    14. }
    15. customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
    16.  
    17. // Look for defaults in the jar
    18. InputStream defConfigStream = this.getResource("config.yml");
    19. if (defConfigStream != null) {
    20. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    21. customConfig.setDefaults(defConfig);
    22. }
    23. }
    24.  
    25. public void saveConfiguration() {
    26. if (customConfig == null || customConfigFile == null) {
    27. return;
    28. }
    29. try {
    30. getConfiguration().save(customConfigFile);
    31. } catch (IOException ex) {
    32. getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
    33. }
    34. }
     
    TopTobster5 likes this.
Thread Status:
Not open for further replies.

Share This Page