|
| 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 | +} |
0 commit comments