diff --git a/pom.xml b/pom.xml index af3a944..d873793 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.skymc TabooLib - 4.3 + 4.31 UTF-8 diff --git a/src/main/java/me/skymc/taboolib/TabooLib.java b/src/main/java/me/skymc/taboolib/TabooLib.java index 46bcf89..19453c6 100644 --- a/src/main/java/me/skymc/taboolib/TabooLib.java +++ b/src/main/java/me/skymc/taboolib/TabooLib.java @@ -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)); } } diff --git a/src/main/java/me/skymc/taboolib/TabooLibLoader.java b/src/main/java/me/skymc/taboolib/TabooLibLoader.java index 53b4daa..5eeae1a 100644 --- a/src/main/java/me/skymc/taboolib/TabooLibLoader.java +++ b/src/main/java/me/skymc/taboolib/TabooLibLoader.java @@ -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()); } } diff --git a/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java b/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java index 933c4c8..cb8aa1c 100644 --- a/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java +++ b/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java @@ -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> 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> 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; } diff --git a/src/main/java/me/skymc/taboolib/listener/ListenerPlayerCommand.java b/src/main/java/me/skymc/taboolib/listener/ListenerPlayerCommand.java index 6625aac..a417dc4 100644 --- a/src/main/java/me/skymc/taboolib/listener/ListenerPlayerCommand.java +++ b/src/main/java/me/skymc/taboolib/listener/ListenerPlayerCommand.java @@ -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."); + } } } diff --git a/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java b/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java index ef90a14..b4083e1 100644 --- a/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java +++ b/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java @@ -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 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); diff --git a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java index 76f6c6d..927419e 100644 --- a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java +++ b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java @@ -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> 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> getListeners() { - return listeners; - } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a1b71a1..77e539f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -31,10 +31,6 @@ PLUGIN-INJECTOR: DISABLE-ON-PLUGIN-EXISTS: - LuckPerms -# 是否启用调试模式 -# 启用后将收到来自其他插件的调试信息 -DEBUG: false - # 是否在当前服务器启用交流网终端 # 启用后将会收到大量调试信息, 不建议使用 SERVER: false