From add76a30cb636406cf0de39f4683dbb4f41ed1f4 Mon Sep 17 00:00:00 2001 From: Izzel_Aliz Date: Wed, 4 Apr 2018 20:00:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8B=89=E5=BC=BA=E8=83=BD=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=8A=A0=E8=BD=BD=E7=B3=BB=E7=BB=9F=E5=81=9A?= =?UTF-8?q?=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...en__com_ilummc_eagletdl_EagletCore_1_0.xml | 13 ++ .../libraries/Maven__org_ow2_asm_asm_5_2.xml | 13 -- .../Maven__org_ow2_asm_asm_6_1_1.xml | 13 ++ ...aven__org_projectlombok_lombok_1_16_20.xml | 13 ++ pom.xml | 31 ++-- .../java/com/ilummc/tlib/ExampleMain.java | 4 +- src/main/java/com/ilummc/tlib/TLib.java | 28 +++ src/main/java/com/ilummc/tlib/TabooLib.java | 8 - .../com/ilummc/tlib/annotations/Config.java | 7 + .../ilummc/tlib/annotations/ConfigNode.java | 7 + .../ilummc/tlib/annotations/Dependencies.java | 9 +- .../ilummc/tlib/annotations/Dependency.java | 16 ++ .../com/ilummc/tlib/annotations/Logger.java | 18 ++ .../tlib/annotations/PluginInstance.java | 7 + .../ilummc/tlib/annotations/db/SQLTable.java | 7 + .../com/ilummc/tlib/bean/BooleanProperty.java | 17 -- .../java/com/ilummc/tlib/bean/Property.java | 39 +++- .../ilummc/tlib/dependency/TDependency.java | 77 ++++++++ .../tlib/dependency/TDependencyLoader.java | 32 ++++ .../tlib/inject/DependencyInjector.java | 99 +++++++++++ .../ilummc/tlib/inject/TLibPluginManager.java | 168 ++++++++++++++++++ .../java/com/ilummc/tlib/util/Strings.java | 27 +++ .../java/com/ilummc/tlib/util/TLogger.java | 63 +++++++ src/main/java/me/skymc/taboolib/Main.java | 6 +- 24 files changed, 665 insertions(+), 57 deletions(-) create mode 100644 .idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml delete mode 100644 .idea/libraries/Maven__org_ow2_asm_asm_5_2.xml create mode 100644 .idea/libraries/Maven__org_ow2_asm_asm_6_1_1.xml create mode 100644 .idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml create mode 100644 src/main/java/com/ilummc/tlib/TLib.java delete mode 100644 src/main/java/com/ilummc/tlib/TabooLib.java create mode 100644 src/main/java/com/ilummc/tlib/annotations/Dependency.java create mode 100644 src/main/java/com/ilummc/tlib/annotations/Logger.java delete mode 100644 src/main/java/com/ilummc/tlib/bean/BooleanProperty.java create mode 100644 src/main/java/com/ilummc/tlib/dependency/TDependency.java create mode 100644 src/main/java/com/ilummc/tlib/dependency/TDependencyLoader.java create mode 100644 src/main/java/com/ilummc/tlib/inject/DependencyInjector.java create mode 100644 src/main/java/com/ilummc/tlib/inject/TLibPluginManager.java create mode 100644 src/main/java/com/ilummc/tlib/util/Strings.java create mode 100644 src/main/java/com/ilummc/tlib/util/TLogger.java diff --git a/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml b/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml new file mode 100644 index 0000000..edabcdb --- /dev/null +++ b/.idea/libraries/Maven__com_ilummc_eagletdl_EagletCore_1_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_ow2_asm_asm_5_2.xml b/.idea/libraries/Maven__org_ow2_asm_asm_5_2.xml deleted file mode 100644 index 02e02fd..0000000 --- a/.idea/libraries/Maven__org_ow2_asm_asm_5_2.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Maven__org_ow2_asm_asm_6_1_1.xml b/.idea/libraries/Maven__org_ow2_asm_asm_6_1_1.xml new file mode 100644 index 0000000..db66af9 --- /dev/null +++ b/.idea/libraries/Maven__org_ow2_asm_asm_6_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml b/.idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml new file mode 100644 index 0000000..bcbf2ac --- /dev/null +++ b/.idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0c643ec..02279a1 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ - org.ow2.asm + com.ilummc.eagletdl false @@ -58,10 +58,20 @@ + + com.ilummc.eagletdl + EagletCore + 1.0 + org.ow2.asm asm - 5.2 + 6.1.1 + + + org.projectlombok + lombok + 1.16.20 bukkit @@ -70,6 +80,11 @@ system ${basedir}/libs/1_12_R1.jar + + net.milkbowl.vault + VaultAPI + 1.6 + bukkit bukkit1_8 @@ -105,13 +120,6 @@ system ${basedir}/libs/com.sun.tools.jar - - lombok - lombok - 1 - system - ${basedir}/libs/lombok.jar - placeholder placeholder @@ -140,11 +148,6 @@ system ${basedir}/libs/TabooCode5.jar - - net.milkbowl.vault - VaultAPI - 1.6 - wg wg diff --git a/src/main/java/com/ilummc/tlib/ExampleMain.java b/src/main/java/com/ilummc/tlib/ExampleMain.java index a22a8ad..46af092 100644 --- a/src/main/java/com/ilummc/tlib/ExampleMain.java +++ b/src/main/java/com/ilummc/tlib/ExampleMain.java @@ -2,7 +2,7 @@ package com.ilummc.tlib; import com.ilummc.tlib.annotations.Config; import com.ilummc.tlib.annotations.ConfigNode; -import com.ilummc.tlib.bean.BooleanProperty; +import com.ilummc.tlib.bean.Property; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -10,7 +10,7 @@ import org.bukkit.plugin.java.JavaPlugin; public class ExampleMain extends JavaPlugin { @ConfigNode("enableUpdate") - private BooleanProperty update = new BooleanProperty(true); + private Property update = Property.of(false); @Override public void onEnable() { diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java new file mode 100644 index 0000000..cd20cc0 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -0,0 +1,28 @@ +package com.ilummc.tlib; + +import com.ilummc.tlib.inject.TLibPluginManager; +import me.skymc.taboolib.Main; +import me.skymc.taboolib.message.MsgUtils; +import org.bukkit.Bukkit; + +import java.io.File; +import java.lang.reflect.Field; + +public class TLib { + + @SuppressWarnings({"unchecked"}) + public static void init() { + // 注入 PluginLoader 用于加载依赖 + try { + Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager"); + field.setAccessible(true); + field.set(Bukkit.getServer(), new TLibPluginManager()); + MsgUtils.send("注入成功"); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + MsgUtils.warn("注入失败"); + } + new File(Main.getInst().getDataFolder(), "/libs").mkdirs(); + } + +} diff --git a/src/main/java/com/ilummc/tlib/TabooLib.java b/src/main/java/com/ilummc/tlib/TabooLib.java deleted file mode 100644 index 57b888d..0000000 --- a/src/main/java/com/ilummc/tlib/TabooLib.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.ilummc.tlib; - -public class TabooLib { - - public void init() { - - } -} diff --git a/src/main/java/com/ilummc/tlib/annotations/Config.java b/src/main/java/com/ilummc/tlib/annotations/Config.java index b702063..1e4548a 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Config.java +++ b/src/main/java/com/ilummc/tlib/annotations/Config.java @@ -1,5 +1,12 @@ package com.ilummc.tlib.annotations; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) public @interface Config { String name() default "config.yml"; diff --git a/src/main/java/com/ilummc/tlib/annotations/ConfigNode.java b/src/main/java/com/ilummc/tlib/annotations/ConfigNode.java index 810a26f..7e2478d 100644 --- a/src/main/java/com/ilummc/tlib/annotations/ConfigNode.java +++ b/src/main/java/com/ilummc/tlib/annotations/ConfigNode.java @@ -1,5 +1,12 @@ package com.ilummc.tlib.annotations; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) public @interface ConfigNode { String value(); diff --git a/src/main/java/com/ilummc/tlib/annotations/Dependencies.java b/src/main/java/com/ilummc/tlib/annotations/Dependencies.java index b17f877..a87a8d7 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Dependencies.java +++ b/src/main/java/com/ilummc/tlib/annotations/Dependencies.java @@ -1,7 +1,14 @@ package com.ilummc.tlib.annotations; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) public @interface Dependencies { - String[] value(); + Dependency[] value(); } diff --git a/src/main/java/com/ilummc/tlib/annotations/Dependency.java b/src/main/java/com/ilummc/tlib/annotations/Dependency.java new file mode 100644 index 0000000..bde26ea --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/Dependency.java @@ -0,0 +1,16 @@ +package com.ilummc.tlib.annotations; + +import java.lang.annotation.*; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Repeatable(Dependencies.class) +public @interface Dependency { + + enum Type {PLUGIN, LIBRARY} + + Type type(); + + String[] args(); + +} diff --git a/src/main/java/com/ilummc/tlib/annotations/Logger.java b/src/main/java/com/ilummc/tlib/annotations/Logger.java new file mode 100644 index 0000000..2e504b3 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/Logger.java @@ -0,0 +1,18 @@ +package com.ilummc.tlib.annotations; + +import com.ilummc.tlib.util.TLogger; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Logger { + + String value() default "[{0}] {1}"; + + int level() default TLogger.INFO; + +} diff --git a/src/main/java/com/ilummc/tlib/annotations/PluginInstance.java b/src/main/java/com/ilummc/tlib/annotations/PluginInstance.java index 02c713e..9e10b55 100644 --- a/src/main/java/com/ilummc/tlib/annotations/PluginInstance.java +++ b/src/main/java/com/ilummc/tlib/annotations/PluginInstance.java @@ -1,5 +1,12 @@ package com.ilummc.tlib.annotations; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) public @interface PluginInstance { String value(); diff --git a/src/main/java/com/ilummc/tlib/annotations/db/SQLTable.java b/src/main/java/com/ilummc/tlib/annotations/db/SQLTable.java index eb868f2..543d3f2 100644 --- a/src/main/java/com/ilummc/tlib/annotations/db/SQLTable.java +++ b/src/main/java/com/ilummc/tlib/annotations/db/SQLTable.java @@ -1,5 +1,12 @@ package com.ilummc.tlib.annotations.db; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) public @interface SQLTable { String value(); diff --git a/src/main/java/com/ilummc/tlib/bean/BooleanProperty.java b/src/main/java/com/ilummc/tlib/bean/BooleanProperty.java deleted file mode 100644 index 2837d76..0000000 --- a/src/main/java/com/ilummc/tlib/bean/BooleanProperty.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.ilummc.tlib.bean; - -import java.util.function.BiConsumer; - -public class BooleanProperty { - - private boolean property; - - public BooleanProperty(boolean property) { - this.property = property; - } - - public void addListener(BiConsumer consumer) { - - } - -} diff --git a/src/main/java/com/ilummc/tlib/bean/Property.java b/src/main/java/com/ilummc/tlib/bean/Property.java index 12b16c7..29cfff7 100644 --- a/src/main/java/com/ilummc/tlib/bean/Property.java +++ b/src/main/java/com/ilummc/tlib/bean/Property.java @@ -1,4 +1,41 @@ package com.ilummc.tlib.bean; -public class Property { +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; + +public class Property { + + private Property(T value) { + this.value = value; + } + + private List> consumers; + + private T value; + + public void set(T value) { + if (value != this.value) { + if (consumers != null) + for (BiConsumer consumer : consumers) { + consumer.accept(this.value, value); + } + this.value = value; + } + } + + public T get() { + return value; + } + + public void addListener(BiConsumer consumer) { + if (consumers == null) + consumers = new ArrayList<>(); + consumers.add(consumer); + } + + public static Property of(T value) { + return new Property<>(value); + } + } diff --git a/src/main/java/com/ilummc/tlib/dependency/TDependency.java b/src/main/java/com/ilummc/tlib/dependency/TDependency.java new file mode 100644 index 0000000..421676d --- /dev/null +++ b/src/main/java/com/ilummc/tlib/dependency/TDependency.java @@ -0,0 +1,77 @@ +package com.ilummc.tlib.dependency; + +import com.ilummc.eagletdl.EagletTask; +import me.skymc.taboolib.Main; +import me.skymc.taboolib.message.MsgUtils; + +import java.io.File; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReentrantLock; + +public class TDependency { + + public static final String MAVEN_REPO = "http://repo.maven.apache.org/maven2"; + + /** + * 请求一个插件作为依赖,这个插件将会在所有已经添加的 Jenkins 仓库、Maven 仓库寻找 + *

+ * 阻塞线程进行下载/加载 + * + * @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 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 aClass, Listener listener, EventPriority eventPriority, EventExecutor eventExecutor, Plugin plugin) { + instance.registerEvent(aClass, listener, eventPriority, eventExecutor, plugin); + } + + @Override + public void registerEvent(Class 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 getDefaultPermissions(boolean b) { + return instance.getDefaultPermissions(b); + } + + @Override + public void recalculatePermissionDefaults(Permission permission) { + instance.recalculatePermissionDefaults(permission); + } + + @Override + public void subscribeToPermission(String s, Permissible permissible) { + instance.subscribeToPermission(s, permissible); + } + + @Override + public void unsubscribeFromPermission(String s, Permissible permissible) { + instance.unsubscribeFromPermission(s, permissible); + } + + @Override + public Set getPermissionSubscriptions(String s) { + return instance.getPermissionSubscriptions(s); + } + + @Override + public void subscribeToDefaultPerms(boolean b, Permissible permissible) { + instance.subscribeToDefaultPerms(b, permissible); + } + + @Override + public void unsubscribeFromDefaultPerms(boolean b, Permissible permissible) { + instance.unsubscribeFromDefaultPerms(b, permissible); + } + + @Override + public Set getDefaultPermSubscriptions(boolean b) { + return instance.getDefaultPermSubscriptions(b); + } + + @Override + public Set getPermissions() { + return instance.getPermissions(); + } + + @Override + public boolean useTimings() { + return instance.useTimings(); + } +} diff --git a/src/main/java/com/ilummc/tlib/util/Strings.java b/src/main/java/com/ilummc/tlib/util/Strings.java new file mode 100644 index 0000000..050c43a --- /dev/null +++ b/src/main/java/com/ilummc/tlib/util/Strings.java @@ -0,0 +1,27 @@ +package com.ilummc.tlib.util; + +public class Strings { + + /** + * 优化过的 String#replace,比默认快了大概 5 倍 + * + * @param template 模板替换文件 + * @param args 替换的参数 + * @return 替换好的字符串 + */ + public static String replaceWithOrder(String template, String... args) { + char[] arr = template.toCharArray(); + StringBuilder stringBuilder = new StringBuilder(template.length()); + for (int i = 0; i < arr.length; i++) { + if (arr[i] == '{' && Character.isDigit(arr[Math.min(i + 1, arr.length - 1)]) + && arr[Math.min(i + 1, arr.length - 1)] - '0' < args.length + && arr[Math.min(i + 2, arr.length - 1)] == '}') { + stringBuilder.append(args[arr[i + 1] - '0']); + i += 2; + } else + stringBuilder.append(arr[i]); + } + return stringBuilder.toString(); + } + +} diff --git a/src/main/java/com/ilummc/tlib/util/TLogger.java b/src/main/java/com/ilummc/tlib/util/TLogger.java new file mode 100644 index 0000000..88dd23d --- /dev/null +++ b/src/main/java/com/ilummc/tlib/util/TLogger.java @@ -0,0 +1,63 @@ +package com.ilummc.tlib.util; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +public class TLogger { + + public static final int VERBOSE = 0, FINEST = 1, FINE = 2, INFO = 3, WARN = 4, ERROR = 5, FATAL = 6; + + @Getter + private final String pattern; + + @Getter + private Plugin plugin; + + @Getter + @Setter + private int level; + + public TLogger(String pattern, Plugin plugin, int level) { + this.pattern = pattern; + this.plugin = plugin; + this.level = level; + } + + public void verbose(String msg) { + if (level >= VERBOSE) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + + public void finest(String msg) { + if (level >= FINEST) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + + public void fine(String msg) { + if (level >= FINE) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + + public void info(String msg) { + if (level >= INFO) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + + public void warn(String msg) { + if (level >= WARN) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + + public void error(String msg) { + if (level >= ERROR) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + + public void fatal(String msg) { + if (level >= FATAL) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), msg)); + } + +} diff --git a/src/main/java/me/skymc/taboolib/Main.java b/src/main/java/me/skymc/taboolib/Main.java index f0ec280..2546d2c 100644 --- a/src/main/java/me/skymc/taboolib/Main.java +++ b/src/main/java/me/skymc/taboolib/Main.java @@ -1,5 +1,7 @@ package me.skymc.taboolib; +import com.ilummc.tlib.TLib; +import com.ilummc.tlib.annotations.Dependency; import lombok.Getter; import lombok.Setter; import me.skymc.taboolib.anvil.AnvilContainerAPI; @@ -103,7 +105,9 @@ public class Main extends JavaPlugin implements Listener { @Override public void onLoad() { inst = this; disable = false; - + + TLib.init(); + // 启动监控 new Metrics(this);