更改依赖加载为 onLoad 前加载

//TODO 在插件启动之前加载依赖
This commit is contained in:
Izzel_Aliz 2018-04-13 13:38:54 +08:00
parent 4e7fc42167
commit b941cac63f
5 changed files with 266 additions and 250 deletions

6
.idea/encodings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

View File

@ -45,22 +45,27 @@ public class TLib {
return tLib;
}
public static void init() {
new File(Main.getInst().getDataFolder(), "/libs").mkdirs();
tLib = new TLib();
DependencyInjector.inject(Main.getInst(), tLib);
public static void injectPluginManager() {
// 注入 PluginLoader 用于加载依赖
try {
Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager");
field.setAccessible(true);
field.set(Bukkit.getServer(), new TLibPluginManager());
tLib.getLogger().info("注入成功");
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
tLib.getLogger().fatal("注入失败");
}
}
public static void init() {
new File(Main.getInst().getDataFolder(), "/libs").mkdirs();
tLib = new TLib();
DependencyInjector.inject(Main.getInst(), tLib);
if (Bukkit.getPluginManager() instanceof TLibPluginManager)
tLib.getLogger().info("注入成功");
else
tLib.getLogger().fatal("注入失败");
}
public static void unload() {
tLib.getConfigWatcher().unregisterAll();
DependencyInjector.eject(Main.getInst(), tLib);

View File

@ -43,7 +43,7 @@ public class DependencyInjector {
public static void eject(Plugin plugin, Object o) {
try {
ejectConfig(plugin, o);
} catch (NoClassDefFoundError ignored) {
} catch (Throwable ignored) {
}
}

View File

@ -49,7 +49,9 @@ public class TLibPluginManager implements PluginManager {
@Override
public Plugin loadPlugin(File file) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException {
return instance.loadPlugin(file);
Plugin plugin = instance.loadPlugin(file);
DependencyInjector.injectOnEnable(plugin);
return plugin;
}
@Override
@ -92,7 +94,6 @@ public class TLibPluginManager implements PluginManager {
@Override
public void enablePlugin(Plugin plugin) {
DependencyInjector.injectOnEnable(plugin);
instance.enablePlugin(plugin);
}

View File

@ -1,7 +1,6 @@
package me.skymc.taboolib;
import com.ilummc.tlib.TLib;
import com.ilummc.tlib.annotations.Dependency;
import lombok.Getter;
import lombok.Setter;
import me.skymc.taboolib.anvil.AnvilContainerAPI;
@ -52,6 +51,21 @@ import java.util.Random;
@SuppressWarnings("deprecation")
public class Main extends JavaPlugin implements Listener {
public Main() {
super();
inst = this;
disable = false;
TLib.injectPluginManager();
// 载入配置
saveDefaultConfig();
// 加载依赖
TLib.init();
}
@Getter
private static Plugin inst;
@Getter
@ -102,13 +116,6 @@ public class Main extends JavaPlugin implements Listener {
@Override
public void onLoad() {
inst = this; disable = false;
// 载入配置
saveDefaultConfig();
// 加载依赖
TLib.init();
// 载入目录
setupDataFolder();
// 注册配置
@ -128,8 +135,7 @@ public class Main extends JavaPlugin implements Listener {
// 如果没有数据
if (!connection.isExists(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID())) {
connection.intoValue(getTablePrefix() + "_serveruuid", TabooLib.getServerUID(), StringUtils.hashKeyForDisk(getDataFolder().getPath()));
}
else {
} else {
String hash = connection.getValue(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID(), "hash").toString();
// 如果这个值和我的值不同
if (!hash.equals(StringUtils.hashKeyForDisk(getDataFolder().getPath()))) {
@ -140,8 +146,7 @@ public class Main extends JavaPlugin implements Listener {
Bukkit.shutdown();
}
}
}
else {
} else {
// 提示
MsgUtils.warn("数据库连接失败, 请检查配置是否正确!");
// 关服
@ -149,8 +154,7 @@ public class Main extends JavaPlugin implements Listener {
}
// 储存方式
storageType = StorageType.SQL;
}
else {
} else {
// 储存方式
storageType = StorageType.LOCAL;
}