From 2cd540a953a317fd4d918b4a97ec973b793a404c Mon Sep 17 00:00:00 2001 From: 502647092 Date: Wed, 20 Jul 2016 10:39:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8B=A6=E6=88=AA=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/Yum/inject/CommandInjector.java | 81 +++++++++++-------- .../pw/yumc/Yum/inject/ListenerInjector.java | 50 +++++++----- .../java/pw/yumc/Yum/inject/TaskInjector.java | 51 +++++++----- 3 files changed, 108 insertions(+), 74 deletions(-) diff --git a/src/main/java/pw/yumc/Yum/inject/CommandInjector.java b/src/main/java/pw/yumc/Yum/inject/CommandInjector.java index ddc899d..6c75ffe 100644 --- a/src/main/java/pw/yumc/Yum/inject/CommandInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/CommandInjector.java @@ -3,6 +3,7 @@ package pw.yumc.Yum.inject; import java.util.Collections; import java.util.List; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -21,7 +22,9 @@ import pw.yumc.Yum.commands.MonitorCommand; import pw.yumc.Yum.managers.MonitorManager; public class CommandInjector implements TabExecutor { - private final String prefix = "§6[§bYum §a命令监控§6] "; + private final static String prefix = "§6[§bYum §a命令监控§6] "; + private final static String inject_error = prefix + "§6插件 §b%s §c注入能耗监控失败!"; + private final static String plugin_is_null = "插件不得为NULL!"; private final CommandExecutor originalExecutor; private final TabCompleter originalCompleter; @@ -36,47 +39,56 @@ public class CommandInjector implements TabExecutor { this.plugin = plugin; } - public static void inject(final Plugin toInjectPlugin) { - final PluginManager pluginManager = Bukkit.getPluginManager(); - final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap"); - for (final Command command : commandMap.getCommands()) { - if (command instanceof PluginCommand) { - final PluginCommand pluginCommand = (PluginCommand) command; - final Plugin plugin = pluginCommand.getPlugin(); - if (plugin.equals(toInjectPlugin)) { - final CommandExecutor executor = Reflect.on(command).get("executor"); - if (executor instanceof CommandInjector) { - return; + public static void inject(final Plugin plugin) { + Validate.notNull(plugin, plugin_is_null); + try { + final PluginManager pluginManager = Bukkit.getPluginManager(); + final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap"); + for (final Command command : commandMap.getCommands()) { + if (command instanceof PluginCommand) { + final PluginCommand pluginCommand = (PluginCommand) command; + final Plugin cp = pluginCommand.getPlugin(); + if (cp.equals(plugin)) { + final CommandExecutor executor = Reflect.on(command).get("executor"); + if (executor instanceof CommandInjector) { + return; + } + final TabCompleter completer = Reflect.on(command).get("completer"); + final CommandInjector commandInjector = new CommandInjector(executor, completer, plugin); + Reflect.on(command).set("executor", commandInjector); + Reflect.on(command).set("completer", commandInjector); } - final TabCompleter completer = Reflect.on(command).get("completer"); - final CommandInjector commandInjector = new CommandInjector(executor, completer, toInjectPlugin); - Reflect.on(command).set("executor", commandInjector); - Reflect.on(command).set("completer", commandInjector); } } + } catch (final Throwable e) { + PluginKit.sc(String.format(inject_error, plugin.getName())); } } - public static void uninject(final Plugin toUninject) { - final PluginManager pluginManager = Bukkit.getPluginManager(); - final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap"); - for (final Command command : commandMap.getCommands()) { - if (command instanceof PluginCommand) { - final PluginCommand pluginCommand = (PluginCommand) command; - final Plugin plugin = pluginCommand.getPlugin(); - if (plugin.equals(toUninject)) { - final CommandExecutor executor = Reflect.on(command).get("executor"); - if (executor instanceof CommandInjector) { - final CommandInjector injected = (CommandInjector) executor; - Reflect.on(command).set("executor", injected.getOriginalExecutor()); - } - final TabCompleter completer = Reflect.on(command).get("completer"); - if (completer instanceof CommandInjector) { - final CommandInjector injected = (CommandInjector) completer; - Reflect.on(command).set("completer", injected.getOriginalCompleter()); + public static void uninject(final Plugin plugin) { + Validate.notNull(plugin, plugin_is_null); + try { + final PluginManager pluginManager = Bukkit.getPluginManager(); + final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap"); + for (final Command command : commandMap.getCommands()) { + if (command instanceof PluginCommand) { + final PluginCommand pluginCommand = (PluginCommand) command; + final Plugin cp = pluginCommand.getPlugin(); + if (cp.equals(plugin)) { + final CommandExecutor executor = Reflect.on(command).get("executor"); + if (executor instanceof CommandInjector) { + final CommandInjector injected = (CommandInjector) executor; + Reflect.on(command).set("executor", injected.getOriginalExecutor()); + } + final TabCompleter completer = Reflect.on(command).get("completer"); + if (completer instanceof CommandInjector) { + final CommandInjector injected = (CommandInjector) completer; + Reflect.on(command).set("completer", injected.getOriginalCompleter()); + } } } } + } catch (final Throwable e) { } } @@ -97,8 +109,7 @@ public class CommandInjector implements TabExecutor { final long end = System.nanoTime(); final long lag = end - start; if (Bukkit.isPrimaryThread() && lag / 1000000 > 10) { - 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!"); + 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; count++; diff --git a/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java b/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java index 77a730a..ecc36a9 100644 --- a/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/ListenerInjector.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang.Validate; import org.bukkit.event.Event; import org.bukkit.event.EventException; import org.bukkit.event.HandlerList; @@ -20,7 +21,9 @@ import pw.yumc.Yum.managers.ConfigManager; import pw.yumc.Yum.managers.MonitorManager; public class ListenerInjector implements EventExecutor { - private final String prefix = "§6[§bYum §a事件监控§6] "; + private final static String prefix = "§6[§bYum §a事件监控§6] "; + private final static String inject_error = prefix + "§6插件 §b%s §c注入能耗监控失败!"; + private final static String plugin_is_null = "插件不得为NULL!"; private final EventExecutor originalExecutor; private final Plugin plugin; @@ -36,30 +39,39 @@ public class ListenerInjector implements EventExecutor { } public static void inject(final Plugin plugin) { - final List listeners = HandlerList.getRegisteredListeners(plugin); - for (final RegisteredListener listener : listeners) { - if (listener instanceof TimedRegisteredListener) { - return; + Validate.notNull(plugin, plugin_is_null); + try { + final List listeners = HandlerList.getRegisteredListeners(plugin); + for (final RegisteredListener listener : listeners) { + if (listener instanceof TimedRegisteredListener) { + return; + } + final EventExecutor originalExecutor = Reflect.on(listener).get("executor"); + if (originalExecutor instanceof ListenerInjector) { + return; + } + final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin); + Reflect.on(listener).set("executor", listenerInjector); } - final EventExecutor originalExecutor = Reflect.on(listener).get("executor"); - if (originalExecutor instanceof ListenerInjector) { - return; - } - final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin); - Reflect.on(listener).set("executor", listenerInjector); + } catch (final Throwable e) { + PluginKit.sc(String.format(inject_error, plugin.getName())); } } public static void uninject(final Plugin plugin) { - final List listeners = HandlerList.getRegisteredListeners(plugin); - for (final RegisteredListener listener : listeners) { - if (listener instanceof TimedRegisteredListener) { - return; - } - final EventExecutor executor = Reflect.on(listener).get("executor"); - if (executor instanceof ListenerInjector) { - Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor()); + Validate.notNull(plugin, plugin_is_null); + try { + final List listeners = HandlerList.getRegisteredListeners(plugin); + for (final RegisteredListener listener : listeners) { + if (listener instanceof TimedRegisteredListener) { + return; + } + final EventExecutor executor = Reflect.on(listener).get("executor"); + if (executor instanceof ListenerInjector) { + Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor()); + } } + } catch (final Throwable e) { } } diff --git a/src/main/java/pw/yumc/Yum/inject/TaskInjector.java b/src/main/java/pw/yumc/Yum/inject/TaskInjector.java index a18ae14..b9fce70 100644 --- a/src/main/java/pw/yumc/Yum/inject/TaskInjector.java +++ b/src/main/java/pw/yumc/Yum/inject/TaskInjector.java @@ -2,6 +2,7 @@ package pw.yumc.Yum.inject; import java.util.List; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitScheduler; @@ -14,8 +15,9 @@ import pw.yumc.Yum.commands.MonitorCommand; import pw.yumc.Yum.managers.MonitorManager; public class TaskInjector implements Runnable { - private final String prefix = "§6[§bYum §a任务监控§6] "; - + private final static String prefix = "§6[§bYum §a任务监控§6] "; + private final static String inject_error = prefix + "§6插件 §b%s §c注入能耗监控失败!"; + private final static String plugin_is_null = "插件不得为NULL!"; private final Runnable originalTask; private final Plugin plugin; @@ -34,32 +36,41 @@ public class TaskInjector implements Runnable { // 当前注入只能对TimerTask有效 // 对于单次执行的任务 我们需要注册一个动态的代理 public static void inject(final Plugin plugin) { - final BukkitScheduler scheduler = Bukkit.getScheduler(); - final List pendingTasks = scheduler.getPendingTasks(); - for (final BukkitTask pendingTask : pendingTasks) { - // 忽略异步任务 - if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { - final Runnable originalTask = Reflect.on(pendingTask).get("task"); - if (originalTask instanceof TaskInjector) { - return; + Validate.notNull(plugin, plugin_is_null); + try { + final BukkitScheduler scheduler = Bukkit.getScheduler(); + final List pendingTasks = scheduler.getPendingTasks(); + for (final BukkitTask pendingTask : pendingTasks) { + // 忽略异步任务 + if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { + final Runnable originalTask = Reflect.on(pendingTask).get("task"); + if (originalTask instanceof TaskInjector) { + return; + } + final TaskInjector taskInjector = new TaskInjector(originalTask, plugin); + Reflect.on(pendingTask).set("task", taskInjector); } - final TaskInjector taskInjector = new TaskInjector(originalTask, plugin); - Reflect.on(pendingTask).set("task", taskInjector); } + } catch (final Throwable e) { + PluginKit.sc(String.format(inject_error, plugin.getName())); } } public static void uninject(final Plugin plugin) { - final BukkitScheduler scheduler = Bukkit.getScheduler(); - final List pendingTasks = scheduler.getPendingTasks(); - for (final BukkitTask pendingTask : pendingTasks) { - // 忽略异步任务 - if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { - final Runnable originalTask = Reflect.on(pendingTask).get("task"); - if (originalTask instanceof TaskInjector) { - Reflect.on(pendingTask).set("task", ((TaskInjector) originalTask).getOriginalTask()); + Validate.notNull(plugin, plugin_is_null); + try { + final BukkitScheduler scheduler = Bukkit.getScheduler(); + final List pendingTasks = scheduler.getPendingTasks(); + for (final BukkitTask pendingTask : pendingTasks) { + // 忽略异步任务 + if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { + final Runnable originalTask = Reflect.on(pendingTask).get("task"); + if (originalTask instanceof TaskInjector) { + Reflect.on(pendingTask).set("task", ((TaskInjector) originalTask).getOriginalTask()); + } } } + } catch (final Throwable e) { } }