Files
AllMusic/core/AllMusic.java
T

431 lines
14 KiB
Java
Raw Normal View History

2024-04-19 09:46:54 +08:00
package com.coloryr.allmusic.server.core;
2020-04-19 20:36:12 +08:00
2026-02-26 20:18:57 +08:00
import com.coloryr.allmusic.server.core.music.MusicHttpClient;
import com.coloryr.allmusic.server.core.music.MusicSearch;
import com.coloryr.allmusic.server.core.music.PlayMusic;
import com.coloryr.allmusic.server.core.music.PlayRuntime;
2024-04-19 09:46:54 +08:00
import com.coloryr.allmusic.server.core.objs.CookieObj;
import com.coloryr.allmusic.server.core.objs.config.ConfigObj;
import com.coloryr.allmusic.server.core.objs.message.MessageObj;
2024-08-03 13:03:04 +08:00
import com.coloryr.allmusic.server.core.objs.music.SongInfoObj;
2024-05-13 09:33:41 +08:00
import com.coloryr.allmusic.server.core.side.BaseSide;
2026-03-04 13:57:59 +08:00
import com.coloryr.allmusic.server.core.side.IAllMusicLogger;
2024-04-19 11:14:17 +08:00
import com.coloryr.allmusic.server.core.sql.DataSql;
2024-04-19 09:46:54 +08:00
import com.coloryr.allmusic.server.core.sql.IEconomy;
2026-04-04 12:33:44 +08:00
import com.coloryr.allmusic.server.core.utils.StringReplacer;
2026-02-26 20:18:57 +08:00
import com.coloryr.allmusic.server.netapi.NetiApiMain;
2020-04-19 20:36:12 +08:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
2026-01-02 17:16:49 +08:00
import com.google.gson.reflect.TypeToken;
2026-03-04 13:57:59 +08:00
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.minimessage.MiniMessage;
2020-04-19 20:36:12 +08:00
import java.io.*;
2026-01-02 17:16:49 +08:00
import java.lang.reflect.Type;
2020-04-19 20:36:12 +08:00
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
2023-01-08 20:55:21 +08:00
import java.util.*;
2020-04-19 20:36:12 +08:00
public class AllMusic {
2026-03-04 13:57:59 +08:00
public static final Gson gson = new GsonBuilder()
.disableHtmlEscaping()
2026-03-04 16:05:57 +08:00
.setPrettyPrinting()
2026-03-04 13:57:59 +08:00
.create();
2025-03-06 16:46:21 +08:00
public static final Random random = new Random();
2023-01-09 15:56:26 +08:00
2026-02-26 20:18:57 +08:00
public static final Map<String, IMusicApi> MUSIC_APIS = new HashMap<>();
2023-01-06 17:03:07 +08:00
/**
* 客户端插件信道名
*/
2020-04-25 20:41:08 +08:00
public static final String channel = "allmusic:channel";
2023-01-06 17:03:07 +08:00
/**
* BC插件信道名
*/
2022-08-04 17:03:52 +08:00
public static final String channelBC = "allmusic:channelbc";
2023-01-06 17:03:07 +08:00
/**
* 插件版本号
*/
2026-03-27 18:17:49 +08:00
public static final String version = "3.9.0";
2023-01-06 17:03:07 +08:00
/**
* 配置文件版本号
*/
2026-04-03 15:35:29 +08:00
public static final String configVersion = "207";
2023-01-06 17:03:07 +08:00
/**
* 语言文件配置版本号
*/
2026-04-04 12:33:44 +08:00
public static final String messageVersion = "212";
2023-01-08 20:55:21 +08:00
/**
* 日志
*/
2026-03-04 13:57:59 +08:00
public static IAllMusicLogger log;
2023-01-08 20:55:21 +08:00
/**
* 服务器端操作
*/
2024-05-13 09:33:41 +08:00
public static BaseSide side;
2023-01-08 20:55:21 +08:00
/**
* 是否在运行
*/
public static boolean isRun;
/**
* Cookie对象
*/
2026-01-02 17:16:49 +08:00
public static List<CookieObj> cookie;
2023-01-08 20:55:21 +08:00
/**
* 经济插件对象
*/
public static IEconomy economy;
2023-01-06 17:03:07 +08:00
/**
* 配置对象
*/
2023-01-09 15:56:26 +08:00
private static ConfigObj config;
2023-01-06 17:03:07 +08:00
/**
* 语言对象
*/
2023-01-09 16:54:42 +08:00
private static MessageObj message;
2023-01-06 17:03:07 +08:00
/**
* 配置文件
*/
2021-11-30 13:37:43 +08:00
private static File configFile;
2023-01-06 17:03:07 +08:00
/**
* Cookie文件
*/
2021-11-30 13:37:43 +08:00
private static File cookieFile;
2023-01-06 17:03:07 +08:00
/**
* 语言文件
*/
2021-11-30 13:37:43 +08:00
private static File messageFile;
2026-04-04 12:33:44 +08:00
/**
* 正则替换器
*/
private static StringReplacer replacer;
public static StringReplacer getReplacer() {
return replacer;
}
2020-04-19 20:36:12 +08:00
2023-01-06 17:03:07 +08:00
/**
* 检查配置文件完整性
*/
2021-02-15 16:05:42 +08:00
public static void configCheck() {
2023-08-03 10:15:57 +08:00
if (config == null || config.check()) {
config = ConfigObj.make();
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件config.json错误,已覆盖");
2023-08-03 10:15:57 +08:00
saveConfig();
2021-02-15 16:05:42 +08:00
}
}
2022-01-03 15:27:38 +08:00
2023-01-06 17:03:07 +08:00
/**
* 检查语言文件完整性
*/
2021-02-15 16:05:42 +08:00
private static void messageCheck() {
2023-08-03 10:15:57 +08:00
if (message == null || message.check()) {
message = MessageObj.make();
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件message.json错误,已覆盖");
2023-08-03 10:15:57 +08:00
saveMessage();
2021-02-15 16:05:42 +08:00
}
}
2023-01-08 20:55:21 +08:00
/**
2025-03-24 20:13:54 +08:00
* 检查是否需要放歌
2023-01-08 20:55:21 +08:00
*
* @param name 用户名
* @param server 服务器名
2025-03-24 20:13:54 +08:00
* @param checkPlay 是否检查正在播放的列表
* @return 是否跳过放歌
2023-01-08 20:55:21 +08:00
*/
2025-03-24 20:13:54 +08:00
public static boolean isSkip(String name, String server, boolean checkPlay) {
2023-01-08 20:55:21 +08:00
try {
2024-07-03 15:38:47 +08:00
name = name.toLowerCase();
2024-04-17 22:15:12 +08:00
if (server != null && AllMusic.getConfig().muteServer.contains(server))
2023-01-08 20:55:21 +08:00
return true;
2025-03-24 18:20:45 +08:00
if (DataSql.checkMutePlayer(name))
return true;
if (PlayMusic.nowPlayMusic != null && PlayMusic.nowPlayMusic.isList() && DataSql.checkMuteListPlayer(name))
2023-01-08 20:55:21 +08:00
return true;
2025-03-24 20:13:54 +08:00
if (!checkPlay)
return false;
2026-03-27 18:38:48 +08:00
return PlayMusic.containNowPlay(name);
2025-03-24 20:13:54 +08:00
} catch (NoSuchElementException e) {
return true;
}
}
/**
* 检查是否需要放歌
*
* @param name 用户名
* @param server 服务器名
* @param checkPlay 是否检查正在播放的列表
* @param islist 是否为空闲歌单的歌
* @return 是否跳过放歌
*/
public static boolean isSkip(String name, String server, boolean checkPlay, boolean islist) {
try {
name = name.toLowerCase();
if (server != null && AllMusic.getConfig().muteServer.contains(server))
return true;
if (DataSql.checkMutePlayer(name))
return true;
if (islist && DataSql.checkMuteListPlayer(name))
return true;
if (!checkPlay)
2023-01-08 20:55:21 +08:00
return false;
2026-03-27 18:38:48 +08:00
return PlayMusic.containNowPlay(name);
2023-01-08 20:55:21 +08:00
} catch (NoSuchElementException e) {
return true;
}
}
2023-01-06 17:03:07 +08:00
/**
* 获取配置文件
2023-01-08 20:55:21 +08:00
*
2023-01-06 17:03:07 +08:00
* @return 配置对象
*/
2023-01-09 15:56:26 +08:00
public static ConfigObj getConfig() {
2022-01-03 15:27:38 +08:00
if (config == null) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件config.json错误,已使用默认配置文件");
2023-08-03 10:15:57 +08:00
config = ConfigObj.make();
2022-01-03 15:27:38 +08:00
}
2021-11-30 13:37:43 +08:00
return config;
2020-04-19 20:36:12 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 获取语言文件
2023-01-08 20:55:21 +08:00
*
2023-01-06 17:03:07 +08:00
* @return 语言对象
*/
2023-01-09 16:54:42 +08:00
public static MessageObj getMessage() {
2023-01-06 17:03:07 +08:00
if (message == null) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件message.json错误,已使用默认配置文件");
2023-08-03 10:15:57 +08:00
message = MessageObj.make();
2023-01-06 17:03:07 +08:00
}
2021-11-30 13:37:43 +08:00
return message;
2020-04-19 20:36:12 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 保存配置文件
*/
2023-08-03 10:15:57 +08:00
public static void saveConfig() {
2020-05-02 21:53:22 +08:00
try {
2026-03-04 16:05:57 +08:00
String data = gson.toJson(config);
2021-11-30 13:37:43 +08:00
FileOutputStream out = new FileOutputStream(configFile);
2021-02-15 16:05:42 +08:00
OutputStreamWriter write = new OutputStreamWriter(
out, StandardCharsets.UTF_8);
write.write(data);
write.close();
2023-08-03 10:15:57 +08:00
out.close();
} catch (Exception e) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件保存错误");
2023-08-03 10:15:57 +08:00
e.printStackTrace();
}
}
public static void saveMessage() {
try {
2026-03-04 16:05:57 +08:00
String data = gson.toJson(message);
2023-08-03 10:15:57 +08:00
FileOutputStream out = new FileOutputStream(messageFile);
OutputStreamWriter write = new OutputStreamWriter(
2021-02-15 16:05:42 +08:00
out, StandardCharsets.UTF_8);
write.write(data);
write.close();
2023-08-03 10:15:57 +08:00
out.close();
2020-05-02 21:53:22 +08:00
} catch (Exception e) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件保存错误");
2020-05-02 21:53:22 +08:00
e.printStackTrace();
}
}
2023-01-06 17:03:07 +08:00
/**
* 保存Cookie
*/
2021-04-03 15:55:25 +08:00
public static void saveCookie() {
try {
2026-03-04 16:05:57 +08:00
String data = gson.toJson(cookie);
2021-11-30 13:37:43 +08:00
FileOutputStream out = new FileOutputStream(cookieFile);
2021-04-03 15:55:25 +08:00
OutputStreamWriter write = new OutputStreamWriter(
out, StandardCharsets.UTF_8);
write.write(data);
write.close();
} catch (Exception e) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>配置文件保存错误");
2021-04-03 15:55:25 +08:00
e.printStackTrace();
}
}
2023-01-06 17:03:07 +08:00
/**
* 启动插件
*/
2020-05-02 21:53:22 +08:00
public static void start() {
2026-03-27 18:17:49 +08:00
isRun = true;
2026-02-26 20:18:57 +08:00
MusicHttpClient.init();
IMusicApi api = new NetiApiMain();
MUSIC_APIS.put(api.getId(), api);
2020-05-02 21:53:22 +08:00
PlayMusic.start();
2024-08-12 11:14:54 +08:00
PlayRuntime.start();
2021-03-19 15:32:47 +08:00
MusicSearch.start();
2021-09-03 18:21:46 +08:00
DataSql.start();
2023-01-03 18:31:35 +08:00
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<yellow>已启动-" + version);
2020-05-02 21:53:22 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 停止插件
*/
2020-05-02 21:53:22 +08:00
public static void stop() {
2026-03-27 18:17:49 +08:00
isRun = false;
2025-06-02 19:48:54 +08:00
PlayRuntime.stop();
DataSql.stop();
2026-03-27 18:17:49 +08:00
side.sendStop();
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<dark_green><yellow>已停止,感谢使用");
2020-05-02 21:53:22 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 加载配置文件
*/
2021-06-21 22:25:17 +08:00
private static void loadConfig() {
2020-04-19 20:36:12 +08:00
try {
InputStreamReader reader = new InputStreamReader(
2022-08-04 17:03:52 +08:00
Files.newInputStream(configFile.toPath()), StandardCharsets.UTF_8);
2020-04-19 20:36:12 +08:00
BufferedReader bf = new BufferedReader(reader);
2026-03-04 16:05:57 +08:00
config = gson.fromJson(bf, ConfigObj.class);
2020-05-02 23:04:57 +08:00
bf.close();
reader.close();
2021-02-15 16:05:42 +08:00
configCheck();
2020-04-19 20:36:12 +08:00
2022-12-26 15:36:01 +08:00
reader = new InputStreamReader(Files.newInputStream(messageFile.toPath()), StandardCharsets.UTF_8);
2020-04-19 20:36:12 +08:00
bf = new BufferedReader(reader);
2026-03-04 16:05:57 +08:00
message = gson.fromJson(bf, MessageObj.class);
2020-05-02 23:04:57 +08:00
bf.close();
reader.close();
2021-02-15 16:05:42 +08:00
messageCheck();
2020-04-19 20:36:12 +08:00
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<yellow>当前语言配置文件版本为:" + messageVersion
2024-04-17 22:15:12 +08:00
+ ",你的语言文件版本为:" + message.version);
2022-12-27 12:02:55 +08:00
2024-04-17 22:15:12 +08:00
if (!message.version.equalsIgnoreCase(messageVersion)) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>语言文件版本号错误,运行可能会发生问题,请删除后重载");
2022-12-26 15:36:01 +08:00
}
reader = new InputStreamReader(Files.newInputStream(cookieFile.toPath()), StandardCharsets.UTF_8);
2021-04-03 15:55:25 +08:00
bf = new BufferedReader(reader);
2026-01-31 19:06:53 +08:00
Type listType = new TypeToken<ArrayList<CookieObj>>() {
}.getType();
2026-03-04 16:05:57 +08:00
cookie = gson.fromJson(bf, listType);
2021-04-03 15:55:25 +08:00
bf.close();
reader.close();
2026-01-02 17:16:49 +08:00
if (cookie == null) {
cookie = new ArrayList<>();
2021-04-03 15:55:25 +08:00
saveCookie();
}
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<yellow>当前插件配置文件版本为:" + configVersion
2024-04-17 22:15:12 +08:00
+ ",你的配置文件版本为:" + config.version);
2020-04-19 20:36:12 +08:00
2024-04-17 22:15:12 +08:00
if (!AllMusic.configVersion.equalsIgnoreCase(config.version)) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>请及时更新配置文件");
2020-07-09 14:07:27 +08:00
}
2026-04-04 12:33:44 +08:00
2026-04-06 12:28:22 +08:00
replacer = new StringReplacer();
2026-04-04 12:33:44 +08:00
if (!config.lyricReplace.isEmpty()) {
for (Map.Entry<String, String> item : config.lyricReplace.entrySet()) {
replacer.put(item.getKey(), item.getValue());
}
}
2020-04-19 20:36:12 +08:00
} catch (Exception e) {
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>读取配置文件错误");
2020-04-19 20:36:12 +08:00
e.printStackTrace();
}
}
2023-01-06 17:03:07 +08:00
/**
* 加入时播放
2023-01-08 20:55:21 +08:00
*
2023-01-06 17:03:07 +08:00
* @param player 用户名
*/
public static void joinPlay(String player) {
2026-04-03 16:59:01 +08:00
AllMusic.side.runTask(() -> joinPlayNow(player), AllMusic.config.joinDelay);
2021-09-05 14:08:44 +08:00
}
2026-04-03 16:59:01 +08:00
public static void joinPlayNow(String player) {
2026-03-28 18:16:03 +08:00
DataSql.task(() -> {
2026-04-03 16:59:01 +08:00
String player1 = player.toLowerCase();
Object player2 = AllMusic.side.getPlayer(player1);
String server = AllMusic.side.getPlayerServer(player2);
2026-03-28 18:16:03 +08:00
if (server != null && AllMusic.getConfig().muteServer.contains(server)) {
return;
}
if (DataSql.checkMutePlayer(player1)) {
return;
}
if (DataSql.checkMuteListPlayer(player1)) {
return;
}
AllMusic.side.runTask(() -> {
SongInfoObj music = PlayMusic.nowPlayMusic;
if (music != null && PlayMusic.url != null) {
AllMusic.side.sendHudPos(player1);
AllMusic.side.sendMusic(player1, PlayMusic.url);
AllMusic.side.sendPic(player1, music.getPicUrl());
2026-04-03 16:59:01 +08:00
AllMusic.side.runTask(() -> AllMusic.side.sendPos(player1, (int) PlayMusic.musicNowTime), 20);
2026-03-28 18:16:03 +08:00
}
2026-04-03 16:59:01 +08:00
});
2026-03-28 18:16:03 +08:00
});
}
2026-03-04 13:57:59 +08:00
public static Component miniMessage(String input) {
MiniMessage mm = MiniMessage.miniMessage();
return mm.deserialize(input);
}
public static Component miniMessageRun(String input, String command) {
Component component = miniMessage(input);
2026-03-27 00:04:22 +08:00
return component.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, command));
2026-03-04 13:57:59 +08:00
}
public static Component miniMessageSuggest(String input, String command) {
Component component = miniMessage(input);
2026-03-27 00:04:22 +08:00
return component.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, command));
2026-03-04 13:57:59 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 读取配置文件
2023-01-08 20:55:21 +08:00
*
2023-01-06 17:03:07 +08:00
* @param file 配置文件文件夹
*/
2026-03-04 16:05:57 +08:00
public static void init(File file) {
log.data("<light_purple>[AllMusic3]<yellow>正在启动,感谢使用,本插件交流群:571239090");
2020-04-19 20:36:12 +08:00
try {
2021-09-03 18:21:46 +08:00
if (!file.exists())
file.mkdir();
2021-11-30 13:37:43 +08:00
if (configFile == null)
2023-08-03 10:15:57 +08:00
configFile = new File(file, "config.json");
2021-11-30 13:37:43 +08:00
if (messageFile == null)
2023-08-03 10:15:57 +08:00
messageFile = new File(file, "message.json");
2022-01-03 15:27:38 +08:00
if (cookieFile == null)
2021-11-30 13:37:43 +08:00
cookieFile = new File(file, "cookie.json");
if (DataSql.sqlFile == null)
DataSql.sqlFile = new File(file, "data.db");
if (!configFile.exists()) {
2023-08-03 10:15:57 +08:00
configFile.createNewFile();
2020-04-19 20:36:12 +08:00
}
2021-11-30 13:37:43 +08:00
if (!messageFile.exists()) {
2023-08-03 10:15:57 +08:00
messageFile.createNewFile();
2020-04-19 20:36:12 +08:00
}
2021-11-30 13:37:43 +08:00
if (!cookieFile.exists()) {
cookieFile.createNewFile();
2021-04-03 15:55:25 +08:00
}
2021-06-21 22:25:17 +08:00
loadConfig();
2020-04-19 20:36:12 +08:00
isRun = true;
} catch (IOException e) {
isRun = false;
2026-03-04 16:05:57 +08:00
log.data("<light_purple>[AllMusic3]<red>启动失败");
2020-04-19 20:36:12 +08:00
e.printStackTrace();
}
}
}