From 3ea16b3e808f2ec497282f8c4f1c9524a632420f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sun, 13 May 2018 21:31:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=E8=87=B3?= =?UTF-8?q?=204.01=20=E8=B0=83=E6=95=B4=EF=BC=9ATLocale$logger=20=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E5=8F=AF=E4=BB=A5=E5=8F=91=E9=80=81=E5=A4=9A=E8=A1=8C?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E4=BA=86=20=E8=B0=83=E6=95=B4=EF=BC=9ATLocal?= =?UTF-8?q?e=20=E5=8F=91=E9=80=81=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E7=94=B1=E7=BA=A2=E8=89=B2=E6=94=B9=E4=B8=BA=E7=81=B0=E8=89=B2?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=8C=BA=E5=88=86=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E6=8F=90=E7=A4=BA=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E3=80=82=20=E8=B0=83=E6=95=B4=EF=BC=9ATLocaleLoader=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20getCallerPlugin=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=BD=AC=E7=A7=BB=E5=88=B0=20Ref=20=E4=B8=AD=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9ABaseMainCommand=20=E6=8E=A5=E5=8F=A3=E4=B8=AD?= =?UTF-8?q?=20getType=20=E6=96=B9=E6=B3=95=E5=A4=B1=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E4=BF=AE=E5=A4=8D=EF=BC=9AMain=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E8=AF=AD=E8=A8=80=E6=8F=90=E7=A4=BA=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=9B=B4=E6=94=B9=E7=9A=84=E9=97=AE=E9=A2=98=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9ATPlugin=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E5=8F=AF=E4=BB=A5=E8=A1=A5=E5=85=A8=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E4=BA=86=20=E6=96=B0=E5=A2=9E=EF=BC=9Aconfig?= =?UTF-8?q?.yml=20=E7=8E=B0=E5=9C=A8=E4=BC=9A=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E4=BA=86=EF=BC=88=E9=83=A8=E5=88=86=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=BF=98=E9=9C=80=E9=87=8D=E5=90=AF=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../com/ilummc/tlib/resources/TLocale.java | 40 +++++++------------ .../tlib/resources/TLocaleInstance.java | 7 +--- .../ilummc/tlib/resources/TLocaleLoader.java | 34 ++++++++-------- .../tlib/resources/TLocaleSerialize.java | 4 +- src/main/java/com/ilummc/tlib/util/Ref.java | 14 +++++++ src/main/java/me/skymc/taboolib/Main.java | 22 +++++----- src/main/java/me/skymc/taboolib/TabooLib.java | 4 ++ .../commands/internal/BaseMainCommand.java | 21 +++++++++- .../plugin/TabooLibPluginMainCommand.java | 17 ++++++++ src/main/resources/lang/zh_CN.yml | 21 ++++++++-- 11 files changed, 122 insertions(+), 64 deletions(-) diff --git a/pom.xml b/pom.xml index c21d414..55733e0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.skymc TabooLib - 4.0 + 4.01 UTF-8 diff --git a/src/main/java/com/ilummc/tlib/resources/TLocale.java b/src/main/java/com/ilummc/tlib/resources/TLocale.java index 94cf72a..b2dc858 100644 --- a/src/main/java/com/ilummc/tlib/resources/TLocale.java +++ b/src/main/java/com/ilummc/tlib/resources/TLocale.java @@ -7,12 +7,11 @@ import com.ilummc.tlib.util.Ref; import com.ilummc.tlib.util.Strings; import me.clip.placeholderapi.PlaceholderAPI; import me.skymc.taboolib.Main; +import me.skymc.taboolib.TabooLib; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import java.lang.reflect.Field; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -24,15 +23,15 @@ public class TLocale { } static String asString(String path, Class callerClass, String... args) { - return TLocaleLoader.asString(getCallerPlugin(callerClass), path, args); + return TLocaleLoader.asString(Ref.getCallerPlugin(callerClass), path, args); } static List asStringList(String path, Class callerClass, String... args) { - return TLocaleLoader.asStringList(getCallerPlugin(callerClass), path, args); + return TLocaleLoader.asStringList(Ref.getCallerPlugin(callerClass), path, args); } private static void sendTo(String path, CommandSender sender, String[] args, Class callerClass) { - TLocaleLoader.sendTo(getCallerPlugin(callerClass), path, sender, args); + TLocaleLoader.sendTo(Ref.getCallerPlugin(callerClass), path, sender, args); } public static void sendToConsole(String path, String... args) { @@ -49,7 +48,7 @@ public class TLocale { public static String asString(String path, String... args) { try { - return asString(path, Ref.getCallerClassNotOptional(3), args); + return asString(path, Ref.getCallerClass(3).orElse(Main.class), args); } catch (Exception e) { TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("FETCH-LOCALE-ERROR"), path)); TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("LOCALE-ERROR-REASON"), e.getMessage())); @@ -59,7 +58,7 @@ public class TLocale { public static List asStringList(String path, String... args) { try { - return asStringList(path, Ref.getCallerClassNotOptional(3), args); + return asStringList(path, Ref.getCallerClass(3).orElse(Main.class), args); } catch (Exception e) { TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("FETCH-LOCALE-ERROR"), path)); TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("LOCALE-ERROR-REASON"), e.getMessage())); @@ -68,18 +67,7 @@ public class TLocale { } public static void reload() { - Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(getCallerPlugin(clazz), false)); - } - - private static JavaPlugin getCallerPlugin(Class callerClass) { - try { - Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin"); - pluginField.setAccessible(true); - return (JavaPlugin) pluginField.get(callerClass.getClassLoader()); - } catch (Exception ignored) { - TLocale.Logger.error("LOCALE.CALLER-PLUGIN-NOT-FOUND", callerClass.getName()); - } - return (JavaPlugin) Main.getInst(); + Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(Ref.getCallerPlugin(clazz), false)); } public static final class Translate extends TLocale { @@ -112,31 +100,31 @@ public class TLocale { public static final class Logger extends TLocale { public static void info(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).info(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).info(locale))); } public static void warn(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).warn(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).warn(locale))); } public static void error(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).error(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).error(locale))); } public static void fatal(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).fatal(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).fatal(locale))); } public static void fine(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).fine(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).fine(locale))); } public static void finest(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).finest(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).finest(locale))); } public static void verbose(String path, String... args) { - Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).verbose(asString(path, clazz, args))); + Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).verbose(locale))); } } diff --git a/src/main/java/com/ilummc/tlib/resources/TLocaleInstance.java b/src/main/java/com/ilummc/tlib/resources/TLocaleInstance.java index 0de1d31..e77ec67 100644 --- a/src/main/java/com/ilummc/tlib/resources/TLocaleInstance.java +++ b/src/main/java/com/ilummc/tlib/resources/TLocaleInstance.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import com.ilummc.tlib.TLib; import com.ilummc.tlib.resources.type.TLocaleText; import com.ilummc.tlib.util.Strings; +import me.skymc.taboolib.TabooLib; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.configuration.ConfigurationSection; @@ -11,10 +12,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import javax.annotation.concurrent.ThreadSafe; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -63,7 +61,6 @@ class TLocaleInstance { } catch (Exception | Error e) { TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("SEND-LOCALE-ERROR"), path)); TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("LOCALE-ERROR-REASON"), e.toString())); - e.printStackTrace(); } } diff --git a/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java b/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java index b6e113c..31ddcad 100644 --- a/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java +++ b/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java @@ -6,7 +6,9 @@ import com.ilummc.tlib.logger.TLogger; import com.ilummc.tlib.resources.type.*; import com.ilummc.tlib.util.IO; import com.ilummc.tlib.util.Strings; +import me.skymc.taboocode.TabooCodeLang; import me.skymc.taboolib.Main; +import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.fileutils.ConfigUtils; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -17,10 +19,7 @@ import org.bukkit.plugin.Plugin; import java.io.File; import java.io.InputStream; import java.nio.charset.Charset; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class TLocaleLoader { @@ -36,6 +35,7 @@ public class TLocaleLoader { } public static void sendTo(Plugin plugin, String path, CommandSender sender, String... args) { + TabooLib.debug(plugin, "TLocaleLoader.sendTo: " + plugin + ", path: " + path + ", sender: " + sender + ", args: " + Arrays.asList(args)); if (Bukkit.isPrimaryThread()) { Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args)); } else { @@ -46,6 +46,7 @@ public class TLocaleLoader { } public static String asString(Plugin plugin, String path, String... args) { + TabooLib.debug(plugin, "TLocaleLoader.asString: " + plugin.getName() + ", path: " + path + ", args: " + Arrays.asList(args)); TLocaleInstance tLocaleInstance = map.get(plugin.getName()); if (tLocaleInstance != null) { return tLocaleInstance.asString(path, args); @@ -55,6 +56,7 @@ public class TLocaleLoader { } public static List asStringList(Plugin plugin, String path, String... args) { + TabooLib.debug(plugin, "TLocaleLoader.asStringList: " + plugin + ", path: " + path + ", args: " + Arrays.asList(args)); TLocaleInstance tLocaleInstance = map.get(plugin.getName()); if (tLocaleInstance != null) { return tLocaleInstance.asStringList(path, args); @@ -98,6 +100,18 @@ public class TLocaleLoader { } } + public static boolean isLocaleLoaded(Plugin plugin) { + return map.containsKey(plugin.getName()); + } + + public static boolean isDependWithTabooLib(Plugin plugin) { + return plugin.getClass().getAnnotation(TLocalePlugin.class) != null || plugin.getDescription().getDepend().contains(Main.getInst().getName()) || plugin.getDescription().getSoftDepend().contains(Main.getInst().getName()); + } + + public static List getLocalePriority() { + return Main.getInst().getConfig().contains("LOCALE.PRIORITY") ? Main.getInst().getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN"); + } + private static boolean isLoadLocale(Plugin plugin, boolean isCover) { return (isCover || !isLocaleLoaded(plugin)) && (plugin.equals(Main.getInst()) || isDependWithTabooLib(plugin)); } @@ -119,18 +133,6 @@ public class TLocaleLoader { getLocalePriority().stream().filter(localeName -> !new File(plugin.getDataFolder(), "lang/" + localeName + ".yml").exists() && plugin.getResource("lang/" + localeName + ".yml") != null).forEach(localeName -> plugin.saveResource("lang/" + localeName + ".yml", true)); } - public static boolean isLocaleLoaded(Plugin plugin) { - return map.containsKey(plugin.getName()); - } - - public static boolean isDependWithTabooLib(Plugin plugin) { - return plugin.getClass().getAnnotation(TLocalePlugin.class) != null || plugin.getDescription().getDepend().contains(Main.getInst().getName()) || plugin.getDescription().getSoftDepend().contains(Main.getInst().getName()); - } - - public static List getLocalePriority() { - return Main.getInst().getConfig().contains("LOCALE.PRIORITY") ? Main.getInst().getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN"); - } - private static TLocaleInstance getLocaleInstance(Plugin plugin) { TLocaleInstance instance = new TLocaleInstance(plugin); map.put(plugin.getName(), instance); diff --git a/src/main/java/com/ilummc/tlib/resources/TLocaleSerialize.java b/src/main/java/com/ilummc/tlib/resources/TLocaleSerialize.java index dad8808..3155d74 100644 --- a/src/main/java/com/ilummc/tlib/resources/TLocaleSerialize.java +++ b/src/main/java/com/ilummc/tlib/resources/TLocaleSerialize.java @@ -57,12 +57,12 @@ public abstract class TLocaleSerialize implements TLocaleSender, ConfigurationSe @Override public void sendTo(CommandSender sender, String... args) { - sender.sendMessage("§4<" + path + "§4>"); + sender.sendMessage("§8<" + path + "§8>"); } @Override public String asString(String... args) { - return "§4<" + path + "§4>"; + return "§8<" + path + "§8>"; } @Override diff --git a/src/main/java/com/ilummc/tlib/util/Ref.java b/src/main/java/com/ilummc/tlib/util/Ref.java index 866cdfc..69374de 100644 --- a/src/main/java/com/ilummc/tlib/util/Ref.java +++ b/src/main/java/com/ilummc/tlib/util/Ref.java @@ -2,7 +2,10 @@ package com.ilummc.tlib.util; import com.google.gson.annotations.SerializedName; import com.ilummc.tlib.TLib; +import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.util.asm.AsmAnalyser; +import me.skymc.taboolib.Main; +import org.bukkit.plugin.java.JavaPlugin; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; import sun.reflect.Reflection; @@ -96,6 +99,17 @@ public class Ref { return Optional.empty(); } + public static JavaPlugin getCallerPlugin(Class callerClass) { + try { + Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin"); + pluginField.setAccessible(true); + return (JavaPlugin) pluginField.get(callerClass.getClassLoader()); + } catch (Exception ignored) { + TLocale.Logger.error("LOCALE.CALLER-PLUGIN-NOT-FOUND", callerClass.getName()); + } + return (JavaPlugin) Main.getInst(); + } + private static abstract class CallerClass { private static CallerClass impl; diff --git a/src/main/java/me/skymc/taboolib/Main.java b/src/main/java/me/skymc/taboolib/Main.java index 4e6268d..a866bb4 100644 --- a/src/main/java/me/skymc/taboolib/Main.java +++ b/src/main/java/me/skymc/taboolib/Main.java @@ -1,6 +1,8 @@ package me.skymc.taboolib; import com.ilummc.tlib.TLib; +import com.ilummc.tlib.inject.TConfigWatcher; +import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.anvil.AnvilContainerAPI; import me.skymc.taboolib.bstats.Metrics; import me.skymc.taboolib.commands.TabooLibMainCommand; @@ -180,7 +182,7 @@ public class Main extends JavaPlugin implements Listener { String hash = connection.getValue(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID(), "hash").toString(); // 如果这个值和我的值不同 if (!hash.equals(StringUtils.hashKeyForDisk(getDataFolder().getPath()))) { - MsgUtils.warn("检测到本服序列号与其他服务器相同, 已重新生成!"); + TLocale.Logger.error("NOTIFY.ERROR-SERVER-KEY"); // 重新生成序列号 TabooLib.resetServerUID(); // 关服 @@ -189,7 +191,7 @@ public class Main extends JavaPlugin implements Listener { } } else { // 提示 - MsgUtils.warn("数据库连接失败, 请检查配置是否正确!"); + TLocale.Logger.error("NOTIFY.ERROR-CONNECTION-FALL"); // 关服 Bukkit.shutdown(); } @@ -235,14 +237,16 @@ public class Main extends JavaPlugin implements Listener { } // 载入完成 - MsgUtils.send("§7插件载入完成!"); - MsgUtils.send("§7插件版本: §f" + getDescription().getVersion()); - MsgUtils.send("§7插件作者: §f" + getDescription().getAuthors()); - MsgUtils.send("§7游戏版本: §f" + TabooLib.getVerint()); + TLocale.Logger.info("NOTIFY.SUCCESS-LOADED", getDescription().getAuthors().toString(), getDescription().getVersion(), String.valueOf(TabooLib.getVersion())); // 文件保存 Bukkit.getScheduler().runTaskTimerAsynchronously(this, DataUtils::saveAllCaches, 20, 20 * 120); Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> PlayerDataManager.saveAllCaches(true, false), 20, 20 * 60); + // 文件监控 + TLib.getTLib().getConfigWatcher().addListener(new File(getDataFolder(), "config.yml"), null, obj -> { + reloadConfig(); + TLocale.Logger.info("CONFIG.RELOAD-SUCCESS", inst.getName(), "config.yml"); + }); // 插件联动 new BukkitRunnable() { @@ -277,8 +281,7 @@ public class Main extends JavaPlugin implements Listener { // 如果插件尚未启动完成 if (!started) { - MsgUtils.send("&c插件尚未启动完成, 已跳过卸载代码"); - MsgUtils.send("&c插件作者: &4坏黑"); + TLocale.Logger.error("NOTIFY.FALL-DISABLE"); return; } @@ -312,8 +315,7 @@ public class Main extends JavaPlugin implements Listener { } // 提示信息 - MsgUtils.send("&c插件已卸载, 感谢您使用&4禁忌书库"); - MsgUtils.send("&c插件作者: &4坏黑"); + TLocale.Logger.error("NOTIFY.SUCCESS-DISABLE"); // 清理头衔 TagUtils.delete(); diff --git a/src/main/java/me/skymc/taboolib/TabooLib.java b/src/main/java/me/skymc/taboolib/TabooLib.java index d0fcfc6..21ced84 100644 --- a/src/main/java/me/skymc/taboolib/TabooLib.java +++ b/src/main/java/me/skymc/taboolib/TabooLib.java @@ -10,6 +10,10 @@ import java.util.UUID; public class TabooLib { + public static boolean isDebug() { + return Main.getInst().getConfig().getBoolean("DEBUG"); + } + public static void debug(Plugin plugin, String... args) { if (Main.getInst().getConfig().getBoolean("DEBUG")) { 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/commands/internal/BaseMainCommand.java b/src/main/java/me/skymc/taboolib/commands/internal/BaseMainCommand.java index d9c9eb8..9116be8 100644 --- a/src/main/java/me/skymc/taboolib/commands/internal/BaseMainCommand.java +++ b/src/main/java/me/skymc/taboolib/commands/internal/BaseMainCommand.java @@ -2,6 +2,8 @@ package me.skymc.taboolib.commands.internal; import com.google.common.base.Preconditions; import com.ilummc.tlib.resources.TLocale; +import com.ilummc.tlib.resources.TLocaleLoader; +import com.ilummc.tlib.util.Ref; import me.skymc.taboolib.Main; import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.commands.internal.type.CommandRegister; @@ -10,8 +12,11 @@ import me.skymc.taboolib.string.ArrayUtils; import me.skymc.taboolib.string.StringUtils; import org.bukkit.Bukkit; import org.bukkit.command.*; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; @@ -35,6 +40,7 @@ public abstract class BaseMainCommand implements IMainCommand, CommandExecutor, baseMainCommand.getRegisterCommand().setExecutor(baseMainCommand); baseMainCommand.getRegisterCommand().setTabCompleter(baseMainCommand); baseMainCommand.getLinkClasses().add(baseMainCommand.getClass()); + baseMainCommand.disguisedPlugin(); loadCommandRegister(baseMainCommand); return baseMainCommand; } @@ -149,7 +155,7 @@ public abstract class BaseMainCommand implements IMainCommand, CommandExecutor, } private boolean isConfirmType(CommandSender sender, CommandType commandType) { - return commandType == CommandType.ALL || sender instanceof ConsoleCommandSender && commandType == CommandType.CONSOLE; + return commandType == CommandType.ALL || (sender instanceof Player && commandType == CommandType.PLAYER); } private void helpCommand(CommandSender sender, String label) { @@ -159,4 +165,17 @@ public abstract class BaseMainCommand implements IMainCommand, CommandExecutor, subCommands.stream().map(subCommand -> subCommand == null ? getEmptyLine() : subCommand.getCommandString(label)).forEach(sender::sendMessage); sender.sendMessage(getEmptyLine()); } + + private void disguisedPlugin() { + linkClasses.forEach(clazz -> disguisedPlugin(clazz, (JavaPlugin) registerCommand.getPlugin())); + } + + private void disguisedPlugin(Class targetClass, JavaPlugin plugin) { + try { + Field pluginField = targetClass.getClassLoader().getClass().getDeclaredField("plugin"); + pluginField.setAccessible(true); + pluginField.set(targetClass.newInstance(), plugin); + } catch (Exception ignored) { + } + } } diff --git a/src/main/java/me/skymc/taboolib/commands/plugin/TabooLibPluginMainCommand.java b/src/main/java/me/skymc/taboolib/commands/plugin/TabooLibPluginMainCommand.java index cf29a86..74bf64a 100644 --- a/src/main/java/me/skymc/taboolib/commands/plugin/TabooLibPluginMainCommand.java +++ b/src/main/java/me/skymc/taboolib/commands/plugin/TabooLibPluginMainCommand.java @@ -4,6 +4,7 @@ import com.google.common.base.Joiner; import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.commands.internal.BaseMainCommand; import me.skymc.taboolib.commands.internal.BaseSubCommand; +import me.skymc.taboolib.commands.internal.ISubCommand; import me.skymc.taboolib.commands.internal.type.CommandArgument; import me.skymc.taboolib.plugin.PluginUtils; import me.skymc.taboolib.string.ArrayUtils; @@ -12,6 +13,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -30,6 +32,21 @@ public class TabooLibPluginMainCommand extends BaseMainCommand { reloadCommand(); } + @Override + public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { + if (args.length == 1) { + return getSubCommands().stream().filter(internalCommandExecutor -> internalCommandExecutor != null && (args[0].isEmpty() || internalCommandExecutor.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(ISubCommand::getLabel).collect(Collectors.toList()); + } else if (args.length > 1 && isPluginCommand(args[0])) { + return Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(x -> !PluginUtils.isIgnored(x)).collect(Collectors.toList()).stream().filter(plugin -> args[1].isEmpty() || plugin.getName().toLowerCase().startsWith(args[1].toLowerCase())).map(Plugin::getName).collect(Collectors.toList()); + } else { + return null; + } + } + + private boolean isPluginCommand(String label) { + return "info".equalsIgnoreCase(label) || "load".equalsIgnoreCase(label) || "unload".equalsIgnoreCase(label) || "reload".equalsIgnoreCase(label); + } + @Override public String getCommandTitle() { return TLocale.asString("COMMANDS.TPLUGIN.COMMAND-TITLE"); diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 13f3a41..9bff7dd 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -30,6 +30,21 @@ CONFIG: RELOAD-FAIL: '插件 {0} 的配置 {1} 成功重载' LISTEN-START: '开始监听 {0} 插件的 {1} 配置文件' +NOTIFY: + ERROR-SERVER-KEY: '&4检测到本服序列号与其他服务器相同, 已重新生成!' + ERROR-CONNECTION-FALL: '&4数据库连接失败, 请检查配置是否正确!' + SUCCESS-LOADED: + - '§7插件载入完成!' + - '§7插件作者: §f{0}' + - '§7插件版本: §f{1}' + - '§7游戏版本: §f{2}' + SUCCESS-DISABLE: + - '&c插件已卸载, 感谢您使用&4禁忌书库' + - '&c插件作者: &4坏黑' + FALL-DISABLE: + - '&c插件尚未启动完成, 已跳过卸载代码' + - '&c插件作者: &4坏黑' + LOCALE: TITLE-SEND-TO-NON-PLAYER: '该语言类型只能发送给玩家:{0}' PLUGIN-NOT-FOUND: '无效的语言文件发送形式: &4{0}' @@ -138,7 +153,7 @@ COMMANDS: SUCCESS-NORMAL: '&8[&3&lTabooLib&8] &7重载成功' SUCCESS-ELAPSED-TIME: '&8[&3&lTabooLib&8] &7重载成功, 耗时: &f{0} ms' TABOOLIB: - COMMAND-TITLE: '&b&l----- &3&lTabooLib Commands &b&l-----' + COMMAND-TITLE: '&e&l----- &6&lTabooLib Commands &e&l-----' SAVE: DESCRIPTION: '载入插件' ARGUMENTS: @@ -400,7 +415,7 @@ COMMANDS: SUCCESS-SEND: '&8[&3&lTabooLib&8] &7信息已发送, 耗时&f: {0}' HELP: - '' - - '&b&l----- &3&lLanguage2 Commands &b&l-----' + - '&e&l----- &6&lLanguage2 Commands &e&l-----' - '' - '&f /{0} send &8[&7玩家/ALL&8] &8[&7语言&8] &8<&7变量&8> &6- &e发送语言提示' - '&f /{0} reload &6- &e重载语言库' @@ -417,7 +432,7 @@ COMMANDS: - '&f /{0} reload &6- &e重载语言库' - '' TPLUGIN: - COMMAND-TITLE: '&e&l----- &6&lTabooLibPlugin Commands &b&l-----' + COMMAND-TITLE: '&e&l----- &6&lTabooLibPlugin Commands &e&l-----' LIST: DESCRIPTION: '列出插件' LIST-PLUGIN: