Config comments not appearing

Discussion in 'Spigot Plugin Development' started by TheGamerAdmin, Apr 21, 2019.

  1. Hello! So basically, I'm working on a plugin but whenever I export the .JAR from eclipse, the config won't appear correctly when used.

    Basically, I try to make it look like this:
    • # PizzaPlugin CONFIGURATION

      # These prefixes/suffixes are used to change the join/leave messages.
      # (JoinPrefix: "Hey!") + (JoinSuffix: "just joined the server!") = ("Hey! {PLAYER} just joined the server!")
      # (QuitPrefix: "Aww!") + (QuitSuffix: "just quit the server. ") = ("Aww! {PLAYER} just quit the server.")
      JoinPrefix: "&7[&a+&7]"
      JoinSuffix: "joined the server!"
      QuitPrefix: "&7[&c-&7]"
      QuitSuffix: "left the server."

      # This is only activated when a player joins the server for the first time.
      # (FirstJoinMessage: true) + (FirstJoinPrefix: "Horray!") + (FirstJoinSuffix: "joined the server for the first time!") = ("Horray! {PLAYER} joined the server for the first time!")
      FirstJoinMessage: true
      FirstJoinPrefix: "&7[&d+&7]"
      FirstJoinSuffix: "joined the server for the first time!"

      # This allows you to change the chatting format.
      # Minecraft Default: "<PLAYER>: MESSAGE"
      # PizzaPlugin Default: "[Chat] PLAYER: MESSAGE"
      ChangeFormat: true
      Format: "[&7Chat&r] %s: %s"
    But in reality, it turns out like this:
    • # PizzaPlugin CONFIGURATION
      #
      # These prefixes/suffixes are used to change the join/leave messages.
      # (JoinPrefix: "Hey!") + (JoinSuffix: "just joined the server!") = ("Hey! {PLAYER} just joined the server!")
      # (QuitPrefix: "Aww!") + (QuitSuffix: "just quit the server. ") = ("Aww! {PLAYER} just quit the server.")
      JoinPrefix: '&7[&a+&7]'
      JoinSuffix: joined the server!
      QuitPrefix: '&7[&c-&7]'
      QuitSuffix: left the server.
      FirstJoinMessage: true
      FirstJoinPrefix: '&7[&d+&7]'
      FirstJoinSuffix: joined the server for the first time!
      ChangeFormat: true
      Format: '[&7Chat&r] %s: %s'
    Can somebody please tell me what I'm doing wrong? Thank you!
     
  2. Hello,

    I don't think it is because of eclipse.
    I think it's about .save() method of a FileConfiguration.
    For example, if you change a value in the config, and then you use the save method, it removes all comments (except the header).

    It's a widespread problem, and I don't know what to do (maybe use an external library?). If someone knows, i'm curious too.
     
  3. snakeyaml (the YAML parser shaded and used by bukkit) skips comments as they are not bound to a node (see this). So when overwriting existing files comments get whiped as the parser does not save them. Comments are only preserved when copying files (JavaPlugin#saveResource(String, boolean) for example) as this does not involve parsing/dumping data but merely copies the file. If you want to preserve comments through saving you need to fork/make your own yaml parser.

    Edit:
    Bukkit's built-in YamlConfiguration reads all comments until the first node and saves those when parsing, and when dumping data it adds the comments back. This makes it able to keep a single multiline header at the start of the file.
     
    • Agree Agree x 1
  4. Use JavaPlugin#saveDefaultConfig to generate it with comments. And NEVER-EVER DO FileConfiguration#save cuz this breaks the comments.
     
    • Agree Agree x 1
  5. The reason that FileConfiguration and SnakeYAML don’t preserve comments is because it’s agaisnt the YAML specs.
     

Share This Page