diff --git a/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java b/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java index 9b70fc1..50e7e8f 100644 --- a/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java +++ b/src/main/java/pw/yumc/Yum/commands/MonitorCommand.java @@ -16,6 +16,7 @@ import org.bukkit.plugin.EventExecutor; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredListener; +import org.bukkit.plugin.TimedRegisteredListener; import org.bukkit.scheduler.BukkitTask; import cn.citycraft.PluginHelper.commands.HandlerCommand; @@ -43,7 +44,6 @@ public class MonitorCommand implements HandlerCommands { public MonitorCommand(final Yum yum) { final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor"); - cmdhandler.setAllCommandOnlyConsole(yum.getConfig().getBoolean("onlyFileCommandConsole", true)); cmdhandler.registerCommands(this); cmdhandler.registerCommands(PluginTabComplete.instence); } @@ -91,7 +91,7 @@ public class MonitorCommand implements HandlerCommands { } @HandlerCommand(name = "event", minimumArguments = 1, possibleArguments = "插件名称") - public void event(final InvokeCommandEvent e) { + public void event(final InvokeCommandEvent e) throws InstantiationException, IllegalAccessException { final String pname = e.getArgs()[0]; final CommandSender sender = e.getSender(); final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname); @@ -102,8 +102,14 @@ public class MonitorCommand implements HandlerCommands { sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!"); final List listeners = HandlerList.getRegisteredListeners(plugin); final Map eventTotalTime = new HashMap<>(); - final Map eventCount = new HashMap<>(); + final Map eventCount = new HashMap<>(); for (final RegisteredListener listener : listeners) { + if (listener instanceof TimedRegisteredListener) { + final TimedRegisteredListener trl = (TimedRegisteredListener) listener; + eventTotalTime.put(trl.getEventClass().newInstance().getEventName(), trl.getTotalTime()); + eventCount.put(trl.getEventClass().newInstance().getEventName(), trl.getCount()); + continue; + } final EventExecutor executor = Reflect.on(listener).get("executor"); if (executor instanceof ListenerInjector) { final ListenerInjector injected = (ListenerInjector) executor; diff --git a/src/main/java/pw/yumc/Yum/commands/NetCommand.java b/src/main/java/pw/yumc/Yum/commands/NetCommand.java index c7e6048..9a0c6d1 100644 --- a/src/main/java/pw/yumc/Yum/commands/NetCommand.java +++ b/src/main/java/pw/yumc/Yum/commands/NetCommand.java @@ -10,7 +10,6 @@ public class NetCommand implements HandlerCommands { public NetCommand(final Yum yum) { final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "net"); - cmdhandler.setAllCommandOnlyConsole(yum.getConfig().getBoolean("onlyNetCommandConsole", false)); cmdhandler.registerCommands(this); cmdhandler.registerCommands(PluginTabComplete.instence); } diff --git a/src/main/java/pw/yumc/Yum/inject/CommandInjector.java b/src/main/java/pw/yumc/Yum/inject/CommandInjector.java index c134f7e..6846cc0 100644 --- a/src/main/java/pw/yumc/Yum/inject/CommandInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/CommandInjector.java @@ -1,5 +1,6 @@ package pw.yumc.Yum.inject; +import java.util.Collections; import java.util.List; import org.bukkit.Bukkit; @@ -21,7 +22,7 @@ public class CommandInjector implements TabExecutor { private final TabCompleter originalCompleter; public long totalTime; - public long count; + public int count; public CommandInjector(final CommandExecutor originalCommandExecutor, final TabCompleter originalTabCompleter) { this.originalExecutor = originalCommandExecutor; @@ -86,7 +87,6 @@ public class CommandInjector implements TabExecutor { // TODO add a more aggressive 10 ms cpu sample final boolean result = originalExecutor.onCommand(sender, command, label, args); final long end = System.nanoTime(); - totalTime += end - start; count++; return result; @@ -94,6 +94,9 @@ public class CommandInjector implements TabExecutor { @Override public List onTabComplete(final CommandSender sender, final Command command, final String alias, final String[] args) { + if (originalCompleter == null) { + return Collections.emptyList(); + } final long start = System.nanoTime(); // TODO add a more aggressive 10 ms cpu sample final List result = originalCompleter.onTabComplete(sender, command, alias, args); diff --git a/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java b/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java index cea4ca4..7e009fa 100644 --- a/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java @@ -14,16 +14,20 @@ import org.bukkit.plugin.RegisteredListener; import org.bukkit.plugin.TimedRegisteredListener; import cn.citycraft.PluginHelper.ext.kit.Reflect; +import cn.citycraft.PluginHelper.kit.PluginKit; public class ListenerInjector implements EventExecutor { - + private final String prefix = "§6[§bYum §a事件监控§6] "; private final EventExecutor originalExecutor; - public Map eventTotalTime; - public Map eventCount; + private final Plugin plugin; - public ListenerInjector(final EventExecutor originalExecutor) { + public Map eventTotalTime; + public Map eventCount; + + public ListenerInjector(final EventExecutor originalExecutor, final Plugin plugin) { this.originalExecutor = originalExecutor; + this.plugin = plugin; eventTotalTime = new HashMap<>(); eventCount = new HashMap<>(); } @@ -38,7 +42,7 @@ public class ListenerInjector implements EventExecutor { if (originalExecutor instanceof ListenerInjector) { return; } - final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor); + final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin); Reflect.on(listener).set("executor", listenerInjector); } } @@ -58,21 +62,34 @@ public class ListenerInjector implements EventExecutor { @Override public void execute(final Listener listener, final Event event) throws EventException { - if (!event.isAsynchronous()) { - final long start = System.nanoTime(); - // TODO add a more aggressive 10 ms cpu sample - originalExecutor.execute(listener, event); - final long end = System.nanoTime(); - final String en = event.getEventName(); - if (eventTotalTime.containsKey(en)) { - eventTotalTime.put(en, eventTotalTime.get(en) + end - start); - eventCount.put(en, eventCount.get(en) + 1); + try { + if (!event.isAsynchronous()) { + final long start = System.nanoTime(); + // TODO add a more aggressive 10 ms cpu sample + originalExecutor.execute(listener, event); + final long end = System.nanoTime(); + final String en = event.getEventName(); + if (eventTotalTime.containsKey(en)) { + eventTotalTime.put(en, eventTotalTime.get(en) + end - start); + eventCount.put(en, eventCount.get(en) + 1); + } else { + eventTotalTime.put(en, end - start); + eventCount.put(en, 1); + } } else { - eventTotalTime.put(en, end - start); - eventCount.put(en, 1L); + originalExecutor.execute(listener, event); + } + } catch (Throwable e) { + while (e.getCause() != null) { + e = e.getCause(); + } + PluginKit.sc(prefix + "插件 §b" + plugin.getName() + " §6处理 " + event.getEventName() + " §a事件时发生异常 §c" + e.getClass().getName() + ": " + e.getMessage()); + PluginKit.sc("§c错误信息如下:"); + 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)"); } - } else { - originalExecutor.execute(listener, event); } } diff --git a/src/main/java/pw/yumc/Yum/inject/TaskInjector.java b/src/main/java/pw/yumc/Yum/inject/TaskInjector.java index 72540fb..1864824 100644 --- a/src/main/java/pw/yumc/Yum/inject/TaskInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/TaskInjector.java @@ -14,7 +14,7 @@ public class TaskInjector implements Runnable { private final Runnable originalTask; public long totalTime; - public long count; + public int count; public TaskInjector(final Runnable originalTask) { this.originalTask = originalTask; @@ -62,7 +62,6 @@ public class TaskInjector implements Runnable { // TODO add a more aggressive 10 ms cpu sample originalTask.run(); final long end = System.nanoTime(); - totalTime += end - start; count++; }