From a643453c88b52531ad49647b2a4ffc5c79e66702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sun, 18 Aug 2019 15:07:39 +0800 Subject: [PATCH] update 5.04 --- build.gradle | 2 +- .../io/izzel/taboolib/TabooLibLoader.java | 30 +++++++--- .../common/event/PlayerJumpEvent.java | 9 ++- .../common/listener/ListenerPlayerJump.java | 58 ++++++++----------- .../module/command/TCommandHandler.java | 2 +- .../taboolib/module/inject/TFunction.java | 18 ++++++ .../module/inject/TFunctionLoader.java | 50 +++++++++------- .../taboolib/module/inject/TInjectLoader.java | 23 ++++++++ .../taboolib/module/lite/SimpleI18n.java | 2 +- .../io/izzel/taboolib/util/lite/Scripts.java | 4 +- .../taboolib/util/lite/cooldown/Cooldown.java | 4 ++ .../taboolib/util/tag/TagDataHandler.java | 2 - 12 files changed, 133 insertions(+), 71 deletions(-) diff --git a/build.gradle b/build.gradle index 2a411f8..5544048 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.4' } group = 'me.skymc' -version = '5.03' +version = '5.04' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/io/izzel/taboolib/TabooLibLoader.java b/src/main/scala/io/izzel/taboolib/TabooLibLoader.java index d51546d..b14337e 100644 --- a/src/main/scala/io/izzel/taboolib/TabooLibLoader.java +++ b/src/main/scala/io/izzel/taboolib/TabooLibLoader.java @@ -26,7 +26,7 @@ public class TabooLibLoader { static Map> pluginClasses = Maps.newHashMap(); static List loaders = Lists.newArrayList(); - static List runnables = Lists.newArrayList(); + static List tasks = Lists.newArrayList(); static boolean started; static void init() { @@ -60,18 +60,30 @@ public class TabooLibLoader { return classes == null ? new ArrayList<>() : new ArrayList<>(classes); } + public static Map> getPluginClasses() { + return pluginClasses; + } + + public static List getLoaders() { + return loaders; + } + + public static List getTasks() { + return tasks; + } + + public static boolean isStarted() { + return started; + } + public static void runTask(Runnable runnable) { if (started) { runnable.run(); } else { - runnables.add(runnable); + tasks.add(runnable); } } - static boolean isLoader(Class pluginClass) { - return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass); - } - @TSchedule static void start() { PluginLoader.active(TabooLib.getPlugin()); @@ -82,7 +94,7 @@ public class TabooLibLoader { // 通讯网络客户端 TabooLibClient.init(); // 执行动作 - for (Runnable runnable : runnables) { + for (Runnable runnable : tasks) { try { runnable.run(); } catch (Throwable t) { @@ -163,6 +175,10 @@ public class TabooLibLoader { }); } + static boolean isLoader(Class pluginClass) { + return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass); + } + public interface Loader { default void preLoad(org.bukkit.plugin.Plugin plugin, Class pluginClass) { diff --git a/src/main/scala/io/izzel/taboolib/common/event/PlayerJumpEvent.java b/src/main/scala/io/izzel/taboolib/common/event/PlayerJumpEvent.java index 27700ef..ca6fed1 100644 --- a/src/main/scala/io/izzel/taboolib/common/event/PlayerJumpEvent.java +++ b/src/main/scala/io/izzel/taboolib/common/event/PlayerJumpEvent.java @@ -1,5 +1,6 @@ package io.izzel.taboolib.common.event; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; @@ -11,8 +12,7 @@ public class PlayerJumpEvent extends Event implements Cancellable { private boolean isCancelled; private Player player; - public PlayerJumpEvent(boolean b, Player player) { - this.isCancelled = false; + public PlayerJumpEvent(Player player) { this.player = player; } @@ -24,6 +24,11 @@ public class PlayerJumpEvent extends Event implements Cancellable { return this.player; } + public PlayerJumpEvent call() { + Bukkit.getPluginManager().callEvent(this); + return this; + } + @Override public boolean isCancelled() { return this.isCancelled; diff --git a/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerJump.java b/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerJump.java index 34b9521..18aa56b 100644 --- a/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerJump.java +++ b/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerJump.java @@ -1,52 +1,40 @@ package io.izzel.taboolib.common.listener; -import io.izzel.taboolib.module.inject.TListener; import io.izzel.taboolib.common.event.PlayerJumpEvent; -import org.bukkit.Bukkit; +import io.izzel.taboolib.module.inject.TInject; +import io.izzel.taboolib.module.inject.TListener; +import io.izzel.taboolib.util.lite.cooldown.Cooldown; import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerQuitEvent; - -import java.util.HashMap; @TListener public class ListenerPlayerJump implements Listener { - public HashMap cooldown = new HashMap<>(); + @TInject + private static Cooldown cooldown = new Cooldown("taboolib:jump", 350); - @EventHandler(priority = EventPriority.HIGH) - public void onJump(PlayerMoveEvent event) { - if ((!event.getPlayer().isFlying()) && (event.getPlayer().getGameMode() != GameMode.CREATIVE) && (event.getFrom().getY() + 0.5D != event.getTo().getY()) && (event.getFrom().getY() + 0.419D < event.getTo().getY())) { - Location loc = event.getFrom(); - loc.setY(event.getFrom().getY() - 1.0D); - if (loc.getBlock().getType() != Material.AIR) { - if (!this.cooldown.containsKey(event.getPlayer())) { - this.cooldown.put(event.getPlayer(), System.currentTimeMillis() + 350L); - PlayerJumpEvent evt = new PlayerJumpEvent(event.isCancelled(), event.getPlayer()); - Bukkit.getPluginManager().callEvent(evt); - if (evt.isCancelled()) { - event.setTo(event.getFrom()); - } - } else if (this.cooldown.get(event.getPlayer()) <= System.currentTimeMillis()) { - this.cooldown.put(event.getPlayer(), System.currentTimeMillis() + 350L); - PlayerJumpEvent evt = new PlayerJumpEvent(event.isCancelled(), event.getPlayer()); - Bukkit.getPluginManager().callEvent(evt); - if (evt.isCancelled()) { - event.setTo(event.getFrom()); - } - } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onJump(PlayerMoveEvent e) { + // 不是飞行 + if (!e.getPlayer().isFlying() + // 生存或冒险模式 + && (e.getPlayer().getGameMode() == GameMode.SURVIVAL || e.getPlayer().getGameMode() == GameMode.ADVENTURE) + // 坐标计算 + && (e.getFrom().getY() + 0.5D != e.getTo().getY()) + && (e.getFrom().getY() + 0.419D < e.getTo().getY()) + // 不在冷却 + && !cooldown.isCooldown(e.getPlayer().getName())) { + + PlayerJumpEvent event = new PlayerJumpEvent(e.getPlayer()).call(); + if (event.isCancelled()) { + // 返回位置 + e.setTo(e.getFrom()); + // 重置冷却 + cooldown.reset(e.getPlayer().getName()); } } } - - @EventHandler - public void onQuit(PlayerQuitEvent event) { - this.cooldown.remove(event.getPlayer()); - } } diff --git a/src/main/scala/io/izzel/taboolib/module/command/TCommandHandler.java b/src/main/scala/io/izzel/taboolib/module/command/TCommandHandler.java index 7ea8446..7eda42e 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/TCommandHandler.java +++ b/src/main/scala/io/izzel/taboolib/module/command/TCommandHandler.java @@ -24,12 +24,12 @@ import java.util.stream.Collectors; * @Author sky * @Since 2018-05-23 2:43 */ -@TFunction(enable = "init") public class TCommandHandler { private static SimpleCommandMap commandMap; private static Map knownCommands; + @TFunction.Init static void init() { SimpleReflection.saveField(SimplePluginManager.class, "commandMap"); SimpleReflection.saveField(SimpleCommandMap.class, "knownCommands"); diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TFunction.java b/src/main/scala/io/izzel/taboolib/module/inject/TFunction.java index 15905d2..aa1099d 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TFunction.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TFunction.java @@ -14,6 +14,24 @@ import java.lang.annotation.Target; public @interface TFunction { String enable() default "onEnable"; + String disable() default "onDisable"; + @Target(ElementType.METHOD) + @Retention(RetentionPolicy.RUNTIME) + @interface Load { + + } + + @Target(ElementType.METHOD) + @Retention(RetentionPolicy.RUNTIME) + @interface Init { + + } + + @Target(ElementType.METHOD) + @Retention(RetentionPolicy.RUNTIME) + @interface Cancel { + + } } \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TFunctionLoader.java b/src/main/scala/io/izzel/taboolib/module/inject/TFunctionLoader.java index 115dc33..0abe855 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TFunctionLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TFunctionLoader.java @@ -1,10 +1,10 @@ package io.izzel.taboolib.module.inject; -import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibLoader; import io.izzel.taboolib.module.locale.logger.TLogger; import org.bukkit.plugin.Plugin; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -14,44 +14,54 @@ import java.lang.reflect.Modifier; */ public class TFunctionLoader implements TabooLibLoader.Loader { + @Override + public void preLoad(Plugin plugin, Class pluginClass) { + invokeMethods(pluginClass, TFunction.Load.class); + } + @Override public void postLoad(Plugin plugin, Class pluginClass) { + invokeMethods(pluginClass, true); + invokeMethods(pluginClass, TFunction.Init.class); + } + + @Override + public void unload(Plugin plugin, Class pluginClass) { + invokeMethods(pluginClass, false); + invokeMethods(pluginClass, TFunction.Cancel.class); + } + + public void invokeMethods(Class pluginClass, boolean enable) { if (pluginClass.isAnnotationPresent(TFunction.class)) { TFunction function = pluginClass.getAnnotation(TFunction.class); try { - Method method = pluginClass.getDeclaredMethod(function.enable()); + Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable()); if (!Modifier.isStatic(method.getModifiers())) { TLogger.getGlobalLogger().error(method.getName() + " is not a static method."); return; } method.setAccessible(true); method.invoke(null); - TabooLibAPI.debug("Function " + pluginClass.getSimpleName() + " loaded. (" + plugin.getName() + ")"); } catch (NoSuchMethodException ignore) { } catch (Exception e) { - TLogger.getGlobalLogger().warn("TFunction load Failed: " + pluginClass.getName()); e.printStackTrace(); } } } - @Override - public void unload(Plugin plugin, Class pluginClass) { - if (pluginClass.isAnnotationPresent(TFunction.class)) { - TFunction function = pluginClass.getAnnotation(TFunction.class); - try { - Method method = pluginClass.getDeclaredMethod(function.disable()); - if (!Modifier.isStatic(method.getModifiers())) { - TLogger.getGlobalLogger().error(method.getName() + " is not a static method."); - return; + public void invokeMethods(Class pluginClass, Class a) { + for (Method declaredMethod : pluginClass.getDeclaredMethods()) { + if (declaredMethod.isAnnotationPresent(a)) { + try { + if (!Modifier.isStatic(declaredMethod.getModifiers())) { + TLogger.getGlobalLogger().error(declaredMethod.getName() + " is not a static method."); + return; + } + declaredMethod.setAccessible(true); + declaredMethod.invoke(null); + } catch (Throwable t) { + t.printStackTrace(); } - method.setAccessible(true); - method.invoke(null); - TabooLibAPI.debug("Function " + pluginClass.getSimpleName() + " unloaded. (" + plugin.getName() + ")"); - } catch (NoSuchMethodException ignore) { - } catch (Exception e) { - TLogger.getGlobalLogger().warn("TFunction unload Failed: " + pluginClass.getName()); - e.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 2440555..5327b3a 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TInjectLoader.java @@ -10,7 +10,9 @@ import io.izzel.taboolib.module.packet.TPacketHandler; import io.izzel.taboolib.module.packet.TPacketListener; import io.izzel.taboolib.util.lite.cooldown.Cooldown; import io.izzel.taboolib.util.lite.cooldown.Cooldowns; +import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -66,6 +68,7 @@ public class TInjectLoader implements TabooLibLoader.Loader { } }); TabooLibLoader.runTask(config::runListener); + } catch (NoSuchMethodException ignore) { } catch (Throwable t) { t.printStackTrace(); } @@ -97,6 +100,26 @@ public class TInjectLoader implements TabooLibLoader.Loader { t.printStackTrace(); } }); + // PluginExists Inject + injectTypes.put(Boolean.TYPE, (plugin, field, args, pluginClass, instance) -> { + try { + if (args.value().length > 0) { + field.set(instance, Bukkit.getPluginManager().getPlugin(args.value()[0]) != null); + } + } catch (Throwable t) { + t.printStackTrace(); + } + }); + // PluginHook Inject + injectTypes.put(JavaPlugin.class, (plugin, field, args, pluginClass, instance) -> { + try { + if (args.value().length > 0) { + field.set(instance, Bukkit.getPluginManager().getPlugin(args.value()[0])); + } + } catch (Throwable t) { + t.printStackTrace(); + } + }); } @Override diff --git a/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java b/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java index f0ee6e4..196d2ec 100644 --- a/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java +++ b/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java @@ -23,12 +23,12 @@ import java.util.Optional; * @Author 坏黑 * @Since 2019-05-22 1:16 */ -@TFunction(enable = "init") public class SimpleI18n { private static FileConfiguration lang; private static boolean released; + @TFunction.Init static void init() { File localeFile = getLocaleFile(TabooLib.getPlugin()); if (localeFile == null) { diff --git a/src/main/scala/io/izzel/taboolib/util/lite/Scripts.java b/src/main/scala/io/izzel/taboolib/util/lite/Scripts.java index 2a7bb68..bdcd953 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/Scripts.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/Scripts.java @@ -1,8 +1,8 @@ package io.izzel.taboolib.util.lite; +import io.izzel.taboolib.module.inject.TFunction; import io.izzel.taboolib.module.locale.logger.TLogger; import jdk.nashorn.api.scripting.NashornScriptEngineFactory; -import io.izzel.taboolib.module.inject.TFunction; import javax.script.Compilable; import javax.script.CompiledScript; @@ -14,12 +14,12 @@ import java.util.Objects; * @Author sky * @Since 2018-06-02 22:48 */ -@TFunction(enable = "init") public class Scripts { private static ScriptEngine scriptEngine; private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); + @TFunction.Init static void init() { try { NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null); diff --git a/src/main/scala/io/izzel/taboolib/util/lite/cooldown/Cooldown.java b/src/main/scala/io/izzel/taboolib/util/lite/cooldown/Cooldown.java index 6879292..b7ba401 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/cooldown/Cooldown.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/cooldown/Cooldown.java @@ -69,4 +69,8 @@ public class Cooldown { } return true; } + + public void reset(String player) { + data.remove(player); + } } diff --git a/src/main/scala/io/izzel/taboolib/util/tag/TagDataHandler.java b/src/main/scala/io/izzel/taboolib/util/tag/TagDataHandler.java index f9ea860..375b86d 100644 --- a/src/main/scala/io/izzel/taboolib/util/tag/TagDataHandler.java +++ b/src/main/scala/io/izzel/taboolib/util/tag/TagDataHandler.java @@ -1,7 +1,6 @@ package io.izzel.taboolib.util.tag; import io.izzel.taboolib.TabooLib; -import io.izzel.taboolib.module.inject.TFunction; import io.izzel.taboolib.module.inject.TListener; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -20,7 +19,6 @@ import java.util.UUID; * @Author sky * @Since 2018-05-23 0:37 */ -@TFunction(enable = "init") @TListener public class TagDataHandler implements Listener {