Solved Load custom yml file on enable

Discussion in 'Plugin Development' started by KarimAKL, Apr 28, 2018.

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

    KarimAKL

    Hey, i have made a plugin with a custom yml file but when i start the server the folder which should have the plugin name doesn't show up with the file until i load it using a command i made in the plugin. :/ (this command is made to load the file)
    Here is my main class:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public File whatever1 = new File(this.getDataFolder() + "/whatever.yml");
    5. public FileConfiguration whatever2 = YamlConfiguration.loadConfiguration(whatever1);
    6.  
    7. public void onEnable() {
    8. saveDefaultConfig();
    9. }
    10.  
    11. }
    12.  

    My guess is i need to do something like the "saveDefaultConfig();" i have in my onEnable but i don't know what it should be for a custom yml file. :/
     
  2. @KarimAKL

    Code:
    public void onEnable()
    {
        if(!getDataFolder().exists())
        {
             getDataFolder().mkdirs();
             try{
                 whatever2.save(whatever1);
                 whatever2.load(whatever1);
             }catch (IOException | InvalidConfigurationException e) {
                            e.printStackTrace();
             }
        }
    }
     
  3. Offline

    KarimAKL

    @Blackwing_Forged Okay i'll try that now, thanks. :)
    EDIT: I just tried this but it still doesn't come up when i run the plugin. :/
     
    Last edited by a moderator: Apr 28, 2018
  4. @KarimAKL
    Keep in mind that when you make changes in game to the custom file they will not be updated until you save and load the custom file again
     
  5. Offline

    KarimAKL

    @Blackwing_Forged I use this:
    Code:Java
    1.  
    2. try {
    3. whatever2.save(whatever1);
    4. whatever2.load(whatever1);
    5. } catch (IOException | InvalidConfigurationException e) {
    6. e.printStackTrace();
    7. }
    8.  

    And it has updated the custom file without any problems, anyway, i just tried what you said before and it didn't work. :/
    Here is my main class:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public File whatever1 = new File(this.getDataFolder() + "/whatever.yml");
    5. public FileConfiguration whatever2 = YamlConfiguration.loadConfiguration(whatever1);
    6.  
    7. public void onEnable() {
    8. new CommandTest(this);
    9. saveDefaultConfig();
    10. if (!getDataFolder().exists()) {
    11. getDataFolder().mkdirs();
    12. try {
    13. whatever2.save(whatever1);
    14. whatever2.load(whatever1);
    15. } catch (IOException | InvalidConfigurationException e) {
    16. e.printStackTrace();
    17. }
    18. }
    19. }
    20.  
    21. }
    22.  
     
  6. @KarimAKL
    is there a folder for your plugin created in the plugins folder? if so delete that and restart the server and also you can get rid of the saveDefaultConfig line
     
  7. Offline

    KarimAKL

    @Blackwing_Forged In my server i have a folder named "plugins" and in that folder i have a folder named the same as my plugin but i delete that folder(the one named the same as my plugin) everytime so that i can see if it works correctly and i have the "saveDefaultConfig" because i have a "config.yml" aswell.
     
  8. @KarimAKL
    Put the saveDefaultConfig in the try catch then because thats why its not working, its creating the folder and then checking if the folder doesn't exist which it does because you're saving the config before that code
     
  9. Offline

    KarimAKL

    @Blackwing_Forged That makes sense, i'll try that now thanks. :)
    EDIT: Works, thanks again. :) Changed to Solved now.
     
  10. Offline

    KarimAKL

    @Blackwing_Forged Okay so i just used this for a yml file that does have text in it onEnable and when i run the server it doesn't have any text? Why is that? Do i need to add the text to it and then save and load or is there another way? (I use this post even tho it was "Solved" because the point of this was to be able to do this and when i said it works i hadn't tried with a file that already has text, sorry. :/)
     
  11. Offline

    KarimAKL

    @Blackwing_Forged Here you go:
    In my Main class:
    Code:Java
    1.  
    2. public File whatever1 = new File(this.getDataFolder() + "/whatever.yml");
    3. public FileConfiguration whatever2 = YamlConfiguration.loadConfiguration(whatever1);
    4.  
    5. @Override
    6. public void onEnable() {
    7. if (!getDataFolder().exists()) {
    8. getDataFolder().mkdirs();
    9. try {
    10. whatever2.save(whatever1);
    11. whatever2.load(whatever1);
    12. } catch (IOException | InvalidConfigurationException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. }
    17.  

    This results in the whatever.yml to look like this(empty):
    View attachment 31517
     
  12. @KarimAKL
    Show me where you are actually writing to the file
     
  13. Online

    timtower Administrator Administrator Moderator

    @KarimAKL whatever1 is invalid as it is being set using this.getDataFolder(), which is not yet available at that time.
     
  14. Offline

    KarimAKL

    @Blackwing_Forged I am writing in the file, not using a method, do i need to do it using a method? @timtower What do you mean? I don't have any errors.
     
  15. Online

    timtower Administrator Administrator Moderator

    @KarimAKL you are probably saving to null/whatever1.yml though.
    Put the first two lines IN the onEnable.
     
  16. Offline

    KarimAKL

    @timtower If i do that i can't access them in my other classes. :/
     
  17. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Then define them there but initialize in the onEnable.
     
  18. Offline

    KarimAKL

    @timtower I don't know how to do that. :/
     
  19. @KarimAKL
    So do
    public File whatever1;
    public FileConfiguration whatever2;

    then in your on enable do this
    whatever1 = new File(this.getDataFolder() + "/whatever.yml");
    whatever2 = YamlConfiguration.loadConfiguration(whatever1);
     
  20. Offline

    KarimAKL

    @Blackwing_Forged Okay so i just did that but it's still empty when i run the server. :/
     
  21. Online

    timtower Administrator Administrator Moderator

    @KarimAKL That is because you are not setting any values in it.
     
  22. Offline

    KarimAKL

    @timtower What do you mean? Should i add the text using a method instead of just typing it in the file itself?
     
  23. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Did you put anything in the yml?
     
  24. Offline

    KarimAKL

    @timtower I did type something in the yml file but not using a method, why?
     
  25. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Because it won't magically fill itself.
     
  26. Offline

    KarimAKL

    @timtower I just thought i could type something in the file itself for it to be there like the "config.yml" :/
     
  27. Online

    timtower Administrator Administrator Moderator

    @KarimAKL If that would be in the jar: not without extra code to handle it.
     
  28. Offline

    KarimAKL

    @timtower The text is in the yml file(in the jar file) but when loading it, it removes it. :/
     
  29. Online

    timtower Administrator Administrator Moderator

    @KarimAKL It doesn't remove it, it never copies it.
     
Thread Status:
Not open for further replies.

Share This Page