mirror of
https://e.coding.net/circlecloud/Yum.git
synced 2024-12-23 05:08:47 +00:00
卸载插件是还原网络代理...
This commit is contained in:
parent
4de1213c3e
commit
e3126b62ed
@ -12,6 +12,7 @@ import cn.citycraft.PluginHelper.utils.VersionChecker;
|
||||
import pw.yumc.Yum.api.YumAPI;
|
||||
import pw.yumc.Yum.commands.FileCommand;
|
||||
import pw.yumc.Yum.commands.YumCommand;
|
||||
import pw.yumc.Yum.manager.NetworkManager;
|
||||
|
||||
/**
|
||||
* MC插件仓库
|
||||
@ -21,6 +22,12 @@ import pw.yumc.Yum.commands.YumCommand;
|
||||
*/
|
||||
public class Yum extends JavaPlugin {
|
||||
public FileConfig config;
|
||||
NetworkManager netmgr;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
netmgr.unregister();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@ -28,7 +35,8 @@ public class Yum extends JavaPlugin {
|
||||
new YumCommand(this);
|
||||
new FileCommand(this);
|
||||
new VersionChecker(this);
|
||||
// new NetworkManager(this).setDebug(true).register();
|
||||
netmgr = new NetworkManager(this);
|
||||
netmgr.setDebug(true).register();
|
||||
YumAPI.updaterepo(Bukkit.getConsoleSender());
|
||||
YumAPI.updatecheck(Bukkit.getConsoleSender());
|
||||
}
|
||||
|
@ -22,8 +22,6 @@ import pw.yumc.Yum.Yum;
|
||||
public class NetworkManager {
|
||||
private static boolean debug;
|
||||
|
||||
private static HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
|
||||
|
||||
private final Yum main;
|
||||
|
||||
public NetworkManager(final Yum plugin) {
|
||||
@ -31,6 +29,7 @@ public class NetworkManager {
|
||||
}
|
||||
|
||||
public void register() {
|
||||
main.getLogger().info("注入网络代理 将托管服务器网络!");
|
||||
ProxySelector.setDefault(new YumProxySelector(ProxySelector.getDefault()));
|
||||
}
|
||||
|
||||
@ -42,12 +41,14 @@ public class NetworkManager {
|
||||
public void unregister() {
|
||||
final ProxySelector cur = ProxySelector.getDefault();
|
||||
if (cur instanceof YumProxySelector) {
|
||||
main.getLogger().info("恢复网络代理 使用默认网络!");
|
||||
ProxySelector.setDefault(((YumProxySelector) cur).getDefaultSelector());
|
||||
}
|
||||
}
|
||||
|
||||
class YumProxySelector extends ProxySelector {
|
||||
private final ProxySelector defaultSelector;
|
||||
private final HashMap<ClassLoader, Plugin> pluginMap = new HashMap<>();
|
||||
|
||||
public YumProxySelector(final ProxySelector defaultSelector) {
|
||||
this.defaultSelector = defaultSelector;
|
||||
@ -55,39 +56,44 @@ public class NetworkManager {
|
||||
|
||||
@Override
|
||||
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
|
||||
this.defaultSelector.connectFailed(uri, sa, ioe);
|
||||
defaultSelector.connectFailed(uri, sa, ioe);
|
||||
}
|
||||
|
||||
public ProxySelector getDefaultSelector() {
|
||||
return this.defaultSelector;
|
||||
return defaultSelector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Proxy> select(final URI uri) {
|
||||
if (NetworkManager.debug || Bukkit.isPrimaryThread()) {
|
||||
final Plugin plugin = this.getRequestingPlugin();
|
||||
final String urlinfo = uri.getHost() + ":" + uri.getPort() + "/" + uri.getPath();
|
||||
final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!";
|
||||
if (plugin == null) {
|
||||
main.getLogger().warning(String.format(str, "未知(请查看堆栈)", urlinfo));
|
||||
Thread.dumpStack();
|
||||
} else if (!plugin.getName().equalsIgnoreCase("Yum")) {
|
||||
main.getLogger().warning(String.format(str, plugin.getName(), urlinfo));
|
||||
if (debug || Bukkit.isPrimaryThread()) {
|
||||
try {
|
||||
final Plugin plugin = this.getRequestingPlugin();
|
||||
final String urlinfo = uri.getHost() + ":" + uri.getPort() + "/" + uri.getPath();
|
||||
final String str = debug ? "[NetDebug] 插件 %s 尝试访问 %s 请注意服务器网络安全!" : "[NetManager] 插件 %s 尝试在主线程访问 %s 可能会导致服务器卡顿或无响应!";
|
||||
if (plugin == null) {
|
||||
main.getLogger().warning(String.format(str, "未知(请查看堆栈)", urlinfo));
|
||||
Thread.dumpStack();
|
||||
} else if (!plugin.getName().equalsIgnoreCase("Yum")) {
|
||||
main.getLogger().warning(String.format(str, plugin.getName(), urlinfo));
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return this.defaultSelector.select(uri);
|
||||
return defaultSelector.select(uri);
|
||||
}
|
||||
|
||||
private void collectPlugin() {
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
pluginMap.put(plugin.getClass().getClassLoader(), plugin);
|
||||
if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) {
|
||||
pluginMap.clear();
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
pluginMap.put(plugin.getClass().getClassLoader(), plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Plugin getRequestingPlugin() {
|
||||
if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) {
|
||||
collectPlugin();
|
||||
}
|
||||
collectPlugin();
|
||||
final StackTraceElement[] stacktrace = new Exception().getStackTrace();
|
||||
for (final StackTraceElement element : stacktrace) {
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user