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.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@@ -80,7 +81,7 @@ public class YamlConfiguration extends FileConfiguration {
node.setEndComments(getCommentLines(options().getFooter(), CommentType.BLOCK)); node.setEndComments(getCommentLines(options().getFooter(), CommentType.BLOCK));
StringWriter writer = new StringWriter(); 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(""); writer.write("");
} else { } else {
if (node.getValue().isEmpty()) { 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 * 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 * @param header The list of heading comments
* @return The modified list * @return The modified list
*/ */
private List<String> loadHeader(List<String> header) { private List<String> loadHeader(List<String> header) {
ArrayList<String> list = new ArrayList<String>(header); LinkedList<String> list = new LinkedList<>(header);
if (list.size() != 0) {
list.remove(list.size() - 1); if (!list.isEmpty()) {
list.removeLast();
} }
while (!list.isEmpty() && list.peek() == null) {
list.remove();
}
return list; return list;
} }
@@ -252,10 +260,12 @@ public class YamlConfiguration extends FileConfiguration {
* @return The modified list * @return The modified list
*/ */
private List<String> saveHeader(List<String> header) { private List<String> saveHeader(List<String> header) {
ArrayList<String> list = new ArrayList<String>(header); LinkedList<String> list = new LinkedList<>(header);
if (list.size() != 0) {
if (!list.isEmpty()) {
list.add(null); list.add(null);
} }
return list; return list;
} }

View File

@@ -163,4 +163,15 @@ public class YamlConfigurationTest extends FileConfigurationTest {
String result = config.saveToString(); String result = config.saveToString();
assertEquals(data, result); 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);
}
} }