AuMcBot/src/main/java/com/yaohun/mcbot/config/Config.java
2025-07-18 22:50:07 +08:00

127 lines
4.3 KiB
Java

package com.yaohun.mcbot.config;
import com.google.common.io.ByteStreams;
import com.yaohun.mcbot.McBot;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class Config {
private static Configuration config;
public static String SQL_Host;
public static String SQL_Port;
public static String SQL_Database;
public static String SQL_Users;
public static String SQL_Password;
public static List<Long> BOT_GroupAccounts = new ArrayList<>();
public static String Group_Info = "";
private static HashMap<String,String> messageMap = new HashMap<>();
public static void reloadConfig(McBot plugin) {
if(!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdir();
}
File file = new File(plugin.getDataFolder(), "config.yml");
if(!file.exists()) {
try {
file.createNewFile();
try (InputStream is = plugin.getResourceAsStream("config.yml");
OutputStream os = Files.newOutputStream(file.toPath())) {
ByteStreams.copy(is, os);
}
} catch (IOException e) {
throw new RuntimeException("Unable to create configuration file", e);
}
}
try {
ConfigurationProvider cfg = ConfigurationProvider.getProvider(YamlConfiguration.class);
config = cfg.load(file);
loadMySQLData();
loadConfigData();
loadMessageData();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveBOT_GroupAccounts() {
List<String > groupAccountList = new ArrayList<>();
for (Long l : BOT_GroupAccounts) {
groupAccountList.add(l.toString());
}
File file = new File(McBot.inst().getDataFolder(), "config.yml");
// 转换 List<Long> 为 List<String>,避免 YAML 不兼容 Long 处理
config.set("Group-Numbers", groupAccountList);
try {
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void loadMySQLData(){
SQL_Host = config.getString("MYSQL.Host");
SQL_Port = config.getString("MYSQL.Port");
SQL_Database = config.getString("MYSQL.Database");
SQL_Users = config.getString("MYSQL.Users");
SQL_Password = config.getString("MYSQL.Password");
}
private static void loadConfigData(){
List<String> stringList = config.getStringList("Group-Numbers");
System.out.println("[AuBot] 监听QQ群: ");
for (String value : stringList) {
BOT_GroupAccounts.add(Long.parseLong(value));
System.out.println("- "+value);
}
Group_Info = config.getString("Group_Info","").replace("&","§");
}
private static void loadMessageData(){
if(config.getString("Message") == null) return;
for (String key : config.getSection("Message").getKeys()){
String value = config.getString("Message." + key);
messageMap.put(key, value.replace("&","§"));
}
System.out.println("[AuBot] 语言参数 "+messageMap.size()+"");
}
public static String getMessage(String key){
if(messageMap.containsKey(key)){
return messageMap.get(key);
}
return "缺少节点."+key;
}
public static String getRandomString() {
String str = "CEFGHIJKLMNPQTXY34679";
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
int number = random.nextInt(21);
sb.append(str.charAt(number));
}
return sb.toString();
}
public static String extractBindCode(String message) {
return message
.replace("/绑定 ", "")
.replace(" ", "")
.toUpperCase()
.replaceAll("[^A-Z0-9]", "");
}
}