mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-08 08:02:13 -07:00
Added new FileConfigurationOptions.copyHeader, defaulting to true. Copies the header from default config, if there is one.
By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
@@ -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() {
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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 "";
|
||||
}
|
||||
|
@@ -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.
|
||||
|
Reference in New Issue
Block a user