feat: 拦截注入错误异常

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-20 10:39:12 +08:00
parent 0fcecd63cf
commit 2cd540a953
3 changed files with 108 additions and 74 deletions

View File

@ -3,6 +3,7 @@ package pw.yumc.Yum.inject;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -21,7 +22,9 @@ import pw.yumc.Yum.commands.MonitorCommand;
import pw.yumc.Yum.managers.MonitorManager; import pw.yumc.Yum.managers.MonitorManager;
public class CommandInjector implements TabExecutor { 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 CommandExecutor originalExecutor;
private final TabCompleter originalCompleter; private final TabCompleter originalCompleter;
@ -36,35 +39,42 @@ public class CommandInjector implements TabExecutor {
this.plugin = plugin; this.plugin = plugin;
} }
public static void inject(final Plugin toInjectPlugin) { public static void inject(final Plugin plugin) {
Validate.notNull(plugin, plugin_is_null);
try {
final PluginManager pluginManager = Bukkit.getPluginManager(); final PluginManager pluginManager = Bukkit.getPluginManager();
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap"); final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
for (final Command command : commandMap.getCommands()) { for (final Command command : commandMap.getCommands()) {
if (command instanceof PluginCommand) { if (command instanceof PluginCommand) {
final PluginCommand pluginCommand = (PluginCommand) command; final PluginCommand pluginCommand = (PluginCommand) command;
final Plugin plugin = pluginCommand.getPlugin(); final Plugin cp = pluginCommand.getPlugin();
if (plugin.equals(toInjectPlugin)) { if (cp.equals(plugin)) {
final CommandExecutor executor = Reflect.on(command).get("executor"); final CommandExecutor executor = Reflect.on(command).get("executor");
if (executor instanceof CommandInjector) { if (executor instanceof CommandInjector) {
return; return;
} }
final TabCompleter completer = Reflect.on(command).get("completer"); final TabCompleter completer = Reflect.on(command).get("completer");
final CommandInjector commandInjector = new CommandInjector(executor, completer, toInjectPlugin); final CommandInjector commandInjector = new CommandInjector(executor, completer, plugin);
Reflect.on(command).set("executor", commandInjector); Reflect.on(command).set("executor", commandInjector);
Reflect.on(command).set("completer", 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) { public static void uninject(final Plugin plugin) {
Validate.notNull(plugin, plugin_is_null);
try {
final PluginManager pluginManager = Bukkit.getPluginManager(); final PluginManager pluginManager = Bukkit.getPluginManager();
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap"); final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
for (final Command command : commandMap.getCommands()) { for (final Command command : commandMap.getCommands()) {
if (command instanceof PluginCommand) { if (command instanceof PluginCommand) {
final PluginCommand pluginCommand = (PluginCommand) command; final PluginCommand pluginCommand = (PluginCommand) command;
final Plugin plugin = pluginCommand.getPlugin(); final Plugin cp = pluginCommand.getPlugin();
if (plugin.equals(toUninject)) { if (cp.equals(plugin)) {
final CommandExecutor executor = Reflect.on(command).get("executor"); final CommandExecutor executor = Reflect.on(command).get("executor");
if (executor instanceof CommandInjector) { if (executor instanceof CommandInjector) {
final CommandInjector injected = (CommandInjector) executor; final CommandInjector injected = (CommandInjector) executor;
@ -78,6 +88,8 @@ public class CommandInjector implements TabExecutor {
} }
} }
} }
} catch (final Throwable e) {
}
} }
public TabCompleter getOriginalCompleter() { public TabCompleter getOriginalCompleter() {
@ -97,8 +109,7 @@ public class CommandInjector implements TabExecutor {
final long end = System.nanoTime(); final long end = System.nanoTime();
final long lag = end - start; final long lag = end - start;
if (Bukkit.isPrimaryThread() && lag / 1000000 > 10) { 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 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!");
+ "ms!");
} }
totalTime += lag; totalTime += lag;
count++; count++;

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.Validate;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.EventException; import org.bukkit.event.EventException;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -20,7 +21,9 @@ import pw.yumc.Yum.managers.ConfigManager;
import pw.yumc.Yum.managers.MonitorManager; import pw.yumc.Yum.managers.MonitorManager;
public class ListenerInjector implements EventExecutor { 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 EventExecutor originalExecutor;
private final Plugin plugin; private final Plugin plugin;
@ -36,6 +39,8 @@ public class ListenerInjector implements EventExecutor {
} }
public static void inject(final Plugin plugin) { public static void inject(final Plugin plugin) {
Validate.notNull(plugin, plugin_is_null);
try {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) { if (listener instanceof TimedRegisteredListener) {
@ -48,9 +53,14 @@ public class ListenerInjector implements EventExecutor {
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin); final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin);
Reflect.on(listener).set("executor", listenerInjector); 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) { public static void uninject(final Plugin plugin) {
Validate.notNull(plugin, plugin_is_null);
try {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) { if (listener instanceof TimedRegisteredListener) {
@ -61,6 +71,8 @@ public class ListenerInjector implements EventExecutor {
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor()); Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
} }
} }
} catch (final Throwable e) {
}
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package pw.yumc.Yum.inject;
import java.util.List; import java.util.List;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@ -14,8 +15,9 @@ import pw.yumc.Yum.commands.MonitorCommand;
import pw.yumc.Yum.managers.MonitorManager; import pw.yumc.Yum.managers.MonitorManager;
public class TaskInjector implements Runnable { 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 Runnable originalTask;
private final Plugin plugin; private final Plugin plugin;
@ -34,6 +36,8 @@ public class TaskInjector implements Runnable {
// 当前注入只能对TimerTask有效 // 当前注入只能对TimerTask有效
// 对于单次执行的任务 我们需要注册一个动态的代理 // 对于单次执行的任务 我们需要注册一个动态的代理
public static void inject(final Plugin plugin) { public static void inject(final Plugin plugin) {
Validate.notNull(plugin, plugin_is_null);
try {
final BukkitScheduler scheduler = Bukkit.getScheduler(); final BukkitScheduler scheduler = Bukkit.getScheduler();
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks(); final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
for (final BukkitTask pendingTask : pendingTasks) { for (final BukkitTask pendingTask : pendingTasks) {
@ -47,9 +51,14 @@ public class TaskInjector implements Runnable {
Reflect.on(pendingTask).set("task", taskInjector); 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) { public static void uninject(final Plugin plugin) {
Validate.notNull(plugin, plugin_is_null);
try {
final BukkitScheduler scheduler = Bukkit.getScheduler(); final BukkitScheduler scheduler = Bukkit.getScheduler();
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks(); final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
for (final BukkitTask pendingTask : pendingTasks) { for (final BukkitTask pendingTask : pendingTasks) {
@ -61,6 +70,8 @@ public class TaskInjector implements Runnable {
} }
} }
} }
} catch (final Throwable e) {
}
} }
public Runnable getOriginalTask() { public Runnable getOriginalTask() {