From 8af253c6ec54344805f4e767a582f84fe8bf20ee Mon Sep 17 00:00:00 2001 From: Izzel_Aliz Date: Sat, 23 Jun 2018 15:39:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8boom=E7=9A=84=E8=BE=B9=E7=BC=98?= =?UTF-8?q?=E6=82=AC=E5=B4=96=E5=8B=92=E9=A9=AC=EF=BC=8C=E5=A4=AA=E5=88=BA?= =?UTF-8?q?=E6=BF=80=E4=BA=86=20=E6=9F=90=E4=B8=AA=E7=B1=BB=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E8=B7=91=E7=9A=84=E6=9B=B4=E6=BA=9C=E4=BA=86=20?= =?UTF-8?q?=E7=94=BB=E4=BA=86=E4=B8=AA=E5=81=87=E7=9A=84=E9=A5=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ilummc/tlib/TLib.java | 9 +++++---- .../tlib/annotations/clr/CommandHandler.java | 17 +++++++++++++++++ .../tlib/annotations/clr/CommandHandlers.java | 14 ++++++++++++++ .../com/ilummc/tlib/annotations/clr/Sub.java | 14 ++++++++++++++ .../ilummc/tlib/clr/CommandLineResolver.java | 7 +++++++ src/main/java/com/ilummc/tlib/util/Ref.java | 8 ++++---- .../ilummc/tlib/util/asm/AsmClassLoader.java | 14 +++++++++++--- .../tlib/util/asm/AsmClassTransformer.java | 2 +- src/main/resources/lang/zh_CN.yml | 6 +++++- 9 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/ilummc/tlib/annotations/clr/CommandHandler.java create mode 100644 src/main/java/com/ilummc/tlib/annotations/clr/CommandHandlers.java create mode 100644 src/main/java/com/ilummc/tlib/annotations/clr/Sub.java create mode 100644 src/main/java/com/ilummc/tlib/clr/CommandLineResolver.java diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java index 9c64381..0c5c7d7 100644 --- a/src/main/java/com/ilummc/tlib/TLib.java +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -17,6 +17,7 @@ import me.skymc.taboolib.fileutils.FileUtils; import org.bukkit.Bukkit; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; import java.io.File; import java.io.IOException; @@ -97,12 +98,12 @@ public class TLib { Field field = Bukkit.getServer().getClass().getDeclaredField("pluginManager"); field.setAccessible(true); field.set(Bukkit.getServer(), new TPluginManager()); - } catch (NoSuchFieldException | IllegalAccessException ignored) { - } - if (Bukkit.getPluginManager() instanceof TPluginManager) { TLocale.Logger.info("TLIB.INJECTION-SUCCESS"); - } else { + } catch (NoSuchFieldException | IllegalAccessException ignored) { TLocale.Logger.fatal("TLIB.INJECTION-FAILED"); + for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { + if (plugin != Main.getInst()) TDependencyInjector.inject(plugin, plugin); + } } } diff --git a/src/main/java/com/ilummc/tlib/annotations/clr/CommandHandler.java b/src/main/java/com/ilummc/tlib/annotations/clr/CommandHandler.java new file mode 100644 index 0000000..c38a610 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/clr/CommandHandler.java @@ -0,0 +1,17 @@ +package com.ilummc.tlib.annotations.clr; + +import java.lang.annotation.*; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Repeatable(CommandHandlers.class) +public @interface CommandHandler { + + /** + * Name of the command + * + * @return Name of the command + */ + String value(); + +} diff --git a/src/main/java/com/ilummc/tlib/annotations/clr/CommandHandlers.java b/src/main/java/com/ilummc/tlib/annotations/clr/CommandHandlers.java new file mode 100644 index 0000000..34cfb32 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/clr/CommandHandlers.java @@ -0,0 +1,14 @@ +package com.ilummc.tlib.annotations.clr; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandHandlers { + + CommandHandler[] value(); + +} diff --git a/src/main/java/com/ilummc/tlib/annotations/clr/Sub.java b/src/main/java/com/ilummc/tlib/annotations/clr/Sub.java new file mode 100644 index 0000000..2ee6e27 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/clr/Sub.java @@ -0,0 +1,14 @@ +package com.ilummc.tlib.annotations.clr; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Sub { + + String value(); + +} diff --git a/src/main/java/com/ilummc/tlib/clr/CommandLineResolver.java b/src/main/java/com/ilummc/tlib/clr/CommandLineResolver.java new file mode 100644 index 0000000..5341f28 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/clr/CommandLineResolver.java @@ -0,0 +1,7 @@ +package com.ilummc.tlib.clr; + +public class CommandLineResolver { + + + +} diff --git a/src/main/java/com/ilummc/tlib/util/Ref.java b/src/main/java/com/ilummc/tlib/util/Ref.java index 69374de..722af03 100644 --- a/src/main/java/com/ilummc/tlib/util/Ref.java +++ b/src/main/java/com/ilummc/tlib/util/Ref.java @@ -101,11 +101,11 @@ public class Ref { public static JavaPlugin getCallerPlugin(Class callerClass) { try { - Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin"); - pluginField.setAccessible(true); - return (JavaPlugin) pluginField.get(callerClass.getClassLoader()); - } catch (Exception ignored) { + return JavaPlugin.getProvidingPlugin(callerClass); + } catch (IllegalArgumentException ignored) { TLocale.Logger.error("LOCALE.CALLER-PLUGIN-NOT-FOUND", callerClass.getName()); + } catch (IllegalStateException e) { + TLocale.Logger.error("LOCALE.STATIC-CLASS-LOADER", callerClass.getName()); } return (JavaPlugin) Main.getInst(); } diff --git a/src/main/java/com/ilummc/tlib/util/asm/AsmClassLoader.java b/src/main/java/com/ilummc/tlib/util/asm/AsmClassLoader.java index 0b58031..d316f82 100644 --- a/src/main/java/com/ilummc/tlib/util/asm/AsmClassLoader.java +++ b/src/main/java/com/ilummc/tlib/util/asm/AsmClassLoader.java @@ -2,12 +2,20 @@ package com.ilummc.tlib.util.asm; public class AsmClassLoader extends ClassLoader { - public AsmClassLoader() { + private static final class AsmClassLoaderHolder { + private static AsmClassLoader instance = new AsmClassLoader(); + } + + public static AsmClassLoader getInstance() { + return AsmClassLoaderHolder.instance; + } + + private AsmClassLoader() { super(AsmClassLoader.class.getClassLoader()); } - public Class createNewClass(String name, byte[] arr) { - return defineClass(name, arr, 0, arr.length, AsmClassLoader.class.getProtectionDomain()); + public static Class createNewClass(String name, byte[] arr) { + return getInstance().defineClass(name, arr, 0, arr.length, AsmClassLoader.class.getProtectionDomain()); } } diff --git a/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java b/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java index fcb98b9..a074273 100644 --- a/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java +++ b/src/main/java/com/ilummc/tlib/util/asm/AsmClassTransformer.java @@ -34,7 +34,7 @@ public class AsmClassTransformer extends ClassVisitor implements Opcodes { newClassName = from.getName() + "_TabooLibRemap_" + this.hashCode() + "_" + toVer; prevName = from.getName().replace('.', '/'); classReader.accept(this, ClassReader.SKIP_DEBUG); - Class clazz = new AsmClassLoader().createNewClass(newClassName, writer.toByteArray()); + Class clazz = AsmClassLoader.createNewClass(newClassName, writer.toByteArray()); Field field = from.getClassLoader().getClass().getDeclaredField("classes"); field.setAccessible(true); ((Map>) field.get(from.getClassLoader())).put(newClassName, clazz); diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 2a90db7..e1f72cd 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -1,6 +1,8 @@ TLIB: INJECTION-SUCCESS: '注入成功' - INJECTION-FAILED: '注入失败' + INJECTION-FAILED: + - '注入失败' + - '&c提前加载依赖于 TabooLib 的所有插件的相关功能' LOAD-FAIL-OFFLINE: - '**********************************************' - '** TabooLib-{0} 无法在您的服务器上使用' @@ -61,6 +63,8 @@ LOCALE: BAR-COLOR-IDENTIFICATION-FAILED: 'TLocaleBossBar 的颜色识别失败: {0}' BAR-STYLE-IDENTIFICATION-FAILED: 'TLocaleBossBar 的类型识别失败: {0}' BAR-PLUGIN-NOT-FOUND: 'TLocaleBossBar 的依赖插件 BossBarAPI 不存在' + CALLER-PLUGIN-NOT-FOUND: '{0} 不是一个由插件加载的类' + STATIC-CLASS-LOADER: '{0} 由静态类加载器初始化,无法获得其从属的插件' MISC: FIELD-COPY-FAILED: '拷贝 {0} 对象失败'