-
Notifications
You must be signed in to change notification settings - Fork 12
3.3.0 自定义快捷触发配置 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
3.3.0 自定义快捷触发配置 #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,4 +161,4 @@ chat: | |
| hover: | ||
| - '&7时间: %server_time_h:mm:ss a%' | ||
| # 点击后执行的命令 | ||
| click: '' | ||
| click: '' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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%' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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), butShortcutUtil.convertmatches againstchatParam.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 👍 / 👎.