diff options
Diffstat (limited to 'src/main/java/org/bukkit')
4 files changed, 79 insertions, 2 deletions
diff --git a/src/main/java/org/bukkit/configuration/file/FileConfiguration.java b/src/main/java/org/bukkit/configuration/file/FileConfiguration.java index a67547b1..409e3921 100644 --- a/src/main/java/org/bukkit/configuration/file/FileConfiguration.java +++ b/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. + * <p> + * 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/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java b/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java index 80427204..1da24047 100644 --- a/src/main/java/org/bukkit/configuration/file/FileConfigurationOptions.java +++ b/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. + * <p> + * 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. + * <p> + * 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. + * <p> + * 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. + * <p> + * 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. + * <p> + * 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. + * <p> + * 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/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index 55918f7d..f9523b39 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/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/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java b/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java index eee75fb1..25fd6266 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java +++ b/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. |