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.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,47 +39,56 @@ 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) {
|
||||||
final PluginManager pluginManager = Bukkit.getPluginManager();
|
Validate.notNull(plugin, plugin_is_null);
|
||||||
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
|
try {
|
||||||
for (final Command command : commandMap.getCommands()) {
|
final PluginManager pluginManager = Bukkit.getPluginManager();
|
||||||
if (command instanceof PluginCommand) {
|
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
|
||||||
final PluginCommand pluginCommand = (PluginCommand) command;
|
for (final Command command : commandMap.getCommands()) {
|
||||||
final Plugin plugin = pluginCommand.getPlugin();
|
if (command instanceof PluginCommand) {
|
||||||
if (plugin.equals(toInjectPlugin)) {
|
final PluginCommand pluginCommand = (PluginCommand) command;
|
||||||
final CommandExecutor executor = Reflect.on(command).get("executor");
|
final Plugin cp = pluginCommand.getPlugin();
|
||||||
if (executor instanceof CommandInjector) {
|
if (cp.equals(plugin)) {
|
||||||
return;
|
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) {
|
public static void uninject(final Plugin plugin) {
|
||||||
final PluginManager pluginManager = Bukkit.getPluginManager();
|
Validate.notNull(plugin, plugin_is_null);
|
||||||
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
|
try {
|
||||||
for (final Command command : commandMap.getCommands()) {
|
final PluginManager pluginManager = Bukkit.getPluginManager();
|
||||||
if (command instanceof PluginCommand) {
|
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
|
||||||
final PluginCommand pluginCommand = (PluginCommand) command;
|
for (final Command command : commandMap.getCommands()) {
|
||||||
final Plugin plugin = pluginCommand.getPlugin();
|
if (command instanceof PluginCommand) {
|
||||||
if (plugin.equals(toUninject)) {
|
final PluginCommand pluginCommand = (PluginCommand) command;
|
||||||
final CommandExecutor executor = Reflect.on(command).get("executor");
|
final Plugin cp = pluginCommand.getPlugin();
|
||||||
if (executor instanceof CommandInjector) {
|
if (cp.equals(plugin)) {
|
||||||
final CommandInjector injected = (CommandInjector) executor;
|
final CommandExecutor executor = Reflect.on(command).get("executor");
|
||||||
Reflect.on(command).set("executor", injected.getOriginalExecutor());
|
if (executor instanceof CommandInjector) {
|
||||||
}
|
final CommandInjector injected = (CommandInjector) executor;
|
||||||
final TabCompleter completer = Reflect.on(command).get("completer");
|
Reflect.on(command).set("executor", injected.getOriginalExecutor());
|
||||||
if (completer instanceof CommandInjector) {
|
}
|
||||||
final CommandInjector injected = (CommandInjector) completer;
|
final TabCompleter completer = Reflect.on(command).get("completer");
|
||||||
Reflect.on(command).set("completer", injected.getOriginalCompleter());
|
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 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++;
|
||||||
|
@ -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,30 +39,39 @@ public class ListenerInjector implements EventExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void inject(final Plugin plugin) {
|
public static void inject(final Plugin plugin) {
|
||||||
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
|
Validate.notNull(plugin, plugin_is_null);
|
||||||
for (final RegisteredListener listener : listeners) {
|
try {
|
||||||
if (listener instanceof TimedRegisteredListener) {
|
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
|
||||||
return;
|
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");
|
} catch (final Throwable e) {
|
||||||
if (originalExecutor instanceof ListenerInjector) {
|
PluginKit.sc(String.format(inject_error, plugin.getName()));
|
||||||
return;
|
|
||||||
}
|
|
||||||
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin);
|
|
||||||
Reflect.on(listener).set("executor", listenerInjector);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void uninject(final Plugin plugin) {
|
public static void uninject(final Plugin plugin) {
|
||||||
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
|
Validate.notNull(plugin, plugin_is_null);
|
||||||
for (final RegisteredListener listener : listeners) {
|
try {
|
||||||
if (listener instanceof TimedRegisteredListener) {
|
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
|
||||||
return;
|
for (final RegisteredListener listener : listeners) {
|
||||||
}
|
if (listener instanceof TimedRegisteredListener) {
|
||||||
final EventExecutor executor = Reflect.on(listener).get("executor");
|
return;
|
||||||
if (executor instanceof ListenerInjector) {
|
}
|
||||||
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
|
final EventExecutor executor = Reflect.on(listener).get("executor");
|
||||||
|
if (executor instanceof ListenerInjector) {
|
||||||
|
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (final Throwable e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,32 +36,41 @@ public class TaskInjector implements Runnable {
|
|||||||
// 当前注入只能对TimerTask有效
|
// 当前注入只能对TimerTask有效
|
||||||
// 对于单次执行的任务 我们需要注册一个动态的代理
|
// 对于单次执行的任务 我们需要注册一个动态的代理
|
||||||
public static void inject(final Plugin plugin) {
|
public static void inject(final Plugin plugin) {
|
||||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
Validate.notNull(plugin, plugin_is_null);
|
||||||
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
try {
|
||||||
for (final BukkitTask pendingTask : pendingTasks) {
|
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||||
// 忽略异步任务
|
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
||||||
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
|
for (final BukkitTask pendingTask : pendingTasks) {
|
||||||
final Runnable originalTask = Reflect.on(pendingTask).get("task");
|
// 忽略异步任务
|
||||||
if (originalTask instanceof TaskInjector) {
|
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
|
||||||
return;
|
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) {
|
public static void uninject(final Plugin plugin) {
|
||||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
Validate.notNull(plugin, plugin_is_null);
|
||||||
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
try {
|
||||||
for (final BukkitTask pendingTask : pendingTasks) {
|
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||||
// 忽略异步任务
|
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
||||||
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
|
for (final BukkitTask pendingTask : pendingTasks) {
|
||||||
final Runnable originalTask = Reflect.on(pendingTask).get("task");
|
// 忽略异步任务
|
||||||
if (originalTask instanceof TaskInjector) {
|
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
|
||||||
Reflect.on(pendingTask).set("task", ((TaskInjector) originalTask).getOriginalTask());
|
final Runnable originalTask = Reflect.on(pendingTask).get("task");
|
||||||
|
if (originalTask instanceof TaskInjector) {
|
||||||
|
Reflect.on(pendingTask).set("task", ((TaskInjector) originalTask).getOriginalTask());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (final Throwable e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user