修复SMTP配置热更新问题:加载时自动补全缺失配置项,支持UTF-8 BOM,添加email reload命令
This commit is contained in:
@@ -119,8 +119,64 @@ public class ConfigManager {
|
||||
public void loadSmtp() {
|
||||
smtpSettings.clear();
|
||||
File file = new File(dataFolder, "SMTP设置.txt");
|
||||
if (!file.exists()) createDefaultSmtp(file);
|
||||
if (!file.exists()) {
|
||||
createDefaultSmtp(file);
|
||||
} else {
|
||||
// 文件存在,先加载现有配置
|
||||
loadMap(file, smtpSettings);
|
||||
// 检查并补全缺失的配置项
|
||||
ensureSmtpConfigComplete(file);
|
||||
}
|
||||
loadMap(file, smtpSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载邮件配置文件(热更新)
|
||||
*/
|
||||
public void reloadSmtp() {
|
||||
loadSmtp();
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保SMTP配置文件包含所有必要的配置项
|
||||
* 如果缺少配置项,自动补充默认值并保存
|
||||
*/
|
||||
private void ensureSmtpConfigComplete(File file) {
|
||||
boolean changed = false;
|
||||
|
||||
// 检查必要的SMTP配置项
|
||||
String[] requiredSmtpKeys = {"smtp地址", "smtp端口", "smtp账号", "smtp密码", "发件人名称", "smtp加密"};
|
||||
for (String key : requiredSmtpKeys) {
|
||||
if (!smtpSettings.containsKey(key)) {
|
||||
// 根据key提供默认值
|
||||
String defaultValue = "";
|
||||
switch (key) {
|
||||
case "smtp地址": defaultValue = "smtp.example.com"; break;
|
||||
case "smtp端口": defaultValue = "465"; break;
|
||||
case "发件人名称": defaultValue = "Sdf1_login"; break;
|
||||
case "smtp加密": defaultValue = "true"; break;
|
||||
}
|
||||
smtpSettings.put(key, defaultValue);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查邮箱验证模式配置项
|
||||
if (!smtpSettings.containsKey("邮箱验证模式")) {
|
||||
smtpSettings.put("邮箱验证模式", "默认");
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// 检查邮箱后缀列表配置项
|
||||
if (!smtpSettings.containsKey("邮箱后缀列表")) {
|
||||
smtpSettings.put("邮箱后缀列表", "");
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// 如果有变更,保存配置文件
|
||||
if (changed) {
|
||||
saveSmtp();
|
||||
}
|
||||
}
|
||||
|
||||
public String getSmtp(String key) {
|
||||
@@ -571,7 +627,15 @@ public class ConfigManager {
|
||||
private void loadMap(File file, Map<String, String> map) {
|
||||
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
boolean firstLine = true;
|
||||
while ((line = r.readLine()) != null) {
|
||||
// 处理UTF-8 BOM头
|
||||
if (firstLine) {
|
||||
firstLine = false;
|
||||
if (line.startsWith("\uFEFF")) {
|
||||
line = line.substring(1);
|
||||
}
|
||||
}
|
||||
line = line.trim();
|
||||
if (line.isEmpty() || line.startsWith("#")) continue;
|
||||
int eq = line.indexOf('=');
|
||||
|
||||
@@ -6701,6 +6701,18 @@ public class Main extends JavaPlugin
|
||||
}
|
||||
Player p2 = (Player) sender;
|
||||
if (args.length >= 2) {
|
||||
// 检查是否为reload子命令(仅管理员可用)
|
||||
if (args[1].equalsIgnoreCase("reload")) {
|
||||
if (!p2.hasPermission("sdf1_login.admin")) {
|
||||
p2.sendMessage("§c权限不足,仅管理员可用");
|
||||
return true;
|
||||
}
|
||||
config.reloadSmtp();
|
||||
p2.sendMessage("§a邮件配置已重载");
|
||||
p2.sendMessage("§7SMTP地址: " + config.getSmtp("smtp地址"));
|
||||
p2.sendMessage("§7验证模式: " + config.getEmailValidationMode());
|
||||
return true;
|
||||
}
|
||||
String emailAddr = args[1];
|
||||
if (!emailAddr.contains("@")
|
||||
|| !emailAddr.contains(".")) {
|
||||
|
||||
Reference in New Issue
Block a user