diff --git a/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java b/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java index 058babe..591fb53 100644 --- a/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java @@ -1,18 +1,6 @@ package pw.yumc.YumCore.config; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -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.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; @@ -21,14 +9,15 @@ 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; + /** * 抽象配置文件 * @@ -37,7 +26,6 @@ import pw.yumc.YumCore.config.yaml.BukkitRepresenter; */ public abstract class AbstractConfig extends YamlConfiguration { private static String CONTENT_NOT_BE_NULL = "内容不能为 null"; - private static String TOP_KEY_MUST_BE_MAP = "顶层键值必须是Map."; protected static Charset UTF_8 = Charset.forName("UTF-8"); @@ -48,8 +36,8 @@ public abstract class AbstractConfig extends YamlConfiguration { protected static Plugin plugin = P.instance; protected DumperOptions yamlOptions = new DumperOptions(); - protected Representer yamlRepresenter = new BukkitRepresenter(); - protected Yaml yamlz = new Yaml(new BukkitConstructor(), yamlRepresenter, yamlOptions); + protected Representer yamlRepresenter = BukkitRepresenter.DEFAULT; + protected Yaml yamlz = new Yaml(BukkitConstructor.DEFAULT, yamlRepresenter, yamlOptions); /** * 配置文件内容MAP @@ -69,7 +57,7 @@ public abstract class AbstractConfig extends YamlConfiguration { } @Override - public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException { + public void load(File file) throws IOException, InvalidConfigurationException { Validate.notNull(file, FILE_NOT_BE_NULL); FileInputStream stream = new FileInputStream(file); load(new InputStreamReader(stream, UTF_8)); @@ -77,16 +65,13 @@ public abstract class AbstractConfig extends YamlConfiguration { @Override public void load(Reader reader) throws IOException, InvalidConfigurationException { - BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); StringBuilder builder = new StringBuilder(); - try { + try (BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader)) { String line; while ((line = input.readLine()) != null) { builder.append(line); builder.append(newLine); } - } finally { - input.close(); } loadFromString(builder.toString()); } @@ -96,10 +81,8 @@ public abstract class AbstractConfig extends YamlConfiguration { Validate.notNull(contents, CONTENT_NOT_BE_NULL); try { contentsMap = (Map) yamlz.load(contents); - } catch (YAMLException e) { + } catch (YAMLException | ClassCastException e) { throw new InvalidConfigurationException(e); - } catch (ClassCastException e) { - throw new InvalidConfigurationException(TOP_KEY_MUST_BE_MAP); } String header = parseHeader(contents); if (header.length() > 0) { @@ -118,11 +101,8 @@ public abstract class AbstractConfig extends YamlConfiguration { file.createNewFile(); Log.info(String.format(CREATE_NEW_CONFIG, file.toPath())); } - Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8); - try { + try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8)) { writer.write(data); - } finally { - writer.close(); } } diff --git a/src/main/java/pw/yumc/YumCore/config/CommentConfig.java b/src/main/java/pw/yumc/YumCore/config/CommentConfig.java index ea362fb..98faf8f 100644 --- a/src/main/java/pw/yumc/YumCore/config/CommentConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/CommentConfig.java @@ -1,25 +1,25 @@ package pw.yumc.YumCore.config; +import org.bukkit.configuration.InvalidConfigurationException; +import org.yaml.snakeyaml.DumperOptions; + import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.bukkit.configuration.InvalidConfigurationException; -import org.yaml.snakeyaml.DumperOptions; - public class CommentConfig extends AbstractConfig { // 新增保留注释字段 protected static String commentPrefixSymbol = "'注释 "; protected static String commentSuffixSymbol = "': 注释"; - protected static String fromRegex = "( {0,})(#.*)"; + protected static String fromRegex = "( *)(#.*)"; protected static Pattern fromPattern = Pattern.compile(fromRegex); - protected static String toRegex = "( {0,})(- ){0,}" + "(" + commentPrefixSymbol + ")" + "(#.*)" + "(" + commentSuffixSymbol + ")"; + protected static String toRegex = "( *)(- )*" + "(" + commentPrefixSymbol + ")" + "(#.*)" + "(" + commentSuffixSymbol + ")"; protected static Pattern toPattern = Pattern.compile(toRegex); - protected static Pattern countSpacePattern = Pattern.compile("( {0,})(- ){0,}(.*)"); + protected static Pattern countSpacePattern = Pattern.compile("( *)(- )*(.*)"); protected static int commentSplitWidth = 90; diff --git a/src/main/java/pw/yumc/YumCore/config/yaml/BukkitConstructor.java b/src/main/java/pw/yumc/YumCore/config/yaml/BukkitConstructor.java index 4a0f69f..5ff16ab 100644 --- a/src/main/java/pw/yumc/YumCore/config/yaml/BukkitConstructor.java +++ b/src/main/java/pw/yumc/YumCore/config/yaml/BukkitConstructor.java @@ -1,21 +1,21 @@ package pw.yumc.YumCore.config.yaml; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; - import org.bukkit.Location; import org.bukkit.configuration.file.YamlConstructor; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.yaml.snakeyaml.error.YAMLException; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Tag; - import pw.yumc.YumCore.bukkit.L; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + public class BukkitConstructor extends YamlConstructor { + public static BukkitConstructor DEFAULT = new BukkitConstructor(); Map constructor = new HashMap<>(); public BukkitConstructor() { @@ -23,6 +23,10 @@ public class BukkitConstructor extends YamlConstructor { this.loadConstructor(); } + public static void register(String classname, Method method) { + DEFAULT.constructor.put(classname, method); + } + private void loadConstructor() { constructor.put(Location.class.getName(), L.deserialize); } @@ -30,9 +34,7 @@ public class BukkitConstructor extends YamlConstructor { private class ConstructCustomObject extends ConstructYamlMap { @Override public Object construct(Node node) { - if (node.isTwoStepsConstruction()) { - throw new YAMLException("Unexpected referential mapping structure. Node: " + node); - } + if (node.isTwoStepsConstruction()) { throw new YAMLException("Unexpected referential mapping structure. Node: " + node); } Map raw = (Map) super.construct(node); diff --git a/src/main/java/pw/yumc/YumCore/config/yaml/BukkitRepresenter.java b/src/main/java/pw/yumc/YumCore/config/yaml/BukkitRepresenter.java index 9c76087..95ffead 100644 --- a/src/main/java/pw/yumc/YumCore/config/yaml/BukkitRepresenter.java +++ b/src/main/java/pw/yumc/YumCore/config/yaml/BukkitRepresenter.java @@ -3,14 +3,20 @@ package pw.yumc.YumCore.config.yaml; import org.bukkit.Location; import org.bukkit.configuration.file.YamlRepresenter; import org.yaml.snakeyaml.nodes.Node; - +import org.yaml.snakeyaml.representer.Represent; import pw.yumc.YumCore.bukkit.L; public class BukkitRepresenter extends YamlRepresenter { + public static BukkitRepresenter DEFAULT = new BukkitRepresenter(); + public BukkitRepresenter() { this.multiRepresenters.put(Location.class, new RepresentLocation()); } + public static void register(Class clazz, Represent represent) { + DEFAULT.multiRepresenters.put(clazz, represent); + } + public class RepresentLocation extends RepresentMap { @Override public Node representData(Object data) {