Skip to content

Commit 39174f0

Browse files
authored
Merge pull request #10 from handy-git/3.3.0
3.3.0 自定义快捷触发配置
2 parents 0c8e86a + 748e6cb commit 39174f0

8 files changed

Lines changed: 179 additions & 6 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cn.handyplus.chat</groupId>
88
<artifactId>PlayerChat</artifactId>
9-
<version>3.2.5</version>
9+
<version>3.3.0</version>
1010
<description>一款有点好用的聊天插件</description>
1111

1212
<properties>

src/main/java/cn/handyplus/chat/core/ChatUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public synchronized static void sendTextMsg(BcUtil.BcMessageParam param, boolean
180180
* @param str 内容
181181
* @return 新内容
182182
*/
183-
private static String replaceStr(Player player, String channelName, String str) {
183+
public static String replaceStr(Player player, String channelName, String str) {
184184
if (StrUtil.isEmpty(str)) {
185185
return str;
186186
}
@@ -203,7 +203,7 @@ private static String replaceStr(Player player, String channelName, String str)
203203
* @param strList 内容集合
204204
* @return 新内容
205205
*/
206-
private static List<String> replaceStr(Player player, String channelName, List<String> strList) {
206+
public static List<String> replaceStr(Player player, String channelName, List<String> strList) {
207207
if (CollUtil.isEmpty(strList)) {
208208
return strList;
209209
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package cn.handyplus.chat.core;
2+
3+
import cn.handyplus.chat.param.ChatChildParam;
4+
import cn.handyplus.chat.param.ChatParam;
5+
import cn.handyplus.chat.util.ConfigUtil;
6+
import cn.handyplus.lib.core.CollUtil;
7+
import cn.handyplus.lib.core.PatternUtil;
8+
import cn.handyplus.lib.core.StrUtil;
9+
import cn.handyplus.lib.util.BaseUtil;
10+
import cn.handyplus.lib.util.MessageUtil;
11+
import org.bukkit.entity.Player;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Set;
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
20+
import java.util.regex.PatternSyntaxException;
21+
22+
/**
23+
* 快捷触发工具
24+
*
25+
* @author handy
26+
* @since 3.3.0
27+
*/
28+
public final class ShortcutUtil {
29+
30+
private ShortcutUtil() {
31+
}
32+
33+
/**
34+
* 快捷键处理
35+
*
36+
* @param player 玩家
37+
* @param channel 频道
38+
* @param chatParam 参数
39+
*/
40+
public static void convert(@NotNull Player player, @NotNull String channel, ChatParam chatParam) {
41+
if (!ConfigUtil.SHORTCUT_CONFIG.getBoolean("enable", false)) {
42+
return;
43+
}
44+
if (chatParam == null || CollUtil.isEmpty(chatParam.getChildList())) {
45+
return;
46+
}
47+
String message = chatParam.getMessage();
48+
String channelName = ChannelUtil.getChannelName(channel);
49+
String stripColorMessage = BaseUtil.stripColor(message);
50+
Set<String> shortcutKeySet = ConfigUtil.SHORTCUT_CONFIG.getKeys(false);
51+
for (String key : shortcutKeySet) {
52+
if ("enable".equalsIgnoreCase(key)) {
53+
continue;
54+
}
55+
// 正则匹配
56+
String pattern = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".pattern", "");
57+
if (!PatternUtil.isMatch(pattern, stripColorMessage)) {
58+
continue;
59+
}
60+
// 文本过滤匹配
61+
String textFilter = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".text-filter");
62+
List<String> textFilterVars = CollUtil.of();
63+
if (StrUtil.isNotEmpty(textFilter)) {
64+
textFilterVars = extractRegexVars(stripColorMessage, textFilter, key + ".text-filter");
65+
if (textFilterVars == null) {
66+
continue;
67+
}
68+
}
69+
String text = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".display.text");
70+
List<String> hover = ConfigUtil.SHORTCUT_CONFIG.getStringList(key + ".display.hover");
71+
String click = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".display.click");
72+
String clickSuggest = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".display.clickSuggest");
73+
if (StrUtil.isEmpty(text)) {
74+
continue;
75+
}
76+
// 处理替换
77+
ChatChildParam msgNode = chatParam.getChildList().get(chatParam.getChildList().size() - 1);
78+
text = replaceTextFilterVar(text, textFilterVars);
79+
hover = replaceTextFilterVar(hover, textFilterVars);
80+
click = replaceTextFilterVar(click, textFilterVars);
81+
clickSuggest = replaceTextFilterVar(clickSuggest, textFilterVars);
82+
msgNode.setText(ChatUtil.replaceStr(player, channelName, text));
83+
msgNode.setHover(ChatUtil.replaceStr(player, channelName, hover));
84+
msgNode.setClick(ChatUtil.replaceStr(player, channelName, click));
85+
msgNode.setClickSuggest(ChatUtil.replaceStr(player, channelName, clickSuggest));
86+
msgNode.setHoverItem(null);
87+
return;
88+
}
89+
}
90+
91+
/**
92+
* 文本过滤变量处理
93+
*
94+
* @param str 内容
95+
* @param vars 变量
96+
* @return 新内容
97+
*/
98+
private static String replaceTextFilterVar(String str, List<String> vars) {
99+
if (StrUtil.isEmpty(str) || CollUtil.isEmpty(vars)) {
100+
return str;
101+
}
102+
String result = str;
103+
for (int i = 0; i < vars.size(); i++) {
104+
result = result.replace("{" + i + "}", vars.get(i));
105+
}
106+
return result;
107+
}
108+
109+
/**
110+
* 文本过滤变量处理
111+
*
112+
* @param strList 内容
113+
* @param vars 变量
114+
* @return 新内容
115+
*/
116+
private static List<String> replaceTextFilterVar(List<String> strList, List<String> vars) {
117+
if (CollUtil.isEmpty(strList) || CollUtil.isEmpty(vars)) {
118+
return strList;
119+
}
120+
List<String> newStrList = new ArrayList<>();
121+
for (String str : strList) {
122+
newStrList.add(replaceTextFilterVar(str, vars));
123+
}
124+
return newStrList;
125+
}
126+
127+
/**
128+
* 正则提取变量
129+
*
130+
* @param message 消息
131+
* @param regex 正则
132+
* @param key 节点
133+
* @return 变量集合, null表示未匹配
134+
*/
135+
private static List<String> extractRegexVars(String message, String regex, String key) {
136+
if (StrUtil.isEmpty(regex)) {
137+
return Collections.emptyList();
138+
}
139+
try {
140+
Matcher matcher = Pattern.compile(regex).matcher(message);
141+
if (!matcher.matches()) {
142+
return null;
143+
}
144+
List<String> vars = new ArrayList<>();
145+
for (int i = 0; i <= matcher.groupCount(); i++) {
146+
vars.add(matcher.group(i));
147+
}
148+
return vars;
149+
} catch (PatternSyntaxException ex) {
150+
MessageUtil.sendConsoleDebugMessage("shortcut." + key + " 正则配置错误: " + regex);
151+
return null;
152+
}
153+
}
154+
155+
}

src/main/java/cn/handyplus/chat/listener/PlayerChatListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cn.handyplus.chat.constants.ChatConstants;
55
import cn.handyplus.chat.core.ChannelUtil;
66
import cn.handyplus.chat.core.ChatUtil;
7+
import cn.handyplus.chat.core.ShortcutUtil;
78
import cn.handyplus.chat.event.PlayerChannelChatEvent;
89
import cn.handyplus.chat.event.PlayerChannelTellEvent;
910
import cn.handyplus.chat.param.ChatParam;
@@ -79,6 +80,8 @@ public static void sendMsg(Player player, String message, String channel, String
7980
chatParam.setNearbyPlayers(ChannelUtil.getNearbyPlayers(channel, player));
8081
// 原消息内容
8182
chatParam.setMessage(message);
83+
// 快捷键节点替换
84+
ShortcutUtil.convert(player, channel, chatParam);
8285
// @玩家处理
8386
chatParam.setMentionedPlayers(mentionedPlayers);
8487
// 有权限进行颜色代码处理

src/main/java/cn/handyplus/chat/util/ConfigUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author handy
1717
*/
1818
public class ConfigUtil {
19-
public static FileConfiguration CHAT_CONFIG, LB_CONFIG, ITEM_CONFIG;
19+
public static FileConfiguration CHAT_CONFIG, LB_CONFIG, ITEM_CONFIG, SHORTCUT_CONFIG;
2020

2121
/**
2222
* 加载全部配置
@@ -25,6 +25,7 @@ public static void init() {
2525
HandyConfigUtil.loadConfig();
2626
HandyConfigUtil.loadLangConfig(true);
2727
CHAT_CONFIG = HandyConfigUtil.load("chat.yml");
28+
SHORTCUT_CONFIG = HandyConfigUtil.load("shortcut.yml");
2829
LB_CONFIG = HandyConfigUtil.load("lb.yml");
2930
ITEM_CONFIG = HandyConfigUtil.load("gui/item.yml");
3031
upConfig();

src/main/resources/chat.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@ chat:
161161
hover:
162162
- '&7时间: %server_time_h:mm:ss a%'
163163
# 点击后执行的命令
164-
click: ''
164+
click: ''

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: PlayerChat
22
main: cn.handyplus.chat.PlayerChat
3-
version: 3.2.5
3+
version: 3.3.0
44
author: handy
55
api-version: 1.13
66
softdepend: [ PlaceholderAPI, DiscordSRV, AuthMe, CatSeedLogin, OauthLogin ]

src/main/resources/shortcut.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 快捷触发配置,命中后会替换最后一个消息节点
2+
# pattern/text-filter 为整段匹配
3+
# text-filter 匹配成功后可在 display 中使用 {0}/{1}... 变量
4+
# {0}=整体匹配, {1}开始为分组
5+
enable: false
6+
7+
tp:
8+
pattern: '((tp我)|(TP我)|(TPme)|(TPW)|(TPw)|(TPME))'
9+
# text-filter: '[1-9]([0-9]{0,3})'
10+
display:
11+
text: '&8[&a&l点击传送&8]'
12+
hover:
13+
- '&7点击传送到 &e%player_name% &7身边'
14+
click: '/tpa %player_name%'

0 commit comments

Comments
 (0)