mirror of
https://e.coding.net/circlecloud/Yum.git
synced 2024-11-22 14:28:46 +00:00
feat: 拦截注入错误异常
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
0fcecd63cf
commit
2cd540a953
@ -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,35 +39,42 @@ public class CommandInjector implements TabExecutor {
|
||||
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 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 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, toInjectPlugin);
|
||||
final CommandInjector commandInjector = new CommandInjector(executor, completer, plugin);
|
||||
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) {
|
||||
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 plugin = pluginCommand.getPlugin();
|
||||
if (plugin.equals(toUninject)) {
|
||||
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;
|
||||
@ -78,6 +88,8 @@ public class CommandInjector implements TabExecutor {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
public TabCompleter getOriginalCompleter() {
|
||||
@ -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++;
|
||||
|
@ -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,6 +39,8 @@ public class ListenerInjector implements EventExecutor {
|
||||
}
|
||||
|
||||
public static void inject(final Plugin plugin) {
|
||||
Validate.notNull(plugin, plugin_is_null);
|
||||
try {
|
||||
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
|
||||
for (final RegisteredListener listener : listeners) {
|
||||
if (listener instanceof TimedRegisteredListener) {
|
||||
@ -48,9 +53,14 @@ public class ListenerInjector implements EventExecutor {
|
||||
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) {
|
||||
Validate.notNull(plugin, plugin_is_null);
|
||||
try {
|
||||
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
|
||||
for (final RegisteredListener listener : listeners) {
|
||||
if (listener instanceof TimedRegisteredListener) {
|
||||
@ -61,6 +71,8 @@ public class ListenerInjector implements EventExecutor {
|
||||
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
|
||||
}
|
||||
}
|
||||
} catch (final Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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,6 +36,8 @@ public class TaskInjector implements Runnable {
|
||||
// 当前注入只能对TimerTask有效
|
||||
// 对于单次执行的任务 我们需要注册一个动态的代理
|
||||
public static void inject(final Plugin plugin) {
|
||||
Validate.notNull(plugin, plugin_is_null);
|
||||
try {
|
||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
||||
for (final BukkitTask pendingTask : pendingTasks) {
|
||||
@ -47,9 +51,14 @@ public class TaskInjector implements Runnable {
|
||||
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) {
|
||||
Validate.notNull(plugin, plugin_is_null);
|
||||
try {
|
||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
||||
for (final BukkitTask pendingTask : pendingTasks) {
|
||||
@ -61,6 +70,8 @@ public class TaskInjector implements Runnable {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
public Runnable getOriginalTask() {
|
||||
|
Loading…
Reference in New Issue
Block a user