update 5.04

This commit is contained in:
坏黑 2019-08-18 15:07:39 +08:00
parent eab2835c52
commit a643453c88
12 changed files with 133 additions and 71 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4' id 'com.github.johnrengelman.shadow' version '4.0.4'
} }
group = 'me.skymc' group = 'me.skymc'
version = '5.03' version = '5.04'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
targetCompatibility = 1.8 targetCompatibility = 1.8

View File

@ -26,7 +26,7 @@ public class TabooLibLoader {
static Map<String, List<Class>> pluginClasses = Maps.newHashMap(); static Map<String, List<Class>> pluginClasses = Maps.newHashMap();
static List<Loader> loaders = Lists.newArrayList(); static List<Loader> loaders = Lists.newArrayList();
static List<Runnable> runnables = Lists.newArrayList(); static List<Runnable> tasks = Lists.newArrayList();
static boolean started; static boolean started;
static void init() { static void init() {
@ -60,18 +60,30 @@ public class TabooLibLoader {
return classes == null ? new ArrayList<>() : new ArrayList<>(classes); return classes == null ? new ArrayList<>() : new ArrayList<>(classes);
} }
public static Map<String, List<Class>> getPluginClasses() {
return pluginClasses;
}
public static List<Loader> getLoaders() {
return loaders;
}
public static List<Runnable> getTasks() {
return tasks;
}
public static boolean isStarted() {
return started;
}
public static void runTask(Runnable runnable) { public static void runTask(Runnable runnable) {
if (started) { if (started) {
runnable.run(); runnable.run();
} else { } else {
runnables.add(runnable); tasks.add(runnable);
} }
} }
static boolean isLoader(Class pluginClass) {
return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass);
}
@TSchedule @TSchedule
static void start() { static void start() {
PluginLoader.active(TabooLib.getPlugin()); PluginLoader.active(TabooLib.getPlugin());
@ -82,7 +94,7 @@ public class TabooLibLoader {
// 通讯网络客户端 // 通讯网络客户端
TabooLibClient.init(); TabooLibClient.init();
// 执行动作 // 执行动作
for (Runnable runnable : runnables) { for (Runnable runnable : tasks) {
try { try {
runnable.run(); runnable.run();
} catch (Throwable t) { } 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 { public interface Loader {
default void preLoad(org.bukkit.plugin.Plugin plugin, Class<?> pluginClass) { default void preLoad(org.bukkit.plugin.Plugin plugin, Class<?> pluginClass) {

View File

@ -1,5 +1,6 @@
package io.izzel.taboolib.common.event; package io.izzel.taboolib.common.event;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -11,8 +12,7 @@ public class PlayerJumpEvent extends Event implements Cancellable {
private boolean isCancelled; private boolean isCancelled;
private Player player; private Player player;
public PlayerJumpEvent(boolean b, Player player) { public PlayerJumpEvent(Player player) {
this.isCancelled = false;
this.player = player; this.player = player;
} }
@ -24,6 +24,11 @@ public class PlayerJumpEvent extends Event implements Cancellable {
return this.player; return this.player;
} }
public PlayerJumpEvent call() {
Bukkit.getPluginManager().callEvent(this);
return this;
}
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
return this.isCancelled; return this.isCancelled;

View File

@ -1,52 +1,40 @@
package io.izzel.taboolib.common.listener; package io.izzel.taboolib.common.listener;
import io.izzel.taboolib.module.inject.TListener;
import io.izzel.taboolib.common.event.PlayerJumpEvent; 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.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap;
@TListener @TListener
public class ListenerPlayerJump implements Listener { public class ListenerPlayerJump implements Listener {
public HashMap<Player, Long> cooldown = new HashMap<>(); @TInject
private static Cooldown cooldown = new Cooldown("taboolib:jump", 350);
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onJump(PlayerMoveEvent event) { public void onJump(PlayerMoveEvent e) {
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(); if (!e.getPlayer().isFlying()
loc.setY(event.getFrom().getY() - 1.0D); // 生存或冒险模式
if (loc.getBlock().getType() != Material.AIR) { && (e.getPlayer().getGameMode() == GameMode.SURVIVAL || e.getPlayer().getGameMode() == GameMode.ADVENTURE)
if (!this.cooldown.containsKey(event.getPlayer())) { // 坐标计算
this.cooldown.put(event.getPlayer(), System.currentTimeMillis() + 350L); && (e.getFrom().getY() + 0.5D != e.getTo().getY())
PlayerJumpEvent evt = new PlayerJumpEvent(event.isCancelled(), event.getPlayer()); && (e.getFrom().getY() + 0.419D < e.getTo().getY())
Bukkit.getPluginManager().callEvent(evt); // 不在冷却
if (evt.isCancelled()) { && !cooldown.isCooldown(e.getPlayer().getName())) {
event.setTo(event.getFrom());
} PlayerJumpEvent event = new PlayerJumpEvent(e.getPlayer()).call();
} else if (this.cooldown.get(event.getPlayer()) <= System.currentTimeMillis()) { if (event.isCancelled()) {
this.cooldown.put(event.getPlayer(), System.currentTimeMillis() + 350L); // 返回位置
PlayerJumpEvent evt = new PlayerJumpEvent(event.isCancelled(), event.getPlayer()); e.setTo(e.getFrom());
Bukkit.getPluginManager().callEvent(evt); // 重置冷却
if (evt.isCancelled()) { cooldown.reset(e.getPlayer().getName());
event.setTo(event.getFrom());
}
}
} }
} }
} }
@EventHandler
public void onQuit(PlayerQuitEvent event) {
this.cooldown.remove(event.getPlayer());
}
} }

View File

@ -24,12 +24,12 @@ import java.util.stream.Collectors;
* @Author sky * @Author sky
* @Since 2018-05-23 2:43 * @Since 2018-05-23 2:43
*/ */
@TFunction(enable = "init")
public class TCommandHandler { public class TCommandHandler {
private static SimpleCommandMap commandMap; private static SimpleCommandMap commandMap;
private static Map<String, Command> knownCommands; private static Map<String, Command> knownCommands;
@TFunction.Init
static void init() { static void init() {
SimpleReflection.saveField(SimplePluginManager.class, "commandMap"); SimpleReflection.saveField(SimplePluginManager.class, "commandMap");
SimpleReflection.saveField(SimpleCommandMap.class, "knownCommands"); SimpleReflection.saveField(SimpleCommandMap.class, "knownCommands");

View File

@ -14,6 +14,24 @@ import java.lang.annotation.Target;
public @interface TFunction { public @interface TFunction {
String enable() default "onEnable"; String enable() default "onEnable";
String disable() default "onDisable"; 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 {
}
} }

View File

@ -1,10 +1,10 @@
package io.izzel.taboolib.module.inject; package io.izzel.taboolib.module.inject;
import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.TabooLibLoader; import io.izzel.taboolib.TabooLibLoader;
import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.logger.TLogger;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -14,44 +14,54 @@ import java.lang.reflect.Modifier;
*/ */
public class TFunctionLoader implements TabooLibLoader.Loader { public class TFunctionLoader implements TabooLibLoader.Loader {
@Override
public void preLoad(Plugin plugin, Class<?> pluginClass) {
invokeMethods(pluginClass, TFunction.Load.class);
}
@Override @Override
public void postLoad(Plugin plugin, Class<?> pluginClass) { 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)) { if (pluginClass.isAnnotationPresent(TFunction.class)) {
TFunction function = pluginClass.getAnnotation(TFunction.class); TFunction function = pluginClass.getAnnotation(TFunction.class);
try { try {
Method method = pluginClass.getDeclaredMethod(function.enable()); Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
if (!Modifier.isStatic(method.getModifiers())) { if (!Modifier.isStatic(method.getModifiers())) {
TLogger.getGlobalLogger().error(method.getName() + " is not a static method."); TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
return; return;
} }
method.setAccessible(true); method.setAccessible(true);
method.invoke(null); method.invoke(null);
TabooLibAPI.debug("Function " + pluginClass.getSimpleName() + " loaded. (" + plugin.getName() + ")");
} catch (NoSuchMethodException ignore) { } catch (NoSuchMethodException ignore) {
} catch (Exception e) { } catch (Exception e) {
TLogger.getGlobalLogger().warn("TFunction load Failed: " + pluginClass.getName());
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
@Override public void invokeMethods(Class<?> pluginClass, Class<? extends Annotation> a) {
public void unload(Plugin plugin, Class<?> pluginClass) { for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
if (pluginClass.isAnnotationPresent(TFunction.class)) { if (declaredMethod.isAnnotationPresent(a)) {
TFunction function = pluginClass.getAnnotation(TFunction.class); try {
try { if (!Modifier.isStatic(declaredMethod.getModifiers())) {
Method method = pluginClass.getDeclaredMethod(function.disable()); TLogger.getGlobalLogger().error(declaredMethod.getName() + " is not a static method.");
if (!Modifier.isStatic(method.getModifiers())) { return;
TLogger.getGlobalLogger().error(method.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();
} }
} }
} }

View File

@ -10,7 +10,9 @@ import io.izzel.taboolib.module.packet.TPacketHandler;
import io.izzel.taboolib.module.packet.TPacketListener; import io.izzel.taboolib.module.packet.TPacketListener;
import io.izzel.taboolib.util.lite.cooldown.Cooldown; import io.izzel.taboolib.util.lite.cooldown.Cooldown;
import io.izzel.taboolib.util.lite.cooldown.Cooldowns; import io.izzel.taboolib.util.lite.cooldown.Cooldowns;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -66,6 +68,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
} }
}); });
TabooLibLoader.runTask(config::runListener); TabooLibLoader.runTask(config::runListener);
} catch (NoSuchMethodException ignore) {
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
@ -97,6 +100,26 @@ public class TInjectLoader implements TabooLibLoader.Loader {
t.printStackTrace(); 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 @Override

View File

@ -23,12 +23,12 @@ import java.util.Optional;
* @Author 坏黑 * @Author 坏黑
* @Since 2019-05-22 1:16 * @Since 2019-05-22 1:16
*/ */
@TFunction(enable = "init")
public class SimpleI18n { public class SimpleI18n {
private static FileConfiguration lang; private static FileConfiguration lang;
private static boolean released; private static boolean released;
@TFunction.Init
static void init() { static void init() {
File localeFile = getLocaleFile(TabooLib.getPlugin()); File localeFile = getLocaleFile(TabooLib.getPlugin());
if (localeFile == null) { if (localeFile == null) {

View File

@ -1,8 +1,8 @@
package io.izzel.taboolib.util.lite; package io.izzel.taboolib.util.lite;
import io.izzel.taboolib.module.inject.TFunction;
import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.logger.TLogger;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory; import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import io.izzel.taboolib.module.inject.TFunction;
import javax.script.Compilable; import javax.script.Compilable;
import javax.script.CompiledScript; import javax.script.CompiledScript;
@ -14,12 +14,12 @@ import java.util.Objects;
* @Author sky * @Author sky
* @Since 2018-06-02 22:48 * @Since 2018-06-02 22:48
*/ */
@TFunction(enable = "init")
public class Scripts { public class Scripts {
private static ScriptEngine scriptEngine; private static ScriptEngine scriptEngine;
private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
@TFunction.Init
static void init() { static void init() {
try { try {
NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null); NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null);

View File

@ -69,4 +69,8 @@ public class Cooldown {
} }
return true; return true;
} }
public void reset(String player) {
data.remove(player);
}
} }

View File

@ -1,7 +1,6 @@
package io.izzel.taboolib.util.tag; package io.izzel.taboolib.util.tag;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.inject.TFunction;
import io.izzel.taboolib.module.inject.TListener; import io.izzel.taboolib.module.inject.TListener;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -20,7 +19,6 @@ import java.util.UUID;
* @Author sky * @Author sky
* @Since 2018-05-23 0:37 * @Since 2018-05-23 0:37
*/ */
@TFunction(enable = "init")
@TListener @TListener
public class TagDataHandler implements Listener { public class TagDataHandler implements Listener {