* 阻塞线程进行下载/加载 * - * @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)); } }