[+] 再重写消息转发逻辑
This commit is contained in:
parent
ad598de368
commit
ce2bd48be8
@ -28,9 +28,11 @@ public class Chatting {
|
|||||||
public static final String NODE_CHATTING_TENCENT = "chatting.tencent";
|
public static final String NODE_CHATTING_TENCENT = "chatting.tencent";
|
||||||
public static final String NODE_CHATTING_MINECRAFT = "chatting.minecraft";
|
public static final String NODE_CHATTING_MINECRAFT = "chatting.minecraft";
|
||||||
|
|
||||||
|
public static final String FORMAT_CODE = "\u00a7";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To handle messages from Tencent
|
* To handle messages from Tencent
|
||||||
* @param evt PicqBotX message event
|
* $param evt PicqBotX message event
|
||||||
*/
|
*/
|
||||||
public void onTencentMessage(EventMessage evt) {
|
public void onTencentMessage(EventMessage evt) {
|
||||||
User user = evt.getSender();
|
User user = evt.getSender();
|
||||||
@ -45,36 +47,23 @@ public class Chatting {
|
|||||||
username = user.getInfo().getNickname();
|
username = user.getInfo().getNickname();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isPrefixed(message)) {
|
Message msg = new Message(message);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 检查消息前缀
|
||||||
|
if(!msg.available()) return;
|
||||||
|
|
||||||
|
// 检查QQ号权限
|
||||||
if(!ut.hasPermission(NODE_CHATTING_TENCENT)) {
|
if(!ut.hasPermission(NODE_CHATTING_TENCENT)) {
|
||||||
evt.respond(MessageLib.getUnauthorizedMessage(user));
|
evt.respond(MessageLib.getUnauthorizedMessage(user));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isPrefixed(message)) {
|
plugin.getServer().broadcastMessage(replaces(Config.chattingFormatMinecraft, username, 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());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To handle messages from Minecraft
|
* To handle messages from Minecraft
|
||||||
* @param evt Minecraft chatting event
|
* $param evt Minecraft chatting event
|
||||||
*/
|
*/
|
||||||
public void onMinecraftMessage(AsyncPlayerChatEvent evt) {
|
public void onMinecraftMessage(AsyncPlayerChatEvent evt) {
|
||||||
|
|
||||||
@ -82,56 +71,78 @@ public class Chatting {
|
|||||||
String username = evt.getPlayer().getName();
|
String username = evt.getPlayer().getName();
|
||||||
UserMinecraft um = UserMinecraft.of(username);
|
UserMinecraft um = UserMinecraft.of(username);
|
||||||
|
|
||||||
if(!isPrefixed(message)) {
|
Message msg = new Message(message);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 检查消息前缀
|
||||||
|
if(!msg.available()) return;
|
||||||
|
|
||||||
|
// 检查QQ号绑定
|
||||||
if(um.getTencentId() == -1L) {
|
if(um.getTencentId() == -1L) {
|
||||||
evt.getPlayer().sendMessage(I18n.format("message.using.set-qq-first"));
|
evt.getPlayer().sendMessage(I18n.format("message.using.set-qq-first"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查QQ号权限
|
||||||
UserTencent ut = UserTencent.of(um.getTencentId());
|
UserTencent ut = UserTencent.of(um.getTencentId());
|
||||||
|
|
||||||
if(!ut.hasPermission(NODE_CHATTING_MINECRAFT)) {
|
if(!ut.hasPermission(NODE_CHATTING_MINECRAFT)) {
|
||||||
evt.getPlayer().sendMessage(I18n.format("message.unauthorized"));
|
evt.getPlayer().sendMessage(I18n.format("message.unauthorized"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isPrefixed(message)) {
|
evt.setMessage(msg.content());
|
||||||
|
BotApi.sendGroupMessage(Config.group_id, replaces(Config.chattingFormatTencent, username, message));
|
||||||
if(shouldSubstring(message)) {
|
}
|
||||||
message = removePrefix(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 boolean available() {
|
||||||
|
return available;
|
||||||
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 String content() {
|
||||||
|
return CQUtils.removeCqCode(message);
|
||||||
public static boolean shouldSubstring(String message) {
|
}
|
||||||
if(!Config.require_prefix) return false;
|
|
||||||
return true;
|
private String getPrefix() {
|
||||||
}
|
for(String p : Config.chatting_prefixes) {
|
||||||
|
if(message.startsWith(p)) {
|
||||||
public static String removePrefix(String message) {
|
return p;
|
||||||
return message.length() < 1 ? "" : message.substring(1);
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,10 @@ public class Config {
|
|||||||
// I18n Settings
|
// I18n Settings
|
||||||
public static boolean useJarLanguageFile;
|
public static boolean useJarLanguageFile;
|
||||||
|
|
||||||
|
// The Chatting Formatter
|
||||||
|
public static String chattingFormatTencent;
|
||||||
|
public static String chattingFormatMinecraft;
|
||||||
|
|
||||||
public static void refresh() {
|
public static void refresh() {
|
||||||
|
|
||||||
port_in = cfg.getInt("in", "constructor", 25560, 0, 65535, "The port for receiving messages");
|
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");
|
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();
|
cfg.save();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,12 @@ command.common.unauthorized=无权执行!
|
|||||||
command.about.message.0=Yes, sir!
|
command.about.message.0=Yes, sir!
|
||||||
command.about.message.1=(command with * requires OP_PERMISSION_NODE)
|
command.about.message.1=(command with * requires OP_PERMISSION_NODE)
|
||||||
command.about.message.2=/about[/bot|/help] - show this notice
|
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.3=/op - query if you're operator
|
||||||
command.about.message.4=/perm* [uid] [node] - get the value of the node
|
command.about.message.4=/op* [uid] - set user as operator
|
||||||
command.about.message.5=/perm* [uid] [node] [true/false] - set the value of the node
|
command.about.message.5=/perm* [uid] [node] - get the value of the node
|
||||||
command.about.message.6=/console* [cmd] - to execute command as Console
|
command.about.message.6=/perm* [uid] [node] [true/false] - set the value of the node
|
||||||
command.about.message.7=/list - to list all the online players
|
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.yeap=您是管理员!
|
||||||
command.operator.nope=您不是管理员!
|
command.operator.nope=您不是管理员!
|
||||||
|
Loading…
Reference in New Issue
Block a user