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

  2. Online

    timtower Administrator Administrator Moderator

    Last edited: May 2, 2018
  3. Offline

    KarimAKL

    @timtower Do i need to add the "getResource"? Also what is that "copyDefaults"?
     
  4. Online

    timtower Administrator Administrator Moderator

    Did you read the block?
    Can't get it more clear than that in my opinion.
    And that copies the default values.
     
  5. Offline

    KarimAKL

    @timtower Yes i did read it but i'm still confused. :/ I thought i needed to do this:
    Code:Java
    1.  
    2. public File whatever1;
    3. public FileConfiguration whatever2;
    4.  

    And then in the onEnable:
    Code:Java
    1.  
    2. whatever1 = new File(this.getDataFolder() + "/whatever.yml");
    3. whatever2 = YamlConfiguration.loadConfiguration(this.getResource(whatever1));
    4.  

    or:
    Code:Java
    1.  
    2. whatever1 = new File(this.getDataFolder() + "/whatever.yml");
    3. whatever2 = YamlConfiguration.loadConfiguration(this.getResource("whatever.yml"));
    4.  

    But those both come with errors and i don't really know what else i should do. :/
     
  6. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Depends on the fact if whatever.yml is there already or not, if it is: then the first one,
    if not: then the second one, or you should at least copy it over.

    And the errors are probably because of the loadConfiguration(Somestreamtype) doesn't exist anymore, am I correct in that?
     
  7. Offline

    KarimAKL

    @timtower The error on the first one is "The method getResource(String) in the type JavaPlugin is not applicable for the arguments (File)"
     
  8. Online

    timtower Administrator Administrator Moderator

    @KarimAKL the getResource is only needed when the file doesn't exist yet.
     
  9. Offline

    KarimAKL

    @timtower I see but then what do i need to do for it to copy it?
     
  10. Online

    timtower Administrator Administrator Moderator

    If the file doesn't exist:
    Load the configuration using the getResource call with the string parameter.
    Save the configuration to the file in the plugins folder.
    Then run the rest of the plugin where it gets loaded.

    We can improve it later
     
  11. Offline

    KarimAKL

  12. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Exists in the plugin folder...
    Like, not yet copied, not being able to load it.
     
  13. Offline

    KarimAKL

    @timtower So i need to check if it exists and then if not i need to load it and save it like this?:
    Code:Java
    1.  
    2. whatever2.save(whatever1);
    3. whatever2.load(whatever1);
    4.  

    Or what?
     
  14. Online

    timtower Administrator Administrator Moderator

    @KarimAKL
    1. Post full methods please, those 2 lines tell nothing.
    2. Move away from the weird names, use normal ones instead.
    3. Depends on the rest of your code, see 1.
     
  15. Offline

    KarimAKL

    @timtower 1. You want me to do this, right?:
    Code:Java
    1.  
    2. public File messagesFile;
    3. public FileConfiguration messages;
    4.  
    5. public void onEnable() {
    6. if (!getDataFolder().exists()) {
    7. getDataFolder().mkdirs();
    8. if (!messagesFile.exists) {
    9. try {
    10. messagesFile = new File(this.getDataFolder() + "/messages.yml");
    11. messages = YamlConfiguration.loadConfiguration(this.getResource("messages.yml"));
    12. messages.save(messagesFile);
    13. messages.load(messagesFile);
    14. } catch (IOException | InvalidConfigurationException e) {
    15. e.printStackTrace();
    16. }
    17. } else {
    18. try {
    19. messagesFile = new File(this.getDataFolder() + "/messages.yml");
    20. messages = YamlConfiguration.loadConfiguration(this.getResource("messagesFile"));
    21. messages.save(messagesFile);
    22. messages.load(messagesFile);
    23. } catch (IOException | InvalidConfigurationException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. }
    28. }
    29.  

    Or did i get that wrong?
    2. Okay, will do then. (Already did above)
    3. Okay.
     
  16. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You are using getResource twice, you ONLY need to do it when the file doesn't exists.
    And your data folder exists check is nesting the messagesFile exists, so if the folder exists then the config won't be loaded.
     
  17. Offline

    KarimAKL

    @timtower 1. So like this then?:
    Code:Java
    1.  
    2. public File messagesFile;
    3. public FileConfiguration messages;
    4.  
    5. public void onEnable() {
    6. if (!getDataFolder().exists()) {
    7. getDataFolder().mkdirs();
    8. if (!messagesFile.exists) {
    9. try {
    10. messagesFile = new File(this.getDataFolder() + "/messages.yml");
    11. messages = YamlConfiguration.loadConfiguration(this.getResource("messages.yml"));
    12. messages.save(messagesFile);
    13. messages.load(messagesFile);
    14. } catch (IOException | InvalidConfigurationException e) {
    15. e.printStackTrace();
    16. }
    17. } else {
    18. try {
    19. messagesFile = new File(this.getDataFolder() + "/messages.yml");
    20. messages = YamlConfiguration.loadConfiguration(messagesFile);
    21. messages.save(messagesFile);
    22. messages.load(messagesFile);
    23. } catch (IOException | InvalidConfigurationException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. }
    28. }
    29.  

    2. So i need to do the same if the folder does exist?
     
  18. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Like that.
    And if the folder doesn't exist then you create it, then you let the rest of the code run.
     
  19. Offline

    KarimAKL

    Okay, so i just did this but when doing that "this.getResource(string filename)" it makes the "loadConfiguration" part red with --- through it and the yellow warning underline and everything after the "loadConfiguration" part only has that yellow warning underline, the error is this:
    "The method loadConfiguration(InputStream) from the type YamlConfiguration is deprecated"
    The line looks like this:
    Code:Java
    1.  
    2. messages = YamlConfiguration.loadConfiguration(this.getResource("messages.yml"));
    3.  
     
  20. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Then you need to copy the file instead.
     
  21. Offline

    KarimAKL

    @timtower But how would i do that? I thought that was what the "this.getResource" did. :/
     
  22. Online

    timtower Administrator Administrator Moderator

    @KarimAKL That just gets it.
    What did you google so far?
     
  23. Offline

    KarimAKL

    @timtower I always try to google things before asking here but when you sent that message i remembered that i hadn't tried searching for a way to copy the default yml file because i didn't know that was what i needed to do, so i tried searching for it and then i found this:
    Code:Java
    1.  
    2. this.saveResource("messages.yml", false);
    3.  

    And i just tried it and it seems to work, what do you think? Is there anything wrong with it or something?
     
  24. Online

    timtower Administrator Administrator Moderator

    And why would there be something wrong? It works right?
    What does the boolean do?
     
  25. Offline

    KarimAKL

    @timtower I don't know i just thought since it is just 1 line and you suggested a way more complicated way of doing it i thought it was because you didn't want to suggest this method. About the boolean, i don't know what it does but if i remove it, it comes with an error saying: "The method saveResource(String, boolean) in the type JavaPlugin is not applicable for the arguments (String)" and it says this when i try hovering over the method:
    "void org.bukkit.plugin.java.JavaPlugin.saveResource(String resourcePath, boolean replace)

    Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
    " So i guess it is something about replacing it so i think it means that if it is false it doesn't replace the file with something when copying it to the plugin folder and if true it does replace it when copying. That is my gues on what it means, if you know could you explain it?
     
  26. Online

    timtower Administrator Administrator Moderator

    @KarimAKL I don't know the default stuff as I use my own config handlers.
    How they function can't be compared to each other.

    Javadocs are a pretty good source in that regard.
     
  27. Offline

    KarimAKL

    @timtower Oh, okay. :p Anyway i guess this is solved now, thanks for all the help. :D
     
Thread Status:
Not open for further replies.

Share This Page