From 156c56d71d537e2b9946aec0531af2942cc11815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Tue, 23 Jul 2019 15:43:45 +0800 Subject: [PATCH] Fix LocalPlayer & WorldGuardHook --- .../common/plugin/bridge/BridgeLoader.java | 6 +---- .../module/lite/SimpleVersionControl.java | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeLoader.java b/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeLoader.java index 93bccf6..9506097 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeLoader.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeLoader.java @@ -14,10 +14,6 @@ public class BridgeLoader extends ClassLoader { private static Method findClass; private static ClassLoader pluginClassLoader; - public static BridgeLoader getInstance() { - return new BridgeLoader(); - } - private BridgeLoader() { super(BridgeLoader.class.getClassLoader()); try { @@ -41,6 +37,6 @@ public class BridgeLoader extends ClassLoader { } public static Class createNewClass(String name, byte[] arr) { - return getInstance().defineClass(name, arr, 0, arr.length, BridgeLoader.class.getProtectionDomain()); + return new BridgeLoader().defineClass(name, arr, 0, arr.length, BridgeLoader.class.getProtectionDomain()); } } diff --git a/src/main/scala/io/izzel/taboolib/module/lite/SimpleVersionControl.java b/src/main/scala/io/izzel/taboolib/module/lite/SimpleVersionControl.java index 0222c3a..26ef398 100644 --- a/src/main/scala/io/izzel/taboolib/module/lite/SimpleVersionControl.java +++ b/src/main/scala/io/izzel/taboolib/module/lite/SimpleVersionControl.java @@ -6,6 +6,7 @@ import io.izzel.taboolib.Version; import io.izzel.taboolib.common.plugin.InternalPlugin; import io.izzel.taboolib.common.plugin.bridge.BridgeLoader; import io.izzel.taboolib.util.Files; +import io.izzel.taboolib.util.IO; import io.izzel.taboolib.util.Ref; import io.izzel.taboolib.util.asm.AsmClassLoader; import org.bukkit.plugin.Plugin; @@ -13,7 +14,9 @@ import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -25,7 +28,7 @@ import java.util.Map; */ public class SimpleVersionControl { - private static Map> cacheClasses = new HashMap<>(); + private static Map cacheClasses = new HashMap<>(); private String target; private String to; private List from = Lists.newArrayList(); @@ -113,20 +116,23 @@ public class SimpleVersionControl { } public Class translate(Plugin plugin) throws IOException { + InputStream inputStream; if (useCache && cacheClasses.containsKey(target)) { - return cacheClasses.get(target); + inputStream = new ByteArrayInputStream(cacheClasses.get(target)); + } else { + inputStream = Files.getResource(plugin, target.replace(".", "/") + ".class"); + if (useCache) { + cacheClasses.put(target, IO.readFully(inputStream)); + } } - ClassReader classReader = new ClassReader(Files.getResource(plugin, target.replace(".", "/") + ".class")); + ClassReader classReader = new ClassReader(inputStream); ClassWriter classWriter = new ClassWriter(0); ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter); classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES); classWriter.visitEnd(); classVisitor.visitEnd(); - Class newClass = plugin instanceof InternalPlugin ? AsmClassLoader.createNewClass(target, classWriter.toByteArray()) : BridgeLoader.createNewClass(target, classWriter.toByteArray()); - if (useCache) { - cacheClasses.put(target, newClass); - } - return newClass; + // 因第三方插件调用该方法时会出现找不到类,所以第三方插件使用 BridgeLoader 加载类 + return plugin instanceof InternalPlugin ? AsmClassLoader.createNewClass(target, classWriter.toByteArray()) : BridgeLoader.createNewClass(target, classWriter.toByteArray()); } public Class translateBridge() throws IOException {