From b066e8a81ec33811f3b0e25db30634e84dc89986 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Wed, 20 Jul 2016 20:39:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- pom.xml | 6 +++--- .../pw/yumc/Yum/commands/MonitorCommand.java | 13 +++++++----- .../pw/yumc/Yum/inject/CommandInjector.java | 15 ++------------ .../pw/yumc/Yum/inject/ListenerInjector.java | 15 ++------------ .../java/pw/yumc/Yum/inject/TaskInjector.java | 16 ++------------- .../pw/yumc/Yum/managers/MonitorManager.java | 20 +++++++++++++++++++ 6 files changed, 37 insertions(+), 48 deletions(-) diff --git a/pom.xml b/pom.xml index 7f785b5..8ba200a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 pw.yumc Yum - 2.6.7 + 2.6.8 Yum Minecraft 服务器插件管理系统 @@ -60,10 +60,10 @@ &a全新 2.X 版本 更多守护与优化 - &c注意 &6- &cYum更新需要重启服务器!重启服务器!重启服务器!; + &c注意 &6- &aYum更新需要&d重启服务器!&c重启服务器!&4重启服务器!; + &b2.6.8 &6- &c修复PerWorldPlugins的监控错误...; &b2.6.7 &6- &d兼容PerWorldPlugins...; &b2.6.6 &6- &c修复快捷点击按钮触发命令错误...; - &b2.6.5 &6- &c修复list命令 &a添加全局能耗统计...; DEBUG UTF-8 diff --git a/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java b/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java index 7cac450..bc93a9b 100644 --- a/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java +++ b/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java @@ -1,5 +1,6 @@ package pw.yumc.Yum.commands; +import java.lang.reflect.Field; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -31,7 +32,6 @@ import pw.yumc.Yum.api.YumAPI; import pw.yumc.Yum.inject.CommandInjector; import pw.yumc.Yum.inject.ListenerInjector; import pw.yumc.Yum.inject.TaskInjector; -import pw.yumc.Yum.managers.ConfigManager; import pw.yumc.Yum.managers.MonitorManager; import pw.yumc.Yum.managers.MonitorManager.MonitorInfo; @@ -41,7 +41,6 @@ import pw.yumc.Yum.managers.MonitorManager.MonitorInfo; * @author 喵♂呜 */ public class MonitorCommand implements HandlerCommands { - public static boolean debug = false; public static Throwable lastError = null; private final String prefix = "§6[§bYum §a能耗监控§6] "; @@ -70,7 +69,6 @@ public class MonitorCommand implements HandlerCommands { final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor"); cmdhandler.registerCommands(this); cmdhandler.registerCommands(PluginTabComplete.instence); - debug = ConfigManager.i().isMonitorDebug(); } @HandlerCommand(name = "cmd", aliases = "c", description = "查看插件命令能耗", minimumArguments = 1, possibleArguments = "[插件名称]") @@ -111,7 +109,7 @@ public class MonitorCommand implements HandlerCommands { } @HandlerCommand(name = "event", aliases = "e", description = "查看插件事件能耗", minimumArguments = 1, possibleArguments = "[插件名称]") - public void event(final InvokeCommandEvent e) throws InstantiationException, IllegalAccessException { + public void event(final InvokeCommandEvent e) throws InstantiationException, IllegalAccessException, NoSuchFieldException { final String pname = e.getArgs()[0]; final CommandSender sender = e.getSender(); final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname); @@ -130,7 +128,12 @@ public class MonitorCommand implements HandlerCommands { eventCount.put(trl.getEventClass().getSimpleName(), trl.getCount()); continue; } - final EventExecutor executor = Reflect.on(listener).get("executor"); + EventExecutor executor = Reflect.on(listener).get("executor"); + if (listener.getClass().getName().contains("PWPRegisteredListener")) { + final Field f = Reflect.on(listener).getDeclaredField(RegisteredListener.class, "executor"); + f.setAccessible(true); + executor = (EventExecutor) f.get(listener); + } if (executor instanceof ListenerInjector) { final ListenerInjector injected = (ListenerInjector) executor; for (final String entry : injected.eventTotalTime.keySet()) { diff --git a/src/main/java/pw/yumc/Yum/inject/CommandInjector.java b/src/main/java/pw/yumc/Yum/inject/CommandInjector.java index 881e1bb..22a9db6 100644 --- a/src/main/java/pw/yumc/Yum/inject/CommandInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/CommandInjector.java @@ -108,7 +108,7 @@ public class CommandInjector implements TabExecutor { final boolean result = originalExecutor.onCommand(sender, command, label, args); final long end = System.nanoTime(); final long lag = end - start; - if (Bukkit.isPrimaryThread() && lag / 1000000 > 10) { + if (Bukkit.isPrimaryThread() && lag / 1000000 > MonitorManager.lagTime) { PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6玩家 §a" + sender.getName() + " §6执行 §b" + plugin.getName() + " §6插件 §d" + label + " " + StrKit.join(args, " ") + " §6命令 §c耗时 §4" + lag / 1000000 + "ms!"); } totalTime += lag; @@ -125,18 +125,7 @@ public class CommandInjector implements TabExecutor { sender.sendMessage("§6异常说明: §3" + e.getMessage()); MonitorCommand.lastError = e; PluginKit.sc(prefix + "§6玩家 §a" + sender.getName() + " §6执行 §b" + plugin.getName() + " §6插件 §d" + label + " " + StrKit.join(args, " ") + " §6命令时发生异常!"); - PluginKit.sc("§6异常名称: §c" + e.getClass().getName()); - PluginKit.sc("§6异常说明: §3" + e.getMessage()); - PluginKit.sc("§6简易错误信息如下:"); - final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length; - for (int i = 0; i < l; i++) { - final StackTraceElement ste = e.getStackTrace()[i]; - PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)"); - } - if (MonitorCommand.debug) { - PluginKit.sc("§c开发人员调试信息如下:"); - e.printStackTrace(); - } + MonitorManager.print(e); } return false; } diff --git a/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java b/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java index 9b02e16..5fa2ab3 100644 --- a/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java @@ -98,7 +98,7 @@ public class ListenerInjector implements EventExecutor { final long end = System.nanoTime(); final String en = event.getEventName(); final long lag = end - start; - if (lag / 1000000 > 10 && !ConfigManager.i().getMonitorIgnoreList().contains(plugin.getName())) { + if (lag / 1000000 > MonitorManager.lagTime && !ConfigManager.i().getMonitorIgnoreList().contains(plugin.getName())) { PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件 §c耗时 §4" + lag / 1000000 + "ms!"); } if (eventTotalTime.containsKey(en)) { @@ -118,18 +118,7 @@ public class ListenerInjector implements EventExecutor { } MonitorCommand.lastError = e; PluginKit.sc(prefix + "§6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时发生异常!"); - PluginKit.sc("§6异常名称: §c" + e.getClass().getName()); - PluginKit.sc("§6异常说明: §3" + e.getMessage()); - PluginKit.sc("§6简易错误信息如下:"); - final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length; - for (int i = 0; i < l; i++) { - final StackTraceElement ste = e.getStackTrace()[i]; - PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)"); - } - if (MonitorCommand.debug) { - PluginKit.sc("§c开发人员调试信息如下:"); - e.printStackTrace(); - } + MonitorManager.print(e); } } diff --git a/src/main/java/pw/yumc/Yum/inject/TaskInjector.java b/src/main/java/pw/yumc/Yum/inject/TaskInjector.java index b9fce70..78e8059 100644 --- a/src/main/java/pw/yumc/Yum/inject/TaskInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/TaskInjector.java @@ -80,14 +80,13 @@ public class TaskInjector implements Runnable { @Override public void run() { - try { final long start = System.nanoTime(); // TODO 当操作大于10ms的时候添加一个Lag提示 originalTask.run(); final long end = System.nanoTime(); final long lag = end - start; - if (Bukkit.isPrimaryThread() && lag / 1000000 > 10) { + if (Bukkit.isPrimaryThread() && lag / 1000000 > MonitorManager.lagTime) { PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6插件 §b" + plugin.getName() + " §6执行 §d" + taskName + " §6任务 §c耗时 §4" + lag / 1000000 + "ms!"); } totalTime += lag; @@ -99,18 +98,7 @@ public class TaskInjector implements Runnable { } MonitorCommand.lastError = e; PluginKit.sc(prefix + "§6插件 §b" + plugin.getName() + " §6执行 §d" + taskName + " §6任务时发生异常!"); - PluginKit.sc("§6异常名称: §c" + e.getClass().getName()); - PluginKit.sc("§6异常说明: §3" + e.getMessage()); - PluginKit.sc("§6简易错误信息如下:"); - final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length; - for (int i = 0; i < l; i++) { - final StackTraceElement ste = e.getStackTrace()[i]; - PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)"); - } - if (MonitorCommand.debug) { - PluginKit.sc("§c开发人员调试信息如下:"); - e.printStackTrace(); - } + MonitorManager.print(e); } } diff --git a/src/main/java/pw/yumc/Yum/managers/MonitorManager.java b/src/main/java/pw/yumc/Yum/managers/MonitorManager.java index b7c46b0..33dd056 100644 --- a/src/main/java/pw/yumc/Yum/managers/MonitorManager.java +++ b/src/main/java/pw/yumc/Yum/managers/MonitorManager.java @@ -10,12 +10,17 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import cn.citycraft.PluginHelper.kit.PluginKit; + /** * * @since 2016年7月19日 下午3:55:54 * @author 喵♂呜 */ public class MonitorManager { + public static int lagTime = 20; + public static boolean debug = ConfigManager.i().isMonitorDebug(); + private static Map monitor = new HashMap<>(); private static Map task = new HashMap<>(); private static Map event = new HashMap<>(); @@ -46,6 +51,21 @@ public class MonitorManager { return new MonitorInfo(monitor.get(pname) / um, cmd.get(pname) / um, event.get(pname) / um, task.get(pname) / um); } + public static void print(final Throwable e) { + PluginKit.sc("§6异常名称: §c" + e.getClass().getName()); + PluginKit.sc("§6异常说明: §3" + e.getMessage()); + PluginKit.sc("§6简易错误信息如下:"); + final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length; + for (int i = 0; i < l; i++) { + final StackTraceElement ste = e.getStackTrace()[i]; + PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)"); + } + if (debug) { + PluginKit.sc("§c开发人员调试信息如下:"); + e.printStackTrace(); + } + } + public static void reset(final String pname) { monitor.put(pname, 0L); task.put(pname, 0L);