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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user