1.15.2 Copying File From Jar To Plugin Folder Removes Comments

Discussion in 'Spigot Plugin Development' started by Bredo, Jan 21, 2020.

  1. Hello, so I am trying to copy a resource out to my plugin folder
    I am using this code:
    Code (Java):

        Files.copy(this.getClass().getResourceAsStream("config.yml"), Paths.get(config.getPath()),
                            StandardCopyOption.REPLACE_EXISTING);
     
    It does copy out the text that I want
    But when I do, it removes my comments?
     
  2. Strahan

    Benefactor

    If you search, there are a plethora of threads about this already. This one includes a link to a resource tchristofferson made to address this issue.
     
  3. You can use the saveDefaultConfig method in your Plugin class. Of course, will only save the default config. The bukkit API does not support yml files updates with keeping commands. You'll need to create your own config handling.
     
  4. If I understand that right, either I have to use an API, or create my own parser/writer is that correct?
     
  5. Strahan

    Benefactor

    Yea, I'm no expert on the topic as I don't put any comments in my files but from what I've read, the SnakeYAML parser does not preserve them so you'd have to.
     
  6. Crap... well I guess my files does neither now, it was just a little extra small comment I wanted to add.
     
  7. SteelPhoenix

    Moderator

    Indeed, and there is a reason for that. The YAML spec states that comments are not tied to any node (they don't affect the (de)serialization tree), so when rewriting the file there is no way to know where the comments need to be put. Therefore there is no reason to parse and save them in the first place, which is why SnakeYAML ignores them.

    There are some hacky resources around that support comments but no solution is perfect.
     
    • Like Like x 1
    • Informative Informative x 1
  8. But I see a lot of plugins have comments in their config files? do they create a whole new way, or use an API?
     
  9. SteelPhoenix

    Moderator

    The files are copied character by character from inside the JAR to the plugin directory. As YAML is often used for configuring purposes, there is not really a need to update values by code. Devs avoid updating and having to save a config because it will get rid of comments
     
  10. Put a config file in your jar with comments, in the onEnable method put saveDefaultConfig() and the comments will be there
     
    • Agree Agree x 1
  11. that doesn't work :(
     
  12. SteelPhoenix

    Moderator

    It does and you've messed up somewhere. You need to show your code. Also, did you make sure the config file in your plugin directory was deleted first? It does not overwrite an existing file.
     
    • Like Like x 1