修复配置文件加载和保存失败

This commit is contained in:
Izzel_Aliz 2018-04-08 22:42:31 +08:00
parent 7758ac2241
commit 33905330f3
4 changed files with 55 additions and 56 deletions

View File

@ -22,7 +22,7 @@ public class TLib {
@Logger("§8[§3§lTabooLib§8][§r{1}§8] §f{2}") @Logger("§8[§3§lTabooLib§8][§r{1}§8] §f{2}")
private TLogger tLogger; private TLogger tLogger;
private TLibConfig config = new TLibConfig(); private TLibConfig config;
private TConfigWatcher configWatcher = new TConfigWatcher(); private TConfigWatcher configWatcher = new TConfigWatcher();

View File

@ -41,33 +41,31 @@ public class TDependency {
TDependencyLoader.addToPath(Main.getInst(), file); TDependencyLoader.addToPath(Main.getInst(), file);
return true; return true;
} else { } else {
boolean downloadFinish = false; if (downloadMaven(repo, arr[0], arr[1], arr[2], file, url)) {
try { TDependencyLoader.addToPath(Main.getInst(), file);
downloadFinish = downloadMaven(repo, arr[0], arr[1], arr[2], file, url); return true;
} catch (Exception ignored) { } else
ignored.printStackTrace(); return false;
}
if (downloadFinish) {
TDependencyLoader.addToPath(Main.getInst(), file);
}
return downloadFinish;
} }
} }
return false; return false;
} }
private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) { private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) {
if (Main.getInst().getConfig().getBoolean("OFFLINE-MODE")) { if (Main.getInst().getConfig().getBoolean("OFFLINE-MODE")) {
TLib.getTLib().getLogger().warn("已启用离线模式, 将不会下载第三方依赖库"); TLib.getTLib().getLogger().warn("已启用离线模式, 将不会下载第三方依赖库");
return false; return false;
} }
ReentrantLock lock = new ReentrantLock(); ReentrantLock lock = new ReentrantLock();
AtomicBoolean failed = new AtomicBoolean(false); AtomicBoolean failed = new AtomicBoolean(false);
String link = dl.length() == 0 ? url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar" : dl;
EagletTask task = new EagletTask() EagletTask task = new EagletTask()
.url(dl.length() == 0 ? url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar" : dl) .url(link)
.file(target) .file(target)
.setThreads(TLib.getTLib().getConfig().getDownloadPoolSize()) .setThreads(TLib.getTLib().getConfig().getDownloadPoolSize())
.setOnStart(event -> lock.lock()) .setOnStart(event -> lock.lock())
.setOnError(event -> {
})
.setOnConnected(event -> TLib.getTLib().getLogger().info(" 正在下载 " + String.join(":", .setOnConnected(event -> TLib.getTLib().getLogger().info(" 正在下载 " + String.join(":",
new String[]{groupId, artifactId, version}) + new String[]{groupId, artifactId, version}) +
" 大小 " + ProgressEvent.format(event.getContentLength()))) " 大小 " + ProgressEvent.format(event.getContentLength())))
@ -78,6 +76,8 @@ public class TDependency {
TLib.getTLib().getLogger().info(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 完成"); TLib.getTLib().getLogger().info(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 完成");
} else { } else {
failed.set(true); failed.set(true);
TLib.getTLib().getLogger().error(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 失败");
TLib.getTLib().getLogger().error(" 请手动下载 " + link + " 并重命名为 " + target.getName() + " 后放在 /TabooLib/libs 文件夹内");
} }
lock.unlock(); lock.unlock();
}); });

View File

@ -14,22 +14,22 @@ import java.lang.reflect.Field;
public class DependencyInjector { public class DependencyInjector {
public static void inject(Plugin plugin, Object o) { public static void inject(Plugin plugin, Object o) {
try { try {
injectConfig(plugin, o); injectLogger(plugin, o);
} catch (NoClassDefFoundError ignored) { } catch (NoClassDefFoundError ignored) {
} }
try { try {
injectLogger(plugin, o); injectConfig(plugin, o);
} catch (NoClassDefFoundError ignored) { } catch (NoClassDefFoundError ignored) {
} }
try { try {
injectPluginInstance(plugin, o); injectPluginInstance(plugin, o);
} catch (NoClassDefFoundError ignored) { } catch (NoClassDefFoundError ignored) {
} }
try { try {
injectDependencies(plugin, o); injectDependencies(plugin, o);
} catch (NoClassDefFoundError ignored) { } catch (NoClassDefFoundError ignored) {
} }
} }
static void injectOnEnable(Plugin plugin) { static void injectOnEnable(Plugin plugin) {
@ -37,17 +37,14 @@ public class DependencyInjector {
} }
static void onDisable(Plugin plugin) { static void onDisable(Plugin plugin) {
try { eject(plugin, plugin);
ejectConfig(plugin, plugin);
} catch (NoClassDefFoundError ignored) {
}
} }
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 (NoClassDefFoundError ignored) {
} }
} }
private static void ejectConfig(Plugin plugin, Object o) { private static void ejectConfig(Plugin plugin, Object o) {
@ -56,10 +53,11 @@ public class DependencyInjector {
if ((config = field.getType().getAnnotation(Config.class)) != null) { if ((config = field.getType().getAnnotation(Config.class)) != null) {
try { try {
field.setAccessible(true); field.setAccessible(true);
TConfigInjector.saveConfig(plugin, o); TConfigInjector.saveConfig(plugin, field.get(o));
TLib.getTLib().getLogger().info("插件 " + plugin + " 的配置 " + config.name() + " 已保存"); TLib.getTLib().getLogger().info("插件 " + plugin + " 的配置 " + config.name() + " 已保存");
} catch (Exception e) { } catch (Exception e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置 " + config.name() + " 保存失败"); TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置 " + config.name() + " 保存失败");
e.printStackTrace();
} }
} }
} }
@ -102,9 +100,9 @@ public class DependencyInjector {
} }
private static void injectLogger(Plugin plugin, Object o) { private static void injectLogger(Plugin plugin, Object o) {
for (Field field : o.getClass().getDeclaredFields()) { for (Field field : o.getClass().getDeclaredFields()) {
try { try {
Logger logger; Logger logger;
if ((logger = field.getAnnotation(Logger.class)) != null) { if ((logger = field.getAnnotation(Logger.class)) != null) {
field.getType().asSubclass(TLogger.class); field.getType().asSubclass(TLogger.class);
TLogger tLogger = new TLogger(logger.value(), plugin, logger.level()); TLogger tLogger = new TLogger(logger.value(), plugin, logger.level());
@ -112,15 +110,15 @@ public class DependencyInjector {
field.setAccessible(true); field.setAccessible(true);
field.set(o, tLogger); field.set(o, tLogger);
} }
} catch (Exception ignored2) { } catch (Exception ignored2) {
} }
} }
} }
private static void injectPluginInstance(Plugin plugin, Object o) { private static void injectPluginInstance(Plugin plugin, Object o) {
for (Field field : o.getClass().getDeclaredFields()) { for (Field field : o.getClass().getDeclaredFields()) {
try { try {
PluginInstance instance; PluginInstance instance;
if ((instance = field.getAnnotation(PluginInstance.class)) != null) { if ((instance = field.getAnnotation(PluginInstance.class)) != null) {
if (!field.isAccessible()) if (!field.isAccessible())
field.setAccessible(true); field.setAccessible(true);
@ -137,20 +135,21 @@ public class DependencyInjector {
if (pl != null) if (pl != null)
field.set(o, pl); field.set(o, pl);
} }
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
} }
private static void injectDependencies(Plugin plugin, Object o) { private static void injectDependencies(Plugin plugin, Object o) {
Dependency[] dependencies = new Dependency[0]; { Dependency[] dependencies = new Dependency[0];
{
Dependencies d = o.getClass().getAnnotation(Dependencies.class); Dependencies d = o.getClass().getAnnotation(Dependencies.class);
if (d != null) { if (d != null) {
dependencies = d.value(); dependencies = d.value();
} }
Dependency d2 = o.getClass().getAnnotation(Dependency.class); Dependency d2 = o.getClass().getAnnotation(Dependency.class);
if (d2 != null) { if (d2 != null) {
dependencies = new Dependency[]{d2}; dependencies = new Dependency[]{d2};
} }
} }
if (dependencies.length != 0) { if (dependencies.length != 0) {

View File

@ -31,7 +31,7 @@ public class TConfigInjector {
Validate.notNull(config); Validate.notNull(config);
File file = new File(plugin.getDataFolder(), config.name()); File file = new File(plugin.getDataFolder(), config.name());
if (!file.exists()) if (config.fromJar()) plugin.saveResource(config.name(), true); if (!file.exists()) if (config.fromJar()) plugin.saveResource(config.name(), true);
else saveConfig(plugin, clazz); else saveConfig(plugin, clazz.newInstance());
return unserialize(plugin, clazz); return unserialize(plugin, clazz);
} catch (NullPointerException e) { } catch (NullPointerException e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败:没有 @Config 注解"); TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败:没有 @Config 注解");