+ * 阻塞线程进行下载/加载 + * + * @param args 插件名称,下载地址(可选) + * @return 是否成功加载了依赖 + */ + public static boolean requestPlugin(String... args) { + return false; + } + + /** + * 请求一个库作为依赖,这个库将会在 Maven Central、oss.sonatype 以及自定义的 Maven 仓库寻找 + *
+ * 阻塞线程进行下载/加载
+ *
+ * @param args 依赖名,格式为 groupId:artifactId:version
+ * @return 是否成功加载库,如果加载成功,插件将可以任意调用使用的类
+ */
+ public static boolean requestLib(String... args) {
+ if (args[0].matches(".*:.*:.*")) {
+ String[] arr = args[0].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)) {
+ 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) {
+ ReentrantLock lock = new ReentrantLock();
+ AtomicBoolean failed = new AtomicBoolean(false);
+ new EagletTask()
+ .url(url + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar")
+ .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))
+ .setOnComplete(event -> {
+ 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();
+ }
+ return !failed.get();
+ }
+
+}
diff --git a/src/main/java/com/ilummc/tlib/dependency/TDependencyLoader.java b/src/main/java/com/ilummc/tlib/dependency/TDependencyLoader.java
new file mode 100644
index 0000000..3c52502
--- /dev/null
+++ b/src/main/java/com/ilummc/tlib/dependency/TDependencyLoader.java
@@ -0,0 +1,32 @@
+package com.ilummc.tlib.dependency;
+
+import org.bukkit.plugin.Plugin;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class TDependencyLoader {
+
+ public static synchronized void addToPath(Plugin plugin, URL url) {
+ try {
+ Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
+ method.setAccessible(true);
+ method.invoke(plugin.getClass().getClassLoader(), url);
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static synchronized void addToPath(Plugin plugin, File file) {
+ try {
+ addToPath(plugin, file.toURI().toURL());
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java b/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java
new file mode 100644
index 0000000..6e1ab86
--- /dev/null
+++ b/src/main/java/com/ilummc/tlib/inject/DependencyInjector.java
@@ -0,0 +1,99 @@
+package com.ilummc.tlib.inject;
+
+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;
+
+import java.lang.reflect.Field;
+
+public class DependencyInjector {
+
+ public static void inject(Plugin plugin, Object o) {
+ injectLogger(plugin, o);
+ injectPluginInstance(plugin, o);
+ injectDependencies(plugin, o);
+ }
+
+ static void injectOnEnable(Plugin plugin) {
+ inject(plugin, plugin);
+ }
+
+ static void onDisable(Plugin plugin) {
+
+ }
+
+ private static void injectLogger(Plugin plugin, Object o) {
+ try {
+ for (Field field : o.getClass().getDeclaredFields()) {
+ Logger logger;
+ if ((logger = field.getAnnotation(Logger.class)) != null) {
+ field.getType().asSubclass(TLogger.class);
+ TLogger tLogger = new TLogger(logger.value(), plugin, logger.level());
+ if (!field.isAccessible())
+ field.setAccessible(true);
+ field.set(o, tLogger);
+ }
+ }
+ } catch (Exception ignored) {
+ }
+ }
+
+ private static void injectPluginInstance(Plugin plugin, Object o) {
+ try {
+ for (Field field : o.getClass().getDeclaredFields()) {
+ PluginInstance instance;
+ if ((instance = field.getAnnotation(PluginInstance.class)) != null) {
+ if (!field.isAccessible())
+ field.setAccessible(true);
+ field.getType().asSubclass(JavaPlugin.class);
+ Plugin pl;
+ if ((pl = Bukkit.getPluginManager().getPlugin(instance.value())) == null) {
+ if (!TDependency.requestPlugin(instance.value())) {
+ MsgUtils.warn(plugin.getName() + " 所需的依赖插件 " + instance.value() + " 自动加载失败");
+ return;
+ } else {
+ pl = Bukkit.getPluginManager().getPlugin(instance.value());
+ }
+ }
+ if (pl != null)
+ field.set(o, pl);
+ }
+ }
+ } catch (Exception ignored) {
+ }
+ }
+
+ private static void injectDependencies(Plugin plugin, Object o) {
+ Dependency[] dependencies = new Dependency[0];
+ {
+ Dependencies d = o.getClass().getAnnotation(Dependencies.class);
+ if (d != null) dependencies = d.value();
+ Dependency d2 = o.getClass().getAnnotation(Dependency.class);
+ if (d2 != null) dependencies = new Dependency[]{d2};
+ }
+ if (dependencies.length != 0) {
+ MsgUtils.send("正在加载 " + plugin.getName() + " 插件所需的依赖");
+ for (Dependency dependency : dependencies) {
+ if (dependency.type() == Dependency.Type.PLUGIN)
+ if (TDependency.requestPlugin(dependency.args()))
+ MsgUtils.send(plugin.getName() + " 请求的插件 " + dependency.args()[0] + " 加载成功。");
+ else
+ MsgUtils.warn(plugin.getName() + " 请求的插件 " + dependency.args()[0] + " 加载失败。");
+ if (dependency.type() == Dependency.Type.LIBRARY)
+ if (TDependency.requestLib(dependency.args()))
+ MsgUtils.send(plugin.getName() + " 请求的库文件 " + String.join(":", dependency.args()) + " 加载成功。");
+ else
+ MsgUtils.send(plugin.getName() + " 请求的库文件 " + String.join(":", dependency.args()) + " 加载失败。");
+ }
+ MsgUtils.send("依赖加载完成");
+ }
+ }
+
+}
diff --git a/src/main/java/com/ilummc/tlib/inject/TLibPluginManager.java b/src/main/java/com/ilummc/tlib/inject/TLibPluginManager.java
new file mode 100644
index 0000000..f8a22f6
--- /dev/null
+++ b/src/main/java/com/ilummc/tlib/inject/TLibPluginManager.java
@@ -0,0 +1,168 @@
+package com.ilummc.tlib.inject;
+
+import org.bukkit.Bukkit;
+import org.bukkit.event.Event;
+import org.bukkit.event.EventPriority;
+import org.bukkit.event.Listener;
+import org.bukkit.permissions.Permissible;
+import org.bukkit.permissions.Permission;
+import org.bukkit.plugin.*;
+
+import java.io.File;
+import java.util.Set;
+
+public class TLibPluginManager implements PluginManager {
+
+ private final PluginManager instance;
+
+ public TLibPluginManager() {
+ instance = Bukkit.getPluginManager();
+ }
+
+ @Override
+ public void registerInterface(Class extends PluginLoader> aClass) throws IllegalArgumentException {
+ instance.registerInterface(aClass);
+ }
+
+ @Override
+ public Plugin getPlugin(String s) {
+ return instance.getPlugin(s);
+ }
+
+ @Override
+ public Plugin[] getPlugins() {
+ return instance.getPlugins();
+ }
+
+ @Override
+ public boolean isPluginEnabled(String s) {
+ return instance.isPluginEnabled(s);
+ }
+
+ @Override
+ public boolean isPluginEnabled(Plugin plugin) {
+ return instance.isPluginEnabled(plugin);
+ }
+
+ @Override
+ public Plugin loadPlugin(File file) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException {
+ return instance.loadPlugin(file);
+ }
+
+ @Override
+ public Plugin[] loadPlugins(File file) {
+ return instance.loadPlugins(file);
+ }
+
+ @Override
+ public void disablePlugins() {
+ instance.disablePlugins();
+ }
+
+ @Override
+ public void clearPlugins() {
+ instance.clearPlugins();
+ }
+
+ @Override
+ public void callEvent(Event event) throws IllegalStateException {
+ instance.callEvent(event);
+ }
+
+ @Override
+ public void registerEvents(Listener listener, Plugin plugin) {
+ instance.registerEvents(listener, plugin);
+ }
+
+ @Override
+ public void registerEvent(Class extends Event> aClass, Listener listener, EventPriority eventPriority, EventExecutor eventExecutor, Plugin plugin) {
+ instance.registerEvent(aClass, listener, eventPriority, eventExecutor, plugin);
+ }
+
+ @Override
+ public void registerEvent(Class extends Event> aClass, Listener listener, EventPriority eventPriority, EventExecutor eventExecutor, Plugin plugin, boolean b) {
+ instance.registerEvent(aClass, listener, eventPriority, eventExecutor, plugin, b);
+ }
+
+ @Override
+ public void enablePlugin(Plugin plugin) {
+ DependencyInjector.injectOnEnable(plugin);
+ instance.enablePlugin(plugin);
+ }
+
+ @Override
+ public void disablePlugin(Plugin plugin) {
+ DependencyInjector.onDisable(plugin);
+ instance.disablePlugin(plugin);
+ }
+
+ @Override
+ public Permission getPermission(String s) {
+ return instance.getPermission(s);
+ }
+
+ @Override
+ public void addPermission(Permission permission) {
+ instance.addPermission(permission);
+ }
+
+ @Override
+ public void removePermission(Permission permission) {
+ instance.removePermission(permission);
+ }
+
+ @Override
+ public void removePermission(String s) {
+ instance.removePermission(s);
+ }
+
+ @Override
+ public Set