2024-04-19 09:46:54 +08:00
|
|
|
package com.coloryr.allmusic.server.core.sql;
|
2021-09-03 18:21:46 +08:00
|
|
|
|
2024-04-19 09:46:54 +08:00
|
|
|
import com.coloryr.allmusic.server.core.AllMusic;
|
|
|
|
|
import com.coloryr.allmusic.server.core.objs.config.SaveObj;
|
2024-04-19 11:14:17 +08:00
|
|
|
import com.coloryr.allmusic.server.core.objs.enums.HudDirType;
|
2024-04-19 09:46:54 +08:00
|
|
|
import com.coloryr.allmusic.server.core.objs.hud.PosObj;
|
2021-09-03 18:21:46 +08:00
|
|
|
|
|
|
|
|
import java.io.File;
|
2024-04-17 22:15:12 +08:00
|
|
|
import java.sql.*;
|
2025-07-31 17:21:20 +08:00
|
|
|
import java.util.ArrayList;
|
2024-04-17 22:15:12 +08:00
|
|
|
import java.util.List;
|
2024-04-17 23:43:11 +08:00
|
|
|
import java.util.Locale;
|
2021-09-03 18:21:46 +08:00
|
|
|
import java.util.Queue;
|
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2022-12-27 10:31:41 +08:00
|
|
|
import java.util.concurrent.Semaphore;
|
2021-09-03 18:21:46 +08:00
|
|
|
|
|
|
|
|
public class DataSql {
|
|
|
|
|
private static final Queue<Runnable> tasks = new ConcurrentLinkedQueue<>();
|
2022-12-27 10:31:41 +08:00
|
|
|
private static final Semaphore semaphore = new Semaphore(0);
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 创建表用
|
|
|
|
|
*/
|
2024-04-17 22:15:12 +08:00
|
|
|
private static final String table = "CREATE TABLE IF NOT EXISTS \"allmusic\" (\n" +
|
2021-09-03 18:21:46 +08:00
|
|
|
" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
|
2025-03-24 18:20:45 +08:00
|
|
|
" \"name\" TEXT(60),\n" +
|
2021-09-03 18:21:46 +08:00
|
|
|
" \"info_x\" integer(6),\n" +
|
|
|
|
|
" \"info_y\" integer(6),\n" +
|
2024-04-17 22:15:12 +08:00
|
|
|
" \"info_color\" integer(20),\n" +
|
|
|
|
|
" \"info_dir\" integer(6),\n" +
|
|
|
|
|
" \"info_shadow\" integer(1),\n" +
|
2021-09-03 18:21:46 +08:00
|
|
|
" \"info_enable\" integer(1),\n" +
|
|
|
|
|
" \"lyric_x\" integer(6),\n" +
|
|
|
|
|
" \"lyric_y\" integer(6),\n" +
|
2024-04-17 22:15:12 +08:00
|
|
|
" \"lyric_color\" integer(20),\n" +
|
|
|
|
|
" \"lyric_dir\" integer(6),\n" +
|
|
|
|
|
" \"lyric_shadow\" integer(1),\n" +
|
2021-09-03 18:21:46 +08:00
|
|
|
" \"lyric_enable\" integer(1),\n" +
|
|
|
|
|
" \"list_x\" integer(6),\n" +
|
|
|
|
|
" \"list_y\" integer(6),\n" +
|
2024-04-17 22:15:12 +08:00
|
|
|
" \"list_color\" integer(20),\n" +
|
|
|
|
|
" \"list_dir\" integer(6),\n" +
|
|
|
|
|
" \"list_shadow\" integer(1),\n" +
|
2021-09-03 18:21:46 +08:00
|
|
|
" \"list_enable\" integer(1),\n" +
|
|
|
|
|
" \"pic_x\" integer(6),\n" +
|
|
|
|
|
" \"pic_y\" integer(6),\n" +
|
2024-04-17 22:15:12 +08:00
|
|
|
" \"pic_dir\" integer(6),\n" +
|
2022-06-19 16:24:15 +08:00
|
|
|
" \"pic_size\" integer(6),\n" +
|
2023-01-09 15:56:26 +08:00
|
|
|
" \"pic_enable\" integer(1),\n" +
|
|
|
|
|
" \"pic_rotate\" integer(1),\n" +
|
|
|
|
|
" \"pic_rotate_speed\" integer(6)\n" +
|
2021-09-03 18:21:46 +08:00
|
|
|
");";
|
2024-04-17 22:15:12 +08:00
|
|
|
|
|
|
|
|
private static final String table1 = "CREATE TABLE IF NOT EXISTS \"allmusic_list\" (\n" +
|
|
|
|
|
" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
|
|
|
|
|
" \"sid\" TEXT(40)\n" +
|
|
|
|
|
");";
|
|
|
|
|
|
2025-03-24 18:20:45 +08:00
|
|
|
private static final String table2 = "CREATE TABLE IF NOT EXISTS \"allmusic_mute\" (\n" +
|
|
|
|
|
" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
|
|
|
|
|
" \"sid\" TEXT(60)\n" +
|
|
|
|
|
");";
|
|
|
|
|
|
|
|
|
|
private static final String table3 = "CREATE TABLE IF NOT EXISTS \"allmusic_mutelist\" (\n" +
|
|
|
|
|
" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
|
|
|
|
|
" \"sid\" TEXT(60)\n" +
|
|
|
|
|
");";
|
|
|
|
|
|
|
|
|
|
private static final String table4 = "CREATE TABLE IF NOT EXISTS \"allmusic_banlist\" (\n" +
|
|
|
|
|
" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
|
|
|
|
|
" \"sid\" TEXT(40)\n" +
|
|
|
|
|
");";
|
|
|
|
|
|
|
|
|
|
private static final String table5 = "CREATE TABLE IF NOT EXISTS \"allmusic_banplayer\" (\n" +
|
|
|
|
|
" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
|
|
|
|
|
" \"sid\" TEXT(60)\n" +
|
|
|
|
|
");";
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 数据库文件
|
|
|
|
|
*/
|
2021-11-30 13:37:43 +08:00
|
|
|
public static File sqlFile;
|
2023-01-08 20:55:21 +08:00
|
|
|
private static boolean isRun;
|
|
|
|
|
/**
|
|
|
|
|
* 数据库链接对象
|
|
|
|
|
*/
|
|
|
|
|
private static Connection connection;
|
2021-09-03 18:21:46 +08:00
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 初始化数据库
|
|
|
|
|
*/
|
2021-09-03 18:21:46 +08:00
|
|
|
public static void init() {
|
|
|
|
|
try {
|
2022-12-27 10:31:41 +08:00
|
|
|
AllMusic.log.info("正在初始化数据库");
|
2021-09-03 18:21:46 +08:00
|
|
|
if (connection != null)
|
|
|
|
|
connection.close();
|
|
|
|
|
Class.forName("org.sqlite.JDBC");
|
2021-11-30 13:37:43 +08:00
|
|
|
connection = DriverManager.getConnection("jdbc:sqlite:" + sqlFile.getPath());
|
2021-09-03 18:21:46 +08:00
|
|
|
Statement stat = connection.createStatement();
|
2024-04-17 22:15:12 +08:00
|
|
|
stat.execute(table);
|
|
|
|
|
stat.execute(table1);
|
2025-03-24 18:20:45 +08:00
|
|
|
stat.execute(table2);
|
|
|
|
|
stat.execute(table3);
|
|
|
|
|
stat.execute(table4);
|
|
|
|
|
stat.execute(table5);
|
2021-09-03 18:21:46 +08:00
|
|
|
stat.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 检查玩家是否在数据库
|
2023-01-08 20:55:21 +08:00
|
|
|
*
|
2023-01-06 17:27:35 +08:00
|
|
|
* @param name 用户名
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
2021-09-03 18:21:46 +08:00
|
|
|
public static boolean check(String name) {
|
|
|
|
|
try {
|
2024-04-17 23:43:11 +08:00
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
2021-09-03 18:21:46 +08:00
|
|
|
boolean have = false;
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
2025-03-25 15:36:06 +08:00
|
|
|
ResultSet set = stat.executeQuery("SELECT id FROM allmusic WHERE name ='" + name + "'");
|
2021-09-03 18:21:46 +08:00
|
|
|
if (set.next()) {
|
|
|
|
|
have = true;
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return have;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 更新玩家Hud数据
|
2023-01-08 20:55:21 +08:00
|
|
|
*
|
2023-01-06 17:27:35 +08:00
|
|
|
* @param name 用户名
|
2023-01-08 20:55:21 +08:00
|
|
|
* @param hud Hud数据
|
2023-01-06 17:27:35 +08:00
|
|
|
*/
|
2023-01-09 16:54:42 +08:00
|
|
|
private static void update(String name, SaveObj hud) {
|
2024-04-17 23:43:11 +08:00
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
2021-09-03 18:21:46 +08:00
|
|
|
String sql = "";
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
2024-04-17 22:56:21 +08:00
|
|
|
sql = "UPDATE allmusic SET " +
|
|
|
|
|
"info_x=" + hud.info.x + "," +
|
|
|
|
|
"info_y=" + hud.info.y + "," +
|
|
|
|
|
"info_color=" + hud.info.color + "," +
|
|
|
|
|
"info_dir=" + hud.info.dir.ordinal() + "," +
|
|
|
|
|
"info_shadow=" + (hud.info.shadow ? 1 : 0) + "," +
|
|
|
|
|
"info_enable=" + (hud.info.enable ? 1 : 0) + "," +
|
|
|
|
|
"lyric_x=" + hud.lyric.x + "," +
|
|
|
|
|
"lyric_y=" + hud.lyric.y + "," +
|
|
|
|
|
"lyric_color=" + hud.lyric.color + "," +
|
|
|
|
|
"lyric_dir=" + hud.lyric.dir.ordinal() + "," +
|
|
|
|
|
"lyric_shadow=" + (hud.lyric.shadow ? 1 : 0) + "," +
|
|
|
|
|
"lyric_enable=" + (hud.lyric.enable ? 1 : 0) + "," +
|
|
|
|
|
"list_x=" + hud.list.x + "," +
|
|
|
|
|
"list_y=" + hud.list.y + "," +
|
|
|
|
|
"list_color=" + hud.list.color + "," +
|
|
|
|
|
"list_dir=" + hud.list.dir.ordinal() + "," +
|
|
|
|
|
"list_shadow=" + (hud.list.shadow ? 1 : 0) + "," +
|
|
|
|
|
"list_enable=" + (hud.list.enable ? 1 : 0) + "," +
|
|
|
|
|
"pic_x=" + hud.pic.x + "," +
|
|
|
|
|
"pic_y=" + hud.pic.y + "," +
|
|
|
|
|
"pic_dir=" + hud.pic.dir.ordinal() + "," +
|
|
|
|
|
"pic_enable=" + (hud.pic.enable ? 1 : 0) + "," +
|
|
|
|
|
"pic_size=" + hud.pic.color + "," +
|
|
|
|
|
"pic_rotate=" + (hud.pic.shadow ? 1 : 0) + "," +
|
|
|
|
|
"pic_rotate_speed=" + hud.picRotateSpeed + " WHERE name='" + name + "'";
|
2021-09-03 18:21:46 +08:00
|
|
|
stat.execute(sql);
|
|
|
|
|
stat.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
AllMusic.log.info(sql);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 添加用户Hud数据
|
2023-01-08 20:55:21 +08:00
|
|
|
*
|
2023-01-06 17:27:35 +08:00
|
|
|
* @param name 用户名
|
2023-01-08 20:55:21 +08:00
|
|
|
* @param hud 数据
|
2023-01-06 17:27:35 +08:00
|
|
|
*/
|
2023-01-09 16:54:42 +08:00
|
|
|
public static void addUser(String name, SaveObj hud) {
|
2024-04-17 23:43:11 +08:00
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
2021-09-03 18:21:46 +08:00
|
|
|
String sql = "";
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
if (check(name)) {
|
|
|
|
|
update(name, hud);
|
|
|
|
|
} else {
|
|
|
|
|
Statement stat = connection.createStatement();
|
2024-04-17 22:56:21 +08:00
|
|
|
sql = "INSERT INTO allmusic (name,info_x,info_y," +
|
|
|
|
|
"info_enable,lyric_x,lyric_y,lyric_enable,list_x,list_y,list_enable," +
|
|
|
|
|
"pic_x,pic_y,pic_enable,pic_size,pic_rotate,pic_rotate_speed," +
|
|
|
|
|
"info_color,info_dir,info_shadow,lyric_color,lyric_dir," +
|
|
|
|
|
"lyric_shadow,list_color,list_dir,list_shadow,pic_dir)" +
|
2024-04-17 23:43:11 +08:00
|
|
|
"VALUES ('" + name + "'," +
|
2024-04-17 22:56:21 +08:00
|
|
|
hud.info.x + "," +
|
|
|
|
|
hud.info.y + "," +
|
|
|
|
|
(hud.info.enable ? 1 : 0) + "," +
|
|
|
|
|
hud.lyric.x + "," +
|
|
|
|
|
hud.lyric.y + "," +
|
|
|
|
|
(hud.lyric.enable ? 1 : 0) + "," +
|
|
|
|
|
hud.list.x + "," +
|
|
|
|
|
hud.list.y + "," +
|
|
|
|
|
(hud.list.enable ? 1 : 0) + "," +
|
|
|
|
|
hud.pic.x + "," +
|
|
|
|
|
hud.pic.y + "," +
|
|
|
|
|
(hud.pic.enable ? 1 : 0) + "," +
|
|
|
|
|
hud.pic.color + "," +
|
|
|
|
|
(hud.pic.shadow ? 1 : 0) + "," +
|
|
|
|
|
hud.picRotateSpeed + "," +
|
|
|
|
|
hud.info.color + "," +
|
|
|
|
|
hud.info.dir.ordinal() + "," +
|
|
|
|
|
(hud.info.shadow ? 1 : 0) + "," +
|
|
|
|
|
hud.lyric.color + "," +
|
|
|
|
|
hud.lyric.dir.ordinal() + "," +
|
|
|
|
|
(hud.lyric.shadow ? 1 : 0) + "," +
|
|
|
|
|
hud.list.color + "," +
|
|
|
|
|
hud.list.dir.ordinal() + "," +
|
|
|
|
|
(hud.list.shadow ? 1 : 0) + "," +
|
|
|
|
|
hud.pic.dir.ordinal() +
|
|
|
|
|
")";
|
2021-09-03 18:21:46 +08:00
|
|
|
stat.execute(sql);
|
|
|
|
|
stat.close();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
AllMusic.log.info(sql);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 读取所有数据
|
|
|
|
|
*/
|
2025-03-24 18:20:45 +08:00
|
|
|
public static SaveObj readHud(String name) {
|
2021-09-03 18:21:46 +08:00
|
|
|
try {
|
2025-03-25 15:36:06 +08:00
|
|
|
AllMusic.log.info("正在读取玩家数据:" + name);
|
2021-09-03 18:21:46 +08:00
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
2025-03-24 18:20:45 +08:00
|
|
|
ResultSet set = stat.executeQuery("SELECT info_x,info_y,info_enable,lyric_x," +
|
2024-04-17 22:15:12 +08:00
|
|
|
"lyric_y,lyric_enable,list_x,list_y,list_enable,pic_x,pic_y,pic_enable,pic_size," +
|
|
|
|
|
"pic_rotate,pic_rotate_speed,info_color,info_dir,info_shadow,lyric_color," +
|
2025-03-25 15:36:06 +08:00
|
|
|
"lyric_dir,lyric_shadow,list_color,list_dir,list_shadow,pic_dir FROM allmusic WHERE name='" + name + "'");
|
2024-04-17 22:15:12 +08:00
|
|
|
HudDirType[] vas = HudDirType.values();
|
2025-03-24 18:20:45 +08:00
|
|
|
SaveObj obj = null;
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
obj = new SaveObj();
|
2023-01-09 16:54:42 +08:00
|
|
|
PosObj pos1 = new PosObj();
|
2025-03-24 18:20:45 +08:00
|
|
|
pos1.x = set.getInt(1);
|
|
|
|
|
pos1.y = set.getInt(2);
|
2024-04-17 22:15:12 +08:00
|
|
|
obj.info = pos1;
|
2025-03-24 18:20:45 +08:00
|
|
|
obj.info.enable = set.getInt(3) == 1;
|
2023-01-09 16:54:42 +08:00
|
|
|
PosObj pos2 = new PosObj();
|
2025-03-24 18:20:45 +08:00
|
|
|
pos2.x = set.getInt(4);
|
|
|
|
|
pos2.y = set.getInt(5);
|
2024-04-17 22:15:12 +08:00
|
|
|
obj.lyric = pos2;
|
2025-03-24 18:20:45 +08:00
|
|
|
obj.lyric.enable = set.getInt(6) == 1;
|
2023-01-09 16:54:42 +08:00
|
|
|
PosObj pos3 = new PosObj();
|
2025-03-24 18:20:45 +08:00
|
|
|
pos3.x = set.getInt(7);
|
|
|
|
|
pos3.y = set.getInt(8);
|
2024-04-17 22:15:12 +08:00
|
|
|
obj.list = pos3;
|
2025-03-24 18:20:45 +08:00
|
|
|
obj.list.enable = set.getInt(9) == 1;
|
2023-01-09 16:54:42 +08:00
|
|
|
PosObj pos4 = new PosObj();
|
2025-03-24 18:20:45 +08:00
|
|
|
pos4.x = set.getInt(10);
|
|
|
|
|
pos4.y = set.getInt(11);
|
2024-04-17 22:15:12 +08:00
|
|
|
obj.pic = pos4;
|
2025-03-24 18:20:45 +08:00
|
|
|
obj.pic.enable = set.getInt(12) == 1;
|
|
|
|
|
obj.pic.color = set.getInt(13);
|
|
|
|
|
obj.pic.shadow = set.getInt(14) == 1;
|
|
|
|
|
obj.picRotateSpeed = set.getInt(15);
|
|
|
|
|
obj.info.color = set.getInt(16);
|
|
|
|
|
obj.info.dir = vas[set.getInt(17)];
|
|
|
|
|
obj.info.shadow = set.getInt(18) == 1;
|
|
|
|
|
obj.lyric.color = set.getInt(19);
|
|
|
|
|
obj.lyric.dir = vas[set.getInt(20)];
|
|
|
|
|
obj.lyric.shadow = set.getInt(21) == 1;
|
|
|
|
|
obj.list.color = set.getInt(22);
|
|
|
|
|
obj.list.dir = vas[set.getInt(23)];
|
|
|
|
|
obj.list.shadow = set.getInt(24) == 1;
|
|
|
|
|
obj.pic.dir = vas[set.getInt(25)];
|
2021-09-03 18:21:46 +08:00
|
|
|
}
|
|
|
|
|
stat.close();
|
2025-03-24 18:20:45 +08:00
|
|
|
|
|
|
|
|
return obj;
|
2021-09-03 18:21:46 +08:00
|
|
|
} catch (Exception e) {
|
2023-01-09 15:56:26 +08:00
|
|
|
AllMusic.log.warning("数据库读取错误,请删除关闭服务器删除数据库,在启动服务器");
|
2021-09-03 18:21:46 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2025-03-24 18:20:45 +08:00
|
|
|
|
|
|
|
|
return null;
|
2021-09-03 18:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-17 22:15:12 +08:00
|
|
|
/**
|
2025-03-24 20:13:54 +08:00
|
|
|
* 随机获取空闲歌单歌曲
|
2024-04-17 22:15:12 +08:00
|
|
|
*/
|
2025-03-24 20:13:54 +08:00
|
|
|
public static String readListItem() {
|
2024-04-17 22:15:12 +08:00
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
2025-04-04 12:58:49 +08:00
|
|
|
ResultSet set = stat.executeQuery("SELECT sid FROM allmusic_list ORDER BY random() limit 1");
|
2025-03-24 18:20:45 +08:00
|
|
|
String name = null;
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
name = set.getString(1);
|
2024-04-17 22:15:12 +08:00
|
|
|
}
|
|
|
|
|
stat.close();
|
2025-03-24 18:20:45 +08:00
|
|
|
return name;
|
2024-04-17 22:15:12 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
AllMusic.log.warning("数据库读取错误,请删除关闭服务器删除数据库,在启动服务器");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2025-03-24 18:20:45 +08:00
|
|
|
return null;
|
2024-04-17 22:15:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-24 20:13:54 +08:00
|
|
|
/**
|
|
|
|
|
* 读取空闲歌单列表
|
|
|
|
|
*/
|
|
|
|
|
public static String readListItem(int index) {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT sid FROM allmusic_list limit 1 offset " + index);
|
|
|
|
|
String name = null;
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
name = set.getString(1);
|
|
|
|
|
}
|
|
|
|
|
stat.close();
|
|
|
|
|
return name;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
AllMusic.log.warning("数据库读取错误,请删除关闭服务器删除数据库,在启动服务器");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取空闲歌单歌曲数量
|
|
|
|
|
*
|
|
|
|
|
* @return 歌曲数量
|
|
|
|
|
*/
|
|
|
|
|
public static int getListSize() {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT count(sid) FROM allmusic_list");
|
|
|
|
|
int count = 0;
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
count = set.getInt(1);
|
|
|
|
|
}
|
|
|
|
|
stat.close();
|
|
|
|
|
return count;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
AllMusic.log.warning("数据库读取错误,请删除关闭服务器删除数据库,在启动服务器");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 22:15:12 +08:00
|
|
|
public static void addIdleList(List<String> list) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
2025-03-24 20:13:54 +08:00
|
|
|
AllMusic.log.info("添加" + list.size() + "首歌到空闲歌单");
|
2024-04-17 22:15:12 +08:00
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO allmusic_list (sid) VALUES (?)");
|
2024-09-16 12:54:35 +08:00
|
|
|
// 遍历 List<String> 并插入每个字符串
|
|
|
|
|
for (String str : list) {
|
|
|
|
|
pstmt.setString(1, str); // 设置参数
|
|
|
|
|
pstmt.addBatch(); // 添加到批处理
|
|
|
|
|
}
|
|
|
|
|
pstmt.executeBatch();
|
2024-04-17 22:15:12 +08:00
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void clearIdleList() {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
stat.execute("DELETE FROM allmusic_list");
|
|
|
|
|
stat.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:20:45 +08:00
|
|
|
public static void addBanMusic(String music) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO allmusic_banlist (sid) VALUES (?)");
|
|
|
|
|
pstmt.setString(1, music); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void removeBanMusic(String music) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("DELETE FROM allmusic_banlist WHERE sid=?");
|
|
|
|
|
pstmt.setString(1, music); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-31 17:21:20 +08:00
|
|
|
* check a music in ban list?
|
2025-03-24 18:20:45 +08:00
|
|
|
*
|
|
|
|
|
* @param name 用户名
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
|
|
|
|
public static boolean checkBanMusic(String name) {
|
|
|
|
|
try {
|
|
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
|
|
|
|
boolean have = false;
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT id FROM allmusic_banlist WHERE sid ='" + name + "'");
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
have = true;
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return have;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 17:21:20 +08:00
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* a way to get ban music list
|
|
|
|
|
* @return ban music list
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getBanMusicList() {
|
|
|
|
|
try {
|
|
|
|
|
List<String> banList = new ArrayList<>();
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT sid FROM allmusic_banlist");
|
|
|
|
|
while (set.next()) {
|
|
|
|
|
String musicName = set.getString("sid");
|
|
|
|
|
if (musicName != null) {
|
|
|
|
|
banList.add(musicName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return banList;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:20:45 +08:00
|
|
|
public static void clearBan() {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
stat.execute("DELETE FROM allmusic_banlist");
|
|
|
|
|
stat.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addBanPlayer(String player) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String player1 = player.toLowerCase(Locale.ROOT);
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO allmusic_banplayer (sid) VALUES (?)");
|
|
|
|
|
pstmt.setString(1, player1); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void removeBanPlayer(String player) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String player1 = player.toLowerCase(Locale.ROOT);
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("DELETE FROM allmusic_banplayer WHERE sid=?");
|
|
|
|
|
pstmt.setString(1, player1); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查玩家是否在数据库
|
|
|
|
|
*
|
|
|
|
|
* @param name 用户名
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
|
|
|
|
public static boolean checkBanPlayer(String name) {
|
|
|
|
|
try {
|
|
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
|
|
|
|
boolean have = false;
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT id FROM allmusic_banplayer WHERE sid ='" + name + "'");
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
have = true;
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return have;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 17:21:20 +08:00
|
|
|
/***
|
|
|
|
|
* @return a list of ban players
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getBanPlayerList() {
|
|
|
|
|
try {
|
|
|
|
|
List<String> banList = new ArrayList<>();
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT sid FROM allmusic_banplayer");
|
|
|
|
|
while (set.next()) {
|
|
|
|
|
String playerName = set.getString("sid");
|
|
|
|
|
if (playerName != null) {
|
|
|
|
|
banList.add(playerName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return banList;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 18:20:45 +08:00
|
|
|
public static void clearBanPlayer() {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
stat.execute("DELETE FROM allmusic_banlist");
|
|
|
|
|
stat.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addMutePlayer(String player) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String player1 = player.toLowerCase(Locale.ROOT);
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO allmusic_mute (sid) VALUES (?)");
|
|
|
|
|
pstmt.setString(1, player1); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void removeMutePlayer(String player) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String player1 = player.toLowerCase(Locale.ROOT);
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("DELETE FROM allmusic_mute WHERE sid=?");
|
|
|
|
|
pstmt.setString(1, player1); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查玩家是否在数据库
|
|
|
|
|
*
|
|
|
|
|
* @param name 用户名
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
|
|
|
|
public static boolean checkMutePlayer(String name) {
|
|
|
|
|
try {
|
|
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
|
|
|
|
boolean have = false;
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT id FROM allmusic_mute WHERE sid ='" + name + "'");
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
have = true;
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return have;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addMuteListPlayer(String player) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String player1 = player.toLowerCase(Locale.ROOT);
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO allmusic_mutelist (sid) VALUES (?)");
|
|
|
|
|
pstmt.setString(1, player1); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void removeMuteListPlayer(String player) {
|
|
|
|
|
task(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String player1 = player.toLowerCase(Locale.ROOT);
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
PreparedStatement pstmt = connection.prepareStatement("DELETE FROM allmusic_mutelist WHERE sid=?");
|
|
|
|
|
pstmt.setString(1, player1); // 设置参数
|
|
|
|
|
pstmt.execute();
|
|
|
|
|
pstmt.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查玩家是否在数据库
|
|
|
|
|
*
|
|
|
|
|
* @param name 用户名
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
|
|
|
|
public static boolean checkMuteListPlayer(String name) {
|
|
|
|
|
try {
|
|
|
|
|
name = name.toLowerCase(Locale.ROOT);
|
|
|
|
|
boolean have = false;
|
|
|
|
|
if (connection.isReadOnly() || connection.isClosed()) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
Statement stat = connection.createStatement();
|
|
|
|
|
ResultSet set = stat.executeQuery("SELECT id FROM allmusic_mutelist WHERE sid ='" + name + "'");
|
|
|
|
|
if (set.next()) {
|
|
|
|
|
have = true;
|
|
|
|
|
}
|
|
|
|
|
set.close();
|
|
|
|
|
stat.close();
|
|
|
|
|
return have;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 18:21:46 +08:00
|
|
|
public static void task(Runnable runnable) {
|
|
|
|
|
tasks.add(runnable);
|
2022-12-27 10:31:41 +08:00
|
|
|
semaphore.release();
|
2021-09-03 18:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 启用Hud数据库
|
|
|
|
|
*/
|
2021-09-03 18:21:46 +08:00
|
|
|
public static void start() {
|
|
|
|
|
init();
|
2022-02-01 21:38:39 +08:00
|
|
|
Thread thread = new Thread(DataSql::run);
|
2021-09-03 18:21:46 +08:00
|
|
|
isRun = true;
|
|
|
|
|
thread.start();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:27:35 +08:00
|
|
|
/**
|
|
|
|
|
* 停止数据库
|
|
|
|
|
*/
|
2021-09-03 18:21:46 +08:00
|
|
|
public static void stop() {
|
|
|
|
|
isRun = false;
|
2022-12-27 10:31:41 +08:00
|
|
|
semaphore.release();
|
2021-09-03 18:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void run() {
|
|
|
|
|
Runnable runnable;
|
|
|
|
|
while (isRun) {
|
|
|
|
|
try {
|
2022-12-27 10:31:41 +08:00
|
|
|
semaphore.acquire();
|
|
|
|
|
if (!isRun)
|
|
|
|
|
return;
|
|
|
|
|
do {
|
|
|
|
|
runnable = tasks.poll();
|
|
|
|
|
if (runnable != null) {
|
|
|
|
|
runnable.run();
|
|
|
|
|
}
|
2021-09-03 18:21:46 +08:00
|
|
|
}
|
2022-12-27 10:31:41 +08:00
|
|
|
while (runnable != null);
|
2021-09-03 18:21:46 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|