From c19590ab1f7ee2ff1edbabb046a17293098aab6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Tue, 25 Sep 2018 11:41:50 +0800 Subject: [PATCH] =?UTF-8?q?v4.31=20=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=89=88=20+=20=E4=BC=98=E5=8C=96=20TConfiguration=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=EF=BC=8C=E5=85=BC=E5=AE=B9=E6=8F=92=E4=BB=B6=E7=83=AD?= =?UTF-8?q?=E9=87=8D=E8=BD=BD=20+=20=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E7=B1=BB=E7=BC=93=E5=AD=98=E9=A1=BA=E5=BA=8F=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E5=85=BC=E5=AE=B9=E6=8F=92=E4=BB=B6=E7=83=AD=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=20+=20=E5=88=A0=E9=99=A4=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=AD=E7=9A=84=20DEBUG=20=E8=8A=82=E7=82=B9?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=8C=87=E4=BB=A4=E8=BE=93=E5=85=A5?= =?UTF-8?q?=20/tDEBUG=20=E5=88=87=E6=8D=A2=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/me/skymc/taboolib/TabooLib.java | 26 +++++-- .../me/skymc/taboolib/TabooLibLoader.java | 4 +- .../common/configuration/TConfiguration.java | 72 ++++++++++++------- .../listener/ListenerPlayerCommand.java | 18 ++++- .../listener/ListenerPluginDisable.java | 21 +++++- .../taboolib/listener/TListenerHandler.java | 22 +++--- src/main/resources/config.yml | 4 -- 8 files changed, 117 insertions(+), 52 deletions(-) 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