From 33905330f3c717dc53b5ec4cda36c8e9427cce07 Mon Sep 17 00:00:00 2001 From: Izzel_Aliz Date: Sun, 8 Apr 2018 22:42:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=A0=E8=BD=BD=E5=92=8C=E4=BF=9D=E5=AD=98=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ilummc/tlib/TLib.java | 2 +- .../ilummc/tlib/dependency/TDependency.java | 30 ++++---- .../tlib/inject/DependencyInjector.java | 77 +++++++++---------- .../ilummc/tlib/inject/TConfigInjector.java | 2 +- 4 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java index f16f01c..c7d2216 100644 --- a/src/main/java/com/ilummc/tlib/TLib.java +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -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(); diff --git a/src/main/java/com/ilummc/tlib/dependency/TDependency.java b/src/main/java/com/ilummc/tlib/dependency/TDependency.java index 3987e25..8ce20c5 100644 --- a/src/main/java/com/ilummc/tlib/dependency/TDependency.java +++ b/src/main/java/com/ilummc/tlib/dependency/TDependency.java @@ -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(); }); diff --git a/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java b/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java index bcf97e8..adb3499 100644 --- a/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java +++ b/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java @@ -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) { diff --git a/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java b/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java index 4d1c0e0..6d2e4bf 100644 --- a/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java +++ b/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java @@ -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 注解");