From 1f94f7f4463278558138e7e51fc4d9c869c30772 Mon Sep 17 00:00:00 2001 From: ObcbO Date: Wed, 3 Aug 2022 01:41:32 +0800 Subject: [PATCH] =?UTF-8?q?INFO=E6=98=BE=E7=A4=BA=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=80=E5=A0=86=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/obcbo/aliwaka/Aliwaka.java | 26 +++++++++++++++---- .../com/obcbo/aliwaka/command/function.java | 6 +++++ .../java/com/obcbo/aliwaka/command/info.java | 10 ++++--- .../java/com/obcbo/aliwaka/file/Config.java | 12 +++++++-- .../java/com/obcbo/aliwaka/file/Message.java | 4 ++- .../obcbo/aliwaka/task/AntiCR/CountChunk.java | 5 ++++ .../aliwaka/task/AntiCR/PointsChecker.java | 2 +- .../java/com/obcbo/aliwaka/task/Guard.java | 14 +++++++--- src/main/resources/config.yml | 9 ++++++- src/main/resources/message.yml | 6 +++-- src/main/resources/plugin.yml | 2 +- 11 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/obcbo/aliwaka/Aliwaka.java b/src/main/java/com/obcbo/aliwaka/Aliwaka.java index 1c419a0..cd38675 100644 --- a/src/main/java/com/obcbo/aliwaka/Aliwaka.java +++ b/src/main/java/com/obcbo/aliwaka/Aliwaka.java @@ -12,12 +12,15 @@ import java.util.Objects; import java.util.logging.Logger; +import static com.obcbo.aliwaka.file.Config.crEnable; +import static com.obcbo.aliwaka.file.Config.guardEnable; + public final class Aliwaka extends JavaPlugin { public static final Logger logger = Logger.getLogger("Aliwaka"); @Override public void onEnable() { - Metrics metrics = new Metrics(this, 15979);// bstats统计 + new Metrics(this, 15979);// bstats统计 logger.info("MAIN > 开始加载"); saveDefaultConfig(); saveResource("message.yml", false);// false为不覆盖 true为每次调用都覆盖 @@ -33,8 +36,17 @@ public void onEnable() { } logger.info("COMMAND > 命令注册完毕"); - Bukkit.getScheduler().runTask(this, PointsChecker::start); - Bukkit.getScheduler().runTask(this, Guard::start); + Bukkit.getScheduler().runTask(this, () -> { + if (crEnable) { + PointsChecker.start(); + } + }); + //Bukkit.getScheduler().runTask(this, Guard::start); + Bukkit.getScheduler().runTask(this, () -> { + if (guardEnable) { + Guard.start(); + } + }); logger.info("TASK > 任务开始加载"); logger.info("MAIN > 成功启用插件"); } @@ -51,8 +63,12 @@ public static void reload() { Guard.stop(); Config.load(); Message.load(); - PointsChecker.start(); - Guard.start(); + if (crEnable) { + PointsChecker.start(); + } + if (guardEnable) { + Guard.start(); + } logger.info("重载完成"); } } diff --git a/src/main/java/com/obcbo/aliwaka/command/function.java b/src/main/java/com/obcbo/aliwaka/command/function.java index 210e973..e5bf423 100644 --- a/src/main/java/com/obcbo/aliwaka/command/function.java +++ b/src/main/java/com/obcbo/aliwaka/command/function.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Map; +import static com.obcbo.aliwaka.file.Config.crEnable; import static com.obcbo.aliwaka.file.Message.*; import static com.obcbo.aliwaka.task.AntiCR.CountChunk.playerPoints; @@ -37,6 +38,11 @@ public static boolean core(CommandSender sender, String[] args) { } else if ("stop".equals(args[2])) { PointsChecker.stop(); } else if ("list".equals(args[2])) { + if (!crEnable) {// 功能关闭时执行 + sender.sendMessage(Message.prefix + functionDisable); + return true; + } + sender.sendMessage(Message.prefix + crOutput); List> list = new ArrayList<>(playerPoints.entrySet()); //转换为list diff --git a/src/main/java/com/obcbo/aliwaka/command/info.java b/src/main/java/com/obcbo/aliwaka/command/info.java index 4c38760..c73d8ce 100644 --- a/src/main/java/com/obcbo/aliwaka/command/info.java +++ b/src/main/java/com/obcbo/aliwaka/command/info.java @@ -1,6 +1,7 @@ package com.obcbo.aliwaka.command; import com.obcbo.aliwaka.file.Message; +import com.obcbo.aliwaka.task.AntiCR.PointsChecker; import com.obcbo.aliwaka.task.Guard; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -16,13 +17,16 @@ public class info { public static boolean core(CommandSender sender) { sender.sendMessage(Message.prefix + "开始输出服务器信息"); - sender.sendMessage("§6服务器版本§7:§f " + Bukkit.getVersion() + String.format(" §6在线玩家§7:§f §f%d/%d", Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers())); - sender.sendMessage("§6危险值§7:§f " + Guard.getWarn()); + sender.sendMessage("§6服务器版本§7:§f " + Bukkit.getVersion() + + String.format(" §6在线玩家§7:§f §f%d/%d", Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers())); + sender.sendMessage("§6危险值§7:§f " + Guard.getWarn() + + " §f(§6Guard§7:§f " + (Guard.guard.isAlive() ? "§a运行中" : "§c已关闭") + + " §6AntiCR§7:§f " + (PointsChecker.PointsChecker.isAlive() ? "§a运行中" : "§c已关闭") + "§f)"); long max = Runtime.getRuntime().maxMemory(); long use = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); sender.sendMessage("§6内存§7:§f " + String.format("%.2f%%", use / (double) max * 100) + " (" + unitByte(max) + "§7-§f" + unitByte(use) + "§7=§f" + unitByte(max - use) + " 分配§7:§f" + unitByte(Runtime.getRuntime().totalMemory()) + ")"); - + DecimalFormat df = new DecimalFormat("#.00");// 保留小数点后两位 List tps = new ArrayList<>(4);// tps值有4个 for (double single : Bukkit.getTPS()) { diff --git a/src/main/java/com/obcbo/aliwaka/file/Config.java b/src/main/java/com/obcbo/aliwaka/file/Config.java index 049bafc..bf7c138 100644 --- a/src/main/java/com/obcbo/aliwaka/file/Config.java +++ b/src/main/java/com/obcbo/aliwaka/file/Config.java @@ -1,13 +1,11 @@ package com.obcbo.aliwaka.file; import com.obcbo.aliwaka.Aliwaka; -import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; import java.util.List; import static com.obcbo.aliwaka.Aliwaka.logger; @@ -15,6 +13,7 @@ public class Config { static final Plugin config = JavaPlugin.getProvidingPlugin(Aliwaka.class); public static int CD; + public static boolean crEnable; public static int crCheckInterval; public static int crSpeedInterval; public static float crSpeedNormalWalk; @@ -32,10 +31,14 @@ public class Config { public static int crSecondCondition; public static int crSecondReduce; + public static boolean guardEnable; public static int guardCheckInterval; + public static boolean memEnable; public static int memPercentage; + public static boolean tpsEnable; public static int tpsWarnThreshold; public static int tpsDangerThreshold; + public static boolean msptEnable; public static int msptDangerThreshold; public static @NotNull FileConfiguration getConfig() { @@ -48,6 +51,7 @@ public static void load() { } CD = getConfig().getInt("CD", 10000); + crEnable = getConfig().getBoolean("AntiCR.enable", true); crCheckInterval = getConfig().getInt("AntiCR.check-interval", 10000); crListenCommand = getConfig().getStringList("AntiCR.listen-command.text"); crCommandImplement = getConfig().getInt("AntiCR.listen-command.implement", 200); @@ -63,10 +67,14 @@ public static void load() { crSecondCondition = getConfig().getInt("AntiCR.num.second-condition", 10); crSecondReduce = getConfig().getInt("AntiCR.num.second-reduce", 10); + guardEnable = getConfig().getBoolean("Guard.enable", true); guardCheckInterval = getConfig().getInt("Guard.check-interval", 10000); + memEnable = getConfig().getBoolean("Guard.mem-check.enable", true); memPercentage = getConfig().getInt("Guard.mem-check.percentage", 90); + tpsEnable = getConfig().getBoolean("Guard.tps-check.enable", true); tpsWarnThreshold = getConfig().getInt("Guard.tps-check.warn-threshold", 18); tpsDangerThreshold = getConfig().getInt("Guard.tps-check.danger-threshold", 3); + msptEnable = getConfig().getBoolean("Guard.mspt-check.enable", true); msptDangerThreshold = getConfig().getInt("Guard.mspt-check.danger-threshold", 80); } } diff --git a/src/main/java/com/obcbo/aliwaka/file/Message.java b/src/main/java/com/obcbo/aliwaka/file/Message.java index a6f9c7a..741f359 100644 --- a/src/main/java/com/obcbo/aliwaka/file/Message.java +++ b/src/main/java/com/obcbo/aliwaka/file/Message.java @@ -25,6 +25,7 @@ public class Message { public static String CD; public static String notComplete; public static String wrongCommand; + public static String functionDisable; public static String gcStart; public static String gcEnd; public static String crOutput; @@ -36,7 +37,7 @@ public class Message { public static String shellError; public static void load() { - if (!(message.getInt("version") == 1)) { + if (!(message.getInt("version") == 2)) { logger.warning("消息文件message.yml是旧版"); } prefix = get("prefix", "&8[&6Ali&ewaka&8]&r "); @@ -48,6 +49,7 @@ public static void load() { CD = get("CD", "冷却中"); notComplete = get("not-complete", "你似乎还没有输入完命令"); wrongCommand = get("wrong-command", "错误的命令"); + functionDisable = get("function-disable", "此功能处于关闭状态"); gcStart = get("GC.start", "服务器开始强制回收内存,可能会有短暂卡顿"); gcEnd = get("GC.end", "服务器内存回收完成 耗时%time%ms"); crLimit = get("AntiCR.limit", "暂时限制速度"); diff --git a/src/main/java/com/obcbo/aliwaka/task/AntiCR/CountChunk.java b/src/main/java/com/obcbo/aliwaka/task/AntiCR/CountChunk.java index 3cd6518..722c16a 100644 --- a/src/main/java/com/obcbo/aliwaka/task/AntiCR/CountChunk.java +++ b/src/main/java/com/obcbo/aliwaka/task/AntiCR/CountChunk.java @@ -18,11 +18,13 @@ public class CountChunk implements Listener { @EventHandler public void playerChunkLoadEvent(PlayerChunkLoadEvent event) { + if (!crEnable) return;// 获取功能是否开启 add(event.getPlayer().getName()); } @EventHandler public void playerCommandPreprocessEvent(PlayerCommandPreprocessEvent event) { + if (!crEnable) return; for (String n : crListenCommand) { if (event.getMessage().startsWith("/" + n)) { int set = playerPoints.get(event.getPlayer().getName()) >= crCommandImplement ? @@ -34,6 +36,7 @@ public void playerCommandPreprocessEvent(PlayerCommandPreprocessEvent event) { @EventHandler public void playerJoinEvent(PlayerJoinEvent event) { + if (!crEnable) return; // 防止玩家退出无法恢复到正常速度 if (!playerPoints.containsKey(event.getPlayer().getName())) { event.getPlayer().setWalkSpeed(crSpeedNormalWalk); @@ -43,11 +46,13 @@ public void playerJoinEvent(PlayerJoinEvent event) { @EventHandler public void playerQuitEvent(PlayerQuitEvent event) { + if (!crEnable) return; // 玩家退出时删除记录 playerPoints.remove(event.getPlayer().getName()); } private void add(String player) { + if (!crEnable) return; if (playerPoints.containsKey(player)) { int now = playerPoints.get(player) + 1; playerPoints.put(player, now); diff --git a/src/main/java/com/obcbo/aliwaka/task/AntiCR/PointsChecker.java b/src/main/java/com/obcbo/aliwaka/task/AntiCR/PointsChecker.java index ab1d676..4afe3fb 100644 --- a/src/main/java/com/obcbo/aliwaka/task/AntiCR/PointsChecker.java +++ b/src/main/java/com/obcbo/aliwaka/task/AntiCR/PointsChecker.java @@ -17,7 +17,7 @@ public class PointsChecker implements Runnable { private static boolean on = true; static final Set controlList = new HashSet<>(); - private static final Thread PointsChecker = new Thread(new PointsChecker(), "Aliwaka-PointsChecker"); + public static final Thread PointsChecker = new Thread(new PointsChecker(), "Aliwaka-PointsChecker"); public static void start() { if (PointsChecker.isAlive()) { diff --git a/src/main/java/com/obcbo/aliwaka/task/Guard.java b/src/main/java/com/obcbo/aliwaka/task/Guard.java index 55ad6b4..e651ea2 100644 --- a/src/main/java/com/obcbo/aliwaka/task/Guard.java +++ b/src/main/java/com/obcbo/aliwaka/task/Guard.java @@ -13,7 +13,7 @@ import static com.obcbo.aliwaka.file.Message.gcStart; public class Guard implements Runnable { - private static final Thread guard = new Thread(new Guard(), "Aliwaka-Guard"); + public static final Thread guard = new Thread(new Guard(), "Aliwaka-Guard"); private static boolean on = true; private static int warn = 0;// 危险值 @@ -47,9 +47,15 @@ public void run() { } private void core() { - memcheck(); - tpscheck(); - msptcheck(); + if (memEnable) { + memcheck(); + } + if (tpsEnable) { + tpscheck(); + } + if (msptEnable) { + msptcheck(); + } try { Thread.sleep(guardCheckInterval); } catch (InterruptedException e) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index dff93af..b331b37 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,9 +1,11 @@ -# 配置文件版本 非必要不要更改 +# 配置文件版本 非必要不要更改 version: 3 # 普通玩家命令CD时间 目前仅用于GC指令 单位ms CD: 10000 # 跑图检测 AntiCR: + # 功能开关 true为开 + enable: true # 检测间隔 单位ms check-interval: 10000 # 命令开头为设置值 就减少设置值 @@ -33,6 +35,7 @@ AntiCR: implement: 1500 # 执行后减少到 after-implement: 1000 + # 后面这四个数值 设置为0代表禁用 # 第一次判断 大于等于数值时 first-condition: 100 # 第一次判断返回为true就减少设置数值 @@ -43,19 +46,23 @@ AntiCR: second-reduce: 10 # 卡顿检测 Guard: + enable: true # 检测间隔 单位ms check-interval: 10000 # 内存检测 mem-check: + enable: true # 占用百分比 当达到设置值及以上时 执行GC percentage: 90 # TPS检测 tps-check: + enable: true # 执行值 当TPS低于此值时 开始计算危险值 warn-threshold: 18 # 危险值 低于设置值时 立刻执行GC和区块回收 danger-threshold: 3 # MSPT检测 mspt-check: + enable: true # MSPT数值 当达到设置值及以上时 执行GC danger-threshold: 80 \ No newline at end of file diff --git a/src/main/resources/message.yml b/src/main/resources/message.yml index bed5b7d..ca2cabd 100644 --- a/src/main/resources/message.yml +++ b/src/main/resources/message.yml @@ -1,5 +1,5 @@ -# 配置文件版本 非必要不要更改 -version: 1 +# 配置文件版本 非必要不要更改 +version: 2 # 消息前缀 prefix: "&8[&6Ali&ewaka&8]&r " # 显示颜色 @@ -25,6 +25,8 @@ CD: "冷却中" not-complete: "你似乎还没有输入完命令" # 错误的命令 wrong-command: "错误的命令" +# 关闭的功能 +function-disable: "此功能处于关闭状态" # 垃圾回收 GC: start: "服务器开始强制回收内存,可能会有短暂卡顿" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d5fdfc3..57db15f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ version: '${version}' main: com.obcbo.aliwaka.Aliwaka api-version: 1.16 authors: [ ObcbO ] -description: Mik automatic management robot +description: Automatic management plugin website: https://github.com/Encinet/Aliwaka #此为非必须依赖 #softdepend: