Skip to content

Commit

Permalink
Merge pull request #27 from Blue-Millennium/18-重写配置文件系统
Browse files Browse the repository at this point in the history
Fin - 18 重写配置文件系统
  • Loading branch information
Suisuroru authored Jan 21, 2025
2 parents f0d41f4 + 31ed979 commit 20a3bf4
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 278 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package fun.suya.suisuroru.commands.execute.othercommands.config;

import fun.suya.suisuroru.config.Config;
import fun.suya.suisuroru.config.ConfigKeys;
import fun.suya.suisuroru.config.ConfigRemap;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
import java.util.Map;

import static fun.suya.suisuroru.config.ConfigManager.getConfigFieldNames;
import static fun.xd.suka.Main.LOGGER;

/**
Expand All @@ -20,8 +18,6 @@
*/
public class Query implements CommandExecutor {

private static final Map<String, ConfigKeys> CONFIG_KEYS_MAP = ConfigKeys.configKeysList;

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
// 检查发送者是否具有OP权限
Expand All @@ -35,17 +31,16 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
} else if (command.getName().equals("baseplugin")) {
sender.sendMessage("§c/baseplugin config query [查询配置]");
}
String allConfigNames = String.join("|", CONFIG_KEYS_MAP.keySet());
String allConfigNames = String.join("|", getConfigFieldNames());
sender.sendMessage("§a所有配置项名称: " + allConfigNames);
} else {
String configName = args[0];
if (!CONFIG_KEYS_MAP.containsKey(configName)) {
if (!getConfigFieldNames().contains(configName)) {
sender.sendMessage("§c配置项 " + configName + " 不存在,请检查拼写");
return true;
}
String internalConfigName = ConfigRemap.configMapping.get(configName);
try {
Field field = Config.class.getDeclaredField(internalConfigName);
Field field = Config.class.getDeclaredField(configName);
Object value = field.get(null);
sender.sendMessage("§a配置项 " + configName + " 当前的值为: " + value);
} catch (NoSuchFieldException | IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package fun.suya.suisuroru.commands.execute.othercommands.config;

import fun.suya.suisuroru.config.ConfigKeys;
import fun.suya.suisuroru.config.ConfigManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

import static fun.suya.suisuroru.config.ConfigManager.getConfigFieldNames;
import static fun.xd.suka.Main.LOGGER;

/**
Expand All @@ -17,7 +15,6 @@
* function: Set new config
*/
public class Set implements CommandExecutor {
private static final Map<String, ConfigKeys> CONFIG_KEYS_MAP = ConfigKeys.configKeysList;
ConfigManager configManager = new ConfigManager();

@Override
Expand All @@ -33,7 +30,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
} else if (command.getName().equals("baseplugin")) {
sender.sendMessage("§c/baseplugin config set [修改参数] [修改值]");
}
String allConfigNames = String.join("|", CONFIG_KEYS_MAP.keySet());
String allConfigNames = String.join("|", getConfigFieldNames());
sender.sendMessage("§a所有配置项名称: " + allConfigNames);
return true;
}
Expand All @@ -42,16 +39,13 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
String value = args[1];

// 检查配置项是否存在
if (!CONFIG_KEYS_MAP.containsKey(configName)) {
if (!getConfigFieldNames().contains(configName)) {
sender.sendMessage("§c配置项 " + configName + " 不存在,请检查拼写");
return true;
}

ConfigKeys key = CONFIG_KEYS_MAP.get(configName);
try {
configManager.setConfigValue(key, value);
configManager.save();
configManager.load();
configManager.setConfigValue(configName, value);
sender.sendMessage("§a配置项 " + configName + " 已成功设置为 " + value);
} catch (Exception e) {
sender.sendMessage("§c修改配置项 " + configName + " 的值时出错,请检查配置文件或联系开发人员。");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public static void BanMessage(String message) {
Bukkit.broadcastMessage("本地黑名单: " + message);
try {
if (Config.QQRobotEnabled) {
Main.BOT.getGroup(Config.syncChatGroup).sendMessage(message);
Main.BOT.getGroup(Config.reportGroup).sendMessage(message);
Main.BOT.getGroup(Config.SyncChatGroup).sendMessage(message);
Main.BOT.getGroup(Config.ReportGroup).sendMessage(message);
}
} catch (Exception e) {
Main.LOGGER.info("Error when report message to QQ group");
Expand Down Expand Up @@ -75,7 +75,7 @@ private void TransferToUnionBan(Player targetPlayer, CommandSender sender, Strin
String message = "玩家 " + targetPlayer.getName() + " 已被 " + sender.getName() + " 以[ " + reason + " ]的理由封禁";
BanMessage(message);
if (!Config.UnionBanCheckOnly) {
reportBanData(new UnionBanMain.BanPair<>(targetPlayer.getUniqueId(), reason, new Date(), Config.servername));
reportBanData(new UnionBanMain.BanPair<>(targetPlayer.getUniqueId(), reason, new Date(), Config.ServerName));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fun.suya.suisuroru.commands.tab.othercommands;

import fun.suya.suisuroru.config.ConfigKeys;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
Expand All @@ -10,6 +9,8 @@
import java.util.ArrayList;
import java.util.List;

import static fun.suya.suisuroru.config.ConfigManager.getConfigFieldNames;

/**
* @author Suisuroru
* Date: 2024/10/18 22:11
Expand All @@ -26,7 +27,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
completions.add("set");
} else if (args.length == 2) {
if (args[0].equals("query") || args[0].equals("set")) {
completions.addAll(ConfigKeys.configKeysList.keySet());
completions.addAll(getConfigFieldNames());
}
}
return completions;
Expand Down
62 changes: 31 additions & 31 deletions src/main/java/fun/suya/suisuroru/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
* function: Initialize config format
*/
public class Config {
public static boolean VanillaCommandsRewritten;
public static boolean qqCheckEnabled;
public static boolean syncChatEnabled;
public static boolean SyncChatEnabledQ2SOnly;
public static String botWsUrl;
public static String botWsToken;
public static long syncChatGroup;
public static long reportGroup;
public static String joinServerMessage;
public static String leaveServerMessage;
public static String sayServerMessage;
public static String sayQQMessage;
public static String reportMessage;
public static String disTitle;
public static String webhookUrl;
public static String servername;
public static boolean RconEnabled;
public static String ExecuteCommandPrefix;
public static String RconEnabledGroups;
public static String RconIP;
public static int RconPort;
public static String RconPassword;
public static boolean RconEnforceOperator;
public static boolean QQRobotEnabled;
public static boolean UnionBanEnabled;
public static boolean UnionBanCheckOnly;
public static String UnionBanCheckUrl;
public static String UnionBanReportUrl;
public static String UnionBanReportKey;
public static String QQCheckStartWord;
}
public static boolean VanillaCommandsRewritten = true;
public static boolean QQCheckEnabled = true;
public static boolean SyncChatEnabled = true;
public static boolean SyncChatEnabledQ2SOnly = false;
public static String BotWsUrl = "ws://0.0.0.0:3001";
public static String BotWsToken = "114514";
public static long SyncChatGroup = 123456L;
public static long ReportGroup = 123456L;
public static String JoinServerMessage = "%NAME% joined the server";
public static String LeaveServerMessage = "%NAME% left the server";
public static String SayServerMessage = "%NAME%: %MESSAGE%";
public static String SayQQMessage = "[QQ] %NAME%: %MESSAGE%";
public static String ReportMessage = "%NAME% was logging in \nIP: %IP% %IPINFO% \nLoginResult: %RESULT%";
public static String DisTitle = "[QQLogin] 请完成登录验证, 验证码: %CODE%";
public static String WebhookUrl = "http://localhost:6888/webhook";
public static String ServerName = "ServerName";
public static boolean RconEnabled = true;
public static String ExecuteCommandPrefix = "*#";
public static String RconEnabledGroups = "123456;234567";
public static String RconIP = "0.0.0.0";
public static int RconPort = 25575;
public static String RconPassword = "password";
public static boolean RconEnforceOperator = true;
public static boolean QQRobotEnabled = true;
public static boolean UnionBanEnabled = false;
public static boolean UnionBanCheckOnly = false;
public static String UnionBanCheckUrl = "https://example.com";
public static String UnionBanReportUrl = "https://example.com";
public static String UnionBanReportKey = "your_report_key";
public static String QQCheckStartWord = "Check#";
}
66 changes: 0 additions & 66 deletions src/main/java/fun/suya/suisuroru/config/ConfigKeys.java

This file was deleted.

Loading

0 comments on commit 20a3bf4

Please sign in to comment.