From 8e84aadc723dffba7abe716fb9b1eefd4e0c294b Mon Sep 17 00:00:00 2001 From: XiaMoHuaHuo-CN <2549619802@qq.com> Date: Thu, 2 Jun 2022 21:40:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=AA=E8=8E=B7=E5=8F=96TPS?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20=E6=B7=BB=E5=8A=A0=E6=BC=8F=E6=8E=89?= =?UTF-8?q?=E7=9A=84=E6=97=A0=E6=9D=83=E6=93=8D=E4=BD=9C=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/moe/xmcn/catsero/Main.java | 4 + .../listeners/GetTPS/OnQQGroupMessage.java | 38 ++++++++ .../listeners/PingHost/OnQQGroupMessage.java | 66 +++++++------- .../WeatherInfo/OnQQGroupMessage.java | 87 ++++++++++++++----- .../java/moe/xmcn/catsero/utils/Config.kt | 6 +- .../moe/xmcn/catsero/utils/ServerTPS.java | 30 +++++++ src/main/resources/usesconfig.yml | 8 +- 7 files changed, 185 insertions(+), 54 deletions(-) create mode 100644 src/main/java/moe/xmcn/catsero/events/listeners/GetTPS/OnQQGroupMessage.java create mode 100644 src/main/java/moe/xmcn/catsero/utils/ServerTPS.java diff --git a/src/main/java/moe/xmcn/catsero/Main.java b/src/main/java/moe/xmcn/catsero/Main.java index 7bb35fba..b0286cc9 100644 --- a/src/main/java/moe/xmcn/catsero/Main.java +++ b/src/main/java/moe/xmcn/catsero/Main.java @@ -6,6 +6,7 @@ import moe.xmcn.catsero.events.listeners.PlayerJoinQuitForward.OnGamePlayerQuit; import moe.xmcn.catsero.utils.Config; import moe.xmcn.catsero.utils.Metrics; +import moe.xmcn.catsero.utils.ServerTPS; import moe.xmcn.xmcore.ThisAPI; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -46,6 +47,9 @@ public void onEnable() { new Metrics((JavaPlugin) Config.INSTANCE.getPlugin(), pluginId); } + // 启动TPS计算程序 + Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new ServerTPS(), 100L, 1L); + System.out.println("CatSero插件加载成功"); // 异步加载更新检查器 diff --git a/src/main/java/moe/xmcn/catsero/events/listeners/GetTPS/OnQQGroupMessage.java b/src/main/java/moe/xmcn/catsero/events/listeners/GetTPS/OnQQGroupMessage.java new file mode 100644 index 00000000..4768396c --- /dev/null +++ b/src/main/java/moe/xmcn/catsero/events/listeners/GetTPS/OnQQGroupMessage.java @@ -0,0 +1,38 @@ +package moe.xmcn.catsero.events.listeners.GetTPS; + +import me.dreamvoid.miraimc.api.MiraiBot; +import me.dreamvoid.miraimc.bukkit.event.MiraiGroupMessageEvent; +import moe.xmcn.catsero.utils.Config; +import moe.xmcn.catsero.utils.ServerTPS; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.Objects; + +public class OnQQGroupMessage implements Listener { + + @EventHandler + public void onMiraiGroupMessageEvent(MiraiGroupMessageEvent event) { + + String message = event.getMessage(); + String[] args = message.split(" "); + if (Objects.equals(args[0], "catsero") && Objects.equals(args[1], "kick") && Config.INSTANCE.getUsesConfig().getBoolean("tps.enabled") && event.getGroupID() == Config.INSTANCE.getUse_Group() && event.getBotID() == Config.INSTANCE.getUse_Bot()) { + if (event.getSenderID() == Config.INSTANCE.getQQ_OP()) { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(String.valueOf(ServerTPS.getTPS())); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } else { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.no-permission")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } + } + } + +} diff --git a/src/main/java/moe/xmcn/catsero/events/listeners/PingHost/OnQQGroupMessage.java b/src/main/java/moe/xmcn/catsero/events/listeners/PingHost/OnQQGroupMessage.java index 330b50ed..9984f3aa 100644 --- a/src/main/java/moe/xmcn/catsero/events/listeners/PingHost/OnQQGroupMessage.java +++ b/src/main/java/moe/xmcn/catsero/events/listeners/PingHost/OnQQGroupMessage.java @@ -18,47 +18,55 @@ public class OnQQGroupMessage implements Listener { @EventHandler public void onMiraiGroupMessageEvent(MiraiGroupMessageEvent event) { if (Config.INSTANCE.getUsesConfig().getBoolean("pinghost.enabled") && event.getGroupID() == Config.INSTANCE.getUse_Group() && event.getBotID() == Config.INSTANCE.getUse_Bot()) { - if (!Config.INSTANCE.getUsesConfig().getBoolean("pinghost.op-only")) { - String msg = event.getMessage(); - String[] args = msg.split(" "); - if (args[0].equalsIgnoreCase("catsero") && args[1].equalsIgnoreCase("ping")) { - try { + if (Config.INSTANCE.getUsesConfig().getBoolean("pinghost.op-only")) { + if (event.getSenderID() == Config.INSTANCE.getQQ_OP()) { + String msg = event.getMessage(); + String[] args = msg.split(" "); + if (args[0].equalsIgnoreCase("catsero") && args[1].equalsIgnoreCase("ping")) { try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.pinghost.doing")); - } catch (NoSuchElementException nse) { - Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); - } - String result = PingHost.PingHostUtils(args[2]); - if (Objects.equals(result, "Error")) { try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.pinghost.error")); + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.pinghost.doing")); } catch (NoSuchElementException nse) { Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); } - } else { - long flag = Long.parseLong(result); + String result = PingHost.PingHostUtils(args[2]); + if (Objects.equals(result, "Error")) { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.pinghost.error")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } else { + long flag = Long.parseLong(result); + try { + String message = Config.INSTANCE.getMsgByMsID("qq.pinghost.success") + .replace("%address_original%", args[2]) + .replace("%address_punycode%", Punycode.encodeURL(args[2])) + .replace("%withdraw%", String.valueOf(flag)) + .replace("%lost%", String.valueOf(4 - flag)) + .replace("%lost_percent%", String.valueOf((4 - flag) * 100 / 4)); + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(message); + //MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(args[2] + "(" + (Punycode.encodeURL(args[2])) + ")" + " 的 Ping 统计信息:\n 数据包:已发送 = 4, 已接收 = " + flag + " ,丢失 = " + (4 - flag) + "(" + (4 - flag) * 100 / 4 + "% 丢失)"); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } + } catch (UnknownHostException e) { try { - String message = Config.INSTANCE.getMsgByMsID("qq.pinghost.success") - .replace("%address_original%", args[2]) - .replace("%address_punycode%", Punycode.encodeURL(args[2])) - .replace("%withdraw%", String.valueOf(flag)) - .replace("%lost%", String.valueOf(4 - flag)) - .replace("%lost_percent%", String.valueOf((4 - flag) * 100 / 4)); - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(message); - //MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(args[2] + "(" + (Punycode.encodeURL(args[2])) + ")" + " 的 Ping 统计信息:\n 数据包:已发送 = 4, 已接收 = " + flag + " ,丢失 = " + (4 - flag) + "(" + (4 - flag) * 100 / 4 + "% 丢失)"); + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.pinghost.failed")); } catch (NoSuchElementException nse) { Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); } } - } catch (UnknownHostException e) { - try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.pinghost.failed")); - } catch (NoSuchElementException nse) { - Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); - } + } + } else { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.no-permission")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); } } - } else if (event.getSenderID() == Config.INSTANCE.getQQ_OP()) { + } else { String msg = event.getMessage(); String[] args = msg.split(" "); if (args[0].equalsIgnoreCase("catsero") && args[1].equalsIgnoreCase("ping")) { diff --git a/src/main/java/moe/xmcn/catsero/events/listeners/WeatherInfo/OnQQGroupMessage.java b/src/main/java/moe/xmcn/catsero/events/listeners/WeatherInfo/OnQQGroupMessage.java index 95b0fec8..4a20ec7a 100644 --- a/src/main/java/moe/xmcn/catsero/events/listeners/WeatherInfo/OnQQGroupMessage.java +++ b/src/main/java/moe/xmcn/catsero/events/listeners/WeatherInfo/OnQQGroupMessage.java @@ -19,39 +19,82 @@ public void onMiraiGroupMessageEvent(MiraiGroupMessageEvent event) { String msg = event.getMessage(); String[] args = msg.split(" "); if (args[0].equalsIgnoreCase("catsero") && args[1].equalsIgnoreCase("weather")) { - if (args.length == 3 && event.getGroupID() == Config.INSTANCE.getUse_Group()) { - try { + if (Config.INSTANCE.getUsesConfig().getBoolean("weatherinfo.op-only")) { + if (event.getSenderID() == Config.INSTANCE.getQQ_OP()) { + if (args.length == 3 && event.getGroupID() == Config.INSTANCE.getUse_Group()) { + try { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.doing")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + String[] resvi = WeatherUtils.getWeather(args[2]); + String message = Config.INSTANCE.getMsgByMsID("qq.weatherinfo.success") + .replace("%type%", resvi[4]) + .replace("%temperature%", resvi[1]) + .replace("%wind%", resvi[2]) + .replace("%wind_direction%", resvi[3]) + .replace("%date%", resvi[0]); + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(message); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } catch (UnsupportedEncodingException uee) { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.error")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } + } else { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.no-permission")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } + } else { try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.doing")); + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.null-city")); } catch (NoSuchElementException nse) { Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); } - String[] resvi = WeatherUtils.getWeather(args[2]); - String message = Config.INSTANCE.getMsgByMsID("qq.weatherinfo.success") - .replace("%type%", resvi[4]) - .replace("%temperature%", resvi[1]) - .replace("%wind%", resvi[2]) - .replace("%wind_direction%", resvi[3]) - .replace("%date%", resvi[0]); + } + } else { + if (args.length == 3 && event.getGroupID() == Config.INSTANCE.getUse_Group()) { try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(message); - } catch (NoSuchElementException nse) { - Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.doing")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + String[] resvi = WeatherUtils.getWeather(args[2]); + String message = Config.INSTANCE.getMsgByMsID("qq.weatherinfo.success") + .replace("%type%", resvi[4]) + .replace("%temperature%", resvi[1]) + .replace("%wind%", resvi[2]) + .replace("%wind_direction%", resvi[3]) + .replace("%date%", resvi[0]); + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(message); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } + } catch (UnsupportedEncodingException uee) { + try { + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.error")); + } catch (NoSuchElementException nse) { + Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); + } } - } catch (UnsupportedEncodingException uee) { + } else { try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.error")); + MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.no-permission")); } catch (NoSuchElementException nse) { Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); } } - - } else { - try { - MiraiBot.getBot(Config.INSTANCE.getUse_Bot()).getGroup(Config.INSTANCE.getUse_Group()).sendMessageMirai(Config.INSTANCE.getPrefix_QQ() + Config.INSTANCE.getMsgByMsID("qq.weatherinfo.null-city")); - } catch (NoSuchElementException nse) { - Config.INSTANCE.getPlugin().getLogger().warning(Config.INSTANCE.getMsgByMsID("general.send-message-qq.error").replace("%error%", nse + Arrays.toString(nse.getStackTrace()))); - } } } } diff --git a/src/main/java/moe/xmcn/catsero/utils/Config.kt b/src/main/java/moe/xmcn/catsero/utils/Config.kt index 84d9b7c9..d579d172 100644 --- a/src/main/java/moe/xmcn/catsero/utils/Config.kt +++ b/src/main/java/moe/xmcn/catsero/utils/Config.kt @@ -9,7 +9,8 @@ import org.bukkit.configuration.file.FileConfiguration import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.entity.Player import org.bukkit.plugin.Plugin -import java.io.* +import java.io.File +import java.io.InputStreamReader /** * Config文件读取 @@ -27,7 +28,8 @@ object Config { val Prefix_MC: String = plugin.config.getString("format-list.prefix.to-mc") val Prefix_QQ: String = plugin.config.getString("format-list.prefix.to-qq") - val PluginInfo: FileConfiguration = YamlConfiguration.loadConfiguration(File(plugin.dataFolder.parent, "XMCore/catsero.info")) + val PluginInfo: FileConfiguration = + YamlConfiguration.loadConfiguration(File(plugin.dataFolder.parent, "XMCore/catsero.info")) /** * 尝试转为PlaceholderAPI文本 diff --git a/src/main/java/moe/xmcn/catsero/utils/ServerTPS.java b/src/main/java/moe/xmcn/catsero/utils/ServerTPS.java new file mode 100644 index 00000000..19f531c7 --- /dev/null +++ b/src/main/java/moe/xmcn/catsero/utils/ServerTPS.java @@ -0,0 +1,30 @@ +package moe.xmcn.catsero.utils; + +public class ServerTPS implements Runnable { + public static int TICK_COUNT = 0; + public static long[] TICKS = new long[600]; + + public static double getTPS() { + return getTPS(100); + } + + public static double getTPS(int ticks) { + if (TICK_COUNT < ticks) { + return 20.0D; + } + int target = (TICK_COUNT - 1 - ticks) % TICKS.length; + long elapsed = System.currentTimeMillis() - TICKS[target]; + + return ticks / (elapsed / 1000.0D); + } + + public static long getElapsed(int tickID) { + long time = TICKS[(tickID % TICKS.length)]; + return System.currentTimeMillis() - time; + } + + public void run() { + TICKS[(TICK_COUNT % TICKS.length)] = System.currentTimeMillis(); + TICK_COUNT += 1; + } +} diff --git a/src/main/resources/usesconfig.yml b/src/main/resources/usesconfig.yml index 0e047423..ecfd6cd5 100644 --- a/src/main/resources/usesconfig.yml +++ b/src/main/resources/usesconfig.yml @@ -11,6 +11,8 @@ pinghost: weatherinfo: # 启用此功能 enabled: false + # 仅OP可用 + op-only: false # QQ封禁玩家 qban-player: @@ -68,4 +70,8 @@ punycode: # 启用此功能 enabled: false # 支持编码URL - url-support: false \ No newline at end of file + url-support: false + +# 获取TPS功能 +get-tps: + enabled: false \ No newline at end of file