diff --git a/pom.xml b/pom.xml index 3f70252..49e72d6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.handyplus.chat PlayerChat - 3.2.5 + 3.3.0 一款有点好用的聊天插件 diff --git a/src/main/java/cn/handyplus/chat/core/ChatUtil.java b/src/main/java/cn/handyplus/chat/core/ChatUtil.java index a8541a1..d711da5 100644 --- a/src/main/java/cn/handyplus/chat/core/ChatUtil.java +++ b/src/main/java/cn/handyplus/chat/core/ChatUtil.java @@ -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; } @@ -203,7 +203,7 @@ private static String replaceStr(Player player, String channelName, String str) * @param strList 内容集合 * @return 新内容 */ - private static List replaceStr(Player player, String channelName, List strList) { + public static List replaceStr(Player player, String channelName, List strList) { if (CollUtil.isEmpty(strList)) { return strList; } diff --git a/src/main/java/cn/handyplus/chat/core/ShortcutUtil.java b/src/main/java/cn/handyplus/chat/core/ShortcutUtil.java new file mode 100644 index 0000000..50b9f79 --- /dev/null +++ b/src/main/java/cn/handyplus/chat/core/ShortcutUtil.java @@ -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 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 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 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 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 replaceTextFilterVar(List strList, List vars) { + if (CollUtil.isEmpty(strList) || CollUtil.isEmpty(vars)) { + return strList; + } + List 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 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 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; + } + } + +} diff --git a/src/main/java/cn/handyplus/chat/listener/PlayerChatListener.java b/src/main/java/cn/handyplus/chat/listener/PlayerChatListener.java index b79eda3..f6080bb 100644 --- a/src/main/java/cn/handyplus/chat/listener/PlayerChatListener.java +++ b/src/main/java/cn/handyplus/chat/listener/PlayerChatListener.java @@ -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; @@ -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); // @玩家处理 chatParam.setMentionedPlayers(mentionedPlayers); // 有权限进行颜色代码处理 diff --git a/src/main/java/cn/handyplus/chat/util/ConfigUtil.java b/src/main/java/cn/handyplus/chat/util/ConfigUtil.java index 447378d..159cfe8 100644 --- a/src/main/java/cn/handyplus/chat/util/ConfigUtil.java +++ b/src/main/java/cn/handyplus/chat/util/ConfigUtil.java @@ -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; /** * 加载全部配置 @@ -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(); diff --git a/src/main/resources/chat.yml b/src/main/resources/chat.yml index bd43e03..060c75a 100644 --- a/src/main/resources/chat.yml +++ b/src/main/resources/chat.yml @@ -161,4 +161,4 @@ chat: hover: - '&7时间: %server_time_h:mm:ss a%' # 点击后执行的命令 - click: '' \ No newline at end of file + click: '' diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 488c656..554cba8 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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 ] diff --git a/src/main/resources/shortcut.yml b/src/main/resources/shortcut.yml new file mode 100644 index 0000000..58bf827 --- /dev/null +++ b/src/main/resources/shortcut.yml @@ -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%'