Files
AllMusic/core/AllMusic.java
T

527 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
2024-04-19 09:46:54 +08:00
import com.coloryr.allmusic.server.core.music.api.APIMain;
import com.coloryr.allmusic.server.core.music.play.MusicSearch;
import com.coloryr.allmusic.server.core.music.play.PlayGo;
import com.coloryr.allmusic.server.core.music.play.PlayMusic;
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;
import com.coloryr.allmusic.server.core.objs.music.SearchPageObj;
import com.coloryr.allmusic.server.core.side.IMyLogger;
import com.coloryr.allmusic.server.core.side.ISide;
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;
import com.coloryr.allmusic.server.core.utils.Logs;
2020-04-19 20:36:12 +08:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.*;
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 {
2023-01-09 15:56:26 +08:00
public static final Gson gson = new Gson();
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
/**
* 插件版本号
*/
2024-04-17 22:15:12 +08:00
public static final String version = "3.0.0";
2023-01-06 17:03:07 +08:00
/**
* 配置文件版本号
*/
2024-04-17 22:15:12 +08:00
public static final String configVersion = "200";
2023-01-06 17:03:07 +08:00
/**
* 语言文件配置版本号
*/
2024-04-17 22:15:12 +08:00
public static final String messageVersion = "200";
2023-01-06 17:03:07 +08:00
/**
* 搜歌结果
* 玩家名 结果
*/
2023-01-06 18:15:10 +08:00
private static final Map<String, SearchPageObj> searchSave = new HashMap<>();
2023-01-06 17:03:07 +08:00
/**
2024-04-17 22:15:12 +08:00
* 切歌投票的玩家
2023-01-06 17:03:07 +08:00
*/
2024-01-02 21:54:54 +08:00
private static final Set<String> votePlayer = new HashSet<>();
2024-04-17 22:15:12 +08:00
/**
* 插歌投票的玩家
*/
private static final Set<String> pushPlayer = new HashSet<>();
2023-01-06 17:03:07 +08:00
/**
* 正在播放的玩家
*/
2024-01-02 21:54:54 +08:00
private static final Set<String> nowPlayPlayer = new HashSet<>();
/**
* 暂时不往玩家发送数据包
*/
private static final Set<String> pauseSendPlayer = new HashSet<>();
2023-01-08 20:55:21 +08:00
/**
* 日志
*/
public static IMyLogger log;
/**
* 服务器端操作
*/
public static ISide side;
/**
* 是否在运行
*/
public static boolean isRun;
/**
* Cookie对象
*/
public static CookieObj cookie;
/**
* 经济插件对象
*/
public static IEconomy economy;
2023-01-06 17:03:07 +08:00
/**
* 网易API
*/
2021-11-30 13:37:43 +08:00
private static APIMain apiMusic;
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;
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();
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件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();
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件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
/**
* 检查是否需要
*
* @param name 用户名
* @param server 服务器名
* @param checkList 是否检查正在播放的列表
* @return 结果
*/
public static boolean isOK(String name, String server, boolean checkList) {
try {
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;
2024-04-17 22:15:12 +08:00
if (AllMusic.getConfig().mutePlayer.contains(name))
2023-01-08 20:55:21 +08:00
return true;
2024-04-19 11:14:17 +08:00
if (AllMusic.isPause(name))
2024-01-02 21:54:54 +08:00
return true;
2023-01-08 20:55:21 +08:00
if (!checkList)
return false;
return AllMusic.containNowPlay(name);
} 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
* @param player 用户名
* @return 是否存在
*/
2020-04-19 20:36:12 +08:00
public static boolean containNowPlay(String player) {
2023-01-08 21:21:23 +08:00
return !nowPlayPlayer.contains(player);
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 15:56:26 +08:00
public static ConfigObj getConfig() {
2022-01-03 15:27:38 +08:00
if (config == null) {
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件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) {
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件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-01-08 20:55:21 +08:00
*
2023-01-06 17:03:07 +08:00
* @param player 用户名
2023-01-08 20:55:21 +08:00
* @param page 结果
2023-01-06 17:03:07 +08:00
*/
2023-01-06 18:15:10 +08:00
public static void addSearch(String player, SearchPageObj page) {
2021-11-30 13:37:43 +08:00
searchSave.put(player, page);
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
* @param player 用户名
* @return 结果
*/
2023-01-06 18:15:10 +08:00
public static SearchPageObj getSearch(String player) {
2021-11-30 13:37:43 +08:00
return searchSave.get(player);
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
* @param player 用户名
*/
2020-04-19 20:36:12 +08:00
public static void removeSearch(String player) {
2021-11-30 13:37:43 +08:00
searchSave.remove(player);
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
* @param player 用户名
*/
2020-04-19 20:36:12 +08:00
public static void addVote(String player) {
2024-01-02 21:54:54 +08:00
votePlayer.add(player);
2020-04-19 20:36:12 +08:00
}
2024-04-17 22:15:12 +08:00
/**
* 添加投票的玩家
*
* @param player 用户名
*/
2024-04-19 11:14:17 +08:00
public static void addPush(String player) {
2024-04-17 22:15:12 +08:00
pushPlayer.add(player);
}
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 数量
*/
2020-04-19 20:36:12 +08:00
public static int getVoteCount() {
2021-11-30 13:37:43 +08:00
return votePlayer.size();
2020-04-19 20:36:12 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 清空投票
*/
2020-04-19 20:36:12 +08:00
public static void clearVote() {
2021-11-30 13:37:43 +08:00
votePlayer.clear();
2020-04-19 20:36:12 +08:00
}
2024-04-17 22:15:12 +08:00
public static void clearPush() {
pushPlayer.clear();
}
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 用户名
* @return 结果
*/
2020-04-19 20:36:12 +08:00
public static boolean containVote(String player) {
2021-11-30 13:37:43 +08:00
return votePlayer.contains(player);
2020-04-19 20:36:12 +08:00
}
2024-04-17 22:15:12 +08:00
public static boolean containPush(String player) {
return pushPlayer.contains(player);
}
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 用户名
*/
2020-04-19 20:36:12 +08:00
public static void addNowPlayPlayer(String player) {
2024-01-02 21:54:54 +08:00
nowPlayPlayer.add(player);
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
* @param player 用户名
*/
2020-04-19 20:36:12 +08:00
public static void removeNowPlayPlayer(String player) {
2021-11-30 13:37:43 +08:00
nowPlayPlayer.remove(player);
2024-01-02 21:54:54 +08:00
pauseSendPlayer.remove(player);
2020-04-19 20:36:12 +08:00
}
2023-01-08 20:55:21 +08:00
/**
* 清空正在播放玩家的列表
*/
public static void clearNowPlayer() {
nowPlayPlayer.clear();
}
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 {
2021-11-30 13:37:43 +08:00
String data = new GsonBuilder().setPrettyPrinting().create().toJson(config);
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) {
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件保存错误");
2023-08-03 10:15:57 +08:00
e.printStackTrace();
}
}
public static void saveMessage() {
try {
String data = new GsonBuilder().setPrettyPrinting().create().toJson(message);
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) {
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件保存错误");
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 {
2021-11-30 13:37:43 +08:00
String data = new GsonBuilder().setPrettyPrinting().create().toJson(cookie);
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) {
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c配置文件保存错误");
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() {
2023-08-11 23:08:08 +08:00
AllMusic.apiMusic = new APIMain();
2020-05-02 21:53:22 +08:00
PlayMusic.start();
2022-02-01 21:38:39 +08:00
PlayGo.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
2024-04-17 22:15:12 +08:00
log.info("§d[AllMusic3]§e已启动-" + 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() {
try {
2021-01-19 15:08:37 +08:00
clearVote();
2022-12-27 10:31:41 +08:00
Logs.stop();
side.sendStop();
2021-03-19 15:32:47 +08:00
MusicSearch.stop();
2021-01-19 15:08:37 +08:00
PlayMusic.stop();
2022-02-01 21:38:39 +08:00
PlayGo.stop();
2021-09-03 18:21:46 +08:00
DataSql.stop();
2020-05-02 21:53:22 +08:00
} catch (IOException e) {
e.printStackTrace();
}
2024-04-17 22:15:12 +08:00
log.info("§d[AllMusic3]§2§e已停止,感谢使用");
2020-05-02 21:53:22 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 获取音乐API
2023-01-08 20:55:21 +08:00
*
2023-01-06 17:03:07 +08:00
* @return 音乐API
*/
2021-06-21 22:25:17 +08:00
public static APIMain getMusicApi() {
2021-11-30 13:37:43 +08:00
return apiMusic;
2021-02-10 10:14:15 +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);
2023-01-09 15:56:26 +08:00
config = new 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);
2023-01-09 16:54:42 +08:00
message = new 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
2024-04-17 22:15:12 +08:00
log.info("§d[AllMusic3]§e当前语言配置文件版本为:" + messageVersion
+ ",你的语言文件版本为:" + message.version);
2022-12-27 12:02:55 +08:00
2024-04-17 22:15:12 +08:00
if (!message.version.equalsIgnoreCase(messageVersion)) {
log.warning("§d[AllMusic3]§c语言文件版本号错误,运行可能会发生问题,请删除后重载");
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);
2021-11-30 13:37:43 +08:00
cookie = new Gson().fromJson(bf, CookieObj.class);
2021-04-03 15:55:25 +08:00
bf.close();
reader.close();
2021-11-30 13:37:43 +08:00
if (cookie == null) {
cookie = new CookieObj();
2021-04-03 15:55:25 +08:00
saveCookie();
}
2024-04-17 22:15:12 +08:00
log.info("§d[AllMusic3]§e当前插件配置文件版本为:" + configVersion
+ ",你的配置文件版本为:" + config.version);
2020-04-19 20:36:12 +08:00
2024-04-17 22:15:12 +08:00
if (!AllMusic.configVersion.equalsIgnoreCase(config.version)) {
log.warning("§d[AllMusic3]§c请及时更新配置文件");
2020-07-09 14:07:27 +08:00
}
2020-04-19 20:36:12 +08:00
} catch (Exception e) {
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c读取配置文件错误");
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) {
2024-04-17 22:15:12 +08:00
if (getConfig().mutePlayer.contains(player) || nowPlayPlayer.contains(player)) {
2024-01-04 23:01:41 +08:00
pauseSendPlayer.remove(player);
2021-09-05 14:08:44 +08:00
return;
2024-01-04 23:01:41 +08:00
}
2022-11-21 19:52:01 +08:00
2023-01-06 18:01:19 +08:00
AllMusic.side.runTask(() -> {
2024-04-17 23:43:11 +08:00
if (PlayMusic.nowPlayMusic != null && PlayGo.url != null) {
2024-04-17 22:15:12 +08:00
AllMusic.side.sendHudPos(player);
2023-01-08 20:55:21 +08:00
AllMusic.side.sendMusic(player, PlayGo.url);
2022-06-19 16:34:20 +08:00
if (!PlayMusic.nowPlayMusic.isUrl()) {
2023-01-06 18:01:19 +08:00
AllMusic.side.runTask(() ->
2023-01-08 20:55:21 +08:00
AllMusic.side.sendPic(player, PlayMusic.nowPlayMusic.getPicUrl()), 15);
2022-06-19 16:24:15 +08:00
}
2023-01-06 18:01:19 +08:00
AllMusic.side.runTask(() ->
2023-01-12 10:53:36 +08:00
AllMusic.side.sendPos(player, PlayMusic.musicNowTime + 1000), 40);
2022-11-21 19:52:01 +08:00
}
2024-01-04 23:01:41 +08:00
pauseSendPlayer.remove(player);
2023-01-12 10:53:36 +08:00
}, 40);
2021-09-05 14:08:44 +08:00
}
2024-01-02 21:54:54 +08:00
/**
* 将玩家添加到暂停发送列表
2024-04-19 11:14:17 +08:00
*
2024-01-02 21:54:54 +08:00
* @param player 玩家名
*/
public static void pauseSend(String player) {
pauseSendPlayer.add(player);
}
/**
* 暂停发送列表中是否存在玩家
2024-04-19 11:14:17 +08:00
*
2024-01-02 21:54:54 +08:00
* @param player 玩家名
* @return 是否存在
*/
2024-04-19 11:14:17 +08:00
public static boolean isPause(String player) {
2024-01-02 21:54:54 +08:00
return pauseSendPlayer.contains(player);
}
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 配置文件文件夹
*/
2020-04-19 20:36:12 +08:00
public void init(File file) {
2024-04-17 22:15:12 +08:00
log.info("§d[AllMusic3]§2§e正在启动,感谢使用,本插件交流群: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");
2022-12-27 10:31:41 +08:00
if (Logs.file == null)
Logs.file = new File(file, "logs.log");
2021-11-30 13:37:43 +08:00
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
}
2022-12-27 10:31:41 +08:00
if (!Logs.file.exists()) {
Logs.file.createNewFile();
2020-04-19 20:36:12 +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;
2024-04-17 22:15:12 +08:00
log.warning("§d[AllMusic3]§c启动失败");
2020-04-19 20:36:12 +08:00
e.printStackTrace();
}
}
}