v4.31 紧急修复版

+ 优化 TConfiguration 模块,兼容插件热重载
+ 修复插件类缓存顺序问题,兼容插件热重载
+ 删除配置文件中的 DEBUG 节点,改为指令输入 /tDEBUG 切换开关
This commit is contained in:
坏黑 2018-09-25 11:41:50 +08:00
parent bc7554a86b
commit c19590ab1f
8 changed files with 117 additions and 52 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.skymc</groupId>
<artifactId>TabooLib</artifactId>
<version>4.3</version>
<version>4.31</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -98,22 +98,40 @@ public class TabooLib {
}
/**
* 是否为 debug 模式
* 是否为调试模式
*
* @return boolean
*/
public static boolean isDebug() {
return Main.getInst().getConfig().getBoolean("DEBUG");
return DataUtils.getPluginData("TabooLibrary", instance()).getBoolean("debug");
}
/**
* 发送 debug 信息
* 切换调试模式
*
* @param debug
*/
public static void setDebug(boolean debug) {
DataUtils.getPluginData("TabooLibrary", instance()).set("debug", debug);
}
/**
* 发送调试信息
*
* @param args 内容
*/
public static void debug(String... args) {
debug(instance(), args);
}
/**
* 发送调试信息
*
* @param plugin 插件名
* @param args 内容
*/
public static void debug(Plugin plugin, String... args) {
if (Main.getInst().getConfig().getBoolean("DEBUG")) {
if (TabooLib.isDebug()) {
Arrays.stream(args).forEach(var -> Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "[TabooLib - DEBUG][" + plugin.getName() + "] " + ChatColor.RED + var));
}
}

View File

@ -158,11 +158,11 @@ public class TabooLibLoader implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onEnable(PluginEnableEvent e) {
pluginClasses.remove(e.getPlugin().getName());
setupClasses(e.getPlugin());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onDisable(PluginDisableEvent e) {
setupClasses(e.getPlugin());
pluginClasses.remove(e.getPlugin().getName());
}
}

View File

@ -1,13 +1,20 @@
package me.skymc.taboolib.common.configuration;
import com.google.common.collect.Maps;
import com.ilummc.tlib.TLib;
import com.ilummc.tlib.logger.TLogger;
import com.ilummc.tlib.util.Ref;
import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
@ -16,34 +23,16 @@ import java.util.Optional;
*/
public class TConfiguration extends YamlConfiguration {
private static Map<String, List<File>> files = Maps.newHashMap();
private File file;
private Runnable runnable;
private TConfiguration(File file) {
private TConfiguration(File file, Plugin plugin) {
files.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(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);
runListener();
} catch (IOException | InvalidConfigurationException e) {
TLogger.getGlobalLogger().warn("Cannot load configuration from stream: " + e.toString());
}
}
public void runListener() {
try {
Optional.ofNullable(runnable).ifPresent(Runnable::run);
} catch (Exception e) {
e.printStackTrace();
}
TabooLib.debug("Loaded TConfiguration \"" + file.getName() + "\" from Plugin \"" + plugin.getName() + "\"");
}
/**
@ -53,7 +42,18 @@ public class TConfiguration extends YamlConfiguration {
* @return {@link TConfiguration}
*/
public static TConfiguration create(File file) {
return new TConfiguration(file);
return new TConfiguration(file, Ref.getCallerPlugin(Ref.getCallerClass(3).orElse(Main.class)));
}
/**
* 创建配置文件
*
* @param file 文件
* @param plugin 插件
* @return {@link TConfiguration}
*/
public static TConfiguration create(File file, Plugin plugin) {
return new TConfiguration(file, plugin);
}
/**
@ -68,7 +68,23 @@ public class TConfiguration extends YamlConfiguration {
if (!file.exists()) {
plugin.saveResource(path, true);
}
return create(file);
return create(file, plugin);
}
public static Map<String, List<File>> getFiles() {
return files;
}
public void release() {
TLib.getTLib().getConfigWatcher().removeListener(file);
}
public void reload() {
try {
load(file);
} catch (IOException | InvalidConfigurationException e) {
TLogger.getGlobalLogger().warn("Cannot load configuration from stream: " + e.toString());
}
}
// *********************************
@ -77,6 +93,14 @@ public class TConfiguration extends YamlConfiguration {
//
// *********************************
public void runListener() {
try {
Optional.ofNullable(runnable).ifPresent(Runnable::run);
} catch (Exception e) {
e.printStackTrace();
}
}
public File getFile() {
return file;
}

View File

@ -1,5 +1,6 @@
package me.skymc.taboolib.listener;
import com.ilummc.tlib.logger.TLogger;
import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.database.PlayerDataManager;
@ -15,17 +16,32 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
/**
* @author sky
*/
@TListener
public class ListenerPlayerCommand implements Listener {
@EventHandler
public void cmd(ServerCommandEvent e) {
if ("savefile".equals(e.getCommand())) {
if (e.getCommand().equalsIgnoreCase("saveFiles")) {
if (TabooLib.getVerint() > 10700) {
e.setCancelled(true);
}
Bukkit.getScheduler().runTask(Main.getInst(), DataUtils::saveAllCaches);
Bukkit.getScheduler().runTask(Main.getInst(), () -> PlayerDataManager.saveAllCaches(true, false));
TLogger.getGlobalLogger().info("Successfully.");
} else if (e.getCommand().equalsIgnoreCase("tDebug")) {
if (TabooLib.getVerint() > 10700) {
e.setCancelled(true);
}
if (TabooLib.isDebug()) {
TabooLib.setDebug(false);
TLogger.getGlobalLogger().info("&cDisabled.");
} else {
TabooLib.setDebug(true);
TLogger.getGlobalLogger().info("&aEnabled.");
}
}
}

View File

@ -1,7 +1,11 @@
package me.skymc.taboolib.listener;
import com.ilummc.tlib.TLib;
import com.ilummc.tlib.inject.TConfigWatcher;
import com.ilummc.tlib.resources.TLocale;
import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.common.configuration.TConfiguration;
import me.skymc.taboolib.mysql.MysqlUtils;
import me.skymc.taboolib.mysql.hikari.HikariHandler;
import me.skymc.taboolib.mysql.protect.MySQLConnection;
@ -11,20 +15,33 @@ import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
/**
* @author sky
*/
@TListener
public class ListenerPluginDisable implements Listener {
@EventHandler
public void disable(PluginDisableEvent e) {
TabooLib.debug("Plugin \"" + e.getPlugin().getName() + "\" was disabled.");
// 注销时间周期
TimeCycleManager.cancel(e.getPlugin());
// 注销插件配置
Optional.ofNullable(TConfiguration.getFiles().get(e.getPlugin().getName())).ifPresent(files -> {
TConfigWatcher tConfigWatcher = TLib.getTLib().getConfigWatcher();
for (File file : files) {
tConfigWatcher.removeListener(file);
TabooLib.debug("Remove TConfiguration \"" + file.getName() + "\" from Plugin \"" + e.getPlugin().getName() + "\"");
}
});
// 注销数据库连接
new HashSet<>(HikariHandler.getDataSource().keySet()).stream().filter(host -> e.getPlugin().equals(host.getPlugin()) && host.isAutoClose()).forEach(HikariHandler::closeDataSource);
// 获取连接
List<MySQLConnection> connection = new ArrayList<>();
for (MySQLConnection conn : MysqlUtils.CONNECTIONS) {
@ -33,7 +50,6 @@ public class ListenerPluginDisable implements Listener {
MysqlUtils.CONNECTIONS.remove(conn);
}
}
// 异步注销
BukkitRunnable runnable = new BukkitRunnable() {
@ -50,7 +66,6 @@ public class ListenerPluginDisable implements Listener {
}
}
};
// 如果插件关闭
try {
runnable.runTaskLater(Main.getInst(), 40);

View File

@ -3,7 +3,6 @@ package me.skymc.taboolib.listener;
import com.ilummc.tlib.util.Strings;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.TabooLibLoader;
import me.skymc.taboolib.fileutils.FileUtils;
import me.skymc.taboolib.methods.ReflectionUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
@ -47,7 +46,7 @@ public class TListenerHandler implements Listener {
public static void setupListener(Plugin plugin) {
TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> {
for (Class<?> pluginClass : classes) {
if (Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
if (org.bukkit.event.Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
try {
TListener tListener = pluginClass.getAnnotation(TListener.class);
// 检查注册条件
@ -59,7 +58,9 @@ public class TListenerHandler implements Listener {
// 实例化监听器
Listener listener = plugin.getClass().equals(pluginClass) ? (Listener) plugin : (Listener) ReflectionUtils.instantiateObject(pluginClass);
listeners.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(listener);
} catch (Exception ignored) {
TabooLib.debug("Listener " + listener.getClass().getSimpleName() + " setup successfully. (" + plugin.getName() + ")");
} catch (Exception e) {
TabooLib.debug("Listener setup failed: " + e.toString());
}
}
}
@ -113,6 +114,7 @@ public class TListenerHandler implements Listener {
}
// 注册监听
Bukkit.getPluginManager().registerEvents(listener, plugin);
TabooLib.debug("Listener " + listener.getClass().getSimpleName() + " registered. (" + plugin.getName() + ")");
}
});
}
@ -154,6 +156,10 @@ public class TListenerHandler implements Listener {
});
}
public static HashMap<String, List<Listener>> getListeners() {
return listeners;
}
@EventHandler
public void onPluginEnable(PluginEnableEvent e) {
try {
@ -170,14 +176,4 @@ public class TListenerHandler implements Listener {
} catch (Exception ignored) {
}
}
// *********************************
//
// Getter and Setter
//
// *********************************
public static HashMap<String, List<Listener>> getListeners() {
return listeners;
}
}

View File

@ -31,10 +31,6 @@ PLUGIN-INJECTOR:
DISABLE-ON-PLUGIN-EXISTS:
- LuckPerms
# 是否启用调试模式
# 启用后将收到来自其他插件的调试信息
DEBUG: false
# 是否在当前服务器启用交流网终端
# 启用后将会收到大量调试信息, 不建议使用
SERVER: false