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 4bd9d4c473..55918f7d12 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 @@ -61,6 +61,7 @@ public class YamlConfiguration extends FileConfiguration { throw new InvalidConfigurationException("Specified contents is not a valid Configuration", ex); } + options().header(parseHeader(contents)); deserializeValues(input, this); } @@ -129,6 +130,32 @@ public class YamlConfiguration extends FileConfiguration { } } + protected String parseHeader(String input) { + String[] lines = input.split("\r?\n", -1); + StringBuilder result = new StringBuilder(); + boolean readingHeader = true; + + for (int i = 0; (i < lines.length) && (readingHeader); i++) { + String line = lines[i]; + + if (line.startsWith(COMMENT_PREFIX)) { + if (i > 0) { + result.append("\n"); + } + + if (line.length() > COMMENT_PREFIX.length()) { + result.append(line.substring(COMMENT_PREFIX.length())); + } + } else if (line.length() == 0) { + result.append("\n"); + } else { + readingHeader = false; + } + } + + return result.toString(); + } + protected String buildHeader() { String header = options().header(); 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 3ec206de69..488ce20867 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 @@ -60,6 +60,25 @@ public class YamlConfigurationTest extends FileConfigurationTest { assertEquals(expected, result); } + @Test + public void testParseHeader() throws Exception { + YamlConfiguration config = getConfig(); + Map values = getTestValues(); + String saved = getTestValuesString(); + String header = "# This is a sample\n# header.\n# \n# Newline above should be commented.\n\n\n"; + String expected = "This is a sample\nheader.\n\nNewline above should be commented.\n\n"; + + config.loadFromString(header + saved); + + assertEquals(expected, config.options().header()); + + for (Map.Entry entry : values.entrySet()) { + assertEquals(entry.getValue(), config.get(entry.getKey())); + } + + assertEquals(values.keySet(), config.getKeys(true)); + } + @Test public void testSaveToStringWithIndent() { YamlConfiguration config = getConfig();