From ec3dc24a5e1d6b73b04dfb8578660ec399006114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Tue, 23 Jul 2019 17:14:49 +0800 Subject: [PATCH] Fix TellrawJson --- .../common/plugin/InternalPluginBridge.java | 7 ++++++ .../common/plugin/bridge/BridgeImpl.java | 17 ++++++++++++++ .../module/lite/SimpleReflection.java | 5 +++-- .../module/lite/SimpleVersionControl.java | 22 ++++++++++--------- .../module/tellraw/TellrawCreator.java | 9 ++++++-- .../taboolib/module/tellraw/TellrawJson.java | 7 +++--- 6 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java index 61748f7..fefab76 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java @@ -69,4 +69,11 @@ public abstract class InternalPluginBridge { abstract public Map taboolibTLocaleSerialize(Object in); abstract public FileConfiguration taboolibGetPlayerData(OfflinePlayer player); + + abstract public int protocolSupportPlayerVersion(Player player); + + abstract public int viaVersionPlayerVersion(Player player); + + abstract public Class getClass(String name) throws ClassNotFoundException; + } diff --git a/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java b/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java index 1730961..8cfc4c9 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java @@ -20,6 +20,8 @@ import org.bukkit.World; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; +import protocolsupport.api.ProtocolSupportAPI; +import us.myles.ViaVersion.api.Via; import java.lang.reflect.Method; import java.util.Collection; @@ -205,4 +207,19 @@ public class BridgeImpl extends InternalPluginBridge { public FileConfiguration taboolibGetPlayerData(OfflinePlayer player) { return PlayerDataManager.getPlayerData(player); } + + @Override + public int protocolSupportPlayerVersion(Player player) { + return ProtocolSupportAPI.getProtocolVersion(player).getId(); + } + + @Override + public int viaVersionPlayerVersion(Player player) { + return Via.getAPI().getPlayerVersion(player); + } + + @Override + public Class getClass(String name) throws ClassNotFoundException { + return Class.forName(name); + } } \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/lite/SimpleReflection.java b/src/main/scala/io/izzel/taboolib/module/lite/SimpleReflection.java index 5b0c8bf..faabd6e 100644 --- a/src/main/scala/io/izzel/taboolib/module/lite/SimpleReflection.java +++ b/src/main/scala/io/izzel/taboolib/module/lite/SimpleReflection.java @@ -1,6 +1,7 @@ package io.izzel.taboolib.module.lite; import com.google.common.collect.Maps; +import io.izzel.taboolib.TabooLibAPI; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; @@ -107,7 +108,7 @@ public class SimpleReflection { try { if (ParameterizedType.class.isAssignableFrom(genericType.getClass())) { for (Type actualTypeArgument : ((ParameterizedType) genericType).getActualTypeArguments()) { - return Class.forName(actualTypeArgument.getTypeName()); + return TabooLibAPI.getPluginBridge().getClass(actualTypeArgument.getTypeName()); } } } catch (Throwable t) { @@ -122,7 +123,7 @@ public class SimpleReflection { Type genericType = field.getGenericType(); if (ParameterizedType.class.isAssignableFrom(genericType.getClass())) { for (Type actualTypeArgument : ((ParameterizedType) genericType).getActualTypeArguments()) { - mapType[mapType[0] == null ? 0 : 1] = Class.forName(actualTypeArgument.getTypeName()); + mapType[mapType[0] == null ? 0 : 1] = TabooLibAPI.getPluginBridge().getClass(actualTypeArgument.getTypeName()); } } } catch (Throwable t) { 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 26ef398..259a95b 100644 --- a/src/main/scala/io/izzel/taboolib/module/lite/SimpleVersionControl.java +++ b/src/main/scala/io/izzel/taboolib/module/lite/SimpleVersionControl.java @@ -71,6 +71,7 @@ public class SimpleVersionControl { this.from.add(from.startsWith("v") ? from : "v" + from); return this; } + /** * 设置原版本,写法如:v1_8_R3, v1_12_R1 */ @@ -80,7 +81,7 @@ public class SimpleVersionControl { } /** - * 设置目标版本 + * 设置目标版本 */ public SimpleVersionControl to(String to) { this.to = to.startsWith("v") ? to : "v" + to; @@ -116,15 +117,16 @@ public class SimpleVersionControl { } public Class translate(Plugin plugin) throws IOException { - InputStream inputStream; - if (useCache && cacheClasses.containsKey(target)) { - inputStream = new ByteArrayInputStream(cacheClasses.get(target)); - } else { - inputStream = Files.getResource(plugin, target.replace(".", "/") + ".class"); - if (useCache) { - cacheClasses.put(target, IO.readFully(inputStream)); + // 防止出现 Class not found 的奇葩问题,使用缓存(目的是兼容热重载) + InputStream inputStream = useCache ? new ByteArrayInputStream(cacheClasses.computeIfAbsent(target, n -> { + try { + return IO.readFully(Files.getResource(plugin, target.replace(".", "/") + ".class")); + } catch (IOException e) { + e.printStackTrace(); } - } + return new byte[0]; + })) : Files.getResource(plugin, target.replace(".", "/") + ".class"); + // 读取 ClassReader classReader = new ClassReader(inputStream); ClassWriter classWriter = new ClassWriter(0); ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter); @@ -132,7 +134,7 @@ public class SimpleVersionControl { classWriter.visitEnd(); classVisitor.visitEnd(); // 因第三方插件调用该方法时会出现找不到类,所以第三方插件使用 BridgeLoader 加载类 - return plugin instanceof InternalPlugin ? AsmClassLoader.createNewClass(target, classWriter.toByteArray()) : BridgeLoader.createNewClass(target, classWriter.toByteArray()); + return plugin instanceof InternalPlugin ? AsmClassLoader.createNewClass(target, classWriter.toByteArray()) : BridgeLoader.createNewClass(target, classWriter.toByteArray()); } public Class translateBridge() throws IOException { diff --git a/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawCreator.java b/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawCreator.java index a792500..ff01df5 100644 --- a/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawCreator.java +++ b/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawCreator.java @@ -1,6 +1,7 @@ package io.izzel.taboolib.module.tellraw; import io.izzel.taboolib.module.inject.TFunction; +import io.izzel.taboolib.module.inject.TSchedule; import io.izzel.taboolib.module.lite.SimpleVersionControl; import io.izzel.taboolib.module.tellraw.internal.AbstractTellraw; import org.bukkit.Bukkit; @@ -17,8 +18,6 @@ public class TellrawCreator { private static boolean protocolSupportLoaded; public static void init() { - viaVersionLoaded = Bukkit.getPluginManager().getPlugin("ViaVersion") != null; - protocolSupportLoaded = Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null; try { abstractTellraw = (AbstractTellraw) SimpleVersionControl.createNMS("io.izzel.taboolib.module.tellraw.internal.InternalTellraw").translate().newInstance(); } catch (Exception e) { @@ -26,6 +25,12 @@ public class TellrawCreator { } } + @TSchedule + static void tick() { + viaVersionLoaded = Bukkit.getPluginManager().getPlugin("ViaVersion") != null; + protocolSupportLoaded = Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null; + } + // ********************************* // // Getter and Setter diff --git a/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawJson.java b/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawJson.java index 3ce5607..918a79a 100644 --- a/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawJson.java +++ b/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawJson.java @@ -1,5 +1,6 @@ package io.izzel.taboolib.module.tellraw; +import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.util.ArrayUtil; import io.izzel.taboolib.util.Strings; @@ -8,8 +9,6 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import protocolsupport.api.ProtocolSupportAPI; -import us.myles.ViaVersion.api.Via; import java.util.*; import java.util.stream.Collectors; @@ -88,11 +87,11 @@ public class TellrawJson { public String toRawMessage(Player player) { // ViaVersion support! if (TellrawCreator.isViaVersionLoaded()) { - return toRawMessage(Via.getAPI().getPlayerVersion(player) > 316 ? TellrawVersion.HIGH_VERSION : TellrawVersion.LOW_VERSION); + return toRawMessage(TabooLibAPI.getPluginBridge().viaVersionPlayerVersion(player) > 316 ? TellrawVersion.HIGH_VERSION : TellrawVersion.LOW_VERSION); } // ProtocolSupport support! else if (TellrawCreator.isProtocolSupportLoaded()) { - return toRawMessage(ProtocolSupportAPI.getProtocolVersion(player).getId() > 316 ? TellrawVersion.HIGH_VERSION : TellrawVersion.LOW_VERSION); + return toRawMessage(TabooLibAPI.getPluginBridge().protocolSupportPlayerVersion(player) > 316 ? TellrawVersion.HIGH_VERSION : TellrawVersion.LOW_VERSION); } return toRawMessage(); }