From b6c154e9146660edb3616c4c1d3033c8c8d5a8b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sun, 18 Aug 2019 01:13:30 +0800 Subject: [PATCH] update 5.03 beta 23 --- .../common/plugin/InternalPluginBridge.java | 3 -- .../common/plugin/bridge/BridgeImpl.java | 27 ++++++------ .../taboolib/module/ai/SimpleAiSelector.java | 21 ++------- .../taboolib/module/inject/TInjectAsm.java | 43 +++++++++++++++++++ .../taboolib/module/inject/TInjectLoader.java | 4 +- .../io/izzel/taboolib/module/nms/NMS.java | 13 +----- .../module/packet/TPacketHandler.java | 15 ++----- .../module/tellraw/TellrawCreator.java | 19 +------- 8 files changed, 68 insertions(+), 77 deletions(-) create mode 100644 src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java 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 cef32aa..e817169 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java @@ -1,6 +1,5 @@ package io.izzel.taboolib.common.plugin; -import com.sk89q.worldguard.protection.managers.RegionManager; import io.izzel.taboolib.module.lite.SimpleVersionControl; import org.bukkit.Location; import org.bukkit.OfflinePlayer; @@ -52,8 +51,6 @@ public abstract class InternalPluginBridge { abstract public boolean permissionHas(Player player, String perm); - abstract public RegionManager worldguardRegionManager(World world); - abstract public Collection worldguardGetRegions(World world); abstract public List worldguardGetRegion(World world, Location location); 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 099afc0..c469e56 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 @@ -128,20 +128,6 @@ public class BridgeImpl extends InternalPluginBridge { return worldguardRegionManager(world).getRegions().values().stream().filter(r -> r.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ())).map(ProtectedRegion::getId).collect(Collectors.toList()); } - @Override - public RegionManager worldguardRegionManager(World world) { - if (WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) { - return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); - } else { - try { - return (RegionManager) getRegionManager.invoke(WorldGuardPlugin.inst(), world); - } catch (Throwable t) { - t.printStackTrace(); - } - return null; - } - } - @Override public boolean economyHooked() { return economy != null; @@ -237,4 +223,17 @@ public class BridgeImpl extends InternalPluginBridge { public Class getClass(String name) throws ClassNotFoundException { return Class.forName(name); } + + private RegionManager worldguardRegionManager(World world) { + if (WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) { + return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); + } else { + try { + return (RegionManager) getRegionManager.invoke(WorldGuardPlugin.inst(), world); + } catch (Throwable t) { + t.printStackTrace(); + } + return null; + } + } } \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/ai/SimpleAiSelector.java b/src/main/scala/io/izzel/taboolib/module/ai/SimpleAiSelector.java index 832ae83..b3381ce 100644 --- a/src/main/scala/io/izzel/taboolib/module/ai/SimpleAiSelector.java +++ b/src/main/scala/io/izzel/taboolib/module/ai/SimpleAiSelector.java @@ -1,33 +1,18 @@ package io.izzel.taboolib.module.ai; -import io.izzel.taboolib.module.inject.TFunction; -import io.izzel.taboolib.module.lite.SimpleVersionControl; +import io.izzel.taboolib.module.inject.TInject; /** * @Author sky * @Since 2018-09-19 20:31 */ -@TFunction(enable = "init") public class SimpleAiSelector { + @TInject(asm = "io.izzel.taboolib.module.ai.internal.InternalPathfinderCreator") private static PathfinderCreator internalPathfinderCreator; + @TInject(asm = "io.izzel.taboolib.module.ai.internal.InternalPathfinderExecutor") private static PathfinderExecutor internalPathfinderExecutor; - static void init() { - try { - internalPathfinderCreator = (PathfinderCreator) SimpleVersionControl.createNMS("io.izzel.taboolib.module.ai.internal.InternalPathfinderCreator").translate().newInstance(); - internalPathfinderExecutor = (PathfinderExecutor) SimpleVersionControl.createNMS("io.izzel.taboolib.module.ai.internal.InternalPathfinderExecutor").translate().newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - // ********************************* - // - // Getter and Setter - // - // ********************************* - public static PathfinderExecutor getExecutor() { return internalPathfinderExecutor; } diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java b/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java new file mode 100644 index 0000000..7e2231f --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java @@ -0,0 +1,43 @@ +package io.izzel.taboolib.module.inject; + +import io.izzel.taboolib.TabooLibLoader; +import io.izzel.taboolib.module.lite.SimpleVersionControl; +import io.izzel.taboolib.module.locale.logger.TLogger; +import org.bukkit.plugin.Plugin; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +/** + * @Author sky + * @Since 2019-08-18 0:47 + */ +public class TInjectAsm implements TabooLibLoader.Loader { + + @Override + public void postLoad(Plugin plugin, Class pluginClass) { + for (Field declaredField : pluginClass.getDeclaredFields()) { + TInject annotation = declaredField.getAnnotation(TInject.class); + if (annotation == null || annotation.asm().isEmpty()) { + continue; + } + Object instance = null; + // 如果是非静态类型 + if (!Modifier.isStatic(declaredField.getModifiers())) { + // 是否为主类 + if (pluginClass.equals(plugin.getClass())) { + instance = plugin; + } else { + TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")"); + continue; + } + } + try { + declaredField.setAccessible(true); + declaredField.set(instance, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance()); + } catch (Throwable t) { + t.printStackTrace(); + } + } + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java b/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java index 79f49aa..f0c568e 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java @@ -148,8 +148,6 @@ public class TInjectLoader implements TabooLibLoader.Loader { TInjectTask tInjectTask = injectTypes.get(declaredField.getType()); if (tInjectTask != null) { inject(plugin, declaredField, instance, annotation, tInjectTask, pluginClass); - } else if (annotation.state() == TInject.State.NONE) { - TLogger.getGlobalLogger().error(declaredField.getName() + " is an invalid inject type. (" + pluginClass.getName() + ")"); } } } @@ -160,7 +158,7 @@ public class TInjectLoader implements TabooLibLoader.Loader { injectTask.run(plugin, field, annotation, pluginClass, instance); TabooLibAPI.debug(field.getName() + " injected. (" + field.getType().getName() + ")"); } catch (Throwable e) { - TLogger.getGlobalLogger().error(field.getName() + " inject failed: " + e.getMessage() + " (" + field.getType().getName() + ")"); + TLogger.getGlobalLogger().error(field.getName() + " inject failed: " + e.getMessage() + " (" + field.getName() + ")"); if (e.getMessage() == null) { e.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/module/nms/NMS.java b/src/main/scala/io/izzel/taboolib/module/nms/NMS.java index f3dc202..07154b2 100644 --- a/src/main/scala/io/izzel/taboolib/module/nms/NMS.java +++ b/src/main/scala/io/izzel/taboolib/module/nms/NMS.java @@ -1,7 +1,6 @@ package io.izzel.taboolib.module.nms; -import io.izzel.taboolib.module.inject.TFunction; -import io.izzel.taboolib.module.lite.SimpleVersionControl; +import io.izzel.taboolib.module.inject.TInject; import io.izzel.taboolib.module.nms.nbt.NBTCompound; import org.bukkit.Particle; import org.bukkit.entity.Entity; @@ -12,23 +11,15 @@ import org.bukkit.inventory.ItemStack; * @Author 坏黑 * @Since 2018-11-09 14:38 */ -@TFunction(enable = "init") public abstract class NMS { + @TInject(asm = "io.izzel.taboolib.module.nms.NMSImpl") private static NMS impl; public static NMS handle() { return impl; } - static void init() { - try { - impl = (NMS) SimpleVersionControl.createNMS("io.izzel.taboolib.module.nms.NMSImpl").translate().newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } - abstract public boolean isRunning(); abstract public Object toPacketPlayOutWorldParticles(Particle var1, boolean var2, float var3, float var4, float var5, float var6, float var7, float var8, float var9, int var10, Object var11); diff --git a/src/main/scala/io/izzel/taboolib/module/packet/TPacketHandler.java b/src/main/scala/io/izzel/taboolib/module/packet/TPacketHandler.java index 4dee6a7..9ffc3a4 100644 --- a/src/main/scala/io/izzel/taboolib/module/packet/TPacketHandler.java +++ b/src/main/scala/io/izzel/taboolib/module/packet/TPacketHandler.java @@ -2,9 +2,9 @@ package io.izzel.taboolib.module.packet; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import io.izzel.taboolib.module.packet.channel.ChannelExecutor; -import io.izzel.taboolib.module.lite.SimpleVersionControl; +import io.izzel.taboolib.module.inject.TInject; import io.izzel.taboolib.module.inject.TListener; +import io.izzel.taboolib.module.packet.channel.ChannelExecutor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -25,16 +25,9 @@ import java.util.Optional; @TListener public class TPacketHandler implements Listener { - private static Map> packetListeners = Maps.newHashMap(); + @TInject(asm = "io.izzel.taboolib.module.packet.channel.InternalChannelExecutor") private static ChannelExecutor channelExecutor; - - public TPacketHandler() { - try { - channelExecutor = (ChannelExecutor) SimpleVersionControl.createNMS("io.izzel.taboolib.module.packet.channel.InternalChannelExecutor").translate().newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } + private static Map> packetListeners = Maps.newHashMap(); @EventHandler public void onJoin(PlayerJoinEvent e) { 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 ff01df5..15b4c5a 100644 --- a/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawCreator.java +++ b/src/main/scala/io/izzel/taboolib/module/tellraw/TellrawCreator.java @@ -1,8 +1,7 @@ package io.izzel.taboolib.module.tellraw; -import io.izzel.taboolib.module.inject.TFunction; +import io.izzel.taboolib.module.inject.TInject; 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; @@ -10,33 +9,19 @@ import org.bukkit.Bukkit; * @Author 坏黑 * @Since 2018-11-07 22:58 */ -@TFunction(enable = "init") public class TellrawCreator { + @TInject(asm = "io.izzel.taboolib.module.tellraw.internal.InternalTellraw") private static AbstractTellraw abstractTellraw; private static boolean viaVersionLoaded; private static boolean protocolSupportLoaded; - public static void init() { - try { - abstractTellraw = (AbstractTellraw) SimpleVersionControl.createNMS("io.izzel.taboolib.module.tellraw.internal.InternalTellraw").translate().newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } - @TSchedule static void tick() { viaVersionLoaded = Bukkit.getPluginManager().getPlugin("ViaVersion") != null; protocolSupportLoaded = Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null; } - // ********************************* - // - // Getter and Setter - // - // ********************************* - public static AbstractTellraw getAbstractTellraw() { return abstractTellraw; }