ConfigTools新增读取配置方法

修复没有配置文件时,QQbot无法初始化的问题
This commit is contained in:
Yutousama 2022-04-11 11:56:23 +08:00
parent cdbafea7b5
commit 00c049573e
2 changed files with 19 additions and 15 deletions

View File

@ -72,7 +72,7 @@ public class QQBotManager implements ApplicationContextAware {
private QQBotManager() { 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(){ public void stop(){
if(bot!=null){ if(bot!=null){
@ -81,7 +81,7 @@ public class QQBotManager implements ApplicationContextAware {
} }
} }
public void init() { public void init() {
if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "qq_bot"))) { if (!ConfigTools.load(ConfigTools.CONFIG, "qq_bot",boolean.class)) {
return; return;
} }
new Thread(() -> { new Thread(() -> {

View File

@ -27,23 +27,27 @@ public class ConfigTools {
} }
} }
public static Object load(String type,String key){ public static Object load(String type, String key) {
File file=new File(type); return load(type, key, Object.class, null);
}
public static <T> T load(String type, String key, Class<T> t) {
return load(type, key, t, null);
}
public static <T> T load(String type, String key, Class<T> t, T def) {
File file = new File(type);
//com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath()); //com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
String src=readFile(file); String src = readFile(file);
if(src!=null){ if (src != null) {
try { try {
JSONObject json=JSONObject.parseObject(src); JSONObject json = JSONObject.parseObject(src);
if(json==null){ return json.getObject(key, t);
json=new JSONObject(); } catch (Exception e) {
saveFile(file,json.toJSONString());
}
return json.getOrDefault(key, "");
}catch (Exception e){
return "";
} }
} }
return "";
return def;
} }
public static boolean save(String type,String key,Object data){ public static boolean save(String type,String key,Object data){
File file=new File(type); File file=new File(type);