From 31d17304f14675d688ce965710a21abe23096705 Mon Sep 17 00:00:00 2001 From: Color_yr <402067010@qq.com> Date: Sat, 4 Apr 2026 12:33:44 +0800 Subject: [PATCH] =?UTF-8?q?up=20=E6=AD=8C=E8=AF=8D=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E5=92=8C=E6=98=BE=E7=A4=BA=E4=BF=A1=E6=81=AF=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/AllMusic.java | 18 ++++++++++- core/command/sub/CommandPush.java | 11 ++++--- core/command/sub/CommandVote.java | 10 ++++-- core/music/PlayRuntime.java | 14 ++++----- core/objs/config/ConfigObj.java | 12 ++++++-- core/objs/message/ARG.java | 1 + core/objs/message/PushObj.java | 4 +-- core/objs/message/VoteObj.java | 4 +-- core/utils/StringReplacer.java | 51 +++++++++++++++++++++++++++++++ netapi/LyricDecoder.java | 20 +++++++----- netapi/NetiApiMain.java | 12 ++++---- version | 2 +- 12 files changed, 123 insertions(+), 36 deletions(-) create mode 100644 core/utils/StringReplacer.java diff --git a/core/AllMusic.java b/core/AllMusic.java index 54e40097..e0f5b309 100644 --- a/core/AllMusic.java +++ b/core/AllMusic.java @@ -12,6 +12,7 @@ import com.coloryr.allmusic.server.core.side.BaseSide; import com.coloryr.allmusic.server.core.side.IAllMusicLogger; import com.coloryr.allmusic.server.core.sql.DataSql; import com.coloryr.allmusic.server.core.sql.IEconomy; +import com.coloryr.allmusic.server.core.utils.StringReplacer; import com.coloryr.allmusic.server.netapi.NetiApiMain; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -54,7 +55,7 @@ public class AllMusic { /** * 语言文件配置版本号 */ - public static final String messageVersion = "211"; + public static final String messageVersion = "212"; /** * 日志 */ @@ -95,6 +96,14 @@ public class AllMusic { * 语言文件 */ private static File messageFile; + /** + * 正则替换器 + */ + private static StringReplacer replacer; + + public static StringReplacer getReplacer() { + return replacer; + } /** * 检查配置文件完整性 @@ -320,6 +329,13 @@ public class AllMusic { if (!AllMusic.configVersion.equalsIgnoreCase(config.version)) { log.data("[AllMusic3]请及时更新配置文件"); } + + if (!config.lyricReplace.isEmpty()) { + for (Map.Entry item : config.lyricReplace.entrySet()) { + replacer.put(item.getKey(), item.getValue()); + replacer = new StringReplacer(); + } + } } catch (Exception e) { log.data("[AllMusic3]读取配置文件错误"); e.printStackTrace(); diff --git a/core/command/sub/CommandPush.java b/core/command/sub/CommandPush.java index 4a07a647..a5b6206c 100644 --- a/core/command/sub/CommandPush.java +++ b/core/command/sub/CommandPush.java @@ -4,6 +4,7 @@ import com.coloryr.allmusic.server.core.AllMusic; import com.coloryr.allmusic.server.core.command.ACommand; import com.coloryr.allmusic.server.core.command.PermissionList; import com.coloryr.allmusic.server.core.music.PlayMusic; +import com.coloryr.allmusic.server.core.music.PlayRuntime; import com.coloryr.allmusic.server.core.objs.message.ARG; import com.coloryr.allmusic.server.core.objs.music.SongInfoObj; import com.coloryr.allmusic.server.core.sql.DataSql; @@ -72,11 +73,12 @@ public class CommandPush extends ACommand { PlayMusic.startPush(name, music); AllMusic.side.sendMessage(sender, AllMusic.getMessage().push.doVote); String data = AllMusic.getMessage().push.bq; - AllMusic.side.broadcast(data - .replace(ARG.player, name) + data = data.replace(ARG.player, name) .replace(ARG.time, String.valueOf(AllMusic.getConfig().voteTime)) .replace(ARG.musicName, music.getName()) - .replace(ARG.musicAuthor, music.getAuthor())); + .replace(ARG.musicAuthor, music.getAuthor()) + .replace(ARG.countAll, String.valueOf(PlayRuntime.getMiniVote())); + AllMusic.side.broadcast(data); AllMusic.side.broadcast(AllMusic.miniMessage(AllMusic.getMessage().push.bq1) .append(AllMusic.miniMessageRun(AllMusic.getMessage().push.bq2, "/music push"))); } else { @@ -89,7 +91,8 @@ public class CommandPush extends ACommand { AllMusic.side.sendMessage(sender, AllMusic.getMessage().push.agree); String data = AllMusic.getMessage().push.bqAgree; data = data.replace(ARG.player, name) - .replace(ARG.count, String.valueOf(PlayMusic.getVoteCount())); + .replace(ARG.count, String.valueOf(PlayMusic.getVoteCount())) + .replace(ARG.countAll, String.valueOf(PlayRuntime.getMiniVote())); AllMusic.side.broadcast(data); } else { AllMusic.side.sendMessage(sender, AllMusic.getMessage().push.arAgree); diff --git a/core/command/sub/CommandVote.java b/core/command/sub/CommandVote.java index 4552c1f1..dc0e6a09 100644 --- a/core/command/sub/CommandVote.java +++ b/core/command/sub/CommandVote.java @@ -4,6 +4,7 @@ import com.coloryr.allmusic.server.core.AllMusic; import com.coloryr.allmusic.server.core.command.ACommand; import com.coloryr.allmusic.server.core.command.PermissionList; import com.coloryr.allmusic.server.core.music.PlayMusic; +import com.coloryr.allmusic.server.core.music.PlayRuntime; import com.coloryr.allmusic.server.core.objs.message.ARG; import com.coloryr.allmusic.server.core.sql.DataSql; @@ -40,8 +41,10 @@ public class CommandVote extends ACommand { PlayMusic.startVote(name); AllMusic.side.sendMessage(sender, AllMusic.getMessage().vote.doVote); String data = AllMusic.getMessage().vote.bq; - AllMusic.side.broadcast(data.replace(ARG.player, name) - .replace(ARG.time, String.valueOf(AllMusic.getConfig().voteTime))); + data = data.replace(ARG.player, name) + .replace(ARG.time, String.valueOf(AllMusic.getConfig().voteTime)) + .replace(ARG.countAll, String.valueOf(PlayRuntime.getMiniVote())); + AllMusic.side.broadcast(data); AllMusic.side.broadcast(AllMusic.miniMessage(AllMusic.getMessage().vote.bq1) .append(AllMusic.miniMessageRun(AllMusic.getMessage().vote.bq2, "/music vote"))); } else { @@ -50,7 +53,8 @@ public class CommandVote extends ACommand { AllMusic.side.sendMessage(sender, AllMusic.getMessage().vote.agree); String data = AllMusic.getMessage().vote.bqAgree; data = data.replace(ARG.player, name) - .replace(ARG.count, String.valueOf(PlayMusic.getVoteCount())); + .replace(ARG.count, String.valueOf(PlayMusic.getVoteCount())) + .replace(ARG.countAll, String.valueOf(PlayRuntime.getMiniVote())); AllMusic.side.broadcast(data); } else { AllMusic.side.sendMessage(sender, AllMusic.getMessage().vote.arAgree); diff --git a/core/music/PlayRuntime.java b/core/music/PlayRuntime.java index f91a9dc1..db6d0a75 100644 --- a/core/music/PlayRuntime.java +++ b/core/music/PlayRuntime.java @@ -137,6 +137,10 @@ public class PlayRuntime { return false; } + public static int getMiniVote() { + return Math.min(AllMusic.getConfig().minVote, AllMusic.side.getPlayers().size()); + } + /** * 事务定时器 */ @@ -156,10 +160,7 @@ public class PlayRuntime { PlayMusic.clearPush(); AllMusic.side.broadcastInTask(AllMusic.getMessage().push.timeOut); } else { - int players = AllMusic.side.getPlayers().size(); - if (PlayMusic.getPushCount() >= AllMusic.getConfig().minVote - || (players <= AllMusic.getConfig().minVote - && players <= PlayMusic.getPushCount())) { + if (PlayMusic.getPushCount() >= getMiniVote()) { PlayMusic.pushMusic(); PlayMusic.clearPush(); AllMusic.side.broadcastInTask(AllMusic.getMessage().push.doPush); @@ -174,10 +175,7 @@ public class PlayRuntime { PlayMusic.clearVote(); AllMusic.side.broadcastInTask(AllMusic.getMessage().vote.timeOut); } else { - int players = AllMusic.side.getPlayers().size(); - if (PlayMusic.getVoteCount() >= AllMusic.getConfig().minVote - || (players <= AllMusic.getConfig().minVote - && players <= PlayMusic.getVoteCount())) { + if (PlayMusic.getVoteCount() >= getMiniVote()) { PlayMusic.musicLessTime = 0; PlayMusic.clearVote(); AllMusic.side.broadcastInTask(AllMusic.getMessage().vote.voteDone); diff --git a/core/objs/config/ConfigObj.java b/core/objs/config/ConfigObj.java index 09918a40..1188fe59 100644 --- a/core/objs/config/ConfigObj.java +++ b/core/objs/config/ConfigObj.java @@ -3,8 +3,7 @@ package com.coloryr.allmusic.server.core.objs.config; import com.coloryr.allmusic.codec.HudPosObj; import com.coloryr.allmusic.server.core.AllMusic; -import java.util.HashSet; -import java.util.Set; +import java.util.*; /** * 配置文件对象 @@ -122,6 +121,10 @@ public class ConfigObj { * 中途加入延迟 */ public int joinDelay; + /** + * 歌词正则替换 + */ + public Map lyricReplace; public static ConfigObj make() { ConfigObj config = new ConfigObj(); @@ -160,6 +163,10 @@ public class ConfigObj { saveConfig = true; cost = CostObj.make(); } + if (lyricReplace == null) { + saveConfig = true; + lyricReplace = new HashMap<>(); + } return saveConfig; } @@ -194,6 +201,7 @@ public class ConfigObj { lyricDelay = 0; sendDelay = 1000; joinDelay = 1000; + lyricReplace = new HashMap<>(); version = AllMusic.configVersion; } } \ No newline at end of file diff --git a/core/objs/message/ARG.java b/core/objs/message/ARG.java index 25fc0070..f3e2f504 100644 --- a/core/objs/message/ARG.java +++ b/core/objs/message/ARG.java @@ -24,5 +24,6 @@ public class ARG { public static String klyric = "%KLyric%"; public static String list = "%List%"; public static String count = "%Count%"; + public static String countAll = "%CountAll%"; public static String time = "%Time%"; } diff --git a/core/objs/message/PushObj.java b/core/objs/message/PushObj.java index 864b4b44..fdaa5073 100644 --- a/core/objs/message/PushObj.java +++ b/core/objs/message/PushObj.java @@ -71,7 +71,7 @@ public class PushObj { if (doVote == null) doVote = "[AllMusic3]已发起插歌投票"; if (bq == null) - bq = "[AllMusic3]" + ARG.player + "发起了插歌投票,将曲目" + ARG.musicName + "-" + ARG.musicAuthor + "调整到下一首播放," + ARG.time + "秒后结束,输入/music push 同意插歌。"; + bq = "[AllMusic3]" + ARG.player + "发起了插歌投票,将曲目" + ARG.musicName + "-" + ARG.musicAuthor + "调整到下一首播放," + ARG.time + "秒后结束,输入/music push 同意插歌,需要至少" + ARG.countAll + "名玩家同意才会插歌。"; if (bq1 == null) bq1 = "[AllMusic3]或者点击 "; if (bq2 == null) @@ -79,7 +79,7 @@ public class PushObj { if (agree == null) agree = "[AllMusic3]你同意插歌"; if (bqAgree == null) - bqAgree = "[AllMusic3]" + ARG.player + "同意插歌,共有" + ARG.count + "名玩家同意插歌。"; + bqAgree = "[AllMusic3]" + ARG.player + "同意插歌,共有" + ARG.count + "名玩家同意插歌,需要至少" + ARG.countAll + "名玩家同意才会插歌。"; if (arAgree == null) arAgree = "[AllMusic3]你已申请插歌"; if (timeOut == null) diff --git a/core/objs/message/VoteObj.java b/core/objs/message/VoteObj.java index c8c91e2e..c76a925f 100644 --- a/core/objs/message/VoteObj.java +++ b/core/objs/message/VoteObj.java @@ -59,7 +59,7 @@ public class VoteObj { if (doVote == null) doVote = "[AllMusic3]已发起切歌投票"; if (bq == null) - bq = "[AllMusic3]" + ARG.player + "发起了切歌投票," + ARG.time + "秒后结束,输入/music vote 同意切歌。"; + bq = "[AllMusic3]" + ARG.player + "发起了切歌投票," + ARG.time + "秒后结束,输入/music vote 同意切歌,需要至少" + ARG.countAll + "名玩家同意才会切歌。"; if (bq1 == null) bq1 = "[AllMusic3]或者点击 "; if (bq2 == null) @@ -67,7 +67,7 @@ public class VoteObj { if (agree == null) agree = "[AllMusic3]你同意切歌"; if (bqAgree == null) - bqAgree = "[AllMusic3]" + ARG.player + "同意切歌,共有" + ARG.count + "名玩家同意切歌。"; + bqAgree = "[AllMusic3]" + ARG.player + "同意切歌,共有" + ARG.count + "名玩家同意切歌,需要至少" + ARG.countAll + "名玩家同意才会切歌。"; if (arAgree == null) arAgree = "[AllMusic3]你已申请切歌"; if (timeOut == null) diff --git a/core/utils/StringReplacer.java b/core/utils/StringReplacer.java new file mode 100644 index 00000000..ae211336 --- /dev/null +++ b/core/utils/StringReplacer.java @@ -0,0 +1,51 @@ +package com.coloryr.allmusic.server.core.utils; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.regex.Pattern; + +public class StringReplacer { + private Map literalMap = new LinkedHashMap<>(); + private Map regexMap = new LinkedHashMap<>(); + + // 自动判断是否为正则表达式 + public void put(String search, String replacement) { + if (isRegex(search)) { + regexMap.put(Pattern.compile(search), replacement); + } else { + literalMap.put(search, replacement); + } + } + + // 判断字符串是否包含正则表达式特征 + private boolean isRegex(String str) { + return str.matches(".*[.\\[\\]{}(),*+?|^$\\\\].*"); + } + + // 强制添加为正则表达式 + public void putRegex(String regex, String replacement) { + regexMap.put(Pattern.compile(regex), replacement); + } + + // 强制添加为普通字符串 + public void putLiteral(String literal, String replacement) { + literalMap.put(literal, replacement); + } + + // 执行替换 + public String replace(String input) { + String result = input; + + // 先替换普通字符串(更快) + for (Map.Entry entry : literalMap.entrySet()) { + result = result.replace(entry.getKey(), entry.getValue()); + } + + // 再替换正则表达式 + for (Map.Entry entry : regexMap.entrySet()) { + result = entry.getKey().matcher(result).replaceAll(entry.getValue()); + } + + return result; + } +} diff --git a/netapi/LyricDecoder.java b/netapi/LyricDecoder.java index 8921fd9b..296664ed 100644 --- a/netapi/LyricDecoder.java +++ b/netapi/LyricDecoder.java @@ -11,13 +11,13 @@ import java.util.regex.Pattern; public class LyricDecoder { private static final Pattern p = Pattern.compile("\\(([0-9]+),[0-9]+.[0-9]\\)"); - private final Map temp = new LinkedHashMap<>(); + private final Map lyrics = new LinkedHashMap<>(); public boolean isHave = false; public boolean isHaveK = false; public Map kly = new LinkedHashMap<>(); - public Map getTemp() { - return temp; + public Map getLyrics() { + return lyrics; } public Map getKLyric() { @@ -62,12 +62,18 @@ public class LyricDecoder { isHaveK = true; } - for (Map.Entry item : temp.entrySet()) { - this.temp.put(item.getKey(), new LyricItemObj(item.getValue(), - haveT ? temp1.get(item.getKey()) : null)); + if (isHaveK) { + for (Map.Entry item : temp.entrySet()) { + this.lyrics.put(item.getKey(), new LyricItemObj(item.getValue(), haveT ? temp1.get(item.getKey()) : null)); + } + } else { + for (Map.Entry item : temp.entrySet()) { + String value = AllMusic.getReplacer().replace(item.getValue()); + String value1 = AllMusic.getReplacer().replace(temp1.get(item.getKey())); + this.lyrics.put(item.getKey(), new LyricItemObj(value, haveT ? value1 : null)); + } } - isHave = true; return false; } diff --git a/netapi/NetiApiMain.java b/netapi/NetiApiMain.java index 492d4405..d8abc95e 100644 --- a/netapi/NetiApiMain.java +++ b/netapi/NetiApiMain.java @@ -205,16 +205,16 @@ public class NetiApiMain implements IMusicApi { if (res != null && res.ok) { try { WLyricObj obj = AllMusic.gson.fromJson(res.data, WLyricObj.class); - LyricDecoder temp = new LyricDecoder(); + LyricDecoder docoder = new LyricDecoder(); for (int times = 0; times < 3; times++) { - if (temp.check(obj)) { + if (docoder.check(obj)) { AllMusic.log.data("[AllMusic3]歌词解析错误,正在进行第" + times + "重试"); } else { - if (temp.isHave) { + if (docoder.isHave) { lyric.setHaveLyric(AllMusic.getConfig().sendLyric); - lyric.setLyric(temp.getTemp()); - if (temp.isHaveK) { - lyric.setKlyric(temp.getKLyric()); + lyric.setLyric(docoder.getLyrics()); + if (docoder.isHaveK) { + lyric.setKlyric(docoder.getKLyric()); } } return lyric; diff --git a/version b/version index ed7555c7..ab6a255a 100644 --- a/version +++ b/version @@ -1 +1 @@ -3.9.4 \ No newline at end of file +3.9.5 \ No newline at end of file