From ce2bd48be8f9d2841dee7c05cd92fc951e5fe7c4 Mon Sep 17 00:00:00 2001 From: Taskeren Date: Thu, 8 Aug 2019 03:01:10 +0800 Subject: [PATCH] =?UTF-8?q?[+]=20=E5=86=8D=E9=87=8D=E5=86=99=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=BD=AC=E5=8F=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nativebot/bot/chatting/Chatting.java | 121 ++++++++++-------- .../ren/taske/nativebot/commons/Config.java | 7 + src/main/resources/i18n.txt | 11 +- 3 files changed, 79 insertions(+), 60 deletions(-) diff --git a/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java b/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java index 76732a9..7199689 100644 --- a/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java +++ b/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java @@ -28,9 +28,11 @@ public class Chatting { public static final String NODE_CHATTING_TENCENT = "chatting.tencent"; public static final String NODE_CHATTING_MINECRAFT = "chatting.minecraft"; + public static final String FORMAT_CODE = "\u00a7"; + /** * To handle messages from Tencent - * @param evt PicqBotX message event + * $param evt PicqBotX message event */ public void onTencentMessage(EventMessage evt) { User user = evt.getSender(); @@ -45,36 +47,23 @@ public class Chatting { username = user.getInfo().getNickname(); } - if(!isPrefixed(message)) { - return; - } + Message msg = new Message(message); + // 检查消息前缀 + if(!msg.available()) return; + + // 检查QQ号权限 if(!ut.hasPermission(NODE_CHATTING_TENCENT)) { evt.respond(MessageLib.getUnauthorizedMessage(user)); return; } - if(isPrefixed(message)) { - - if(shouldSubstring(message)) { - message = removePrefix(message); - } - - message = CQUtils.removeCqCode(message); - - StringBuffer sb = new StringBuffer(); - sb.append("<").append(username).append("> ").append("\u00a7r"); // \u007a = § - sb.append(message); - - plugin.getServer().broadcastMessage(sb.toString()); - - } - + plugin.getServer().broadcastMessage(replaces(Config.chattingFormatMinecraft, username, message)); } /** * To handle messages from Minecraft - * @param evt Minecraft chatting event + * $param evt Minecraft chatting event */ public void onMinecraftMessage(AsyncPlayerChatEvent evt) { @@ -82,56 +71,78 @@ public class Chatting { String username = evt.getPlayer().getName(); UserMinecraft um = UserMinecraft.of(username); - if(!isPrefixed(message)) { - return; - } + Message msg = new Message(message); + // 检查消息前缀 + if(!msg.available()) return; + + // 检查QQ号绑定 if(um.getTencentId() == -1L) { evt.getPlayer().sendMessage(I18n.format("message.using.set-qq-first")); return; } + // 检查QQ号权限 UserTencent ut = UserTencent.of(um.getTencentId()); - if(!ut.hasPermission(NODE_CHATTING_MINECRAFT)) { evt.getPlayer().sendMessage(I18n.format("message.unauthorized")); return; } - if(isPrefixed(message)) { - - if(shouldSubstring(message)) { - message = removePrefix(message); + evt.setMessage(msg.content()); + BotApi.sendGroupMessage(Config.group_id, replaces(Config.chattingFormatTencent, username, message)); + } + + public static String replaces(String format, String username, String message) { + return format.replace("$PLAYER$", username).replace("$MESSAGE$", message).replace("&", FORMAT_CODE); + } + + public static class Message { + + protected String message; + protected boolean available; + + public Message(String message) { + this.message = message; + check(); + } + + /** + * 检查,并移除前缀 + */ + void check() { + if(Config.require_prefix) { + String prefix = getPrefix(); + if(prefix != null) { + available = true; + message.substring(prefix.length()); + } + } + else if(message.startsWith("/")) { + return; + } + else { + available = true; } - - evt.setMessage(message); - - StringBuffer sb = new StringBuffer(); - sb.append("\uff3b").append(username).append("\uff3d "); - sb.append(message); - - BotApi.sendGroupMessage(Config.group_id, sb.toString()); - } - } - - public static boolean isPrefixed(String message) { - if(!Config.require_prefix) return true; - - for(String prefix : Config.chatting_prefixes) { - if(message.startsWith(prefix)) return true; + public boolean available() { + return available; } - return false; - } - - public static boolean shouldSubstring(String message) { - if(!Config.require_prefix) return false; - return true; - } - - public static String removePrefix(String message) { - return message.length() < 1 ? "" : message.substring(1); + + public String content() { + return CQUtils.removeCqCode(message); + } + + private String getPrefix() { + for(String p : Config.chatting_prefixes) { + if(message.startsWith(p)) { + return p; + } + } + return null; + } + } } diff --git a/src/main/java/ren/taske/nativebot/commons/Config.java b/src/main/java/ren/taske/nativebot/commons/Config.java index 6b60e88..c0e80a6 100644 --- a/src/main/java/ren/taske/nativebot/commons/Config.java +++ b/src/main/java/ren/taske/nativebot/commons/Config.java @@ -25,6 +25,10 @@ public class Config { // I18n Settings public static boolean useJarLanguageFile; + // The Chatting Formatter + public static String chattingFormatTencent; + public static String chattingFormatMinecraft; + public static void refresh() { port_in = cfg.getInt("in", "constructor", 25560, 0, 65535, "The port for receiving messages"); @@ -38,6 +42,9 @@ public class Config { useJarLanguageFile = cfg.getBoolean("lang_refresh", "lang", false, "True if want use the language file in jar"); + chattingFormatTencent = cfg.getString("formatTencent", "format", "\uff3b$PLAYER$\uff3d $MESSAGE$", "The format of the message to Tencent"); + chattingFormatMinecraft = cfg.getString("formatMinecraft", "format", "<$PLAYER$> $MESSAGE$", "The format of the message to Minecraft"); + cfg.save(); } diff --git a/src/main/resources/i18n.txt b/src/main/resources/i18n.txt index 2d0a05f..3427014 100644 --- a/src/main/resources/i18n.txt +++ b/src/main/resources/i18n.txt @@ -19,11 +19,12 @@ command.common.unauthorized=无权执行! command.about.message.0=Yes, sir! command.about.message.1=(command with * requires OP_PERMISSION_NODE) command.about.message.2=/about[/bot|/help] - show this notice -command.about.message.3=/op - query if you're operator$/op* [uid] - set user as operator -command.about.message.4=/perm* [uid] [node] - get the value of the node -command.about.message.5=/perm* [uid] [node] [true/false] - set the value of the node -command.about.message.6=/console* [cmd] - to execute command as Console -command.about.message.7=/list - to list all the online players +command.about.message.3=/op - query if you're operator +command.about.message.4=/op* [uid] - set user as operator +command.about.message.5=/perm* [uid] [node] - get the value of the node +command.about.message.6=/perm* [uid] [node] [true/false] - set the value of the node +command.about.message.7=/console* [cmd] - to execute command as Console +command.about.message.8=/list - to list all the online players command.operator.yeap=您是管理员! command.operator.nope=您不是管理员!