From a228ba5fe5c8bf05d077ca6ef33bfcee52dd641d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Fri, 9 Nov 2018 16:41:22 +0800 Subject: [PATCH] + optimize SimpleVersionControl + optimize TFunction + optimize TLocale + add TLocale.Display --- TabooLib.iml | 2 +- .../com/ilummc/tlib/resources/TLocale.java | 38 +++++++++++----- .../tlib/resources/type/TLocaleActionBar.java | 3 +- .../tlib/resources/type/TLocaleTitle.java | 2 +- .../me/skymc/taboolib/cloud/TCloudLoader.java | 4 +- .../common/function/TFunctionLoader.java | 22 ++++++--- .../common/loader/InstantiableLoader.java | 2 + .../skymc/taboolib/common/nms/NMSHandler.java | 31 +++++++++++++ .../taboolib/common/nms/NMSHandlerImpl.java | 33 ++++++++++++++ .../common/packet/TPacketHandler.java | 6 +-- .../common/pathfinder/SimpleAiSelector.java | 14 ++---- .../versioncontrol/SimpleVersionControl.java | 45 +++++++++++++++++-- .../taboolib/itagapi/TagDataHandler.java | 3 +- .../taboolib/javascript/ScriptHandler.java | 2 +- .../taboolib/json/tellraw/TellrawCreator.java | 6 +-- .../me/skymc/taboolib/sound/SoundUtils.java | 2 +- 16 files changed, 164 insertions(+), 51 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/common/nms/NMSHandler.java create mode 100644 src/main/java/me/skymc/taboolib/common/nms/NMSHandlerImpl.java diff --git a/TabooLib.iml b/TabooLib.iml index abc9fff..6b0e5b3 100644 --- a/TabooLib.iml +++ b/TabooLib.iml @@ -1,4 +1,4 @@ - + diff --git a/src/main/java/com/ilummc/tlib/resources/TLocale.java b/src/main/java/com/ilummc/tlib/resources/TLocale.java index 8f3f115..12175fe 100644 --- a/src/main/java/com/ilummc/tlib/resources/TLocale.java +++ b/src/main/java/com/ilummc/tlib/resources/TLocale.java @@ -9,6 +9,7 @@ import com.ilummc.tlib.util.Ref; import com.ilummc.tlib.util.Strings; import me.clip.placeholderapi.PlaceholderAPI; import me.skymc.taboolib.Main; +import me.skymc.taboolib.common.nms.NMSHandler; import me.skymc.taboolib.json.tellraw.TellrawCreator; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -74,6 +75,32 @@ public class TLocale { Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(Ref.getCallerPlugin(clazz), false)); } + public static final class Tellraw extends TLocale { + + public static void send(CommandSender sender, String rawMessage) { + if (sender instanceof Player) { + TellrawCreator.getAbstractTellraw().sendRawMessage((Player) sender, rawMessage); + } else { + sender.sendMessage(TextComponent.toLegacyText(ComponentSerializer.parse(rawMessage))); + } + } + } + + public static final class Display extends TLocale { + + public static void sendTitle(Player player, String title, String subTitle) { + sendTitle(player, title, subTitle, 10, 20, 10); + } + + public static void sendTitle(Player player, String title, String subTitle, int fadein, int stay, int fadeout) { + NMSHandler.getHandler().sendTitle(player, title, fadein, stay, fadeout, subTitle, fadein, stay, fadeout); + } + + public static void sendActionBar(Player player, String text) { + NMSHandler.getHandler().sendActionBar(player, text); + } + } + public static final class Translate extends TLocale { public static boolean isPlaceholderUseDefault() { @@ -131,15 +158,4 @@ public class TLocale { Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).verbose(locale))); } } - - public static class Tellraw extends TLocale { - - public static void send(CommandSender sender, String rawMessage) { - if (sender instanceof Player) { - TellrawCreator.getAbstractTellraw().sendRawMessage((Player) sender, rawMessage); - } else { - sender.sendMessage(TextComponent.toLegacyText(ComponentSerializer.parse(rawMessage))); - } - } - } } diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleActionBar.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleActionBar.java index 70f3f3d..ec7296c 100644 --- a/src/main/java/com/ilummc/tlib/resources/type/TLocaleActionBar.java +++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleActionBar.java @@ -2,6 +2,7 @@ package com.ilummc.tlib.resources.type; import com.google.common.collect.Maps; import com.ilummc.tlib.compat.PlaceholderHook; +import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.resources.TLocaleSerialize; import com.ilummc.tlib.util.Strings; import me.skymc.taboolib.display.ActionUtils; @@ -33,7 +34,7 @@ public class TLocaleActionBar extends TLocaleSerialize { @Override public void sendTo(CommandSender sender, String... args) { if (sender instanceof Player) { - ActionUtils.send(((Player) sender), replace(sender, text, args)); + TLocale.Display.sendActionBar(((Player) sender), replace(sender, text, args)); } } diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java index fb90a40..1b1a710 100644 --- a/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java +++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java @@ -52,7 +52,7 @@ public class TLocaleTitle extends TLocaleSerialize { @Override public void sendTo(CommandSender sender, String... args) { if (sender instanceof Player) { - TitleUtils.sendTitle((Player) sender, replaceText(sender, Strings.replaceWithOrder(title, args)), replaceText(sender, Strings.replaceWithOrder(subtitle, args)), fadein, stay, fadeout); + TLocale.Display.sendTitle((Player) sender, replaceText(sender, Strings.replaceWithOrder(title, args)), replaceText(sender, Strings.replaceWithOrder(subtitle, args)), fadein, stay, fadeout); } else { TLocale.Logger.error("LOCALE.TITLE-SEND-TO-NON-PLAYER", asString(args)); } diff --git a/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java b/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java index 4ac2f6b..38fdcf4 100644 --- a/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java +++ b/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java @@ -20,7 +20,7 @@ import java.util.Map; * @Author sky * @Since 2018-09-30 17:30 */ -@TFunction +@TFunction(enable = "init") public class TCloudLoader { private static String url = "https://gitee.com/bkm016/TabooLibCloud/raw/master/cloud.json"; @@ -30,7 +30,7 @@ public class TCloudLoader { private static Map expansionInternal = Maps.newHashMap(); private static File expansionInternalFolder; - public void onEnable() { + static void init() { createFolder(); refresh(); } diff --git a/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java b/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java index 4da0219..95b517c 100644 --- a/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java +++ b/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java @@ -1,7 +1,9 @@ package me.skymc.taboolib.common.function; +import com.ilummc.tlib.logger.TLogger; import me.skymc.taboolib.TabooLibLoader; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; import java.lang.reflect.Method; @@ -17,9 +19,13 @@ public class TFunctionLoader implements TabooLibLoader.Loader { TFunction function = pluginClass.getAnnotation(TFunction.class); try { Method method = pluginClass.getDeclaredMethod(function.enable()); - method.setAccessible(true); - method.invoke(pluginClass.newInstance()); - } catch (Exception ignored) { + if (method != null) { + method.setAccessible(true); + method.invoke(null); + } + } catch (Exception e) { + TLogger.getGlobalLogger().warn("TFunction load Failed: " + pluginClass.getName()); + e.printStackTrace(); } } } @@ -30,9 +36,13 @@ public class TFunctionLoader implements TabooLibLoader.Loader { TFunction function = pluginClass.getAnnotation(TFunction.class); try { Method method = pluginClass.getDeclaredMethod(function.disable()); - method.setAccessible(true); - method.invoke(pluginClass.newInstance()); - } catch (Exception ignored) { + if (method != null) { + method.setAccessible(true); + method.invoke(null); + } + } catch (Exception e) { + TLogger.getGlobalLogger().warn("TFunction unload Failed: " + pluginClass.getName()); + e.printStackTrace(); } } } diff --git a/src/main/java/me/skymc/taboolib/common/loader/InstantiableLoader.java b/src/main/java/me/skymc/taboolib/common/loader/InstantiableLoader.java index 62ae29b..95a906c 100644 --- a/src/main/java/me/skymc/taboolib/common/loader/InstantiableLoader.java +++ b/src/main/java/me/skymc/taboolib/common/loader/InstantiableLoader.java @@ -1,5 +1,6 @@ package me.skymc.taboolib.common.loader; +import com.ilummc.tlib.logger.TLogger; import com.ilummc.tlib.util.Ref; import me.skymc.taboolib.TabooLibLoader; import me.skymc.taboolib.listener.TListener; @@ -59,6 +60,7 @@ public class InstantiableLoader implements Listener { try { instance.put(instantiable.value(), ReflectionUtils.instantiateObject(pluginClass)); } catch (Exception e) { + TLogger.getGlobalLogger().warn("Instance Failed: " + pluginClass.getName()); e.printStackTrace(); } } diff --git a/src/main/java/me/skymc/taboolib/common/nms/NMSHandler.java b/src/main/java/me/skymc/taboolib/common/nms/NMSHandler.java new file mode 100644 index 0000000..1373f2b --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/nms/NMSHandler.java @@ -0,0 +1,31 @@ +package me.skymc.taboolib.common.nms; + +import me.skymc.taboolib.common.function.TFunction; +import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl; +import org.bukkit.entity.Player; + +/** + * @Author 坏黑 + * @Since 2018-11-09 14:38 + */ +@TFunction(enable = "init") +public abstract class NMSHandler { + + private static NMSHandler handler; + + static void init() { + try { + handler = (NMSHandler) SimpleVersionControl.createNMS("me.skymc.taboolib.common.nms.NMSHandlerImpl").translate().newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + abstract public void sendTitle(Player player, String title, int titleFadein, int titleStay, int titleFadeout, String subtitle, int subtitleFadein, int subtitleStay, int subtitleFadeout); + + abstract public void sendActionBar(Player player, String text); + + public static NMSHandler getHandler() { + return handler; + } +} diff --git a/src/main/java/me/skymc/taboolib/common/nms/NMSHandlerImpl.java b/src/main/java/me/skymc/taboolib/common/nms/NMSHandlerImpl.java new file mode 100644 index 0000000..e954c0c --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/nms/NMSHandlerImpl.java @@ -0,0 +1,33 @@ +package me.skymc.taboolib.common.nms; + +import me.skymc.taboolib.TabooLib; +import me.skymc.taboolib.common.packet.TPacketHandler; +import net.minecraft.server.v1_12_R1.ChatMessageType; +import net.minecraft.server.v1_8_R3.ChatComponentText; +import net.minecraft.server.v1_8_R3.PacketPlayOutChat; +import net.minecraft.server.v1_8_R3.PacketPlayOutTitle; +import org.bukkit.entity.Player; + +/** + * @Author 坏黑 + * @Since 2018-11-09 14:42 + */ +public class NMSHandlerImpl extends NMSHandler { + + @Override + public void sendTitle(Player player, String title, int titleFadein, int titleStay, int titleFadeout, String subtitle, int subtitleFadein, int subtitleStay, int subtitleFadeout) { + TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, new ChatComponentText(String.valueOf(title)), titleFadein, titleStay, titleFadeout)); + TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, new ChatComponentText(String.valueOf(title)))); + TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, new ChatComponentText(String.valueOf(subtitle)), subtitleFadein, subtitleStay, subtitleFadeout)); + TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, new ChatComponentText(String.valueOf(subtitle)))); + } + + @Override + public void sendActionBar(Player player, String text) { + if (TabooLib.getVersionNumber() > 11100) { + TPacketHandler.sendPacket(player, new net.minecraft.server.v1_12_R1.PacketPlayOutChat(new net.minecraft.server.v1_12_R1.ChatComponentText(String.valueOf(text)), ChatMessageType.GAME_INFO)); + } else { + TPacketHandler.sendPacket(player, new PacketPlayOutChat(new ChatComponentText(String.valueOf(text)), (byte) 2)); + } + } +} diff --git a/src/main/java/me/skymc/taboolib/common/packet/TPacketHandler.java b/src/main/java/me/skymc/taboolib/common/packet/TPacketHandler.java index 8f957ec..5a00d14 100644 --- a/src/main/java/me/skymc/taboolib/common/packet/TPacketHandler.java +++ b/src/main/java/me/skymc/taboolib/common/packet/TPacketHandler.java @@ -30,11 +30,7 @@ public class TPacketHandler implements Listener { public TPacketHandler() { try { - channelExecutor = (ChannelExecutor) SimpleVersionControl.create() - .from("v1_8_R3") - .target("me.skymc.taboolib.common.packet.channel.InternalChannelExecutor") - .translate() - .newInstance(); + channelExecutor = (ChannelExecutor) SimpleVersionControl.createNMS("me.skymc.taboolib.common.packet.channel.InternalChannelExecutor").translate().newInstance(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAiSelector.java b/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAiSelector.java index ea74712..6a45ce2 100644 --- a/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAiSelector.java +++ b/src/main/java/me/skymc/taboolib/common/pathfinder/SimpleAiSelector.java @@ -1,7 +1,7 @@ package me.skymc.taboolib.common.pathfinder; -import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl; import me.skymc.taboolib.common.loader.Instantiable; +import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl; /** * @Author sky @@ -15,16 +15,8 @@ public class SimpleAiSelector { public SimpleAiSelector() { try { - internalPathfinderCreator = (PathfinderCreator) SimpleVersionControl.create() - .from("1_8_R3") - .target("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderCreator") - .translate() - .newInstance(); - internalPathfinderExecutor = (PathfinderExecutor) SimpleVersionControl.create() - .from("1_8_R3") - .target("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderExecutor") - .translate() - .newInstance(); + internalPathfinderCreator = (PathfinderCreator) SimpleVersionControl.createNMS("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderCreator").translate().newInstance(); + internalPathfinderExecutor = (PathfinderExecutor) SimpleVersionControl.createNMS("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderExecutor").translate().newInstance(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java index 8f4444d..23c80c6 100644 --- a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java @@ -11,6 +11,7 @@ import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -27,6 +28,7 @@ public class SimpleVersionControl { private List from = Lists.newArrayList(); private Plugin plugin; private boolean useCache; + private boolean useNMS; SimpleVersionControl() { useCache = false; @@ -40,36 +42,69 @@ public class SimpleVersionControl { return new SimpleVersionControl().to(toVersion).plugin(Main.getInst()); } - public SimpleVersionControl target(Class target) { - this.target = target.getName(); - return this; + public static SimpleVersionControl createSimple(String target, String... from) { + return create().target(target).from(from); } + public static SimpleVersionControl createNMS(String target) { + return create().target(target).useNMS(); + } + + /** + * 设置转换类地址,写法如:me.skymc.taboolib.packet.InternalPacket + */ public SimpleVersionControl target(String target) { this.target = target; return this; } + /** + * 设置原版本,写法如:v1_8_R3 + */ public SimpleVersionControl from(String from) { this.from.add(from.startsWith("v") ? from : "v" + from); return this; } + /** + * 设置原版本,写法如:v1_8_R3, v1_12_R1 + */ + public SimpleVersionControl from(String... from) { + Arrays.stream(from).forEach(v -> this.from.add(v.startsWith("v") ? v : "v" + v)); + return this; + } + /** + * 设置目标版本 + */ public SimpleVersionControl to(String to) { this.to = to.startsWith("v") ? to : "v" + to; return this; } + /** + * 设置插件,不填默认指向 TabooLib + */ public SimpleVersionControl plugin(Plugin plugin) { this.plugin = plugin; return this; } + /** + * 转换类将会保存在 TabooLib 中,防止出现 NoClassDefFoundError 异常 + */ public SimpleVersionControl useCache() { this.useCache = true; return this; } + /** + * 自动转换所有使用到的 NMS 或 OBC 方法 + */ + public SimpleVersionControl useNMS() { + this.useNMS = true; + return this; + } + public Class translate() throws IOException { return translate(plugin); } @@ -110,10 +145,12 @@ public class SimpleVersionControl { } public String replace(String origin) { + if (useNMS) { + origin = origin.replaceAll("net/minecraft/server/.*?/", "net/minecraft/server/" + to + "/").replaceAll("org/bukkit/craftbukkit/.*?/", "org/bukkit/craftbukkit/" + to + "/"); + } for (String from : from) { origin = origin.replace("/" + from + "/", "/" + to + "/"); } return origin; } - } \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/itagapi/TagDataHandler.java b/src/main/java/me/skymc/taboolib/itagapi/TagDataHandler.java index 69329ac..8146a42 100644 --- a/src/main/java/me/skymc/taboolib/itagapi/TagDataHandler.java +++ b/src/main/java/me/skymc/taboolib/itagapi/TagDataHandler.java @@ -13,7 +13,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; @@ -31,7 +30,7 @@ public class TagDataHandler implements Listener { private static TagDataHandler handler; private HashMap playersData = new HashMap<>(); - public static void init() { + static void init() { Preconditions.checkArgument(handler == null, "TagDataHandler is already instanced!"); handler = new TagDataHandler(); // 注册监听 diff --git a/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java b/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java index abf9342..3c61d93 100644 --- a/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java +++ b/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java @@ -21,7 +21,7 @@ public class ScriptHandler { private static ScriptEngine scriptEngine; private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); - public static void init() { + static void init() { try { NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null); scriptEngine = Objects.requireNonNull(factory).getScriptEngine("-doe", "--global-per-engine"); diff --git a/src/main/java/me/skymc/taboolib/json/tellraw/TellrawCreator.java b/src/main/java/me/skymc/taboolib/json/tellraw/TellrawCreator.java index d634aeb..d65397c 100644 --- a/src/main/java/me/skymc/taboolib/json/tellraw/TellrawCreator.java +++ b/src/main/java/me/skymc/taboolib/json/tellraw/TellrawCreator.java @@ -20,11 +20,7 @@ public class TellrawCreator { viaVersionLoaded = Bukkit.getPluginManager().getPlugin("ViaVersion") != null; protocolSupportLoaded = Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null; try { - abstractTellraw = (AbstractTellraw) SimpleVersionControl.create() - .from("v1_8_R3") - .target("me.skymc.taboolib.json.tellraw.internal.InternalTellraw") - .translate() - .newInstance(); + abstractTellraw = (AbstractTellraw) SimpleVersionControl.createNMS("me.skymc.taboolib.json.tellraw.internal.InternalTellraw").translate().newInstance(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/me/skymc/taboolib/sound/SoundUtils.java b/src/main/java/me/skymc/taboolib/sound/SoundUtils.java index de67dc8..2c51e12 100644 --- a/src/main/java/me/skymc/taboolib/sound/SoundUtils.java +++ b/src/main/java/me/skymc/taboolib/sound/SoundUtils.java @@ -16,7 +16,7 @@ public class SoundUtils { } } - static String getModifiedSound(String str) { + public static String getModifiedSound(String str) { if (TabooLib.getVerint() < 10900) { str = str.replace("BLOCK_FIRE_EXTINGUISH", "FIZZ"); str = str.replace("BLOCK_NOTE_HAT", "NOTE_STICKS");