diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index 1ee942b47..df250bf41 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java @@ -8,6 +8,7 @@ import java.io.Reader; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -80,7 +81,7 @@ public class YamlConfiguration extends FileConfiguration { node.setEndComments(getCommentLines(options().getFooter(), CommentType.BLOCK)); StringWriter writer = new StringWriter(); - if (node.getEndComments().isEmpty() && node.getEndComments().isEmpty() && node.getValue().isEmpty()) { + if (node.getBlockComments().isEmpty() && node.getEndComments().isEmpty() && node.getValue().isEmpty()) { writer.write(""); } else { if (node.getValue().isEmpty()) { @@ -231,16 +232,23 @@ public class YamlConfiguration extends FileConfiguration { /** * Removes the empty line at the end of the header that separates the header - * from further comments. + * from further comments. Also removes all empty header starts (backwards + * compat). * * @param header The list of heading comments * @return The modified list */ private List loadHeader(List header) { - ArrayList list = new ArrayList(header); - if (list.size() != 0) { - list.remove(list.size() - 1); + LinkedList list = new LinkedList<>(header); + + if (!list.isEmpty()) { + list.removeLast(); } + + while (!list.isEmpty() && list.peek() == null) { + list.remove(); + } + return list; } @@ -252,10 +260,12 @@ public class YamlConfiguration extends FileConfiguration { * @return The modified list */ private List saveHeader(List header) { - ArrayList list = new ArrayList(header); - if (list.size() != 0) { + LinkedList list = new LinkedList<>(header); + + if (!list.isEmpty()) { list.add(null); } + return list; } diff --git a/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java b/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java index 01ede4444..a0ff42abd 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/file/YamlConfigurationTest.java @@ -163,4 +163,15 @@ public class YamlConfigurationTest extends FileConfigurationTest { String result = config.saveToString(); assertEquals(data, result); } + + @Test + public void testOnlyHeader() { + YamlConfiguration config = getConfig(); + config.options().header("# Test"); + + String result = config.saveToString(); + String expected = "# # Test\n\n{}\n"; + + assertEquals(expected, result); + } }