Fix saving configs which are only a header

By: md_5 <git@md-5.net>
This commit is contained in:
Bukkit/Spigot 2022-01-08 09:39:38 +11:00
parent 78dab8fe52
commit 215952d23f
2 changed files with 28 additions and 7 deletions

View file

@ -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<String> loadHeader(List<String> header) {
ArrayList<String> list = new ArrayList<String>(header);
if (list.size() != 0) {
list.remove(list.size() - 1);
LinkedList<String> 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<String> saveHeader(List<String> header) {
ArrayList<String> list = new ArrayList<String>(header);
if (list.size() != 0) {
LinkedList<String> list = new LinkedList<>(header);
if (!list.isEmpty()) {
list.add(null);
}
return list;
}

View file

@ -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);
}
}