diff --git a/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java b/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java index 1553356..ac8a610 100644 --- a/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java @@ -1,18 +1,8 @@ package pw.yumc.YumCore.config; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; -import java.nio.charset.Charset; -import java.util.Map; - +import com.google.common.io.Files; import org.apache.commons.lang.Validate; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; @@ -20,19 +10,20 @@ import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.error.YAMLException; import org.yaml.snakeyaml.representer.Representer; - -import com.google.common.io.Files; - import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.config.yaml.BukkitConstructor; import pw.yumc.YumCore.config.yaml.BukkitRepresenter; +import java.io.*; +import java.nio.charset.Charset; +import java.util.Map; + /** * 抽象配置文件 * - * @since 2016年3月12日 下午4:46:45 * @author 喵♂呜 + * @since 2016年3月12日 下午4:46:45 */ public abstract class AbstractConfig extends YamlConfiguration { private static String CONTENT_NOT_BE_NULL = "内容不能为 null"; @@ -103,6 +94,72 @@ public abstract class AbstractConfig extends YamlConfiguration { } } + protected void convertMapsToSections(Map input, ConfigurationSection section) { + for (Map.Entry entry : input.entrySet()) { + String key = entry.getKey().toString(); + Object value = entry.getValue(); + + if (value instanceof Map) { + convertMapsToSections((Map) value, section.createSection(key)); + } else { + section.set(key, value); + } + } + } + + protected String parseHeader(String input) { + String[] lines = input.split("\r?\n", -1); + StringBuilder result = new StringBuilder(); + boolean readingHeader = true; + boolean foundHeader = false; + + 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())); + } + + foundHeader = true; + } else if ((foundHeader) && (line.length() == 0)) { + result.append("\n"); + } else if (foundHeader) { + readingHeader = false; + } + } + + return result.toString(); + } + + @Override + protected String buildHeader() { + String header = options().header(); + if (header == null) { + return ""; + } + + StringBuilder builder = new StringBuilder(); + String[] lines = header.split("\r?\n", -1); + boolean startedHeader = false; + + for (int i = lines.length - 1; i >= 0; i--) { + builder.insert(0, "\n"); + + if ((startedHeader) || (lines[i].length() != 0)) { + builder.insert(0, lines[i]); + builder.insert(0, COMMENT_PREFIX); + startedHeader = true; + } + } + + return builder.toString(); + } + @Override public void save(File file) throws IOException { Validate.notNull(file, FILE_NOT_BE_NULL);