更改依赖加载为 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; return tLib;
} }
public static void init() { public static void injectPluginManager() {
new File(Main.getInst().getDataFolder(), "/libs").mkdirs();
tLib = new TLib();
DependencyInjector.inject(Main.getInst(), tLib);
// 注入 PluginLoader 用于加载依赖 // 注入 PluginLoader 用于加载依赖
try { try {
Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager"); Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager");
field.setAccessible(true); field.setAccessible(true);
field.set(Bukkit.getServer(), new TLibPluginManager()); field.set(Bukkit.getServer(), new TLibPluginManager());
tLib.getLogger().info("注入成功");
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace(); 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() { public static void unload() {
tLib.getConfigWatcher().unregisterAll(); tLib.getConfigWatcher().unregisterAll();
DependencyInjector.eject(Main.getInst(), tLib); DependencyInjector.eject(Main.getInst(), tLib);

View File

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

View File

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

View File

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