diff --git a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java index 4cfcf2b..fb73a68 100644 --- a/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java +++ b/src/main/java/com/yutou/bilibili/QQBot/QQBotManager.java @@ -72,7 +72,7 @@ public class QQBotManager implements ApplicationContextAware { private QQBotManager() { - qqGroup = Long.parseLong((String) ConfigTools.load(ConfigTools.CONFIG, "qq_group")); + qqGroup = ConfigTools.load(ConfigTools.CONFIG, "qq_group",long.class,950075833L); } public void stop(){ if(bot!=null){ @@ -81,7 +81,7 @@ public class QQBotManager implements ApplicationContextAware { } } public void init() { - if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "qq_bot"))) { + if (!ConfigTools.load(ConfigTools.CONFIG, "qq_bot",boolean.class)) { return; } new Thread(() -> { diff --git a/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java b/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java index c387466..66befd4 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java +++ b/src/main/java/com/yutou/bilibili/Tools/ConfigTools.java @@ -27,23 +27,27 @@ public class ConfigTools { } } - public static Object load(String type,String key){ - File file=new File(type); + public static Object load(String type, String key) { + return load(type, key, Object.class, null); + } + + public static T load(String type, String key, Class t) { + return load(type, key, t, null); + } + + public static T load(String type, String key, Class t, T def) { + File file = new File(type); //com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath()); - String src=readFile(file); - if(src!=null){ + String src = readFile(file); + if (src != null) { try { - JSONObject json=JSONObject.parseObject(src); - if(json==null){ - json=new JSONObject(); - saveFile(file,json.toJSONString()); - } - return json.getOrDefault(key, ""); - }catch (Exception e){ - return ""; + JSONObject json = JSONObject.parseObject(src); + return json.getObject(key, t); + } catch (Exception e) { } } - return ""; + + return def; } public static boolean save(String type,String key,Object data){ File file=new File(type);