Updating yml files
When a plugin update adds settings or messages, the server's own files don't have them. YamlUpdate adds every key the new bundled file has and the server's file doesn't, with its comments, next to the key it follows in the bundled file.
java
YamlUpdate.Result result = YamlUpdate.addMissing(serverFile, bundledFile, Arrays.asList("kits"));
if (result.changed()) {
Files.copy(file, backup, StandardCopyOption.REPLACE_EXISTING);
Files.write(file, result.text().getBytes(StandardCharsets.UTF_8));
getLogger().info("Added " + result.added());
}- The server's values, comments, order and own keys stay line for line.
- Paths in the third argument are the user's own entries (kits, maps): bundled entries are never added there.
- It works on the text, so comments survive on 1.8 too (Bukkit's yml writer drops them before 1.18).
- Plain Java 8, in
EranoAPI-Common.