mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-24 02:08:48 +00:00
feat: 添加自定义解析注册 优化配置代码
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
fffe3156a2
commit
7a82f63e83
@ -1,18 +1,6 @@
|
|||||||
package pw.yumc.YumCore.config;
|
package pw.yumc.YumCore.config;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import com.google.common.io.Files;
|
||||||
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 org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
@ -21,14 +9,15 @@ import org.yaml.snakeyaml.DumperOptions;
|
|||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
import org.yaml.snakeyaml.error.YAMLException;
|
import org.yaml.snakeyaml.error.YAMLException;
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
import org.yaml.snakeyaml.representer.Representer;
|
||||||
|
|
||||||
import com.google.common.io.Files;
|
|
||||||
|
|
||||||
import pw.yumc.YumCore.bukkit.Log;
|
import pw.yumc.YumCore.bukkit.Log;
|
||||||
import pw.yumc.YumCore.bukkit.P;
|
import pw.yumc.YumCore.bukkit.P;
|
||||||
import pw.yumc.YumCore.config.yaml.BukkitConstructor;
|
import pw.yumc.YumCore.config.yaml.BukkitConstructor;
|
||||||
import pw.yumc.YumCore.config.yaml.BukkitRepresenter;
|
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 {
|
public abstract class AbstractConfig extends YamlConfiguration {
|
||||||
private static String CONTENT_NOT_BE_NULL = "内容不能为 null";
|
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");
|
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 static Plugin plugin = P.instance;
|
||||||
|
|
||||||
protected DumperOptions yamlOptions = new DumperOptions();
|
protected DumperOptions yamlOptions = new DumperOptions();
|
||||||
protected Representer yamlRepresenter = new BukkitRepresenter();
|
protected Representer yamlRepresenter = BukkitRepresenter.DEFAULT;
|
||||||
protected Yaml yamlz = new Yaml(new BukkitConstructor(), yamlRepresenter, yamlOptions);
|
protected Yaml yamlz = new Yaml(BukkitConstructor.DEFAULT, yamlRepresenter, yamlOptions);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置文件内容MAP
|
* 配置文件内容MAP
|
||||||
@ -69,7 +57,7 @@ public abstract class AbstractConfig extends YamlConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
Validate.notNull(file, FILE_NOT_BE_NULL);
|
||||||
FileInputStream stream = new FileInputStream(file);
|
FileInputStream stream = new FileInputStream(file);
|
||||||
load(new InputStreamReader(stream, UTF_8));
|
load(new InputStreamReader(stream, UTF_8));
|
||||||
@ -77,16 +65,13 @@ public abstract class AbstractConfig extends YamlConfiguration {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
||||||
BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
|
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
try {
|
try (BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader)) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = input.readLine()) != null) {
|
while ((line = input.readLine()) != null) {
|
||||||
builder.append(line);
|
builder.append(line);
|
||||||
builder.append(newLine);
|
builder.append(newLine);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
input.close();
|
|
||||||
}
|
}
|
||||||
loadFromString(builder.toString());
|
loadFromString(builder.toString());
|
||||||
}
|
}
|
||||||
@ -96,10 +81,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
|
|||||||
Validate.notNull(contents, CONTENT_NOT_BE_NULL);
|
Validate.notNull(contents, CONTENT_NOT_BE_NULL);
|
||||||
try {
|
try {
|
||||||
contentsMap = (Map) yamlz.load(contents);
|
contentsMap = (Map) yamlz.load(contents);
|
||||||
} catch (YAMLException e) {
|
} catch (YAMLException | ClassCastException e) {
|
||||||
throw new InvalidConfigurationException(e);
|
throw new InvalidConfigurationException(e);
|
||||||
} catch (ClassCastException e) {
|
|
||||||
throw new InvalidConfigurationException(TOP_KEY_MUST_BE_MAP);
|
|
||||||
}
|
}
|
||||||
String header = parseHeader(contents);
|
String header = parseHeader(contents);
|
||||||
if (header.length() > 0) {
|
if (header.length() > 0) {
|
||||||
@ -118,11 +101,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
|
|||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
Log.info(String.format(CREATE_NEW_CONFIG, file.toPath()));
|
Log.info(String.format(CREATE_NEW_CONFIG, file.toPath()));
|
||||||
}
|
}
|
||||||
Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8);
|
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8)) {
|
||||||
try {
|
|
||||||
writer.write(data);
|
writer.write(data);
|
||||||
} finally {
|
|
||||||
writer.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
package pw.yumc.YumCore.config;
|
package pw.yumc.YumCore.config;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
|
||||||
|
|
||||||
public class CommentConfig extends AbstractConfig {
|
public class CommentConfig extends AbstractConfig {
|
||||||
// 新增保留注释字段
|
// 新增保留注释字段
|
||||||
protected static String commentPrefixSymbol = "'注释 ";
|
protected static String commentPrefixSymbol = "'注释 ";
|
||||||
protected static String commentSuffixSymbol = "': 注释";
|
protected static String commentSuffixSymbol = "': 注释";
|
||||||
|
|
||||||
protected static String fromRegex = "( {0,})(#.*)";
|
protected static String fromRegex = "( *)(#.*)";
|
||||||
protected static Pattern fromPattern = Pattern.compile(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 toPattern = Pattern.compile(toRegex);
|
||||||
|
|
||||||
protected static Pattern countSpacePattern = Pattern.compile("( {0,})(- ){0,}(.*)");
|
protected static Pattern countSpacePattern = Pattern.compile("( *)(- )*(.*)");
|
||||||
|
|
||||||
protected static int commentSplitWidth = 90;
|
protected static int commentSplitWidth = 90;
|
||||||
|
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
package pw.yumc.YumCore.config.yaml;
|
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.Location;
|
||||||
import org.bukkit.configuration.file.YamlConstructor;
|
import org.bukkit.configuration.file.YamlConstructor;
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||||
import org.yaml.snakeyaml.error.YAMLException;
|
import org.yaml.snakeyaml.error.YAMLException;
|
||||||
import org.yaml.snakeyaml.nodes.Node;
|
import org.yaml.snakeyaml.nodes.Node;
|
||||||
import org.yaml.snakeyaml.nodes.Tag;
|
import org.yaml.snakeyaml.nodes.Tag;
|
||||||
|
|
||||||
import pw.yumc.YumCore.bukkit.L;
|
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 class BukkitConstructor extends YamlConstructor {
|
||||||
|
public static BukkitConstructor DEFAULT = new BukkitConstructor();
|
||||||
Map<String, Method> constructor = new HashMap<>();
|
Map<String, Method> constructor = new HashMap<>();
|
||||||
|
|
||||||
public BukkitConstructor() {
|
public BukkitConstructor() {
|
||||||
@ -23,6 +23,10 @@ public class BukkitConstructor extends YamlConstructor {
|
|||||||
this.loadConstructor();
|
this.loadConstructor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void register(String classname, Method method) {
|
||||||
|
DEFAULT.constructor.put(classname, method);
|
||||||
|
}
|
||||||
|
|
||||||
private void loadConstructor() {
|
private void loadConstructor() {
|
||||||
constructor.put(Location.class.getName(), L.deserialize);
|
constructor.put(Location.class.getName(), L.deserialize);
|
||||||
}
|
}
|
||||||
@ -30,9 +34,7 @@ public class BukkitConstructor extends YamlConstructor {
|
|||||||
private class ConstructCustomObject extends ConstructYamlMap {
|
private class ConstructCustomObject extends ConstructYamlMap {
|
||||||
@Override
|
@Override
|
||||||
public Object construct(Node node) {
|
public Object construct(Node node) {
|
||||||
if (node.isTwoStepsConstruction()) {
|
if (node.isTwoStepsConstruction()) { throw new YAMLException("Unexpected referential mapping structure. Node: " + node); }
|
||||||
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<?, ?> raw = (Map<?, ?>) super.construct(node);
|
Map<?, ?> raw = (Map<?, ?>) super.construct(node);
|
||||||
|
|
||||||
|
@ -3,14 +3,20 @@ package pw.yumc.YumCore.config.yaml;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.configuration.file.YamlRepresenter;
|
import org.bukkit.configuration.file.YamlRepresenter;
|
||||||
import org.yaml.snakeyaml.nodes.Node;
|
import org.yaml.snakeyaml.nodes.Node;
|
||||||
|
import org.yaml.snakeyaml.representer.Represent;
|
||||||
import pw.yumc.YumCore.bukkit.L;
|
import pw.yumc.YumCore.bukkit.L;
|
||||||
|
|
||||||
public class BukkitRepresenter extends YamlRepresenter {
|
public class BukkitRepresenter extends YamlRepresenter {
|
||||||
|
public static BukkitRepresenter DEFAULT = new BukkitRepresenter();
|
||||||
|
|
||||||
public BukkitRepresenter() {
|
public BukkitRepresenter() {
|
||||||
this.multiRepresenters.put(Location.class, new RepresentLocation());
|
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 {
|
public class RepresentLocation extends RepresentMap {
|
||||||
@Override
|
@Override
|
||||||
public Node representData(Object data) {
|
public Node representData(Object data) {
|
||||||
|
Loading…
Reference in New Issue
Block a user