From 00c049573eb323c71c5d559f025dd8ebc8e75c1b Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Mon, 11 Apr 2022 11:56:23 +0800 Subject: [PATCH] =?UTF-8?q?ConfigTools=E6=96=B0=E5=A2=9E=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=B9=E6=B3=95=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=EF=BC=8CQQbot=E6=97=A0=E6=B3=95=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yutou/bilibili/QQBot/QQBotManager.java | 4 +-- .../com/yutou/bilibili/Tools/ConfigTools.java | 30 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) 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);