Skip to content

Commit

Permalink
添加引用回复内容的渲染; 更新版本号 1.7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Jan 30, 2023
1 parent eb17219 commit 28cf976
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Chat2QQ-Bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>Chat2QQ</artifactId>
<groupId>io.github.dreamvoid</groupId>
<version>1.7.3</version>
<version>1.7.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package me.dreamvoid.chat2qq.bukkit.listener;

import me.clip.placeholderapi.PlaceholderAPI;
import me.dreamvoid.chat2qq.bukkit.BukkitPlugin;
import me.dreamvoid.miraimc.api.MiraiMC;
import me.dreamvoid.miraimc.bukkit.event.message.passive.MiraiGroupMessageEvent;
import org.bukkit.Bukkit;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static me.dreamvoid.chat2qq.bukkit.utils.renderGroupMessage.*;
import static org.bukkit.Bukkit.getServer;

public class onGroupMessage implements Listener {
private final BukkitPlugin plugin;
Expand All @@ -22,139 +22,159 @@ public onGroupMessage(BukkitPlugin plugin){
public void onGroupMessageReceive(MiraiGroupMessageEvent e) {
if(plugin.getConfig().getStringList("blacklist.word").stream().anyMatch(s -> e.getMessage().contains(s)) || plugin.getConfig().getLongList("blacklist.qq").contains(e.getSenderID())) return;

String name = e.getSenderNameCard();
if(name.equalsIgnoreCase("") && plugin.getConfig().getBoolean("general.use-nick-if-namecard-null",true)){
name = e.getSenderName();
}

String message = e.getMessage();
// 判断消息是否带前缀
boolean allowPrefix = false;
if(plugin.getConfig().getBoolean("general.requite-special-word-prefix.enabled",false)){
for(String prefix : plugin.getConfig().getStringList("general.requite-special-word-prefix.prefix")){
if(e.getMessage().startsWith(prefix)){
allowPrefix = true;
message = message.substring(prefix.length());
break;
}
}
} else allowPrefix = true;


// 预设的格式调整功能. 是否删除 %message% 消息 中的格式化字符
if(plugin.getConfig().getBoolean("aplini.other-format-presets.render-message_format-code",false)){
message = message.replace("§", "");
}

// 预设的格式调整功能. 是否删除 %nick% 群名片 中的格式化字符
if(plugin.getConfig().getBoolean("aplini.other-format-presets.render-nick_format-code",true)){
name = name.replace("§", "");
}

// 预设的格式调整功能. 更好的多行消息
if(plugin.getConfig().getBoolean("aplini.other-format-presets.multiline-message.enabled",true) && message.contains("\n")){
String _l0 = plugin.getConfig().getString("aplini.other-format-presets.multiline-message.line-0","line-0");
String _l1 = plugin.getConfig().getString("aplini.other-format-presets.multiline-message.line-prefix","line-prefix");
message = _l0 +"\n"+ _l1 + message.replace("\n", "\n"+ _l1);
}


// cleanup-name
String $regex_nick = "%regex_nick%";
if(plugin.getConfig().getBoolean("aplini.cleanup-name.enabled",false)){
Matcher matcher = Pattern.compile(plugin.getConfig().getString("aplini.cleanup-name.regex")).matcher(name);
if(matcher.find()){
$regex_nick = matcher.group(1);
} else {
$regex_nick = plugin.getConfig().getString("aplini.cleanup-name.not-captured")
.replace("%groupname%", e.getGroupName())
.replace("%groupid%", String.valueOf(e.getGroupID()))
.replace("%nick%", name)
// .replace("%regex_nick%", "%regex_nick%")
.replace("%qq%", String.valueOf(e.getSenderID()))
.replace("%message%", message);
}
}

// 预处理
if(plugin.getConfig().getBoolean("aplini.pretreatment.enabled",false)){
for(Map<?, ?> config : plugin.getConfig().getMapList("aplini.pretreatment.list")){
// 前缀匹配
if(config.get("prefix") != null && message.startsWith((String) config.get("prefix"))){
if(config.get("send") != null){
return;
}
else if(config.get("to_all") != null){
message = (String) config.get("to_all");
}
else if(config.get("to_replace") != null){
message = message.replace((String) config.get("prefix"), (String) config.get("to_replace"));
}
}

// 包含
else if(config.get("contain") != null && message.contains((String) config.get("contain"))){
if(config.get("send") != null){
return;
}
else if(config.get("to_replace") != null){
message = message.replace((String) config.get("contain"), (String) config.get("to_replace"));
}
else if(config.get("to_all") != null){
message = (String) config.get("to_all");
}
}

// 相等
else if(config.get("equal") != null && Objects.equals(message, config.get("equal"))){
if(config.get("send") != null){
return;
}
else if(config.get("to_all") != null){
message = (String) config.get("to_all");
}
}

// 正则匹配
else if(config.get("regular") != null && Pattern.compile((String) config.get("regular")).matcher(message).find()){
if(config.get("send") != null){
return;
}
else if(config.get("to_regular") != null){
message = message.replaceAll((String) config.get("regular"), (String) config.get("to_regular"));
}
else if(config.get("to_all") != null){
message = (String) config.get("to_all");
}
}
// // 预设的格式调整功能. 是否删除 %message% 消息 中的格式化字符
// if(plugin.getConfig().getBoolean("aplini.other-format-presets.render-message_format-code",false)){
// message = message.replace("§", "");
// }
//
// // 预设的格式调整功能. 是否删除 %nick% 群名片 中的格式化字符
// if(plugin.getConfig().getBoolean("aplini.other-format-presets.render-nick_format-code",true)){
// name = name.replace("§", "");
// }
//
// // 预设的格式调整功能. 更好的多行消息
// if(plugin.getConfig().getBoolean("aplini.other-format-presets.multiline-message.enabled",true) && message.contains("\n")){
// String _l0 = plugin.getConfig().getString("aplini.other-format-presets.multiline-message.line-0","line-0");
// String _l1 = plugin.getConfig().getString("aplini.other-format-presets.multiline-message.line-prefix","line-prefix");
// message = _l0 +"\n"+ _l1 + message.replace("\n", "\n"+ _l1);
// }
//
//
// // cleanup-name
// String $regex_nick = "%regex_nick%";
// if(plugin.getConfig().getBoolean("aplini.cleanup-name.enabled",false)){
// Matcher matcher = Pattern.compile(plugin.getConfig().getString("aplini.cleanup-name.regex")).matcher(name);
// if(matcher.find()){
// $regex_nick = matcher.group(1);
// } else {
// $regex_nick = plugin.getConfig().getString("aplini.cleanup-name.not-captured")
// .replace("%groupname%", e.getGroupName())
// .replace("%groupid%", String.valueOf(e.getGroupID()))
// .replace("%nick%", name)
//// .replace("%regex_nick%", "%regex_nick%")
// .replace("%qq%", String.valueOf(e.getSenderID()))
// .replace("%message%", message);
// }
// }
//
// // 预处理
// if(plugin.getConfig().getBoolean("aplini.pretreatment.enabled",false)){
// for(Map<?, ?> config : plugin.getConfig().getMapList("aplini.pretreatment.list")){
// // 前缀匹配
// if(config.get("prefix") != null && message.startsWith((String) config.get("prefix"))){
// if(config.get("send") != null){
// return;
// }
// else if(config.get("to_all") != null){
// message = (String) config.get("to_all");
// }
// else if(config.get("to_replace") != null){
// message = message.replace((String) config.get("prefix"), (String) config.get("to_replace"));
// }
// }
//
// // 包含
// else if(config.get("contain") != null && message.contains((String) config.get("contain"))){
// if(config.get("send") != null){
// return;
// }
// else if(config.get("to_replace") != null){
// message = message.replace((String) config.get("contain"), (String) config.get("to_replace"));
// }
// else if(config.get("to_all") != null){
// message = (String) config.get("to_all");
// }
// }
//
// // 相等
// else if(config.get("equal") != null && Objects.equals(message, config.get("equal"))){
// if(config.get("send") != null){
// return;
// }
// else if(config.get("to_all") != null){
// message = (String) config.get("to_all");
// }
// }
//
// // 正则匹配
// else if(config.get("regular") != null && Pattern.compile((String) config.get("regular")).matcher(message).find()){
// if(config.get("send") != null){
// return;
// }
// else if(config.get("to_regular") != null){
// message = message.replaceAll((String) config.get("regular"), (String) config.get("to_regular"));
// }
// else if(config.get("to_all") != null){
// message = (String) config.get("to_all");
// }
// }
// }
// }



// String formatText;
// if(plugin.getConfig().getBoolean("general.use-miraimc-bind",false) && MiraiMC.getBind(e.getSenderID()) != null){
// formatText = plugin.getConfig().getString("general.bind-chat-format")
// .replace("%groupname%",e.getGroupName())
// .replace("%groupid%",String.valueOf(e.getGroupID()))
// .replace("%nick%",name)
// .replace("%regex_nick%", $regex_nick)
// .replace("%qq%",String.valueOf(e.getSenderID()))
// .replace("%message%", message);
//// if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
//// formatText = PlaceholderAPI.setPlaceholders(Bukkit.getOfflinePlayer(MiraiMC.getBind(e.getSenderID())), formatText);
//// }
// } else {
// formatText = plugin.getConfig().getString("general.in-game-chat-format")
// .replace("%groupname%",e.getGroupName())
// .replace("%groupid%",String.valueOf(e.getGroupID()))
// .replace("%nick%",name)
// .replace("%regex_nick%", $regex_nick)
// .replace("%qq%",String.valueOf(e.getSenderID()))
// .replace("%message%", message);
// }


String message = renderMessage(plugin, e);


if(! message.equals("") &&
plugin.getConfig().getLongList("bot.bot-accounts").contains(e.getBotID()) &&
plugin.getConfig().getLongList("general.group-ids").contains(e.getGroupID())){

TextComponent formatText = new TextComponent(message);

// 回复消息
if(e.getQuoteReplyMessage() != null){
String replyMessage = plugin.getConfig().getString("aplini.reply-message.message")
.replace("%qq%", ""+ e.getQuoteReplySenderID())
.replace("%_/n_%", "\n")
.replace("%message%", ""+ _renderMessage(plugin, e.getQuoteReplyMessage()));

formatText.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(replyMessage).create()));
}
}

String formatText;
if(plugin.getConfig().getBoolean("general.use-miraimc-bind",false) && MiraiMC.getBind(e.getSenderID()) != null){
formatText = plugin.getConfig().getString("general.bind-chat-format")
.replace("%groupname%",e.getGroupName())
.replace("%groupid%",String.valueOf(e.getGroupID()))
.replace("%nick%",name)
.replace("%regex_nick%", $regex_nick)
.replace("%qq%",String.valueOf(e.getSenderID()))
.replace("%message%", message);
if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
formatText = PlaceholderAPI.setPlaceholders(Bukkit.getOfflinePlayer(MiraiMC.getBind(e.getSenderID())), formatText);
}
} else {
formatText = plugin.getConfig().getString("general.in-game-chat-format")
.replace("%groupname%",e.getGroupName())
.replace("%groupid%",String.valueOf(e.getGroupID()))
.replace("%nick%",name)
.replace("%regex_nick%", $regex_nick)
.replace("%qq%",String.valueOf(e.getSenderID()))
.replace("%message%", message);
//
getServer().spigot().broadcast(formatText);
}

if(plugin.getConfig().getLongList("bot.bot-accounts").contains(e.getBotID()) && plugin.getConfig().getLongList("general.group-ids").contains(e.getGroupID()) && allowPrefix){
Bukkit.broadcastMessage(formatText);
}
// if(plugin.getConfig().getLongList("bot.bot-accounts").contains(e.getBotID()) && plugin.getConfig().getLongList("general.group-ids").contains(e.getGroupID())){
//
// String message1 = renderGroupMessage(plugin, e);
//
// TextComponent formatText2 = new TextComponent(formatText);
// // 回复消息
// if(e.getQuoteReplyMessage() != null){
// formatText2.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(e.getQuoteReplyMessage()).create()));
// }
//
// getServer().spigot().broadcast(formatText2);
// }
}

}
Loading

0 comments on commit 28cf976

Please sign in to comment.