feat: 调整注入流程 完善监控命令

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-07 20:54:26 +08:00
parent d9004b0b9e
commit ffeccab654
8 changed files with 53 additions and 32 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>Yum</artifactId> <artifactId>Yum</artifactId>
<version>2.5</version> <version>2.5.1</version>
<name>Yum</name> <name>Yum</name>
<description>Minecraft 服务器插件管理系统</description> <description>Minecraft 服务器插件管理系统</description>
<build> <build>
@ -60,6 +60,7 @@
<properties> <properties>
<update.description>&amp;a全新 2.X 版本 更多守护与优化</update.description> <update.description>&amp;a全新 2.X 版本 更多守护与优化</update.description>
<update.changes> <update.changes>
&amp;b2.5.1 &amp;6- &amp;b添加能耗监控系统(Bate)...;
&amp;b2.5 &amp;6- &amp;d修复主线程守护系统错误...; &amp;b2.5 &amp;6- &amp;d修复主线程守护系统错误...;
&amp;b2.4 &amp;6- &amp;c修复重载插件导致线程安全错误的问题...; &amp;b2.4 &amp;6- &amp;c修复重载插件导致线程安全错误的问题...;
&amp;b2.3 &amp;6- &amp;a网络监控添加域名白名单功能...; &amp;b2.3 &amp;6- &amp;a网络监控添加域名白名单功能...;

View File

@ -8,7 +8,6 @@ import java.util.TimerTask;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.CommonData.UpdatePlugin; import cn.citycraft.CommonData.UpdatePlugin;
@ -19,6 +18,7 @@ import pw.yumc.Yum.commands.FileCommand;
import pw.yumc.Yum.commands.MonitorCommand; import pw.yumc.Yum.commands.MonitorCommand;
import pw.yumc.Yum.commands.NetCommand; import pw.yumc.Yum.commands.NetCommand;
import pw.yumc.Yum.commands.YumCommand; import pw.yumc.Yum.commands.YumCommand;
import pw.yumc.Yum.listeners.PluginListener;
import pw.yumc.Yum.listeners.PluginNetworkListener; import pw.yumc.Yum.listeners.PluginNetworkListener;
import pw.yumc.Yum.listeners.SecurityListener; import pw.yumc.Yum.listeners.SecurityListener;
import pw.yumc.Yum.listeners.ThreadSafetyListener; import pw.yumc.Yum.listeners.ThreadSafetyListener;
@ -45,9 +45,9 @@ public class Yum extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
NetworkManager.unregister(); NetworkManager.unregister();
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { // for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
YumAPI.uninject(plugin); // YumAPI.uninject(plugin);
} // }
} }
@Override @Override
@ -59,7 +59,6 @@ public class Yum extends JavaPlugin {
initCommands(); initCommands();
initListeners(); initListeners();
initRunnable(); initRunnable();
initInject();
new VersionChecker(this); new VersionChecker(this);
YumAPI.updaterepo(Bukkit.getConsoleSender()); YumAPI.updaterepo(Bukkit.getConsoleSender());
YumAPI.updatecheck(Bukkit.getConsoleSender()); YumAPI.updatecheck(Bukkit.getConsoleSender());
@ -85,17 +84,11 @@ public class Yum extends JavaPlugin {
new MonitorCommand(this); new MonitorCommand(this);
} }
private void initInject() {
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
YumAPI.inject(plugin);
}
PluginKit.scp("§a性能监控系统已启用...");
}
/** /**
* 初始化监听 * 初始化监听
*/ */
private void initListeners() { private void initListeners() {
new PluginListener();
if (ConfigManager.i().isSetOpEnable()) { if (ConfigManager.i().isSetOpEnable()) {
try { try {
final ClassLoader cl = Class.forName("pw.yumc.injected.event.SetOpEvent").getClassLoader(); final ClassLoader cl = Class.forName("pw.yumc.injected.event.SetOpEvent").getClassLoader();

View File

@ -13,6 +13,7 @@ import org.bukkit.plugin.Plugin;
import cn.citycraft.CommonData.UpdatePlugin; import cn.citycraft.CommonData.UpdatePlugin;
import cn.citycraft.PluginHelper.kit.PKit; import cn.citycraft.PluginHelper.kit.PKit;
import pw.yumc.Yum.inject.CommandInjector; import pw.yumc.Yum.inject.CommandInjector;
import pw.yumc.Yum.inject.TaskInjector;
import pw.yumc.Yum.managers.ConfigManager; import pw.yumc.Yum.managers.ConfigManager;
import pw.yumc.Yum.managers.DownloadManager; import pw.yumc.Yum.managers.DownloadManager;
import pw.yumc.Yum.managers.PluginsManager; import pw.yumc.Yum.managers.PluginsManager;
@ -105,7 +106,7 @@ public class YumAPI {
public static void inject(final Plugin plugin) { public static void inject(final Plugin plugin) {
CommandInjector.inject(plugin); CommandInjector.inject(plugin);
// ListenerInjector.inject(plugin); // ListenerInjector.inject(plugin);
// TaskInjector.inject(plugin); TaskInjector.inject(plugin);
} }
/** /**
@ -211,7 +212,7 @@ public class YumAPI {
public static void uninject(final Plugin plugin) { public static void uninject(final Plugin plugin) {
CommandInjector.uninject(plugin); CommandInjector.uninject(plugin);
// ListenerInjector.uninject(plugin); // ListenerInjector.uninject(plugin);
// TaskInjector.uninject(plugin); TaskInjector.uninject(plugin);
} }
/** /**

View File

@ -11,9 +11,11 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap; import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabCompleter; import org.bukkit.event.HandlerList;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import cn.citycraft.PluginHelper.commands.HandlerCommand; import cn.citycraft.PluginHelper.commands.HandlerCommand;
@ -23,6 +25,7 @@ import cn.citycraft.PluginHelper.commands.InvokeSubCommand;
import cn.citycraft.PluginHelper.ext.kit.Reflect; import cn.citycraft.PluginHelper.ext.kit.Reflect;
import pw.yumc.Yum.Yum; import pw.yumc.Yum.Yum;
import pw.yumc.Yum.inject.CommandInjector; import pw.yumc.Yum.inject.CommandInjector;
import pw.yumc.Yum.inject.ListenerInjector;
import pw.yumc.Yum.inject.TaskInjector; import pw.yumc.Yum.inject.TaskInjector;
/** /**
@ -79,11 +82,33 @@ public class MonitorCommand implements HandlerCommands {
str.append(String.format("§6平均耗时: §d%.2f秒!", injected.totalTime / 1000000.0 / injected.count)); str.append(String.format("§6平均耗时: §d%.2f秒!", injected.totalTime / 1000000.0 / injected.count));
} }
e.getSender().sendMessage(str.toString()); e.getSender().sendMessage(str.toString());
} else { }
final TabCompleter completer = Reflect.on(command).get("completer"); }
final CommandInjector commandInjector = new CommandInjector(executor, completer); }
Reflect.on(command).set("executor", commandInjector);
Reflect.on(command).set("completer", commandInjector); @HandlerCommand(name = "event", minimumArguments = 1, possibleArguments = "插件名称")
public void event(final InvokeCommandEvent e) {
final String pname = e.getArgs()[0];
final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
if (plugin == null) {
sender.sendMessage("§c插件不存在!");
return;
}
sender.sendMessage("§6插件 §b" + pname + " §6的事件能耗如下!");
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) {
final EventExecutor executor = Reflect.on(listener).get("executor");
if (executor instanceof ListenerInjector) {
final ListenerInjector injected = (ListenerInjector) executor;
final StringBuffer str = new StringBuffer();
str.append("§6- §e" + injected.getOriginalExecutor().getClass().getSimpleName() + " ");
str.append(String.format("§6总耗时: §a%.2f秒 ", injected.totalTime / 1000000.0));
str.append("§6执行次数: §b" + injected.count + "");
if (injected.count != 0) {
str.append(String.format("§6平均耗时: §d%.2f秒!", injected.totalTime / 1000000.0 / injected.count));
}
e.getSender().sendMessage(str.toString());
} }
} }
} }
@ -104,16 +129,13 @@ public class MonitorCommand implements HandlerCommands {
if (task instanceof TaskInjector) { if (task instanceof TaskInjector) {
final TaskInjector executor = (TaskInjector) task; final TaskInjector executor = (TaskInjector) task;
final StringBuffer str = new StringBuffer(); final StringBuffer str = new StringBuffer();
str.append("§6- §e" + task.getClass().getSimpleName() + " "); str.append("§6- §e" + executor.getOriginalTask().getClass().getSimpleName() + " ");
str.append(String.format("§6总耗时: §a%.2f秒 ", executor.totalTime / 1000000.0)); str.append(String.format("§6总耗时: §a%.2f秒 ", executor.totalTime / 1000000.0));
str.append("§6执行次数: §b" + executor.count + ""); str.append("§6执行次数: §b" + executor.count + "");
if (executor.count != 0) { if (executor.count != 0) {
str.append(String.format("§6平均耗时: §d%.2f秒!", executor.totalTime / 1000000.0 / executor.count)); str.append(String.format("§6平均耗时: §d%.2f秒!", executor.totalTime / 1000000.0 / executor.count));
} }
e.getSender().sendMessage(str.toString()); e.getSender().sendMessage(str.toString());
} else {
final TaskInjector taskInjector = new TaskInjector(task);
Reflect.on(pendingTask).set("task", taskInjector);
} }
} }
} }

View File

@ -14,7 +14,6 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import cn.citycraft.PluginHelper.ext.kit.Reflect; import cn.citycraft.PluginHelper.ext.kit.Reflect;
import cn.citycraft.PluginHelper.kit.PluginKit;
public class CommandInjector implements TabExecutor { public class CommandInjector implements TabExecutor {
@ -48,7 +47,6 @@ public class CommandInjector implements TabExecutor {
} }
} }
} }
PluginKit.scp("§a注入命令性能监控到 " + toInjectPlugin.getName());
} }
public static void uninject(final Plugin toUninject) { public static void uninject(final Plugin toUninject) {

View File

@ -35,8 +35,10 @@ public class ListenerInjector implements EventExecutor {
public static void uninject(final Plugin plugin) { public static void uninject(final Plugin plugin) {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
final ListenerInjector executor = Reflect.on(listener).get("executor"); final EventExecutor executor = Reflect.on(listener).get("executor");
Reflect.on(listener).set("executor", executor.getOriginalExecutor()); if (executor instanceof ListenerInjector) {
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
}
} }
} }

View File

@ -41,8 +41,10 @@ public class TaskInjector implements Runnable {
for (final BukkitTask pendingTask : pendingTasks) { for (final BukkitTask pendingTask : pendingTasks) {
// we could ignore async tasks for now // we could ignore async tasks for now
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
final TaskInjector originalTask = Reflect.on(pendingTask).get("task"); final Runnable originalTask = Reflect.on(pendingTask).get("task");
Reflect.on(pendingTask).set("task", originalTask.getOriginalTask()); if (originalTask instanceof TaskInjector) {
Reflect.on(pendingTask).set("task", ((TaskInjector) originalTask).getOriginalTask());
}
} }
} }
} }

View File

@ -7,6 +7,7 @@ import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.PluginEnableEvent;
import cn.citycraft.PluginHelper.kit.PKit; import cn.citycraft.PluginHelper.kit.PKit;
import cn.citycraft.PluginHelper.kit.PluginKit;
import pw.yumc.Yum.api.YumAPI; import pw.yumc.Yum.api.YumAPI;
/** /**
@ -18,6 +19,7 @@ public class PluginListener implements Listener {
public PluginListener() { public PluginListener() {
Bukkit.getPluginManager().registerEvents(this, PKit.i()); Bukkit.getPluginManager().registerEvents(this, PKit.i());
PluginKit.scp("§a性能监控系统已启用...");
} }
@EventHandler @EventHandler