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

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}")
private TLogger tLogger;
private TLibConfig config = new TLibConfig();
private TLibConfig config;
private TConfigWatcher configWatcher = new TConfigWatcher();

View File

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

View File

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

View File

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