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

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,16 +41,11 @@ 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 {
downloadFinish = downloadMaven(repo, arr[0], arr[1], arr[2], file, url);
} catch (Exception ignored) {
ignored.printStackTrace();
}
if (downloadFinish) {
TDependencyLoader.addToPath(Main.getInst(), file); TDependencyLoader.addToPath(Main.getInst(), file);
} return true;
return downloadFinish; } else
return false;
} }
} }
return false; return false;
@ -63,11 +58,14 @@ public class TDependency {
} }
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

@ -15,11 +15,11 @@ 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 {
@ -37,10 +37,7 @@ 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) {
@ -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();
} }
} }
} }
@ -143,7 +141,8 @@ public class DependencyInjector {
} }
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();

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 注解");