feat: 调整注册逻辑 修改分离配置类

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-06-27 16:56:04 +08:00
parent c4a9d9184a
commit 1be30bf249
8 changed files with 60 additions and 24 deletions

View File

@ -42,7 +42,7 @@ public class Yum extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
new YumAPI(this); new YumAPI();
initCommands(); initCommands();
initListeners(); initListeners();
initRunnable(); initRunnable();
@ -92,15 +92,21 @@ public class Yum extends JavaPlugin {
new PluginNetworkListener(this); new PluginNetworkListener(this);
PluginKit.scp("§a网络管理系统已启用..."); PluginKit.scp("§a网络管理系统已启用...");
} }
if (ConfigManager.i().isThreadSafe()) {
new ThreadSafetyListener(this); new ThreadSafetyListener(this);
PluginKit.scp("§a线程管理系统已启用...");
}
} }
/** /**
* 初始化任务 * 初始化任务
*/ */
private void initRunnable() { private void initRunnable() {
// 需要在主线程注册任务
if (ConfigManager.i().isMainThreadCheck() && Bukkit.isPrimaryThread()) {
final Timer task = new Timer(); final Timer task = new Timer();
PluginKit.scp("§a线程管理系统已启用..."); PluginKit.scp("§aIO管理系统已启用...");
task.scheduleAtFixedRate(new MainThreadCheckTask(Thread.currentThread()), 0, 3000); task.scheduleAtFixedRate(new MainThreadCheckTask(Thread.currentThread()), 0, 3000);
} }
} }
}

View File

@ -11,6 +11,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.CommonData.UpdatePlugin; import cn.citycraft.CommonData.UpdatePlugin;
import cn.citycraft.PluginHelper.kit.PKit;
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;
@ -360,8 +361,8 @@ public class YumAPI {
* @param plugin * @param plugin
* 插件实体 * 插件实体
*/ */
public YumAPI(final Plugin plugin) { public YumAPI() {
YumAPI.main = plugin; YumAPI.main = PKit.instance;
plugman = new PluginsManager(main); plugman = new PluginsManager(main);
download = new DownloadManager(main); download = new DownloadManager(main);
repo = new RepositoryManager(main); repo = new RepositoryManager(main);

View File

@ -14,16 +14,18 @@ public class PluginNetworkListener implements Listener {
public String prefix = "§6[§bYum §a网络管理§6] "; public String prefix = "§6[§bYum §a网络管理§6] ";
public String warnMain = "§c插件 §6%s §c尝试在主线程访问网络 §4可能会导致服务器卡顿或无响应!"; public String warnMain = "§c插件 §6%s §c尝试在主线程访问网络 §4可能会导致服务器卡顿或无响应!";
public String warn = "§c插件 §6%s §c尝试访问网络 §4请注意服务器网络安全!"; public String warn = "§c插件 §6%s §c尝试访问网络 §4请注意服务器网络安全!";
public String breaked = "§4已阻止黑名单插件 §b%s §4访问网络!"; public String breaked = "§c已阻止插件 §6%s §c访问网络!";
public String url = "§4地址: %s"; public String url = "§c地址: %s";
public PluginNetworkListener(final Yum yum) { public PluginNetworkListener(final Yum yum) {
Bukkit.getPluginManager().registerEvents(this, yum); Bukkit.getPluginManager().registerEvents(this, yum);
} }
public void breakNetwork(final PluginNetworkEvent e) { public void breakNetwork(final PluginNetworkEvent e) {
if (ConfigManager.i().isNetworkShowInfo()) {
PluginKit.sc(String.format(prefix + breaked, e.getPlugin().getName())); PluginKit.sc(String.format(prefix + breaked, e.getPlugin().getName()));
PluginKit.sc(String.format(url, e.getUrl().toString())); PluginKit.sc(String.format(prefix + url, e.getUrl().toString()));
}
e.setCancelled(true); e.setCancelled(true);
} }
@ -39,7 +41,6 @@ public class PluginNetworkListener implements Listener {
大神你好.isEmpty(); 大神你好.isEmpty();
return; return;
} }
final boolean isPrimaryThread = e.isPrimaryThread();
if (plugin != null) { if (plugin != null) {
if (ConfigManager.i().getNetworkBlackList().contains(plugin.getName())) { if (ConfigManager.i().getNetworkBlackList().contains(plugin.getName())) {
breakNetwork(e); breakNetwork(e);
@ -48,10 +49,15 @@ public class PluginNetworkListener implements Listener {
if (ConfigManager.i().getNetworkIgnoreList().contains(plugin.getName())) { if (ConfigManager.i().getNetworkIgnoreList().contains(plugin.getName())) {
return; return;
} }
PluginKit.sc(String.format(prefix + (isPrimaryThread ? warnMain : warn), plugin.getName(), urlinfo)); if (e.isPrimaryThread()) {
if (!ConfigManager.i().isAllowPrimaryThread() && isPrimaryThread) { PluginKit.sc(String.format(prefix + warnMain, plugin.getName()));
if (!ConfigManager.i().isAllowPrimaryThread()) {
breakNetwork(e); breakNetwork(e);
} }
} else {
PluginKit.sc(String.format(prefix + warn, plugin.getName()));
PluginKit.sc(String.format(prefix + url, urlinfo));
}
} }
} }

View File

@ -73,12 +73,10 @@ public class ThreadSafetyListener implements Listener {
checkSafety(teleportEvent); checkSafety(teleportEvent);
} }
@EventHandler
public void onPluginDisable(final PluginDisableEvent pluginDisableEvent) { public void onPluginDisable(final PluginDisableEvent pluginDisableEvent) {
checkSafety(pluginDisableEvent); checkSafety(pluginDisableEvent);
} }
@EventHandler
public void onPluginEnable(final PluginEnableEvent pluginEnableEvent) { public void onPluginEnable(final PluginEnableEvent pluginEnableEvent) {
checkSafety(pluginEnableEvent); checkSafety(pluginEnableEvent);
} }
@ -88,7 +86,6 @@ public class ThreadSafetyListener implements Listener {
checkSafety(worldLoadEvent); checkSafety(worldLoadEvent);
} }
@EventHandler
public void onWorldSave(final WorldSaveEvent worldSaveEvent) { public void onWorldSave(final WorldSaveEvent worldSaveEvent) {
checkSafety(worldSaveEvent); checkSafety(worldSaveEvent);
} }

View File

@ -17,6 +17,7 @@ public class ConfigManager {
public final FileConfig config; public final FileConfig config;
public final FileConfig setop; public final FileConfig setop;
public final FileConfig network; public final FileConfig network;
public final FileConfig thread;
public static ConfigManager i() { public static ConfigManager i() {
return i; return i;
@ -26,6 +27,7 @@ public class ConfigManager {
config = new FileConfig(plugin); config = new FileConfig(plugin);
setop = new FileConfig(plugin, "setop.yml"); setop = new FileConfig(plugin, "setop.yml");
network = new FileConfig(plugin, "network.yml"); network = new FileConfig(plugin, "network.yml");
thread = new FileConfig(plugin, "thread.yml");
} }
public List<String> getBlackList() { public List<String> getBlackList() {
@ -56,6 +58,10 @@ public class ConfigManager {
return network.getBoolean("AllowPrimaryThread", false); return network.getBoolean("AllowPrimaryThread", false);
} }
public boolean isMainThreadCheck() {
return thread.getBoolean("MainThreadCheck", true);
}
public boolean isNetworkDebug() { public boolean isNetworkDebug() {
return network.getBoolean("NetworkDebug", false); return network.getBoolean("NetworkDebug", false);
} }
@ -64,12 +70,21 @@ public class ConfigManager {
return network.getBoolean(ENABLE, true); return network.getBoolean(ENABLE, true);
} }
public boolean isNetworkShowInfo() {
return network.getBoolean("ShowInfo", true);
}
public boolean isSetOpEnable() { public boolean isSetOpEnable() {
return setop.getBoolean(ENABLE, true); return setop.getBoolean(ENABLE, true);
} }
public boolean isThreadSafe() {
return thread.getBoolean("ThreadSafe", true);
}
public void reload() { public void reload() {
setop.reload(); setop.reload();
network.reload(); network.reload();
thread.reload();
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.logging.Level;
import cn.citycraft.PluginHelper.kit.PKit; import cn.citycraft.PluginHelper.kit.PKit;
/** /**
* 线程安全检查任务
* *
* @since 2016年6月22日 下午4:57:32 * @since 2016年6月22日 下午4:57:32
* @author * @author

View File

@ -1,8 +1,14 @@
#配置版本号 请勿修改!!! #配置版本号 请勿修改!!!
Version: 1.0 Version: 1.1
#是否开启 #是否开启
Enable: true Enable: true
#是否显示详细信息
ShowInfo: true
#网络调试模式
NetworkDebug: false
#是否允许插件主线程访问网络
AllowPrimaryThread: false
#黑名单列表 #黑名单列表
Black: Black:
- BukkitInjectedTools - BukkitInjectedTools
@ -10,6 +16,3 @@ Black:
#忽略检测列表 #忽略检测列表
Ignore: Ignore:
- Essentials - Essentials
#URL白名单网址
WhiteKeyList:
- yum

View File

@ -0,0 +1,7 @@
#配置版本号 请勿修改!!!
Version: 1.0
#是否开启线程安全检测
ThreadSafe: true
#是否开启主线程IO检测
MainThreadCheck: true