Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.handyplus.chat</groupId>
<artifactId>PlayerChat</artifactId>
<version>3.2.5</version>
<version>3.3.0</version>
<description>一款有点好用的聊天插件</description>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/handyplus/chat/core/ChatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public synchronized static void sendTextMsg(BcUtil.BcMessageParam param, boolean
* @param str 内容
* @return 新内容
*/
private static String replaceStr(Player player, String channelName, String str) {
public static String replaceStr(Player player, String channelName, String str) {
if (StrUtil.isEmpty(str)) {
return str;
}
Expand All @@ -203,7 +203,7 @@ private static String replaceStr(Player player, String channelName, String str)
* @param strList 内容集合
* @return 新内容
*/
private static List<String> replaceStr(Player player, String channelName, List<String> strList) {
public static List<String> replaceStr(Player player, String channelName, List<String> strList) {
if (CollUtil.isEmpty(strList)) {
return strList;
}
Expand Down
155 changes: 155 additions & 0 deletions src/main/java/cn/handyplus/chat/core/ShortcutUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package cn.handyplus.chat.core;

import cn.handyplus.chat.param.ChatChildParam;
import cn.handyplus.chat.param.ChatParam;
import cn.handyplus.chat.util.ConfigUtil;
import cn.handyplus.lib.core.CollUtil;
import cn.handyplus.lib.core.PatternUtil;
import cn.handyplus.lib.core.StrUtil;
import cn.handyplus.lib.util.BaseUtil;
import cn.handyplus.lib.util.MessageUtil;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
* 快捷触发工具
*
* @author handy
* @since 3.3.0
*/
public final class ShortcutUtil {

private ShortcutUtil() {
}

/**
* 快捷键处理
*
* @param player 玩家
* @param channel 频道
* @param chatParam 参数
*/
public static void convert(@NotNull Player player, @NotNull String channel, ChatParam chatParam) {
if (!ConfigUtil.SHORTCUT_CONFIG.getBoolean("enable", false)) {
return;
}
if (chatParam == null || CollUtil.isEmpty(chatParam.getChildList())) {
return;
}
String message = chatParam.getMessage();
String channelName = ChannelUtil.getChannelName(channel);
String stripColorMessage = BaseUtil.stripColor(message);
Set<String> shortcutKeySet = ConfigUtil.SHORTCUT_CONFIG.getKeys(false);
for (String key : shortcutKeySet) {
if ("enable".equalsIgnoreCase(key)) {
continue;
}
// 正则匹配
String pattern = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".pattern", "");
if (!PatternUtil.isMatch(pattern, stripColorMessage)) {
continue;
}
// 文本过滤匹配
String textFilter = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".text-filter");
List<String> textFilterVars = CollUtil.of();
if (StrUtil.isNotEmpty(textFilter)) {
textFilterVars = extractRegexVars(stripColorMessage, textFilter, key + ".text-filter");
if (textFilterVars == null) {
continue;
}
}
String text = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".display.text");
List<String> hover = ConfigUtil.SHORTCUT_CONFIG.getStringList(key + ".display.hover");
String click = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".display.click");
String clickSuggest = ConfigUtil.SHORTCUT_CONFIG.getString(key + ".display.clickSuggest");
if (StrUtil.isEmpty(text)) {
continue;
}
// 处理替换
ChatChildParam msgNode = chatParam.getChildList().get(chatParam.getChildList().size() - 1);
text = replaceTextFilterVar(text, textFilterVars);
hover = replaceTextFilterVar(hover, textFilterVars);
click = replaceTextFilterVar(click, textFilterVars);
clickSuggest = replaceTextFilterVar(clickSuggest, textFilterVars);
msgNode.setText(ChatUtil.replaceStr(player, channelName, text));
msgNode.setHover(ChatUtil.replaceStr(player, channelName, hover));
msgNode.setClick(ChatUtil.replaceStr(player, channelName, click));
msgNode.setClickSuggest(ChatUtil.replaceStr(player, channelName, clickSuggest));
msgNode.setHoverItem(null);
return;
}
}

/**
* 文本过滤变量处理
*
* @param str 内容
* @param vars 变量
* @return 新内容
*/
private static String replaceTextFilterVar(String str, List<String> vars) {
if (StrUtil.isEmpty(str) || CollUtil.isEmpty(vars)) {
return str;
}
String result = str;
for (int i = 0; i < vars.size(); i++) {
result = result.replace("{" + i + "}", vars.get(i));
}
return result;
}

/**
* 文本过滤变量处理
*
* @param strList 内容
* @param vars 变量
* @return 新内容
*/
private static List<String> replaceTextFilterVar(List<String> strList, List<String> vars) {
if (CollUtil.isEmpty(strList) || CollUtil.isEmpty(vars)) {
return strList;
}
List<String> newStrList = new ArrayList<>();
for (String str : strList) {
newStrList.add(replaceTextFilterVar(str, vars));
}
return newStrList;
}

/**
* 正则提取变量
*
* @param message 消息
* @param regex 正则
* @param key 节点
* @return 变量集合, null表示未匹配
*/
private static List<String> extractRegexVars(String message, String regex, String key) {
if (StrUtil.isEmpty(regex)) {
return Collections.emptyList();
}
try {
Matcher matcher = Pattern.compile(regex).matcher(message);
if (!matcher.matches()) {
return null;
}
List<String> vars = new ArrayList<>();
for (int i = 0; i <= matcher.groupCount(); i++) {
vars.add(matcher.group(i));
}
return vars;
} catch (PatternSyntaxException ex) {
MessageUtil.sendConsoleDebugMessage("shortcut." + key + " 正则配置错误: " + regex);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.handyplus.chat.constants.ChatConstants;
import cn.handyplus.chat.core.ChannelUtil;
import cn.handyplus.chat.core.ChatUtil;
import cn.handyplus.chat.core.ShortcutUtil;
import cn.handyplus.chat.event.PlayerChannelChatEvent;
import cn.handyplus.chat.event.PlayerChannelTellEvent;
import cn.handyplus.chat.param.ChatParam;
Expand Down Expand Up @@ -79,6 +80,8 @@ public static void sendMsg(Player player, String message, String channel, String
chatParam.setNearbyPlayers(ChannelUtil.getNearbyPlayers(channel, player));
// 原消息内容
chatParam.setMessage(message);
// 快捷键节点替换
ShortcutUtil.convert(player, channel, chatParam);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Evaluate shortcuts before mention rewriting

This call runs after ChatUtil.at(...) has already rewritten the user’s input (it can prepend @ and inject color formatting when a token matches an online player), but ShortcutUtil.convert matches against chatParam.getMessage() rather than the raw chat text. In practice, a shortcut pattern that should match the original input can stop matching whenever the same word is also a player name, so trigger behavior becomes dependent on who is online.

Useful? React with 👍 / 👎.

// @玩家处理
chatParam.setMentionedPlayers(mentionedPlayers);
// 有权限进行颜色代码处理
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/handyplus/chat/util/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @author handy
*/
public class ConfigUtil {
public static FileConfiguration CHAT_CONFIG, LB_CONFIG, ITEM_CONFIG;
public static FileConfiguration CHAT_CONFIG, LB_CONFIG, ITEM_CONFIG, SHORTCUT_CONFIG;

/**
* 加载全部配置
Expand All @@ -25,6 +25,7 @@ public static void init() {
HandyConfigUtil.loadConfig();
HandyConfigUtil.loadLangConfig(true);
CHAT_CONFIG = HandyConfigUtil.load("chat.yml");
SHORTCUT_CONFIG = HandyConfigUtil.load("shortcut.yml");
LB_CONFIG = HandyConfigUtil.load("lb.yml");
ITEM_CONFIG = HandyConfigUtil.load("gui/item.yml");
upConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@ chat:
hover:
- '&7时间: %server_time_h:mm:ss a%'
# 点击后执行的命令
click: ''
click: ''
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PlayerChat
main: cn.handyplus.chat.PlayerChat
version: 3.2.5
version: 3.3.0
author: handy
api-version: 1.13
softdepend: [ PlaceholderAPI, DiscordSRV, AuthMe, CatSeedLogin, OauthLogin ]
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/shortcut.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 快捷触发配置,命中后会替换最后一个消息节点
# pattern/text-filter 为整段匹配
# text-filter 匹配成功后可在 display 中使用 {0}/{1}... 变量
# {0}=整体匹配, {1}开始为分组
enable: false

tp:
pattern: '((tp我)|(TP我)|(TPme)|(TPW)|(TPw)|(TPME))'
# text-filter: '[1-9]([0-9]{0,3})'
display:
text: '&8[&a&l点击传送&8]'
hover:
- '&7点击传送到 &e%player_name% &7身边'
click: '/tpa %player_name%'