[SPIGOT-7611] config.yml doesn't set comments to default values Created: 01/Apr/24 Updated: 25/Dec/24 |
|
| Status: | Open |
| Project: | Spigot |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Minor |
| Reporter: | Mento Bento | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | Configuration, bug | ||
| Version: | 1.20.4 |
| Guidelines Read: | Yes |
| Description |
|
config.addComments doesn't work on added defaults. config.addComments only works on values that are already in the config.yml file or set values. Relevant spigot forum post (by me) The main method
@Override
public void onEnable() {
saveDefaultConfig();
FileConfiguration configuration = getConfig();
configuration.addDefault("test.bar", "foo");
configuration.set("test.asd", "dsa");
configuration.options().copyDefaults(true);
configuration.setComments("test.bar", Collections.singletonList("this is the default set in the code"));
configuration.setComments("test.foo", Collections.singletonList("this is the default set in the config.yml"));
configuration.setComments("test.asd", Collections.singletonList("this is a set value using code"));
saveConfig();
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Plugin has finished.");
}
The config.yml in the resource folder test: foo: "bar" The config generated by the plugin on the first time it loaded test: bar: foo # this is the default set in the config.yml foo: bar # this is a set value using code asd: dsa The config generated by the plugin after a server restart test: # this is the default set in the code bar: foo # this is the default set in the config.yml foo: bar # this is a set value using code asd: dsa |
| Comments |
| Comment by Marvin Rieple [ 26/May/24 ] |
|
Personally #setComments should not modify default values, if no normal value is present. On the other hand #set can already modify values in the default configuration, same with #setComments, if there is no normal value in the section e.g. a configuration section only exists in the defaults. I think how to fix this is a matter of whether or not we want modifying calls to be able to change defaults or not (and enforce it on every modifying method). Other thoughts and perspectives are welcomed.
As for the behavior that, foo: bar has a comment and bar: foo not, is because #getConfig loads the configuration on first call and since you call #saveDefaultConfig before #getConfig foo:bar is no longer treated as default value but as normal. |