1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-28 02:48:48 +00:00

feat: 添加自定义解析

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-10-03 01:12:20 +08:00
parent 2cf2703c45
commit 1d51ddf7a4
3 changed files with 95 additions and 4 deletions

View File

@ -16,8 +16,6 @@ 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;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
@ -28,6 +26,8 @@ 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.BukkitRepresenter;
/** /**
* 抽象配置文件 * 抽象配置文件
@ -48,8 +48,8 @@ public abstract class AbstractConfig extends YamlConfiguration {
protected static Plugin plugin = P.instance; protected static Plugin plugin = P.instance;
protected final DumperOptions yamlOptions = new DumperOptions(); protected final DumperOptions yamlOptions = new DumperOptions();
protected final Representer yamlRepresenter = new YamlRepresenter(); protected final Representer yamlRepresenter = new BukkitRepresenter();
protected final Yaml yamlz = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions); protected final Yaml yamlz = new Yaml(new BukkitConstructor(), yamlRepresenter, yamlOptions);
/** /**
* 配置文件内容MAP * 配置文件内容MAP

View File

@ -0,0 +1,71 @@
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;
public class BukkitConstructor extends YamlConstructor {
Map<String, Method> constructor = new HashMap<>();
public BukkitConstructor() {
this.yamlConstructors.put(Tag.MAP, new ConstructCustomObject());
this.loadConstructor();
}
private void loadConstructor() {
constructor.put(Location.class.getName(), L.deserialize);
}
private class ConstructCustomObject extends ConstructYamlMap {
@Override
public Object construct(final Node node) {
if (node.isTwoStepsConstruction()) {
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
final Map<?, ?> raw = (Map<?, ?>) super.construct(node);
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
final Map<String, Object> typed = new LinkedHashMap<>(raw.size());
for (final Map.Entry<?, ?> entry : raw.entrySet()) {
typed.put(entry.getKey().toString(), entry.getValue());
}
// 自定义解析部分
final String key = raw.get(ConfigurationSerialization.SERIALIZED_TYPE_KEY).toString();
if (constructor.containsKey(key)) {
try {
return constructor.get(key).invoke(null, raw);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new YAMLException("Could not deserialize object", ex);
}
}
// Bukkit自动解析
try {
return ConfigurationSerialization.deserializeObject(typed);
} catch (final IllegalArgumentException ex) {
throw new YAMLException("Could not deserialize object", ex);
}
}
return raw;
}
@Override
public void construct2ndStep(final Node node, final Object object) {
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
}
}

View File

@ -0,0 +1,20 @@
package pw.yumc.YumCore.config.yaml;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlRepresenter;
import org.yaml.snakeyaml.nodes.Node;
import pw.yumc.YumCore.bukkit.L;
public class BukkitRepresenter extends YamlRepresenter {
public BukkitRepresenter() {
this.multiRepresenters.put(Location.class, new RepresentLocation());
}
public class RepresentLocation extends RepresentMap {
@Override
public Node representData(final Object data) {
return super.representData(L.serialize((Location) data));
}
}
}