From ef15eefc5d7cf305846107a6f28baa4a66581e6d Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 20 Sep 2016 16:07:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../YumCore/config/AbstractInjectConfig.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java b/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java index 1878b7d..9b47c4c 100644 --- a/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java +++ b/src/main/java/pw/yumc/YumCore/config/AbstractInjectConfig.java @@ -5,7 +5,9 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collections; +import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; import pw.yumc.YumCore.bukkit.Log; @@ -20,7 +22,7 @@ public abstract class AbstractInjectConfig { private static final String INJECT_TYPE_ERROR = "配置节点 %s 数据类型不匹配 应该为: %s 但实际为: %s!"; private static final String INJECT_ERROR = "自动注入配置失败 可能造成插件运行错误 %s: %s!"; private static final String DATE_PARSE_ERROR = "配置节点 {0} 日期解析失败 格式应该为: {1} 但输入值为: {2}!"; - private static final String PATH_NOT_FOUND = "配置节点 %s 丢失 可能造成插件运行错误!"; + private static final String PATH_NOT_FOUND = "配置节点 %s 丢失 已设置为默认空值!"; private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; private static final SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); private ConfigurationSection config; @@ -51,6 +53,21 @@ public abstract class AbstractInjectConfig { } } + /** + * 添加默认值 + * + * @param field + * 字段 + * @param value + * 值 + */ + private void applyDefault(final Field field, Object value) { + switch (field.getType().getName()) { + case "java.util.List": + value = Collections.emptyList(); + } + } + /** * 转换字段值类型 * @@ -123,6 +140,9 @@ public abstract class AbstractInjectConfig { if (!type.equals(value.getClass())) { value = convertType(type, path, value); } + if (type.equals(String.class)) { + value = ChatColor.translateAlternateColorCodes('&', (String) value); + } field.set(this, value); } catch (final IllegalArgumentException ex) { Log.w(INJECT_TYPE_ERROR, path, field.getType().getName(), value.getClass().getName()); @@ -152,6 +172,7 @@ public abstract class AbstractInjectConfig { if (value == null) { if (field.getAnnotation(Nullable.class) == null) { Log.w(PATH_NOT_FOUND, path); + applyDefault(field, value); } return; }