From 399a0b4668f9e8730bbfb13f10adaca040adaef8 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 14 Oct 2016 18:46:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E5=8C=96=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/bukkit/Log.java | 130 +++++++----------- .../pw/yumc/YumCore/commands/CommandHelp.java | 16 +-- .../java/pw/yumc/YumCore/global/I18N.java | 75 ++++++++++ 3 files changed, 127 insertions(+), 94 deletions(-) create mode 100644 src/main/java/pw/yumc/YumCore/global/I18N.java diff --git a/src/main/java/pw/yumc/YumCore/bukkit/Log.java b/src/main/java/pw/yumc/YumCore/bukkit/Log.java index 3d38f55..0e7d23c 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/Log.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/Log.java @@ -1,19 +1,19 @@ package pw.yumc.YumCore.bukkit; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + import java.io.File; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; - /** * 插件日志输出类 * - * @since 2016年7月23日 上午9:11:01 * @author 喵♂呜 + * @since 2016年7月23日 上午9:11:01 */ public class Log { private static boolean debug = new File(String.format("plugins%1$sYumCore%1$sdebug", File.separatorChar)).exists(); @@ -31,11 +31,9 @@ public class Log { * Typically the root Logger is configured with a set of Handlers * that essentially act as default handlers for all loggers. * - * @param handler - * a logging Handler - * @exception SecurityException - * if a security manager exists and if - * the caller does not have LoggingPermission("control"). + * @param handler a logging Handler + * @throws SecurityException if a security manager exists and if + * the caller does not have LoggingPermission("control"). */ public static void addHandler(Handler handler) throws SecurityException { logger.addHandler(handler); @@ -44,8 +42,7 @@ public class Log { /** * Sends console a message * - * @param message - * Message to be displayed + * @param message Message to be displayed */ public static void console(String message) { console.sendMessage(prefix + message); @@ -54,10 +51,8 @@ public class Log { /** * Sends console a message * - * @param message - * 消息 - * @param object - * 格式化参数 + * @param message 消息 + * @param object 格式化参数 */ public static void console(String message, Object... object) { console.sendMessage(prefix + String.format(message, object)); @@ -66,8 +61,7 @@ public class Log { /** * Sends console a message * - * @param message - * Message to be displayed + * @param msg Message to be displayed */ public static void console(String[] msg) { for (String str : msg) { @@ -78,10 +72,8 @@ public class Log { /** * 调试消息 * - * @param msg - * 消息 - * @param object - * 参数 + * @param msg 消息 + * @param object 参数 */ public static void d(String msg, Object... object) { debug(String.format(msg, object)); @@ -90,8 +82,7 @@ public class Log { /** * 格式化调试消息 * - * @param msg - * 消息 + * @param msg 消息 */ public static void debug(String msg) { if (debug) { @@ -102,10 +93,8 @@ public class Log { /** * 调试消息 * - * @param msg - * 消息 - * @param object - * 参数 + * @param msg 消息 + * @param object 参数 */ public static void debug(String msg, Object... object) { if (debug) { @@ -116,10 +105,8 @@ public class Log { /** * 调试消息 * - * @param msg - * 消息 - * @param e - * 异常 + * @param msg 消息 + * @param e 异常 */ public static void debug(String msg, Throwable e) { if (debug) { @@ -131,8 +118,7 @@ public class Log { /** * 调试消息 * - * @param e - * 异常 + * @param e 异常 */ public static void debug(Throwable e) { if (debug) { @@ -147,6 +133,10 @@ public class Log { return prefix; } + public static void i(String msg, Object... objs) { + logger.info(String.format(msg, objs)); + } + /** * Log an INFO message. *

@@ -155,8 +145,7 @@ public class Log { * registered output Handler objects. *

* - * @param msg - * The string message (or a key in the message catalog) + * @param msg The string message (or a key in the message catalog) */ public static void info(String msg) { logger.info(msg); @@ -170,10 +159,8 @@ public class Log { * registered output Handler objects. *

* - * @param level - * One of the message level identifiers, e.g., SEVERE - * @param msg - * The string message (or a key in the message catalog) + * @param level One of the message level identifiers, e.g., SEVERE + * @param msg The string message (or a key in the message catalog) */ public static void log(Level level, String msg) { logger.log(level, msg); @@ -187,12 +174,9 @@ public class Log { * to all the registered output Handler objects. *

* - * @param level - * One of the message level identifiers, e.g., SEVERE - * @param msg - * The string message (or a key in the message catalog) - * @param param1 - * parameter to the message + * @param level One of the message level identifiers, e.g., SEVERE + * @param msg The string message (or a key in the message catalog) + * @param param1 parameter to the message */ public static void log(Level level, String msg, Object param1) { logger.log(level, msg, param1); @@ -206,12 +190,9 @@ public class Log { * to all the registered output Handler objects. *

* - * @param level - * One of the message level identifiers, e.g., SEVERE - * @param msg - * The string message (or a key in the message catalog) - * @param params - * array of parameters to the message + * @param level One of the message level identifiers, e.g., SEVERE + * @param msg The string message (or a key in the message catalog) + * @param params array of parameters to the message */ public static void log(Level level, String msg, Object[] params) { logger.log(level, msg, params); @@ -230,20 +211,16 @@ public class Log { * as a formatting parameter to the LogRecord message property. *

* - * @param level - * One of the message level identifiers, e.g., SEVERE - * @param msg - * The string message (or a key in the message catalog) - * @param thrown - * Throwable associated with log message. + * @param level One of the message level identifiers, e.g., SEVERE + * @param msg The string message (or a key in the message catalog) + * @param thrown Throwable associated with log message. */ public static void log(Level level, String msg, Throwable thrown) { logger.log(level, msg, thrown); } /** - * @param prefix - * 插件前缀 + * @param prefix 插件前缀 */ public static void setPrefix(String prefix) { Log.prefix = ChatColor.translateAlternateColorCodes('&', prefix); @@ -254,8 +231,7 @@ public class Log { *

* If the logger is currently enabled for the SEVERE message level then the given message is forwarded to all the registered output Handler objects. * - * @param msg - * The string message (or a key in the message catalog) + * @param msg The string message (or a key in the message catalog) */ public static void severe(String msg) { logger.severe(msg); @@ -264,10 +240,8 @@ public class Log { /** * Sends this sender a message * - * @param sender - * 命令发送者 - * @param msg - * 消息 + * @param sender 命令发送者 + * @param msg 消息 */ public static void toSender(CommandSender sender, String msg) { sender.sendMessage(prefix + msg); @@ -276,12 +250,9 @@ public class Log { /** * Sends this sender a message * - * @param sender - * 命令发送者 - * @param msg - * 消息 - * @param objs - * 参数 + * @param sender 命令发送者 + * @param msg 消息 + * @param objs 参数 */ public static void toSender(CommandSender sender, String msg, Object... objs) { sender.sendMessage(prefix + String.format(msg, objs)); @@ -290,10 +261,8 @@ public class Log { /** * Sends this sender a message * - * @param sender - * 命令发送者 - * @param msg - * 消息 + * @param sender 命令发送者 + * @param msg 消息 */ public static void toSender(CommandSender sender, String[] msg) { for (String str : msg) { @@ -304,10 +273,8 @@ public class Log { /** * 格式化警告消息 * - * @param string - * 消息 - * @param objects - * 参数 + * @param string 消息 + * @param objects 参数 */ public static void w(String string, Object... objects) { logger.warning(String.format(string, objects)); @@ -321,8 +288,7 @@ public class Log { * registered output Handler objects. *

* - * @param msg - * The string message (or a key in the message catalog) + * @param msg The string message (or a key in the message catalog) */ public static void warning(String msg) { logger.warning(msg); diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java index d9bb355..a5a03df 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java @@ -1,22 +1,14 @@ package pw.yumc.YumCore.commands; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - import org.bukkit.command.Command; import org.bukkit.command.CommandSender; - import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.info.CommandInfo; import pw.yumc.YumCore.commands.interfaces.CommandHelpParse; +import java.util.*; + /** * 命令帮助生成类 * @@ -28,9 +20,9 @@ public class CommandHelp { * 消息配置 */ private static String prefix = String.format("§6[§b%s§6] ", P.instance.getName()); - private static String commandNotFound = prefix + "§c当前插件未注册任何子命令!"; + private static String commandNotFound = prefix + "§c当前插件未注册默认命令以及子命令!"; private static String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s"; - private static String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix); + private static String helpTitle = String.format("§6========= %s §6插件帮助列表 §aBy §b喵♂呜 §6=========", prefix); private static String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s"; private static String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s"; /** diff --git a/src/main/java/pw/yumc/YumCore/global/I18N.java b/src/main/java/pw/yumc/YumCore/global/I18N.java new file mode 100644 index 0000000..5f2e265 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/global/I18N.java @@ -0,0 +1,75 @@ +package pw.yumc.YumCore.global; + +import org.bukkit.command.CommandSender; +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.config.ext.YumConfig; + +import java.util.HashMap; +import java.util.Map; + +/** + * 国际化工具类 + * Created by 喵♂呜 on 2016/10/14 0014. + */ +public class I18N { + private static String LANG = "zh_CN"; + private static Map content; + + static { + content = new HashMap<>(); + Log.info("Async init I18N tool ..."); + load(); + } + + private I18N() { + } + + public static String $(String key) { + return $(key, ""); + } + + public static String $(String key, String def) { + return content.containsKey(key) ? content.get(key) : def; + } + + public static void info(String str, Object... objs) { + Log.i($(str), objs); + } + + public static void info(String str, String def, Object... objs) { + Log.i($(str, def), objs); + } + + public static void send(CommandSender sender, String message, Object... objs) { + send(sender, message, "", objs); + } + + public static void send(CommandSender sender, String message, String def, Object... objs) { + Log.toSender(sender, $(message, def), objs); + } + + /** + * 载入数据 + */ + private static void load() { + new Thread(new Runnable() { + @Override + public void run() { + try { + Map local = YumConfig.getLocal(LANG).getContentMap(); + Map remote = YumConfig.getRemote(LANG).getContentMap(); + if (local != null) { + content.putAll(local); + } + if (remote != null) { + content.putAll(remote); + } + Log.info("本地化工具初始化完毕..."); + } catch (Exception e) { + Log.warning(String.format("本地化工具初始化失败: %s %s", e.getClass().getName(), e.getMessage())); + Log.debug(LANG, e); + } + } + }).start(); + } +}