mirror of
https://github.com/PaperMC/Paper.git
synced 2025-02-08 08:31:51 +01:00
Fixed YamlConfiguration creating empty lines when reading a file with no contents, and made it trim the initial newlines of any existing config header
By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
parent
5fc8f4f576
commit
e73be72fae
@ -148,6 +148,7 @@ public class YamlConfiguration extends FileConfiguration {
|
|||||||
String[] lines = input.split("\r?\n", -1);
|
String[] lines = input.split("\r?\n", -1);
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
boolean readingHeader = true;
|
boolean readingHeader = true;
|
||||||
|
boolean foundHeader = false;
|
||||||
|
|
||||||
for (int i = 0; (i < lines.length) && (readingHeader); i++) {
|
for (int i = 0; (i < lines.length) && (readingHeader); i++) {
|
||||||
String line = lines[i];
|
String line = lines[i];
|
||||||
@ -160,9 +161,11 @@ public class YamlConfiguration extends FileConfiguration {
|
|||||||
if (line.length() > COMMENT_PREFIX.length()) {
|
if (line.length() > COMMENT_PREFIX.length()) {
|
||||||
result.append(line.substring(COMMENT_PREFIX.length()));
|
result.append(line.substring(COMMENT_PREFIX.length()));
|
||||||
}
|
}
|
||||||
} else if (line.length() == 0) {
|
|
||||||
|
foundHeader = true;
|
||||||
|
} else if ((foundHeader) && (line.length() == 0)) {
|
||||||
result.append("\n");
|
result.append("\n");
|
||||||
} else {
|
} else if (foundHeader) {
|
||||||
readingHeader = false;
|
readingHeader = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,19 +170,40 @@ public abstract class FileConfigurationTest extends MemoryConfigurationTest {
|
|||||||
String saved = getTestValuesString();
|
String saved = getTestValuesString();
|
||||||
String header = getTestHeaderResult();
|
String header = getTestHeaderResult();
|
||||||
String expected = getTestHeaderInput();
|
String expected = getTestHeaderInput();
|
||||||
|
|
||||||
defaults.loadFromString(header);
|
defaults.loadFromString(header);
|
||||||
config.loadFromString(saved);
|
config.loadFromString(saved);
|
||||||
config.setDefaults(defaults);
|
config.setDefaults(defaults);
|
||||||
|
|
||||||
assertNull(config.options().header());
|
assertNull(config.options().header());
|
||||||
assertEquals(expected, defaults.options().header());
|
assertEquals(expected, defaults.options().header());
|
||||||
|
|
||||||
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
||||||
assertEquals(entry.getValue(), config.get(entry.getKey()));
|
assertEquals(entry.getValue(), config.get(entry.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(values.keySet(), config.getKeys(true));
|
assertEquals(values.keySet(), config.getKeys(true));
|
||||||
assertEquals(header + "\n" + saved, config.saveToString());
|
assertEquals(header + "\n" + saved, config.saveToString());
|
||||||
|
|
||||||
|
config = getConfig();
|
||||||
|
config.loadFromString(getTestHeaderResult() + saved);
|
||||||
|
assertEquals(getTestHeaderResult() + saved, config.saveToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReloadEmptyConfig() throws Exception {
|
||||||
|
FileConfiguration config = getConfig();
|
||||||
|
|
||||||
|
assertEquals("", config.saveToString());
|
||||||
|
|
||||||
|
config = getConfig();
|
||||||
|
config.loadFromString("");
|
||||||
|
|
||||||
|
assertEquals("", config.saveToString());
|
||||||
|
|
||||||
|
config = getConfig();
|
||||||
|
config.loadFromString("\n\n"); // Should trim the first newlines of a header
|
||||||
|
|
||||||
|
assertEquals("", config.saveToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user