[+] 再重写消息转发逻辑

This commit is contained in:
Taskeren 2019-08-08 03:01:10 +08:00
parent ad598de368
commit ce2bd48be8
3 changed files with 79 additions and 60 deletions

View File

@ -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));
}
evt.setMessage(message);
public static String replaces(String format, String username, String message) {
return format.replace("$PLAYER$", username).replace("$MESSAGE$", message).replace("&", FORMAT_CODE);
}
StringBuffer sb = new StringBuffer();
sb.append("\uff3b").append(username).append("\uff3d ");
sb.append(message);
public static class Message {
BotApi.sendGroupMessage(Config.group_id, sb.toString());
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;
}
}
public boolean available() {
return available;
}
public String content() {
return CQUtils.removeCqCode(message);
}
private String getPrefix() {
for(String p : Config.chatting_prefixes) {
if(message.startsWith(p)) {
return p;
}
}
return null;
}
}
public static boolean isPrefixed(String message) {
if(!Config.require_prefix) return true;
for(String prefix : Config.chatting_prefixes) {
if(message.startsWith(prefix)) return true;
}
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);
}
}

View File

@ -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();
}

View File

@ -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=您不是管理员!