diff --git a/build.gradle.kts b/build.gradle.kts index d4dbb28..d57c198 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { group = "cn.huohuas001.huHoBot" description = "HuHoBot Allay Adapter" -version = "0.0.9" +version = "0.0.10" java { toolchain { @@ -17,19 +17,16 @@ java { repositories { mavenCentral() - maven("https://jitpack.io/") - maven("https://repo.opencollab.dev/maven-releases/") - maven("https://repo.opencollab.dev/maven-snapshots/") maven("https://storehouse.okaeri.eu/repository/maven-public/") } dependencies { - compileOnly(group = "org.allaymc.allay", name = "api", version = "master-SNAPSHOT") + compileOnly(group = "org.allaymc.allay", name = "api", version = "0.13.0") compileOnly(group = "org.projectlombok", name = "lombok", version = "1.18.34") - implementation("org.java-websocket:Java-WebSocket:1.5.4") - implementation("com.alibaba.fastjson2:fastjson2:2.0.52") - implementation(group = "com.github.MineBuilders", name = "allaymc-kotlinx", version = "master-SNAPSHOT") + implementation(group = "org.java-websocket", name = "Java-WebSocket", version = "1.5.4") + implementation(group = "com.alibaba.fastjson2", name = "fastjson2", version = "2.0.52") + implementation(group = "eu.okaeri", name = "okaeri-configs-yaml-snakeyaml", version = "5.0.13") annotationProcessor(group = "org.projectlombok", name = "lombok", version = "1.18.34") } @@ -79,28 +76,3 @@ tasks.clean { tasks.shadowJar { archiveClassifier = "AllayMC" } - -tasks.register("runServer") { - outputs.upToDateWhen { false } - dependsOn("shadowJar") - val launcherRepo = "https://raw.githubusercontent.com/AllayMC/AllayLauncher/refs/heads/main/scripts" - val cmdWin = "Invoke-Expression (Invoke-WebRequest -Uri \"${launcherRepo}/install_windows.ps1\").Content" - val cmdLinux = "wget -qO- ${launcherRepo}/install_linux.sh | bash" - val cwd = layout.buildDirectory.file("run").get().asFile - - val shadowJar = tasks.named("shadowJar", ShadowJar::class).get() - from(shadowJar.archiveFile.get().asFile) - into(cwd.resolve("plugins").apply { mkdirs() }) - - val isDownloaded = cwd.listFiles()!!.any { it.isFile && it.nameWithoutExtension == "allay" } - val isWindows = System.getProperty("os.name").startsWith("Windows") - fun launch() = exec { - workingDir = cwd - val cmd = if (isDownloaded) "./allay" else if (isWindows) cmdWin else cmdLinux - if (isWindows) commandLine("powershell", "-Command", cmd) - else commandLine("sh", "-c", cmd) - } - - // https://github.com/gradle/gradle/issues/18716 // kill it manually by click X... - doLast { launch() } -} diff --git a/src/main/java/cn/huohuas001/huHoBot/Api/BotCustomCommand.java b/src/main/java/cn/huohuas001/huHoBot/Api/BotCustomCommand.java index a56afb2..67dae0d 100644 --- a/src/main/java/cn/huohuas001/huHoBot/Api/BotCustomCommand.java +++ b/src/main/java/cn/huohuas001/huHoBot/Api/BotCustomCommand.java @@ -1,24 +1,24 @@ package cn.huohuas001.huHoBot.Api; + import cn.huohuas001.huHoBot.HuHoBot; import cn.huohuas001.huHoBot.WsClient; import com.alibaba.fastjson2.JSONObject; +import lombok.AccessLevel; import lombok.Getter; import org.allaymc.api.eventbus.event.CancellableEvent; import org.allaymc.api.eventbus.event.Event; import java.util.List; +@Getter public class BotCustomCommand extends Event implements CancellableEvent { - @Getter + private final String command; - @Getter private final JSONObject data; - @Getter private final List param; + @Getter(AccessLevel.NONE) private final String packId; - @Getter private final boolean runByAdmin; - private boolean cancelled = false; public BotCustomCommand(String command, JSONObject data, String packId, boolean runByAdmin) { this.command = command; @@ -37,19 +37,4 @@ public void respone(JSONObject msg, String type) { WsClient client = HuHoBot.getClientManager().getClient(); client.respone(msg.toJSONString(), type, packId); } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean b) { - cancelled = b; - } - - @Override - public void cancel() { - setCancelled(true); - } } diff --git a/src/main/java/cn/huohuas001/huHoBot/Command/HuHoBotCommand.java b/src/main/java/cn/huohuas001/huHoBot/Command/HuHoBotCommand.java index d0a0ade..5a4d55c 100644 --- a/src/main/java/cn/huohuas001/huHoBot/Command/HuHoBotCommand.java +++ b/src/main/java/cn/huohuas001/huHoBot/Command/HuHoBotCommand.java @@ -2,54 +2,15 @@ import cn.huohuas001.huHoBot.HuHoBot; import cn.huohuas001.huHoBot.NetEvent.bindRequest; -import org.allaymc.api.command.SimpleCommand; -import org.allaymc.api.command.tree.CommandContext; +import org.allaymc.api.command.Command; import org.allaymc.api.command.tree.CommandTree; import org.allaymc.api.permission.PermissionGroups; import org.allaymc.api.utils.TextFormat; -public class HuHoBotCommand extends SimpleCommand { +public class HuHoBotCommand extends Command { public HuHoBotCommand() { super("huhobot", "HuHoBot's control command"); getPermissions().forEach(PermissionGroups.OPERATOR::addPermission); - - } - - private void onReconnect(CommandContext sender) { - if (HuHoBot.getPlugin().reconnect()) { - sender.addOutput(TextFormat.GOLD + "重连机器人成功."); - } else { - sender.addOutput(TextFormat.DARK_RED + "重连机器人失败:已在连接状态."); - } - - } - - private void onDisconnect(CommandContext sender) { - if (HuHoBot.getPlugin().disConnectServer()) { - sender.addOutput(TextFormat.GOLD + "已断开机器人连接."); - } - } - - private void onBind(CommandContext sender, String args) { - bindRequest obj = HuHoBot.getPlugin().bindRequestObj; - if(obj.confirmBind(args)){ - sender.addOutput(TextFormat.GOLD + "已向服务器发送确认绑定请求,请等待服务端下发配置文件."); - }else{ - sender.addOutput(TextFormat.DARK_RED + "绑定码错误,请重新输入."); - } - } - - private void onReload(CommandContext sender) { - HuHoBot.reloadConfig(); - sender.addOutput(TextFormat.GOLD + "已重载配置文件."); - } - - private void onHelp(CommandContext sender) { - sender.addOutput(TextFormat.AQUA + "HuHoBot 操作相关命令"); - sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reconnect - 重新连接服务器"); - sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot disconnect - 断开服务器连接"); - sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reload - 重载配置文件"); - sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot bind - 确认绑定"); } @Override @@ -57,13 +18,20 @@ public void prepareCommandTree(CommandTree tree) { tree.getRoot() .key("reconnect") .exec(context -> { - onReconnect(context); + if (HuHoBot.getPlugin().reconnect()) { + context.addOutput(TextFormat.GOLD + "重连机器人成功."); + } else { + context.addOutput(TextFormat.DARK_RED + "重连机器人失败:已在连接状态."); + } + return context.success(); }) .root() .key("disconnect") .exec(context -> { - onDisconnect(context); + if (HuHoBot.getPlugin().disConnectServer()) { + context.addOutput(TextFormat.GOLD + "已断开机器人连接."); + } return context.success(); }) .root() @@ -71,19 +39,29 @@ public void prepareCommandTree(CommandTree tree) { .str("code") .exec(context -> { String Code = context.getResult(1); - onBind(context, Code); + bindRequest obj = HuHoBot.getPlugin().bindRequestObj; + if (obj.confirmBind(Code)) { + context.addOutput(TextFormat.GOLD + "已向服务器发送确认绑定请求,请等待服务端下发配置文件."); + } else { + context.addOutput(TextFormat.DARK_RED + "绑定码错误,请重新输入."); + } return context.success(); }) .root() .key("help") .exec(context -> { - onHelp(context); + context.addOutput(TextFormat.AQUA + "HuHoBot 操作相关命令"); + context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reconnect - 重新连接服务器"); + context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot disconnect - 断开服务器连接"); + context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reload - 重载配置文件"); + context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot bind - 确认绑定"); return context.success(); }) .root() .key("reload") .exec(context -> { - onReload(context); + HuHoBot.reloadConfig(); + context.addOutput(TextFormat.GOLD + "已重载配置文件."); return context.success(); }); diff --git a/src/main/java/cn/huohuas001/huHoBot/GameEvent/onChat.java b/src/main/java/cn/huohuas001/huHoBot/GameEvent/GameEventListener.java similarity index 92% rename from src/main/java/cn/huohuas001/huHoBot/GameEvent/onChat.java rename to src/main/java/cn/huohuas001/huHoBot/GameEvent/GameEventListener.java index 8901d61..50e54c2 100644 --- a/src/main/java/cn/huohuas001/huHoBot/GameEvent/onChat.java +++ b/src/main/java/cn/huohuas001/huHoBot/GameEvent/GameEventListener.java @@ -6,9 +6,9 @@ import org.allaymc.api.eventbus.EventHandler; import org.allaymc.api.eventbus.event.player.PlayerChatEvent; -public class onChat { +public class GameEventListener { @EventHandler - private void onChatEvent(PlayerChatEvent event){ + private void onPlayerChat(PlayerChatEvent event) { String message = event.getMessage(); String playerName = event.getPlayer().getDisplayName(); diff --git a/src/main/java/cn/huohuas001/huHoBot/HuHoBot.java b/src/main/java/cn/huohuas001/huHoBot/HuHoBot.java index 50d0ac5..e047495 100644 --- a/src/main/java/cn/huohuas001/huHoBot/HuHoBot.java +++ b/src/main/java/cn/huohuas001/huHoBot/HuHoBot.java @@ -1,11 +1,12 @@ package cn.huohuas001.huHoBot; import cn.huohuas001.huHoBot.Command.HuHoBotCommand; -import cn.huohuas001.huHoBot.GameEvent.onChat; +import cn.huohuas001.huHoBot.GameEvent.GameEventListener; import cn.huohuas001.huHoBot.NetEvent.*; import cn.huohuas001.huHoBot.Settings.PluginConfig; import com.alibaba.fastjson2.JSONObject; import eu.okaeri.configs.ConfigManager; +import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.allaymc.api.command.CommandResult; @@ -13,7 +14,6 @@ import org.allaymc.api.plugin.Plugin; import org.allaymc.api.registry.Registries; import org.allaymc.api.server.Server; -import org.allaymc.api.utils.Utils; import java.nio.file.Path; import java.util.HashMap; @@ -21,8 +21,8 @@ @Slf4j public class HuHoBot extends Plugin { - private static HuHoBot instance; private static final String CONFIG_FILE_NAME = "plugins/HuHoBot/config.yml"; + private static HuHoBot instance; @Getter private static PluginConfig config; @Getter @@ -31,9 +31,18 @@ public class HuHoBot extends Plugin { public bindRequest bindRequestObj; + public static HuHoBot getPlugin() { + return instance; + } + + public static void reloadConfig() { + config.load(); + config.save(); + } + @Override public void onEnable() { - Server.getInstance().getEventBus().registerListener(new onChat()); + Server.getInstance().getEventBus().registerListener(new GameEventListener()); } @Override @@ -43,8 +52,18 @@ public void onLoad() { // 创建配置管理器 config = ConfigManager.create( PluginConfig.class, - Utils.createConfigInitializer(Path.of(CONFIG_FILE_NAME) - ) + it -> { + // Specify configurer implementation, optionally additional serdes packages + it.withConfigurer(new YamlSnakeYamlConfigurer()); + // Specify Path, File or pathname + it.withBindFile(Path.of(CONFIG_FILE_NAME)); + // Automatic removal of undeclared keys + it.withRemoveOrphans(true); + // Save the file if it does not exist + it.saveDefaults(); + // Load and save to update comments/new fields + it.load(true); + } ); // 初始化默认值 @@ -74,7 +93,6 @@ private void registerEvent(String eventName, EventRunner event) { eventList.put(eventName, event); } - /** * 统一事件注册 */ @@ -95,7 +113,7 @@ private void totalRegEvent() { registerEvent("bindRequest", bindRequestObj); } - public void onWsMsg(JSONObject data){ + public void onWsMsg(JSONObject data) { JSONObject header = data.getJSONObject("header"); JSONObject body = data.getJSONObject("body"); @@ -105,7 +123,7 @@ public void onWsMsg(JSONObject data){ EventRunner event = eventList.get(type); if (event != null) { event.EventCall(packId, body); - }else{ + } else { log.error("在处理消息是遇到错误: 未知的消息类型{}", type); log.error("此错误具有不可容错性!请检查插件是否为最新!"); log.info("正在断开连接..."); @@ -113,22 +131,13 @@ public void onWsMsg(JSONObject data){ } } - public static HuHoBot getPlugin(){ - return instance; - } - - public static void reloadConfig(){ - config.load(); - config.save(); - } - public void runCommand(String command, String packId) { CommandSender orginalSender = Server.getInstance(); CommandResult result = Registries.COMMANDS.execute(orginalSender, command); String resultTextBuilder = "暂不支持返回值."; - if(result.isSuccess()){ + if (result.isSuccess()) { clientManager.getClient().respone("已执行,命令回调如下:\n" + resultTextBuilder, "success", packId); - }else{ + } else { clientManager.getClient().respone("已执行,命令回调如下:\n" + resultTextBuilder, "error", packId); } @@ -178,5 +187,5 @@ public boolean disConnectServer() { return clientManager.shutdownClient(); } - + } \ No newline at end of file diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/AddAllowList.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/AddAllowList.java index 30ba80d..9e00503 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/AddAllowList.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/AddAllowList.java @@ -7,7 +7,7 @@ public class AddAllowList extends EventRunner { @Override public boolean run() { String XboxId = body.getString("xboxid"); - if(Server.getInstance().getPlayerService().addToWhitelist(XboxId)){ + if (Server.getInstance().getPlayerManager().addToWhitelist(XboxId)) { PluginConfig config = this.getConfig(); String name = config.getServerName(); respone(name + "已接受添加名为" + XboxId + "的白名单请求", "success"); diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/Chat.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/Chat.java index caffe66..26149b5 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/Chat.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/Chat.java @@ -10,8 +10,8 @@ public boolean run() { String msg = body.getString("msg"); PluginConfig config = this.getConfig(); String message = config.getChatConfig().getFromGroup().replace("{nick}", nick).replace("{msg}", msg); - if(config.getChatConfig().isPostChat()){ - Server.getInstance().broadcastText(message); + if (config.getChatConfig().isPostChat()) { + Server.getInstance().getMessageChannel().broadcastMessage(message); } return true; } diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRun.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRun.java index 46cb892..bf883fe 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRun.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRun.java @@ -5,14 +5,14 @@ import cn.huohuas001.huHoBot.HuHoBot; import cn.huohuas001.huHoBot.Settings.PluginConfig; - import java.util.List; import java.util.Map; public class CustomRun extends EventRunner { private final HuHoBot plugin = HuHoBot.getPlugin(); - @Override public boolean run() { + @Override + public boolean run() { String keyWord = body.getString("key"); List param = body.getList("runParams", String.class); @@ -23,7 +23,7 @@ public class CustomRun extends EventRunner { BotCustomCommand event = new BotCustomCommand(keyWord, body, packId, false); event.call(); - if(!event.isCancelled()){ + if (!event.isCancelled()) { respone("无效的关键字", "error"); } return true; @@ -32,9 +32,9 @@ public class CustomRun extends EventRunner { String command = result.getCommand(); for (int i = 0; i < param.size(); i++) { int replaceNum = i + 1; - command = command.replace("&" + String.valueOf(replaceNum), param.get(i)); + command = command.replace("&" + replaceNum, param.get(i)); } - if(result.getPermission() > 0){ + if (result.getPermission() > 0) { respone("权限不足,若您为管理员,请使用/管理员执行", "error"); return false; } diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRunAdmin.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRunAdmin.java index ef84d9b..2666bbe 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRunAdmin.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/CustomRunAdmin.java @@ -3,7 +3,6 @@ import cn.huohuas001.huHoBot.Api.BotCustomCommand; import cn.huohuas001.huHoBot.Settings.PluginConfig; - import java.util.List; import java.util.Map; @@ -19,7 +18,7 @@ public boolean run() { if (result == null) { BotCustomCommand event = new BotCustomCommand(keyWord, body, packId, true); - if(!event.isCancelled()){ + if (!event.isCancelled()) { respone("无效的关键字", "error"); } return true; @@ -28,7 +27,7 @@ public boolean run() { String command = result.getCommand(); for (int i = 0; i < param.size(); i++) { int replaceNum = i + 1; - command = command.replace("&" + String.valueOf(replaceNum), param.get(i)); + command = command.replace("&" + replaceNum, param.get(i)); } runCommand(command); diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/DelAllowList.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/DelAllowList.java index 988eba6..43023dd 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/DelAllowList.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/DelAllowList.java @@ -1,6 +1,5 @@ package cn.huohuas001.huHoBot.NetEvent; -import cn.huohuas001.huHoBot.HuHoBot; import cn.huohuas001.huHoBot.Settings.PluginConfig; import org.allaymc.api.server.Server; @@ -8,7 +7,7 @@ public class DelAllowList extends EventRunner { @Override public boolean run() { String XboxId = body.getString("xboxid"); - if(Server.getInstance().getPlayerService().removeFromWhitelist(XboxId)){ + if (Server.getInstance().getPlayerManager().removeFromWhitelist(XboxId)) { PluginConfig config = this.getConfig(); String name = config.getServerName(); respone(name + "已接受删除名为" + XboxId + "的白名单请求", "success"); diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/EventRunner.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/EventRunner.java index 67f8fae..11b3771 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/EventRunner.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/EventRunner.java @@ -37,5 +37,4 @@ boolean run() { return true; } - ; } diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryAllowList.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryAllowList.java index 97c17b7..0241fdb 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryAllowList.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryAllowList.java @@ -4,14 +4,13 @@ import com.alibaba.fastjson2.JSONObject; import org.allaymc.api.server.Server; - import java.util.List; import java.util.Set; public class QueryAllowList extends EventRunner { @Override public boolean run() { - Set whiteList = Server.getInstance().getPlayerService().getWhitelistedPlayers(); + Set whiteList = Server.getInstance().getPlayerManager().getWhitelistedPlayers(); StringBuilder whitelistNameString = new StringBuilder(); JSONObject rBody = new JSONObject(); if (body.containsKey("key")) { diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryOnline.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryOnline.java index 95724aa..571cb36 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryOnline.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/QueryOnline.java @@ -1,6 +1,5 @@ package cn.huohuas001.huHoBot.NetEvent; -import cn.huohuas001.huHoBot.HuHoBot; import com.alibaba.fastjson2.JSONObject; import lombok.extern.slf4j.Slf4j; import org.allaymc.api.entity.interfaces.EntityPlayer; @@ -13,7 +12,7 @@ public class QueryOnline extends EventRunner { @Override public boolean run() { - Map onlinePlayers = Server.getInstance().getPlayerService().getPlayers(); + Map onlinePlayers = Server.getInstance().getPlayerManager().getPlayers(); //获取motd Config String server_ip = getConfig().getMotd().getServerIp(); diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/Shaked.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/Shaked.java index 4dfe1de..711cccb 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/Shaked.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/Shaked.java @@ -8,11 +8,11 @@ public class Shaked extends EventRunner { private final HuHoBot plugin = HuHoBot.getPlugin(); - private void shakedProcess(){ + private void shakedProcess() { HuHoBot.getClientManager().setShouldReconnect(true); HuHoBot.getClientManager().cancelCurrentTask(); HuHoBot.getClientManager().setAutoDisConnectTask(); - Server.getInstance().getScheduler().scheduleRepeating(plugin, ()->{ + Server.getInstance().getScheduler().scheduleRepeating(plugin, () -> { HuHoBot.getClientManager().sendHeart(); return true; }, 5 * 20); diff --git a/src/main/java/cn/huohuas001/huHoBot/NetEvent/bindRequest.java b/src/main/java/cn/huohuas001/huHoBot/NetEvent/bindRequest.java index 273b4f6..d9f0c1a 100644 --- a/src/main/java/cn/huohuas001/huHoBot/NetEvent/bindRequest.java +++ b/src/main/java/cn/huohuas001/huHoBot/NetEvent/bindRequest.java @@ -10,19 +10,19 @@ @Slf4j public class bindRequest extends EventRunner { private final HuHoBot plugin = HuHoBot.getPlugin(); - private final Map bindMap = new HashMap<>(); + private final Map bindMap = new HashMap<>(); @Override public boolean run() { String bindCode = body.getString("bindCode"); log.info("收到一个新的绑定请求,如确认绑定,请输入\"/huhobot bind {}\"来进行确认", bindCode); - bindMap.put(bindCode,packId); + bindMap.put(bindCode, packId); return true; } - public boolean confirmBind(String bindCode){ - if(bindMap.containsKey(bindCode)){ - sendMessage("bindConfirm",new JSONObject()); + public boolean confirmBind(String bindCode) { + if (bindMap.containsKey(bindCode)) { + sendMessage("bindConfirm", new JSONObject()); bindMap.remove(bindCode); return true; } diff --git a/src/main/java/cn/huohuas001/huHoBot/Settings/PluginConfig.java b/src/main/java/cn/huohuas001/huHoBot/Settings/PluginConfig.java index 3900d5a..595171d 100644 --- a/src/main/java/cn/huohuas001/huHoBot/Settings/PluginConfig.java +++ b/src/main/java/cn/huohuas001/huHoBot/Settings/PluginConfig.java @@ -2,7 +2,10 @@ import cn.huohuas001.huHoBot.Tools.PackId; import eu.okaeri.configs.OkaeriConfig; -import eu.okaeri.configs.annotation.*; +import eu.okaeri.configs.annotation.Comment; +import eu.okaeri.configs.annotation.CustomKey; +import eu.okaeri.configs.annotation.NameStrategy; +import eu.okaeri.configs.annotation.Names; import lombok.Getter; import lombok.Setter; @@ -45,39 +48,11 @@ public class PluginConfig extends OkaeriConfig { "post_prefix: 消息转发前缀" }) @CustomKey("chatConfig") - private ChatConfig chatConfig = new ChatConfig(); - - @Names(strategy = NameStrategy.IDENTITY) - public static class ChatConfig extends OkaeriConfig { - @Comment("游戏端消息格式") - @CustomKey("from_game") - private String fromGame = "<{name}> {msg}"; - - @Comment("群聊消息格式") - @CustomKey("from_group") - private String fromGroup = "群:<{nick}> {msg}"; - - @Comment("是否转发聊天消息") - @CustomKey("post_chat") - private boolean postChat = true; - - @Comment("消息转发前缀") - @CustomKey("post_prefix") - private String postPrefix = ""; - - // Getters - public String getFromGame() { return fromGame; } - public String getFromGroup() { return fromGroup; } - public boolean isPostChat() { return postChat; } - public String getPostPrefix() { return postPrefix; } - } - + private final ChatConfig chatConfig = new ChatConfig(); // 弃用旧字段并标记为 transient 防止持久化 @Deprecated @CustomKey("chatFormatGroup") private transient String chatFormatGroup; - // endregion - // region 新的MOTD配置结构 @Comment({ "MOTD服务器配置", @@ -85,112 +60,48 @@ public static class ChatConfig extends OkaeriConfig { "text: 显示文本格式,使用{online}作为在线人数占位符" }) @CustomKey("motd") - private MotdConfig motd = new MotdConfig(); - - @Names(strategy = NameStrategy.IDENTITY) - public static class MotdConfig extends OkaeriConfig { - @Comment("服务器IP地址") - @CustomKey("server_ip") - private String serverIp = "play.easecation.net"; - - @Comment("服务器端口") - @CustomKey("server_port") - private int serverPort = 19132; - - @Comment("状态查询API地址") - private String api = "https://motdbe.blackbe.work/status_img?host={server_ip}:{server_port}"; - - @Comment("显示文本格式") - private String text = "共{online}人在线"; - - @Comment("是否输出在线列表") - @CustomKey("output_online_list") - private boolean outputOnlineList = true; - - @Comment("是否发布状态图片") - @CustomKey("post_img") - private boolean postImg = true; - - // Getters - public String getServerIp() { return serverIp; } - public int getServerPort() { return serverPort; } - public String getApi() { return api; } - public String getText() { return text; } - public boolean isOutputOnlineList() { return outputOnlineList; } - public boolean isPostImg() { return postImg; } - } + private final MotdConfig motd = new MotdConfig(); // endregion - - @Getter @Comment({ "服务器显示名称" }) @CustomKey("serverName") - private String serverName = "AllayMC"; - // endregion - + private final String serverName = "AllayMC"; // region 自定义命令 @Getter @Comment("自定义命令列表") @CustomKey("customCommand") - private List customCommand = Arrays.asList( + private final List customCommand = Arrays.asList( new CustomCommand("加白名", "whitelist add &1", 0), new CustomCommand("管理加白名", "whitelist add &1", 1) ); - - @Names(strategy = NameStrategy.IDENTITY) - public static class CustomCommand extends OkaeriConfig { - @Comment("触发指令 (支持中文)") - private String key; - - @Comment("实际执行的命令 (&1=第一个参数)") - private String command; - - @Comment("权限等级 0=玩家 1=管理") - private int permission; - - // 需要无参构造器 - public CustomCommand() { - } - - public CustomCommand(String key, String command, int permission) { - this.key = key; - this.command = command; - this.permission = permission; - } - - // Getters 必须存在 - public String getKey() { return key; } - public String getCommand() { return command; } - public int getPermission() { return permission; } - } - + // endregion // 在PluginConfig类顶部添加版本字段 @Comment("配置版本 (检测到版本小于1时会自动迁移旧配置)") @CustomKey("version") private int configVersion = 1; - // endregion + // 废弃原有motdUrl字段 + @Deprecated + @CustomKey("motdUrl") + private transient String motdUrl; - public MotdConfig getMotd() { return motd; } - + public MotdConfig getMotd() { + return motd; + } @Deprecated public String getMotdUrl() { return motd.getServerIp() + ":" + motd.getServerPort(); } + // endregion + public Map getCustomCommandMap() { return customCommand.stream() - .collect(Collectors.toMap(CustomCommand::getKey, customCommand -> customCommand,(existing, replacement) -> existing)); + .collect(Collectors.toMap(CustomCommand::getKey, customCommand -> customCommand, (existing, replacement) -> existing)); } - // endregion - - // 废弃原有motdUrl字段 - @Deprecated - @CustomKey("motdUrl") - private transient String motdUrl; // 初始化方法示例 public void initializeDefaults() { @@ -204,7 +115,6 @@ public void initializeDefaults() { } } - private void performConfigMigration() { // 如果存在旧版 motdUrl 配置 if (this.motdUrl != null && !this.motdUrl.isEmpty()) { @@ -228,4 +138,126 @@ private void performConfigMigration() { this.configVersion = 2; // 更新版本号 } + // endregion + + @Names(strategy = NameStrategy.IDENTITY) + public static class ChatConfig extends OkaeriConfig { + @Comment("游戏端消息格式") + @CustomKey("from_game") + private final String fromGame = "<{name}> {msg}"; + + @Comment("群聊消息格式") + @CustomKey("from_group") + private String fromGroup = "群:<{nick}> {msg}"; + + @Comment("是否转发聊天消息") + @CustomKey("post_chat") + private boolean postChat = true; + + @Comment("消息转发前缀") + @CustomKey("post_prefix") + private String postPrefix = ""; + + // Getters + public String getFromGame() { + return fromGame; + } + + public String getFromGroup() { + return fromGroup; + } + + public boolean isPostChat() { + return postChat; + } + + public String getPostPrefix() { + return postPrefix; + } + } + + @Names(strategy = NameStrategy.IDENTITY) + public static class MotdConfig extends OkaeriConfig { + @Comment("服务器IP地址") + @CustomKey("server_ip") + private String serverIp = "play.easecation.net"; + + @Comment("服务器端口") + @CustomKey("server_port") + private int serverPort = 19132; + + @Comment("状态查询API地址") + private final String api = "https://motdbe.blackbe.work/status_img?host={server_ip}:{server_port}"; + + @Comment("显示文本格式") + private final String text = "共{online}人在线"; + + @Comment("是否输出在线列表") + @CustomKey("output_online_list") + private final boolean outputOnlineList = true; + + @Comment("是否发布状态图片") + @CustomKey("post_img") + private final boolean postImg = true; + + // Getters + public String getServerIp() { + return serverIp; + } + + public int getServerPort() { + return serverPort; + } + + public String getApi() { + return api; + } + + public String getText() { + return text; + } + + public boolean isOutputOnlineList() { + return outputOnlineList; + } + + public boolean isPostImg() { + return postImg; + } + } + + @Names(strategy = NameStrategy.IDENTITY) + public static class CustomCommand extends OkaeriConfig { + @Comment("触发指令 (支持中文)") + private String key; + + @Comment("实际执行的命令 (&1=第一个参数)") + private String command; + + @Comment("权限等级 0=玩家 1=管理") + private int permission; + + // 需要无参构造器 + public CustomCommand() { + } + + public CustomCommand(String key, String command, int permission) { + this.key = key; + this.command = command; + this.permission = permission; + } + + // Getters 必须存在 + public String getKey() { + return key; + } + + public String getCommand() { + return command; + } + + public int getPermission() { + return permission; + } + } } \ No newline at end of file diff --git a/src/main/java/cn/huohuas001/huHoBot/WebsocketClientManager.java b/src/main/java/cn/huohuas001/huHoBot/WebsocketClientManager.java index 810f3fb..84d5928 100644 --- a/src/main/java/cn/huohuas001/huHoBot/WebsocketClientManager.java +++ b/src/main/java/cn/huohuas001/huHoBot/WebsocketClientManager.java @@ -12,19 +12,17 @@ import java.net.URI; import java.net.URISyntaxException; import java.security.cert.X509Certificate; -import java.util.HashMap; -import java.util.Map; @Slf4j public class WebsocketClientManager { + private static final String websocketUrl = ServerConfig.WS_SERVER_URL; //Websocket地址 + private static WsClient client; //Websocket客户端 private final int RECONNECT_DELAY = 5; // 重连延迟时间,单位为秒 private final int MAX_RECONNECT_ATTEMPTS = 5; // 最大重连尝试次数 - private int ReconnectAttempts = 0; - private boolean shouldReconnect = true; // 控制是否重连的变量 - private static WsClient client; //Websocket客户端 - private static final String websocketUrl = ServerConfig.WS_SERVER_URL; //Websocket地址 private final HuHoBot plugin; private final Scheduler scheduler = Server.getInstance().getScheduler(); + private int ReconnectAttempts = 0; + private boolean shouldReconnect = true; // 控制是否重连的变量 private int currentTask = 0; private int autoDisConnectTask = 0; @@ -39,15 +37,15 @@ public WebsocketClientManager() { * @param shouldReconnect 是否应该重连 */ public void setShouldReconnect(boolean shouldReconnect) { - this.shouldReconnect = shouldReconnect; + this.shouldReconnect = shouldReconnect; } /** * 客户端自动重连循环 */ private boolean autoReconnect() { - synchronized (this){ - if(currentTask == 0){ + synchronized (this) { + if (currentTask == 0) { return false; } ReconnectAttempts++; @@ -56,7 +54,7 @@ private boolean autoReconnect() { cancelCurrentTask(); return false; } - if(!shouldReconnect){ + if (!shouldReconnect) { cancelCurrentTask(); return false; } @@ -77,25 +75,25 @@ public WsClient getClient() { return client; } - public boolean shutdownClient(){ - if (client != null && client.isOpen()) { + public boolean shutdownClient() { + if (client != null && client.isOpen()) { client.close(1000); return true; } return false; } - public void autoDisConnectClient(){ + public void autoDisConnectClient() { log.info("连接超时,已自动重连"); shutdownClient(); } - public void setAutoDisConnectTask(){ - if(autoDisConnectTask == 0){ - scheduler.scheduleDelayed(plugin,()->{ + public void setAutoDisConnectTask() { + if (autoDisConnectTask == 0) { + scheduler.scheduleDelayed(plugin, () -> { autoDisConnectClient(); return true; - }, 6*60*60*20); + }, 6 * 60 * 60 * 20); autoDisConnectTask = 1; } } @@ -115,7 +113,7 @@ public boolean connectServer() { return true; } catch (URISyntaxException e) { log.error(e.getStackTrace().toString()); - }catch (Exception e) { + } catch (Exception e) { log.error("连接HuHoBot失败: " + e.getMessage()); e.printStackTrace(); } @@ -146,15 +144,15 @@ public X509Certificate[] getAcceptedIssuers() { return context; } - public boolean isOpen(){ + public boolean isOpen() { return client.isOpen(); } - public void sendHeart(){ - client.sendMessage("heart", new JSONObject()); + public void sendHeart() { + client.sendMessage("heart", new JSONObject()); } - public void clientReconnect(){ + public void clientReconnect() { if (shouldReconnect && currentTask == 0) { scheduler.scheduleRepeating(plugin, this::autoReconnect, this.RECONNECT_DELAY * 20); currentTask = 1; diff --git a/src/main/java/cn/huohuas001/huHoBot/WsClient.java b/src/main/java/cn/huohuas001/huHoBot/WsClient.java index da710dd..b21fa72 100644 --- a/src/main/java/cn/huohuas001/huHoBot/WsClient.java +++ b/src/main/java/cn/huohuas001/huHoBot/WsClient.java @@ -10,11 +10,7 @@ import org.allaymc.api.server.Server; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; -import org.java_websocket.drafts.Draft_6455; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; import java.net.URI; import java.util.HashMap; import java.util.Map; @@ -87,7 +83,7 @@ public void onMessage(String message) { responseFutureList.remove(packId); } else { Scheduler scheduler = Server.getInstance().getScheduler(); - scheduler.runLater(HuHoBot.getPlugin(), ()->{ + scheduler.runLater(HuHoBot.getPlugin(), () -> { plugin.onWsMsg(jsonData); }); } @@ -106,7 +102,6 @@ public void onError(Exception ex) { } - /** * 向服务端发送一条消息 * diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json index b70d356..e36ddbd 100644 --- a/src/main/resources/plugin.json +++ b/src/main/resources/plugin.json @@ -2,5 +2,6 @@ "entrance": "cn.huohuas001.huHoBot.HuHoBot", "name": "HuHoBot", "authors": ["HuoHuas001"], - "version": "0.0.9" + "version": "0.0.10", + "api_version": ">=0.13.0" } \ No newline at end of file