Files
AllMusic/core/command/CommandEX.java
T

332 lines
12 KiB
Java
Raw Normal View History

2024-04-19 09:46:54 +08:00
package com.coloryr.allmusic.server.core.command;
2020-02-29 22:09:52 +08:00
2024-04-19 09:46:54 +08:00
import com.coloryr.allmusic.server.core.AllMusic;
2026-02-26 20:18:57 +08:00
import com.coloryr.allmusic.server.core.IMusicApi;
2025-08-28 13:49:55 +08:00
import com.coloryr.allmusic.server.core.command.sub.*;
2026-02-26 20:18:57 +08:00
import com.coloryr.allmusic.server.core.music.MusicSearch;
import com.coloryr.allmusic.server.core.music.PlayMusic;
2025-09-24 22:16:24 +08:00
import com.coloryr.allmusic.server.core.objs.message.ARG;
2026-02-26 20:18:57 +08:00
import com.coloryr.allmusic.server.core.objs.music.PlayerAddMusicObj;
2025-03-24 18:20:45 +08:00
import com.coloryr.allmusic.server.core.sql.DataSql;
2020-02-29 22:09:52 +08:00
2024-04-17 22:15:12 +08:00
import java.util.ArrayList;
2023-10-22 10:14:55 +08:00
import java.util.HashMap;
2024-04-17 22:15:12 +08:00
import java.util.List;
2023-10-22 10:14:55 +08:00
import java.util.Map;
2021-06-12 15:57:35 +08:00
2020-02-29 22:09:52 +08:00
public class CommandEX {
2023-10-22 10:14:55 +08:00
2024-04-17 22:15:12 +08:00
public static final Map<String, ICommand> commandList = new HashMap<>();
public static final Map<String, ICommand> commandAdminList = new HashMap<>();
2026-03-04 13:57:59 +08:00
private static final List<String> normal = new ArrayList<>();
2026-01-31 19:06:53 +08:00
/**
* 管理员的指令
*/
2026-03-04 13:57:59 +08:00
private static final List<String> admin = new ArrayList<>();
2026-01-31 19:06:53 +08:00
/**
* 搜歌的指令
*/
2026-03-04 13:57:59 +08:00
private static final List<String> search = new ArrayList<>();
2023-10-22 10:14:55 +08:00
static {
2024-04-17 22:15:12 +08:00
commandList.put("stop", new CommandStop());
commandList.put("help", new CommandHelp());
commandList.put("list", new CommandList());
commandList.put("vote", new CommandVote());
commandList.put("mute", new CommandMute());
commandList.put("search", new CommandSearch());
2026-02-26 20:35:11 +08:00
commandList.put("searchapi", new CommandSearchApi());
2024-04-17 22:15:12 +08:00
commandList.put("select", new CommandSelect());
commandList.put("nextpage", new CommandNextPage());
commandList.put("lastpage", new CommandLastPage());
commandList.put("hud", new CommandHud());
commandList.put("push", new CommandPush());
commandList.put("cancel", new CommandCancel());
2026-01-02 17:16:49 +08:00
commandAdminList.put("reload", new CommandReload());
2024-04-17 22:15:12 +08:00
commandAdminList.put("next", new CommandNext());
commandAdminList.put("ban", new CommandBan());
2025-03-24 18:20:45 +08:00
commandAdminList.put("unban", new CommandUnban());
2024-04-17 22:15:12 +08:00
commandAdminList.put("banplayer", new CommandBanPlayer());
2025-03-24 18:20:45 +08:00
commandAdminList.put("unbanplayer", new CommandUnbanPlayer());
2024-04-17 22:15:12 +08:00
commandAdminList.put("delete", new CommandDelete());
commandAdminList.put("addlist", new CommandAddList());
commandAdminList.put("clearlist", new CommandClearList());
2025-03-24 18:20:45 +08:00
commandAdminList.put("clearban", new CommandClearBanList());
commandAdminList.put("clearbanplayer", new CommandClearBanPlayerList());
2024-07-09 11:06:38 +08:00
commandAdminList.put("test", new CommandTest());
2026-03-04 13:57:59 +08:00
normal.addAll(commandList.keySet());
admin.addAll(commandAdminList.keySet());
search.add("select");
search.add("nextpage");
search.add("lastpage");
2023-10-22 10:14:55 +08:00
}
2023-01-06 17:03:07 +08:00
/**
* 搜索音乐
2023-01-08 20:55:21 +08:00
*
* @param sender 发送者
* @param name 用户名
* @param args 参数
2023-01-06 17:03:07 +08:00
* @param isDefault 是否是默认点歌方式
*/
2023-10-22 10:14:55 +08:00
public static void searchMusic(Object sender, String name, String[] args, boolean isDefault) {
2026-02-26 20:18:57 +08:00
PlayerAddMusicObj obj = new PlayerAddMusicObj();
2021-02-07 20:26:36 +08:00
obj.sender = sender;
2021-11-30 13:37:43 +08:00
obj.name = name;
2021-02-07 20:26:36 +08:00
obj.args = args;
obj.isDefault = isDefault;
2026-02-26 20:35:11 +08:00
obj.api = AllMusic.getConfig().defaultApi;
if (AllMusic.side.onMusicAdd(sender, obj)) {
AllMusic.side.sendMessage(sender, AllMusic.getMessage().addMusic.cancel);
return;
}
MusicSearch.addSearch(obj);
}
/**
* 搜索音乐
*
* @param sender 发送者
* @param name 用户名
* @param args 参数
* @param isDefault 是否是默认点歌方式
*/
public static void searchMusicApi(Object sender, String name, String[] args, boolean isDefault) {
PlayerAddMusicObj obj = new PlayerAddMusicObj();
obj.sender = sender;
obj.name = name;
obj.args = new String[args.length - 1];
System.arraycopy(args, 1, obj.args, 0, obj.args.length);
obj.isDefault = isDefault;
obj.api = args[0];
2022-12-27 11:33:26 +08:00
if (AllMusic.side.onMusicAdd(sender, obj)) {
2024-04-17 22:15:12 +08:00
AllMusic.side.sendMessage(sender, AllMusic.getMessage().addMusic.cancel);
2022-12-27 11:33:26 +08:00
return;
}
2021-03-19 15:32:47 +08:00
MusicSearch.addSearch(obj);
2020-08-19 19:18:38 +08:00
}
2020-02-29 22:09:52 +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 sender 发送者
2023-01-08 20:55:21 +08:00
* @param name 用户名
* @param cost 金额
2023-01-06 17:03:07 +08:00
* @return 结果
*/
2023-10-22 10:14:55 +08:00
public static boolean checkMoney(Object sender, String name, int cost) {
2024-05-13 09:33:41 +08:00
if (!AllMusic.getConfig().cost.useCost || AllMusic.economy == null) {
2023-01-08 14:31:20 +08:00
return false;
}
if (!AllMusic.economy.check(name, cost)) {
2024-04-17 22:15:12 +08:00
AllMusic.side.sendMessage(sender, AllMusic.getMessage().cost.noMoney);
2023-01-08 14:31:20 +08:00
return true;
2023-01-03 18:31:35 +08:00
}
return false;
}
2023-01-06 17:03:07 +08:00
/**
* 扣钱
2023-01-08 20:55:21 +08:00
*
* @param sender 发送者
* @param name 用户名
* @param cost 金额
2023-01-06 17:03:07 +08:00
* @param message 结果消息
* @return 结果
*/
2023-10-22 10:14:55 +08:00
public static boolean cost(Object sender, String name, int cost, String message) {
2024-05-13 09:33:41 +08:00
if (!AllMusic.getConfig().cost.useCost || AllMusic.economy == null) {
2023-01-08 14:31:20 +08:00
return false;
}
2023-01-06 17:03:07 +08:00
if (AllMusic.economy.cost(name, cost)) {
2023-01-03 18:31:35 +08:00
AllMusic.side.sendMessage(sender, message
2025-09-24 22:16:24 +08:00
.replace(ARG.cost, String.valueOf(cost)));
2023-01-03 18:31:35 +08:00
return false;
} else {
2024-04-17 22:15:12 +08:00
AllMusic.side.sendMessage(sender, AllMusic.getMessage().cost.costFail);
2023-01-03 18:31:35 +08:00
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 sender 发送者
2023-01-08 20:55:21 +08:00
* @param name 用户名
2026-02-26 20:35:11 +08:00
* @param arg 参数
2023-01-06 17:03:07 +08:00
*/
2026-02-26 20:18:57 +08:00
public static void addMusic(Object sender, String name, String api, String arg) {
2021-06-21 22:25:17 +08:00
String musicID;
2026-02-26 20:18:57 +08:00
IMusicApi api1 = AllMusic.MUSIC_APIS.get(api);
if (api1 == null) {
AllMusic.side.sendMessage(sender, AllMusic.getMessage().musicPlay.error2);
return;
}
musicID = api1.getMusicId(arg);
if (api1.checkId(musicID)) {
2024-04-17 22:15:12 +08:00
if (PlayMusic.getListSize() >= AllMusic.getConfig().maxPlayList) {
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.listFull);
} else if (DataSql.checkBanMusic(musicID)) {
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.banMusic);
2026-02-26 20:18:57 +08:00
} else if (PlayMusic.haveMusic(musicID, api)) {
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.existMusic);
2024-06-09 20:41:33 +08:00
} else if (PlayMusic.isPlayerMax(name)) {
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.playerToMany);
} else if (DataSql.checkBanPlayer(name)) {
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.playerBan);
2020-02-29 22:09:52 +08:00
} else {
2024-05-13 09:33:41 +08:00
if (checkMoney(sender, name, AllMusic.getConfig().cost.addMusicCost)) {
2023-01-03 18:31:35 +08:00
return;
}
2024-05-13 09:33:41 +08:00
if (cost(sender, name, AllMusic.getConfig().cost.addMusicCost,
2024-04-17 22:15:12 +08:00
AllMusic.getMessage().cost.addMusic)) {
2023-01-03 18:31:35 +08:00
return;
2020-08-19 19:18:38 +08:00
}
2025-03-24 18:20:45 +08:00
DataSql.removeMutePlayer(name);
2025-03-24 20:13:54 +08:00
if (AllMusic.side.needPlay(false)) {
2026-02-26 20:18:57 +08:00
PlayerAddMusicObj obj = new PlayerAddMusicObj();
2022-12-26 15:36:01 +08:00
obj.sender = sender;
obj.id = musicID;
2021-11-30 13:37:43 +08:00
obj.name = name;
2021-02-07 20:41:12 +08:00
obj.isDefault = false;
2026-02-26 20:35:11 +08:00
obj.api = api;
2022-12-27 11:33:26 +08:00
if (AllMusic.side.onMusicAdd(sender, obj)) {
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.cancel);
2022-12-27 11:33:26 +08:00
return;
}
2021-02-07 20:41:12 +08:00
PlayMusic.addTask(obj);
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.success);
2020-02-29 22:09:52 +08:00
} else
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.noPlayer);
2020-02-29 22:09:52 +08:00
}
} else
2025-03-24 18:20:45 +08:00
AllMusic.side.sendMessageTask(sender, AllMusic.getMessage().addMusic.noID);
2020-02-29 22:09:52 +08:00
}
2025-03-09 21:24:18 +08:00
private static boolean isAdmin(String name) {
2024-11-19 10:53:23 +08:00
for (String item : AllMusic.getConfig().adminList) {
if (item.equalsIgnoreCase(name)) {
return true;
}
}
2025-03-09 21:24:18 +08:00
return false;
2024-11-19 10:53:23 +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 sender 发送者
2023-01-08 20:55:21 +08:00
* @param name 用户名
* @param args 参数
2023-01-06 17:03:07 +08:00
*/
2025-01-19 22:51:01 +08:00
public static void execute(Object sender, String name, String[] args) {
2025-09-24 22:16:24 +08:00
try {
if (args.length == 0) {
AllMusic.side.sendMessage(sender, AllMusic.getMessage().command.error);
return;
}
ICommand command = commandList.get(args[0]);
2023-10-22 10:14:55 +08:00
if (command != null) {
2025-01-19 22:51:01 +08:00
command.execute(sender, name, args);
2021-09-04 20:29:47 +08:00
return;
2021-09-04 17:22:48 +08:00
}
2025-09-24 22:16:24 +08:00
if (checkAdmin(sender, name)) {
command = commandAdminList.get(args[0]);
if (command != null) {
command.execute(sender, name, args);
return;
}
}
if (AllMusic.getConfig().needPermission &&
!AllMusic.side.checkPermission(sender, PermissionList.PERMISSION_ADD_MUSIC))
AllMusic.side.sendMessage(sender, AllMusic.getMessage().command.noPer);
else {
switch (AllMusic.getConfig().defaultAddMusic) {
case 1:
searchMusic(sender, name, args, true);
break;
case 0:
default:
2026-02-26 20:35:11 +08:00
if (args.length == 1) {
DataSql.task(() -> addMusic(sender, name, AllMusic.getConfig().defaultApi, args[0]));
} else if (args.length == 2) {
DataSql.task(() -> addMusic(sender, name, args[0], args[1]));
}
2025-09-24 22:16:24 +08:00
}
2020-08-19 19:18:38 +08:00
}
2025-09-24 22:16:24 +08:00
} catch (Exception e) {
e.printStackTrace();
2020-08-19 19:18:38 +08:00
}
2020-02-29 22:09:52 +08:00
}
2024-04-17 22:15:12 +08:00
/**
* 获取Tab指令列表
*
* @param name 用户名
* @param arg 参数
* @return 指令列表
*/
2025-03-09 21:24:18 +08:00
public static List<String> getTabList(Object sender, String name, String[] arg) {
2024-04-17 22:15:12 +08:00
List<String> arguments = new ArrayList<>();
if (arg.length == 0) {
arguments.addAll(normal);
2025-08-28 13:49:55 +08:00
if (checkAdmin(sender, name)) {
2024-04-17 22:15:12 +08:00
arguments.addAll(admin);
}
if (AllMusic.getSearch(name) != null) {
return search;
}
2024-04-17 23:43:11 +08:00
} else {
2024-04-30 14:11:07 +08:00
if (arg[0] == null || arg[0].isEmpty() || arg.length == 1) {
2024-04-17 22:15:12 +08:00
arguments.addAll(normal);
2025-08-28 13:49:55 +08:00
if (checkAdmin(sender, name)) {
2024-04-17 22:15:12 +08:00
arguments.addAll(admin);
}
2024-04-30 14:11:07 +08:00
if (arg[0] == null || arg[0].isEmpty()) {
2024-04-17 22:15:12 +08:00
if (AllMusic.getSearch(name) != null) {
return search;
}
}
2024-04-18 09:03:23 +08:00
} else {
2024-04-17 22:15:12 +08:00
ICommand command = CommandEX.commandList.get(arg[0]);
if (command != null) {
2025-03-09 21:24:18 +08:00
arguments.addAll(command.tab(sender, name, arg, 1));
2024-04-17 22:15:12 +08:00
}
2025-08-28 13:49:55 +08:00
if (checkAdmin(sender, name)) {
2024-07-03 17:42:27 +08:00
command = CommandEX.commandAdminList.get(arg[0]);
if (command != null) {
2025-03-09 21:24:18 +08:00
arguments.addAll(command.tab(sender, name, arg, 1));
2024-07-03 17:42:27 +08:00
}
2024-04-17 22:15:12 +08:00
}
}
}
return arguments;
}
2025-08-28 13:49:55 +08:00
/**
* 检查是否为管理员
2026-01-31 19:06:53 +08:00
*
2025-08-28 13:49:55 +08:00
* @param sender 用户
2026-01-31 19:06:53 +08:00
* @param name 用户名
2025-08-28 13:49:55 +08:00
* @return 是否为管理员
*/
public static boolean checkAdmin(Object sender, String name) {
return isAdmin(name) || AllMusic.side.checkPermission(sender);
}
2020-02-29 22:09:52 +08:00
}