diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java b/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java index a67547b1f4..409e39212f 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java +++ b/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java @@ -178,6 +178,16 @@ public abstract class FileConfiguration extends MemoryConfiguration { * @throws IllegalArgumentException Thrown if contents is null. */ public abstract void loadFromString(String contents) throws InvalidConfigurationException; + + /** + * Compiles the header for this {@link FileConfiguration} and returns the result. + *
+ * This will use the header from {@link #options()} -> {@link FileConfigurationOptions#header()}, + * respecting the rules of {@link FileConfigurationOptions#copyHeader()} if set. + * + * @return Compiled header + */ + protected abstract String buildHeader(); @Override public FileConfigurationOptions options() { diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java b/paper-api/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java index 804272045b..1da240477b 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java +++ b/paper-api/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java @@ -7,6 +7,7 @@ import org.bukkit.configuration.*; */ public class FileConfigurationOptions extends MemoryConfigurationOptions { private String header = null; + private boolean copyHeader = true; protected FileConfigurationOptions(MemoryConfiguration configuration) { super(configuration); @@ -64,4 +65,45 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions { this.header = value; return this; } + + /** + * Gets whether or not the header should be copied from a default source. + *
+ * If this is true, if a default {@link FileConfiguration} is passed to + * {@link FileConfiguration#setDefaults(org.bukkit.configuration.Configuration)} + * then upon saving it will use the header from that config, instead of the one provided here. + *
+ * If no default is set on the configuration, or the default is not of type FileConfiguration, + * or that config has no header ({@link #header()} returns null) then the header + * specified in this configuration will be used. + *
+ * Defaults to true. + * + * @return Whether or not to copy the header + */ + public boolean copyHeader() { + return copyHeader; + } + + /** + * Sets whether or not the header should be copied from a default source. + *
+ * If this is true, if a default {@link FileConfiguration} is passed to + * {@link FileConfiguration#setDefaults(org.bukkit.configuration.Configuration)} + * then upon saving it will use the header from that config, instead of the one provided here. + *
+ * If no default is set on the configuration, or the default is not of type FileConfiguration, + * or that config has no header ({@link #header()} returns null) then the header + * specified in this configuration will be used. + *
+ * Defaults to true.
+ *
+ * @param value Whether or not to copy the header
+ * @return This object, for chaining
+ */
+ public FileConfigurationOptions copyHeader(boolean value) {
+ copyHeader = value;
+
+ return this;
+ }
}
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 55918f7d12..f9523b3926 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
@@ -39,13 +39,14 @@ public class YamlConfiguration extends FileConfiguration {
serializeValues(output, getValues(false));
+ String header = buildHeader();
String dump = yaml.dump(output);
if (dump.equals(BLANK_CONFIG)) {
dump = "";
}
- return buildHeader() + dump;
+ return header + dump;
}
@Override
@@ -61,7 +62,12 @@ public class YamlConfiguration extends FileConfiguration {
throw new InvalidConfigurationException("Specified contents is not a valid Configuration", ex);
}
- options().header(parseHeader(contents));
+ String header = parseHeader(contents);
+
+ if (header.length() > 0) {
+ options().header(header);
+ }
+
deserializeValues(input, this);
}
@@ -159,6 +165,19 @@ public class YamlConfiguration extends FileConfiguration {
protected String buildHeader() {
String header = options().header();
+ if (options().copyHeader()) {
+ Configuration def = getDefaults();
+
+ if ((def != null) && (def instanceof FileConfiguration)) {
+ FileConfiguration filedefaults = (FileConfiguration)def;
+ String defaultsHeader = filedefaults.buildHeader();
+
+ if ((defaultsHeader != null) && (defaultsHeader.length() > 0)) {
+ return defaultsHeader;
+ }
+ }
+ }
+
if (header == null) {
return "";
}
diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java
index eee75fb183..25fd6266b4 100644
--- a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java
+++ b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java
@@ -32,6 +32,12 @@ public class YamlConfigurationOptions extends FileConfigurationOptions {
super.header(value);
return this;
}
+
+ @Override
+ public YamlConfigurationOptions copyHeader(boolean value) {
+ super.copyHeader(value);
+ return this;
+ }
/**
* Gets how much spaces should be used to indent each line.
diff --git a/paper-api/src/test/java/org/bukkit/configuration/file/FileConfigurationTest.java b/paper-api/src/test/java/org/bukkit/configuration/file/FileConfigurationTest.java
index 5deb458168..2b976725a0 100644
--- a/paper-api/src/test/java/org/bukkit/configuration/file/FileConfigurationTest.java
+++ b/paper-api/src/test/java/org/bukkit/configuration/file/FileConfigurationTest.java
@@ -1,9 +1,7 @@
package org.bukkit.configuration.file;
-import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
-import java.io.FileReader;
import java.io.FileWriter;
import java.util.Map;
import org.bukkit.configuration.MemoryConfigurationTest;
@@ -21,6 +19,10 @@ public abstract class FileConfigurationTest extends MemoryConfigurationTest {
public abstract String getTestValuesString();
+ public abstract String getTestHeaderInput();
+
+ public abstract String getTestHeaderResult();
+
@Test
public void testSave_File() throws Exception {
FileConfiguration config = getConfig();
@@ -122,5 +124,65 @@ public abstract class FileConfigurationTest extends MemoryConfigurationTest {
}
assertEquals(values.keySet(), config.getKeys(true));
+ assertEquals(saved, config.saveToString());
+ }
+
+ @Test
+ public void testSaveToStringWithHeader() {
+ FileConfiguration config = getConfig();
+ config.options().header(getTestHeaderInput());
+
+ for (Map.Entry