feat: 添加插件载入类 修改能耗监控显示

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-27 20:27:09 +08:00
parent 844ae43eef
commit 829351e576
5 changed files with 128 additions and 29 deletions

View File

@ -63,11 +63,12 @@
<url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement>
<properties>
<update.description>&amp;a全新 2.X 版本 更多守护与优化</update.description>
<update.description>§a全新 2.X 版本 更多守护与优化</update.description>
<update.changes>
&amp;c注意 &amp;6- &amp;aYum更新需要&amp;d重启服务器!&amp;c重启服务器!&amp;4重启服务器!;
&amp;b2.7.0 &amp;6- &amp;a完善能耗监控流程...;
&amp;b &amp;6- &amp;a报错能耗添加日志记录;
§c注意 §6- §aYum更新需要§d重启服务器!§c重启服务器!§4重启服务器!;
§b2.7.0 §6- §d完善能耗监控流程...;
§b §6- §c报错§6/§d能耗§a添加日志记录;
§b §6- §a文件处理添加复制命令;
</update.changes>
<env.GIT_COMMIT>DEBUG</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -46,10 +46,12 @@ public class MonitorCommand implements CommandExecutor {
private final String prefix = "§6[§bYum §a能耗监控§6] ";
private final String total = "§6总耗时: §a%.2f毫秒 ";
private final String count = "§6执行次数: §b%s次 ";
private final String avg = "§6平均耗时: §d%.5f毫秒!";
private final String avg_warn = "§6平均耗时: §c%.5f毫秒!";
private final String micprefix = " §6命令名称 §a总耗时 §b执行次数 §d平均耗时";
private final String mieprefix = " §6事件名称 §a总耗时 §b执行次数 §d平均耗时";
private final String mitprefix = " §6任务名称 §a总耗时 §b执行次数 §d平均耗时";
private final String milist = "§6- §e%-20s §a%-9.2f §b%-9s §d%-9.5f";
private final String mialist = "§6- §e%-20s §a%-9.2f §b%-9s";
private final String miwlist = "§6- §c%-20s §a%-9.2f §b%-9s §c%-9.5f";
private final String reinject = prefix + "§a能耗监控器重载完毕!";
private final String injected = prefix + "§a插件 §b%s §a成功注入能耗监控器!";
@ -93,18 +95,17 @@ public class MonitorCommand implements CommandExecutor {
}
}
}
sender.sendMessage(micprefix);
for (final Entry<String, Command> command : temp.entrySet()) {
final org.bukkit.command.CommandExecutor executor = Reflect.on(command.getValue()).get("executor");
if (executor instanceof CommandInjector) {
final CommandInjector injected = (CommandInjector) executor;
final StringBuffer str = new StringBuffer();
str.append("§6- §e" + command.getValue().getName() + " ");
str.append(String.format(total, injected.totalTime / um));
str.append(String.format(count, injected.count));
if (injected.count != 0) {
str.append(String.format(avg, injected.totalTime / um / injected.count));
final double avgTime = injected.totalTime / um / injected.count;
sender.sendMessage(String.format(avgTime < 10 ? milist : miwlist, command.getValue().getName(), injected.totalTime / um, injected.count, avgTime));
} else {
sender.sendMessage(String.format(mialist, command.getValue().getName(), injected.totalTime / um, injected.count));
}
e.getSender().sendMessage(str.toString());
}
}
}
@ -150,16 +151,10 @@ public class MonitorCommand implements CommandExecutor {
}
}
}
sender.sendMessage(mieprefix);
for (final String event : eventTotalTime.keySet()) {
final StringBuffer str = new StringBuffer();
str.append(String.format("§6- §e%-25s ", event));
str.append(String.format(total, eventTotalTime.get(event) / um));
str.append(String.format(count, eventCount.get(event)));
if (eventCount.get(event) != 0) {
final double avgTime = eventTotalTime.get(event) / um / eventCount.get(event);
str.append(String.format(avgTime < 10 ? avg : avg_warn, avgTime));
}
e.getSender().sendMessage(str.toString());
sender.sendMessage(String.format(avgTime < 10 ? milist : miwlist, event, eventTotalTime.get(event) / um, eventCount.get(event), avgTime));
}
}
@ -241,19 +236,18 @@ public class MonitorCommand implements CommandExecutor {
}
final List<BukkitTask> pendingTasks = Bukkit.getScheduler().getPendingTasks();
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的任务能耗如下!");
sender.sendMessage(mitprefix);
for (final BukkitTask pendingTask : pendingTasks) {
if (pendingTask.getOwner().getName().equalsIgnoreCase(pname)) {
final Runnable task = Reflect.on(pendingTask).get("task");
if (task instanceof TaskInjector) {
final TaskInjector executor = (TaskInjector) task;
final StringBuffer str = new StringBuffer();
str.append("§6- §e" + getClassName(executor.getOriginalTask().getClass()) + " ");
str.append(String.format(total, executor.totalTime / um));
str.append(String.format(count, executor.count));
if (executor.count != 0) {
str.append(String.format(avg, executor.totalTime / um / executor.count));
final double avgTime = executor.totalTime / um / executor.count;
sender.sendMessage(String.format(avgTime < 10 ? milist : miwlist, getClassName(executor.getOriginalTask().getClass()), executor.totalTime / um, executor.count, avgTime));
} else {
sender.sendMessage(String.format(mialist, getClassName(executor.getOriginalTask().getClass()), executor.totalTime / um, executor.count));
}
e.getSender().sendMessage(str.toString());
}
}
}

View File

@ -0,0 +1,97 @@
/**
* Created on 17 May 2014 by _MylesC
* Copyright 2014
*/
package pw.yumc.Yum.inject;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.UnknownDependencyException;
import org.bukkit.plugin.java.JavaPluginLoader;
public class YumPluginLoader implements PluginLoader {
private final JavaPluginLoader internal_loader;
private final Server server;
@SuppressWarnings("deprecation")
public YumPluginLoader(final Server instance) {
server = instance;
internal_loader = new JavaPluginLoader(instance);
}
public static void cleanJavaPluginLoaders() {
final YumPluginLoader yumPluginLoader = new YumPluginLoader(Bukkit.getServer());
final PluginManager spm = Bukkit.getPluginManager();
try {
final Field field = spm.getClass().getDeclaredField("fileAssociations");
field.setAccessible(true);
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
@SuppressWarnings("unchecked")
final Map<Pattern, PluginLoader> map = (Map<Pattern, PluginLoader>) field.get(spm);
final Iterator<Map.Entry<Pattern, PluginLoader>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
final Entry<Pattern, PluginLoader> entry = iter.next();
if (entry.getValue() instanceof JavaPluginLoader) {
entry.setValue(yumPluginLoader);
}
}
field.set(spm, map);
} catch (final Exception e) {
Bukkit.getServer().getLogger().log(Level.SEVERE, "Yum failed replacing the existing PluginLoader, contact the developers on YUMC!", e);
}
}
@Override
public Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(final Listener listener, final Plugin plugin) {
return internal_loader.createRegisteredListeners(listener, plugin);
}
@Override
public void disablePlugin(final Plugin plugin) {
if (!plugin.getName().equalsIgnoreCase("Yum")) {
internal_loader.disablePlugin(plugin);
}
}
@Override
public void enablePlugin(final Plugin arg0) {
internal_loader.enablePlugin(arg0);
}
@Override
public PluginDescriptionFile getPluginDescription(final File arg0) throws InvalidDescriptionException {
return internal_loader.getPluginDescription(arg0);
}
@Override
public Pattern[] getPluginFileFilters() {
return internal_loader.getPluginFileFilters();
}
@Override
public Plugin loadPlugin(final File arg0) throws InvalidPluginException, UnknownDependencyException {
return internal_loader.loadPlugin(arg0);
}
}

View File

@ -9,6 +9,7 @@ import org.bukkit.event.server.PluginEnableEvent;
import cn.citycraft.PluginHelper.bukkit.P;
import cn.citycraft.PluginHelper.kit.PluginKit;
import pw.yumc.Yum.api.YumAPI;
import pw.yumc.Yum.managers.ConfigManager;
import pw.yumc.Yum.managers.MonitorManager;
/**
@ -30,6 +31,9 @@ public class PluginListener implements Listener {
@EventHandler
public void onPluginEnable(final PluginEnableEvent e) {
if (ConfigManager.i().getMonitorIgnoreList().contains(e.getPlugin().getName())) {
return;
}
MonitorManager.reset(e.getPlugin().getName());
PluginKit.runTaskLater(new Runnable() {
@Override

View File

@ -16,6 +16,7 @@ import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.kit.LogKit;
import cn.citycraft.PluginHelper.kit.PluginKit;
import cn.citycraft.PluginHelper.sql.DataBase;
/**
*
@ -41,6 +42,8 @@ public class MonitorManager {
private final static Map<String, Long> event = new ConcurrentHashMap<>();
private final static Map<String, Long> cmd = new ConcurrentHashMap<>();
private final static DataBase db = null;
private final static double um = 1000000.00;
private final static LogKit mlog = new LogKit("monitor.log");