1.12.2 Well documented config files

Discussion in 'Spigot Plugin Development' started by Wazup92, Jul 17, 2021.

  1. Hello! I have a small question regarding custom config files. I want to create a file with various options that have comments next to them, as shown below.
    Code (Text):
    # Test

    # This option is true
    isTrue: true

    # This option is false
    isFalse: false
    I create this file just fine using the JavaPlugin.saveResource method, but my problem is modifying this file using YamlConfiguration.loadConfiguration.

    The moment i modify a variable in the file and then use YamlConfiguration.save(File), the interior comments are all erased and any space is replaced with # as shown below
    Code (Text):
    # Test
    #
    # This option is true
    isTrue: true
    isFalse: true
    Any suggestions?
     
  2. Unfortunately snakeYAML doesn't keep comments other than the header. There are resources available that allow you to update a users configuration without breaking comments, like https://www.spigotmc.org/threads/configupdater-keep-comments-and-values.398466/. I don't think there is an easy way currently to modify the values in the configuration without breaking the comments (apart from writing your own yaml parser).

    I usually keep my configurations read-only (and update them using a forked version of the resource I linked), and use a separate file for everything that has to be changed by my plugin.
     
    • Useful Useful x 2
  3. Got it! Thank you so much