From 96269db54df5e6c19323a50983572d56106fefa9 Mon Sep 17 00:00:00 2001 From: Izzel_Aliz Date: Thu, 5 Apr 2018 02:53:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=BA=93=E4=BB=A5=E5=8F=8A=20TLib=20?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...n__com_ilummc_eagletdl_EagletCore_1_1.xml} | 8 +-- .../Maven__com_zaxxer_HikariCP_3_0_0.xml | 13 +++++ .../Maven__org_slf4j_slf4j_api_1_7_25.xml | 13 +++++ pom.xml | 7 ++- src/main/java/com/ilummc/tlib/TLib.java | 31 ++++++++++-- .../com/ilummc/tlib/annotations/Config.java | 2 +- .../ilummc/tlib/annotations/Dependency.java | 10 +++- .../com/ilummc/tlib/annotations/Logger.java | 2 +- .../ilummc/tlib/dependency/TDependency.java | 49 ++++++++++--------- .../tlib/inject/DependencyInjector.java | 27 ++++++---- .../ilummc/tlib/inject/TConfigInjector.java | 6 +++ .../java/com/ilummc/tlib/util/TLogger.java | 14 +++--- 12 files changed, 130 insertions(+), 52 deletions(-) rename .idea/libraries/{Maven__com_ilummc_eagletdl_EagletCore_1_0.xml => Maven__com_ilummc_eagletdl_EagletCore_1_1.xml} (63%) create mode 100644 .idea/libraries/Maven__com_zaxxer_HikariCP_3_0_0.xml create mode 100644 .idea/libraries/Maven__org_slf4j_slf4j_api_1_7_25.xml create mode 100644 src/main/java/com/ilummc/tlib/inject/TConfigInjector.java diff --git a/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml b/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_1.xml similarity index 63% rename from .idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml rename to .idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_1.xml index edabcdb..ab97cb6 100644 --- a/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml +++ b/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_1.xml @@ -1,13 +1,13 @@ - + - + - + - + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_zaxxer_HikariCP_3_0_0.xml b/.idea/libraries/Maven__com_zaxxer_HikariCP_3_0_0.xml new file mode 100644 index 0000000..f69ad99 --- /dev/null +++ b/.idea/libraries/Maven__com_zaxxer_HikariCP_3_0_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_25.xml b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_25.xml new file mode 100644 index 0000000..20e8163 --- /dev/null +++ b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_25.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 02279a1..a92e266 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,12 @@ com.ilummc.eagletdl EagletCore - 1.0 + 1.1 + + + com.zaxxer + HikariCP + 3.0.0 org.ow2.asm diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java index cd20cc0..e193b09 100644 --- a/src/main/java/com/ilummc/tlib/TLib.java +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -1,28 +1,51 @@ package com.ilummc.tlib; +import com.ilummc.tlib.annotations.Dependency; +import com.ilummc.tlib.annotations.Logger; +import com.ilummc.tlib.inject.DependencyInjector; import com.ilummc.tlib.inject.TLibPluginManager; +import com.ilummc.tlib.util.TLogger; import me.skymc.taboolib.Main; -import me.skymc.taboolib.message.MsgUtils; import org.bukkit.Bukkit; import java.io.File; import java.lang.reflect.Field; +@Dependency(type = Dependency.Type.LIBRARY, maven = "org.ow2.asm:asm:6.1.1") +@Dependency(type = Dependency.Type.LIBRARY, maven = "com.zaxxer:HikariCP:3.0.0") public class TLib { + private static TLib tLib; + + @Logger("§3[§6TLib§3|{1}§3] §f{2}") + private TLogger tLogger; + + private TLib() { + } + + public TLogger getLogger() { + return tLogger; + } + + public static TLib getTLib() { + return tLib; + } + @SuppressWarnings({"unchecked"}) public static void init() { + new File(Main.getInst().getDataFolder(), "/libs").mkdirs(); + tLib = new TLib(); + DependencyInjector.inject(Main.getInst(), tLib); // 注入 PluginLoader 用于加载依赖 try { Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager"); field.setAccessible(true); field.set(Bukkit.getServer(), new TLibPluginManager()); - MsgUtils.send("注入成功"); + tLib.getLogger().info("注入成功"); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); - MsgUtils.warn("注入失败"); + tLib.getLogger().fatal("注入失败"); } - new File(Main.getInst().getDataFolder(), "/libs").mkdirs(); } } diff --git a/src/main/java/com/ilummc/tlib/annotations/Config.java b/src/main/java/com/ilummc/tlib/annotations/Config.java index 1e4548a..ca16a4f 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Config.java +++ b/src/main/java/com/ilummc/tlib/annotations/Config.java @@ -11,7 +11,7 @@ public @interface Config { String name() default "config.yml"; - boolean fromJar() default true; + boolean fromJar() default false; boolean saveOnExit() default false; diff --git a/src/main/java/com/ilummc/tlib/annotations/Dependency.java b/src/main/java/com/ilummc/tlib/annotations/Dependency.java index bde26ea..9c706c9 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Dependency.java +++ b/src/main/java/com/ilummc/tlib/annotations/Dependency.java @@ -1,5 +1,7 @@ package com.ilummc.tlib.annotations; +import com.ilummc.tlib.dependency.TDependency; + import java.lang.annotation.*; @Target(ElementType.TYPE) @@ -11,6 +13,12 @@ public @interface Dependency { Type type(); - String[] args(); + String plugin() default ""; + + String maven() default ""; + + String mavenRepo() default TDependency.MAVEN_REPO; + + String url() default ""; } diff --git a/src/main/java/com/ilummc/tlib/annotations/Logger.java b/src/main/java/com/ilummc/tlib/annotations/Logger.java index 2e504b3..bec8bc5 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Logger.java +++ b/src/main/java/com/ilummc/tlib/annotations/Logger.java @@ -11,7 +11,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) public @interface Logger { - String value() default "[{0}] {1}"; + String value() default "[{0}|{1}] {1}"; int level() default TLogger.INFO; diff --git a/src/main/java/com/ilummc/tlib/dependency/TDependency.java b/src/main/java/com/ilummc/tlib/dependency/TDependency.java index 421676d..5488a45 100644 --- a/src/main/java/com/ilummc/tlib/dependency/TDependency.java +++ b/src/main/java/com/ilummc/tlib/dependency/TDependency.java @@ -1,8 +1,9 @@ package com.ilummc.tlib.dependency; import com.ilummc.eagletdl.EagletTask; +import com.ilummc.eagletdl.ProgressEvent; +import com.ilummc.tlib.TLib; import me.skymc.taboolib.Main; -import me.skymc.taboolib.message.MsgUtils; import java.io.File; import java.util.concurrent.atomic.AtomicBoolean; @@ -29,17 +30,17 @@ public class TDependency { *

* 阻塞线程进行下载/加载 * - * @param args 依赖名,格式为 groupId:artifactId:version + * @param type 依赖名,格式为 groupId:artifactId:version * @return 是否成功加载库,如果加载成功,插件将可以任意调用使用的类 */ - public static boolean requestLib(String... args) { - if (args[0].matches(".*:.*:.*")) { - String[] arr = args[0].split(":"); + public static boolean requestLib(String type, String repo, String url) { + if (type.matches(".*:.*:.*")) { + String[] arr = type.split(":"); File file = new File(Main.getInst().getDataFolder(), "/libs/" + String.join("-", arr) + ".jar"); if (file.exists()) { TDependencyLoader.addToPath(Main.getInst(), file); return true; - } else if (downloadMaven(MAVEN_REPO, arr[0], arr[1], arr[2], file)) { + } else if (downloadMaven(repo, arr[0], arr[1], arr[2], file, url)) { TDependencyLoader.addToPath(Main.getInst(), file); return true; } else return false; @@ -47,30 +48,32 @@ public class TDependency { return false; } - private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target) { + private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) { ReentrantLock lock = new ReentrantLock(); AtomicBoolean failed = new AtomicBoolean(false); - new EagletTask() - .url(url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar") + EagletTask task = new EagletTask() + .url(dl == null ? url + "/" + groupId.replace('.', '/') + "/" + + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar" : dl) .file(target) .setThreads(8) .setOnStart(event -> lock.lock()) - .setOnProgress(event -> MsgUtils.send(" 下载速度 " + event.getSpeedFormatted())) - .setOnConnected(event -> MsgUtils.send(" 正在下载 " + String.join(":", new String[]{groupId, artifactId, version}) + - " 大小 " + event.getContentLength())) - .setOnError(event -> failed.set(true)) + .setOnConnected(event -> TLib.getTLib().getLogger().info(" 正在下载 " + String.join(":", + new String[]{groupId, artifactId, version}) + + " 大小 " + ProgressEvent.format(event.getContentLength()))) + .setOnProgress(event -> TLib.getTLib().getLogger().info(" 下载速度 " + event.getSpeedFormatted() + + " 进度 " + event.getPercentageFormatted())) .setOnComplete(event -> { + if (event.isSuccess()) { + TLib.getTLib().getLogger().info(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 完成"); + } else { + failed.set(true); + } lock.unlock(); - MsgUtils.send(" 下载 " + String.join(":", new String[]{groupId, artifactId, version}) + " 完成"); - }) - .start(); - try { - while (lock.tryLock()) lock.unlock(); - } catch (Exception ignored) { - } finally { - lock.lock(); - lock.unlock(); - } + }); + task.start(); + while (lock.tryLock()) lock.unlock(); + lock.lock(); + lock.unlock(); return !failed.get(); } diff --git a/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java b/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java index 6e1ab86..faca285 100644 --- a/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java +++ b/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java @@ -1,12 +1,12 @@ package com.ilummc.tlib.inject; +import com.ilummc.tlib.TLib; import com.ilummc.tlib.annotations.Dependencies; import com.ilummc.tlib.annotations.Dependency; import com.ilummc.tlib.annotations.Logger; import com.ilummc.tlib.annotations.PluginInstance; import com.ilummc.tlib.dependency.TDependency; import com.ilummc.tlib.util.TLogger; -import me.skymc.taboolib.message.MsgUtils; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; @@ -19,6 +19,7 @@ public class DependencyInjector { injectLogger(plugin, o); injectPluginInstance(plugin, o); injectDependencies(plugin, o); + injectConfig(plugin, o); } static void injectOnEnable(Plugin plugin) { @@ -29,6 +30,10 @@ public class DependencyInjector { } + private static void injectConfig(Plugin plugin, Object o) { + + } + private static void injectLogger(Plugin plugin, Object o) { try { for (Field field : o.getClass().getDeclaredFields()) { @@ -56,7 +61,7 @@ public class DependencyInjector { Plugin pl; if ((pl = Bukkit.getPluginManager().getPlugin(instance.value())) == null) { if (!TDependency.requestPlugin(instance.value())) { - MsgUtils.warn(plugin.getName() + " 所需的依赖插件 " + instance.value() + " 自动加载失败"); + TLib.getTLib().getLogger().warn(plugin.getName() + " 所需的依赖插件 " + instance.value() + " 自动加载失败"); return; } else { pl = Bukkit.getPluginManager().getPlugin(instance.value()); @@ -79,20 +84,22 @@ public class DependencyInjector { if (d2 != null) dependencies = new Dependency[]{d2}; } if (dependencies.length != 0) { - MsgUtils.send("正在加载 " + plugin.getName() + " 插件所需的依赖"); + TLib.getTLib().getLogger().info("正在加载 " + plugin.getName() + " 插件所需的依赖"); for (Dependency dependency : dependencies) { if (dependency.type() == Dependency.Type.PLUGIN) - if (TDependency.requestPlugin(dependency.args())) - MsgUtils.send(plugin.getName() + " 请求的插件 " + dependency.args()[0] + " 加载成功。"); + if (TDependency.requestPlugin(dependency.plugin())) + TLib.getTLib().getLogger().info(" " + plugin.getName() + " 请求的插件 " + dependency.plugin() + " 加载成功。"); else - MsgUtils.warn(plugin.getName() + " 请求的插件 " + dependency.args()[0] + " 加载失败。"); + TLib.getTLib().getLogger().warn(" " + plugin.getName() + " 请求的插件 " + dependency.plugin() + " 加载失败。"); if (dependency.type() == Dependency.Type.LIBRARY) - if (TDependency.requestLib(dependency.args())) - MsgUtils.send(plugin.getName() + " 请求的库文件 " + String.join(":", dependency.args()) + " 加载成功。"); + if (TDependency.requestLib(dependency.maven(), dependency.mavenRepo(), dependency.url())) + TLib.getTLib().getLogger().info(" " + plugin.getName() + " 请求的库文件 " + String.join(":", + dependency.maven()) + " 加载成功。"); else - MsgUtils.send(plugin.getName() + " 请求的库文件 " + String.join(":", dependency.args()) + " 加载失败。"); + TLib.getTLib().getLogger().warn(" " + plugin.getName() + " 请求的库文件 " + String.join(":", + dependency.maven()) + " 加载失败。"); } - MsgUtils.send("依赖加载完成"); + TLib.getTLib().getLogger().info("依赖加载完成"); } } diff --git a/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java b/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java new file mode 100644 index 0000000..76921a9 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java @@ -0,0 +1,6 @@ +package com.ilummc.tlib.inject; + +public class TConfigInjector { + + +} diff --git a/src/main/java/com/ilummc/tlib/util/TLogger.java b/src/main/java/com/ilummc/tlib/util/TLogger.java index 88dd23d..5d5aa42 100644 --- a/src/main/java/com/ilummc/tlib/util/TLogger.java +++ b/src/main/java/com/ilummc/tlib/util/TLogger.java @@ -27,37 +27,37 @@ public class TLogger { public void verbose(String msg) { if (level >= VERBOSE) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§f全部", msg)); } public void finest(String msg) { if (level >= FINEST) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§e良好", msg)); } public void fine(String msg) { if (level >= FINE) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§a正常", msg)); } public void info(String msg) { if (level >= INFO) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§b信息", msg)); } public void warn(String msg) { if (level >= WARN) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§6警告", msg)); } public void error(String msg) { if (level >= ERROR) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§c错误", msg)); } public void fatal(String msg) { if (level >= FATAL) - Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§4致命错误", msg)); } }