TabooLib v4.25
+ 新增 TConfiguration 工具,与 TConfigWatcher 联动创建能够自动重载的配置文件。(尚未测试) + 新增 TFunction 注解,自动执行载入与卸载方法。(变懒第一步,放弃注册步骤) + 调整 TLogger 工具,允许以自定义名称创建,并支持在 BungeeCord 下使用。 + 调整 TListener 与 Instantiable 注解,不会再重复读取插件类了。 + 调整 ReflectionUtils 工具,对部分语法进行了修改。 + 调整 TabooLib 类下的 isSpigot 与 getVersion 算法。 + 重做 AnvilContainerAPI 工具,现在可以正常使用了。(丢人玩意儿终于重写了) + InstanceHandler 类更名为 InstantiableLoader + MsgUtils 类被赋予尊贵的 @Deprecated
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package me.skymc.taboolib.common.configuration;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-09-08 15:00
|
||||
*/
|
||||
public class TConfiguration extends YamlConfiguration {
|
||||
|
||||
private File file;
|
||||
private Runnable runnable;
|
||||
|
||||
private TConfiguration(File file) {
|
||||
this.file = file;
|
||||
reload();
|
||||
TLib.getTLib().getConfigWatcher().addSimpleListener(this.file, this::reload);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放文件监听
|
||||
*/
|
||||
public void release() {
|
||||
TLib.getTLib().getConfigWatcher().removeListener(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新载入配置
|
||||
*/
|
||||
public void reload() {
|
||||
try {
|
||||
load(file);
|
||||
Optional.ofNullable(runnable).ifPresent(Runnable::run);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
TLogger.getGlobalLogger().warn("Cannot load configuration from stream: " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建配置文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return {@link TConfiguration}
|
||||
*/
|
||||
public static TConfiguration create(File file) {
|
||||
return new TConfiguration(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从插件里释放文件并创建
|
||||
*
|
||||
* @param plugin 插件
|
||||
* @param path 目录
|
||||
* @return {@link TConfiguration}
|
||||
*/
|
||||
public static TConfiguration createInResource(Plugin plugin, String path) {
|
||||
File file = new File(plugin.getDataFolder(), path);
|
||||
if (!file.exists()) {
|
||||
plugin.saveResource(path, true);
|
||||
}
|
||||
return create(file);
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public TConfiguration listener(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package me.skymc.taboolib.common.function;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-09-08 14:01
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TFunction {
|
||||
|
||||
String enable() default "onEnable";
|
||||
String disable() default "onDisable";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package me.skymc.taboolib.common.function;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.TabooLibLoader;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.event.server.PluginEnableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-09-08 14:00
|
||||
*/
|
||||
@TListener
|
||||
public class TFunctionLoader implements Listener {
|
||||
|
||||
private static HashMap<String, List<Class>> pluginFunction = new HashMap<>();
|
||||
|
||||
TFunctionLoader() {
|
||||
loadFunction();
|
||||
}
|
||||
|
||||
public static void loadFunction() {
|
||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TFunctionLoader::loadFunction);
|
||||
}
|
||||
|
||||
public static void loadFunction(Plugin plugin) {
|
||||
if (!(TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin))) {
|
||||
return;
|
||||
}
|
||||
TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> {
|
||||
for (Class pluginClass : classes) {
|
||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||
TFunction function = (TFunction) pluginClass.getAnnotation(TFunction.class);
|
||||
try {
|
||||
Method method = pluginClass.getDeclaredMethod(function.enable());
|
||||
if (method == null) {
|
||||
continue;
|
||||
}
|
||||
method.setAccessible(true);
|
||||
method.invoke(pluginClass.newInstance());
|
||||
pluginFunction.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(pluginClass);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void unloadFunction() {
|
||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TFunctionLoader::unloadFunction);
|
||||
}
|
||||
|
||||
public static void unloadFunction(Plugin plugin) {
|
||||
Optional.ofNullable(pluginFunction.remove(plugin.getName())).ifPresent(classes -> {
|
||||
for (Class pluginClass : classes) {
|
||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||
TFunction function = (TFunction) pluginClass.getAnnotation(TFunction.class);
|
||||
try {
|
||||
Method method = pluginClass.getDeclaredMethod(function.disable());
|
||||
if (method == null) {
|
||||
continue;
|
||||
}
|
||||
method.setAccessible(true);
|
||||
method.invoke(pluginClass.newInstance());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEnable(PluginEnableEvent e) {
|
||||
loadFunction(e.getPlugin());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDisable(PluginDisableEvent e) {
|
||||
unloadFunction(e.getPlugin());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user