mirror of
https://github.com/System-End/Discord-MC-Chat.git
synced 2026-04-19 23:22:49 +00:00
单线程执行命令
This commit is contained in:
parent
4d92192413
commit
30379da4a3
3 changed files with 40 additions and 40 deletions
|
|
@ -3,6 +3,7 @@ package com.xujiayao.discord_mc_chat.commands;
|
|||
import com.xujiayao.discord_mc_chat.commands.impl.HelpCommand;
|
||||
import com.xujiayao.discord_mc_chat.commands.impl.ReloadCommand;
|
||||
import com.xujiayao.discord_mc_chat.commands.impl.ShutdownCommand;
|
||||
import com.xujiayao.discord_mc_chat.utils.ExecutorServiceUtils;
|
||||
import com.xujiayao.discord_mc_chat.utils.config.ModeManager;
|
||||
import com.xujiayao.discord_mc_chat.utils.i18n.I18nManager;
|
||||
|
||||
|
|
@ -10,6 +11,8 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Central registry and dispatcher for DMCC commands.
|
||||
|
|
@ -19,6 +22,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
public class CommandManager {
|
||||
|
||||
private static final Map<String, Command> COMMANDS = new ConcurrentHashMap<>();
|
||||
private static final ExecutorService COMMAND_EXECUTOR = Executors.newSingleThreadExecutor(ExecutorServiceUtils.newThreadFactory("DMCC-Command"));
|
||||
|
||||
/**
|
||||
* Initialize and register built-in commands based on the current operating mode.
|
||||
|
|
@ -59,26 +63,28 @@ public class CommandManager {
|
|||
* @param rawInput The raw command line
|
||||
*/
|
||||
public static void execute(CommandSender sender, String rawInput) {
|
||||
String line = rawInput == null ? "" : rawInput.trim();
|
||||
if (line.isEmpty()) {
|
||||
sender.reply(I18nManager.getDmccTranslation("terminal.unknown_command", rawInput));
|
||||
return;
|
||||
}
|
||||
COMMAND_EXECUTOR.submit(() -> {
|
||||
String line = rawInput == null ? "" : rawInput.trim();
|
||||
if (line.isEmpty()) {
|
||||
sender.reply(I18nManager.getDmccTranslation("terminal.unknown_command", rawInput));
|
||||
return;
|
||||
}
|
||||
|
||||
String[] parts = line.split("\\s+");
|
||||
String name = parts[0].toLowerCase();
|
||||
String[] args = parts.length > 1 ? line.substring(line.indexOf(' ') + 1).split("\\s+") : new String[0];
|
||||
String[] parts = line.split("\\s+");
|
||||
String name = parts[0].toLowerCase();
|
||||
String[] args = parts.length > 1 ? line.substring(line.indexOf(' ') + 1).split("\\s+") : new String[0];
|
||||
|
||||
Command command = COMMANDS.get(name);
|
||||
if (command == null) {
|
||||
sender.reply(I18nManager.getDmccTranslation("terminal.unknown_command", rawInput));
|
||||
return;
|
||||
}
|
||||
Command command = COMMANDS.get(name);
|
||||
if (command == null) {
|
||||
sender.reply(I18nManager.getDmccTranslation("terminal.unknown_command", rawInput));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
command.execute(sender, args);
|
||||
} catch (Exception e) {
|
||||
sender.reply("Command execution failed: " + e.getMessage());
|
||||
}
|
||||
try {
|
||||
command.execute(sender, args);
|
||||
} catch (Exception e) {
|
||||
sender.reply("Command execution failed: " + e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,15 +29,12 @@ public class ReloadCommand implements Command {
|
|||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String... args) {
|
||||
Thread thread = new Thread(() -> {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.reload.reloading"));
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.reload.reloading"));
|
||||
|
||||
if (DMCC.reload()) {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.reload.success"));
|
||||
} else {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.reload.failure"));
|
||||
}
|
||||
}, "DMCC-ReloadCommand");
|
||||
thread.start();
|
||||
if (DMCC.reload()) {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.reload.success"));
|
||||
} else {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.reload.failure"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,22 +32,19 @@ public class ShutdownCommand implements Command {
|
|||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String... args) {
|
||||
Thread thread = new Thread(() -> {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.shutdown.shutting_down"));
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.shutdown.shutting_down"));
|
||||
|
||||
if (DMCC.shutdown()) {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.shutdown.success"));
|
||||
if (DMCC.shutdown()) {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.shutdown.success"));
|
||||
|
||||
Runtime.getRuntime().removeShutdownHook(SHUTDOWN_THREAD);
|
||||
Runtime.getRuntime().removeShutdownHook(SHUTDOWN_THREAD);
|
||||
|
||||
// Logger cleanup
|
||||
LoggerImpl.shutdown();
|
||||
// Logger cleanup
|
||||
LoggerImpl.shutdown();
|
||||
|
||||
System.exit(0);
|
||||
} else {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.shutdown.failure"));
|
||||
}
|
||||
}, "DMCC-ShutdownCommand");
|
||||
thread.start();
|
||||
System.exit(0);
|
||||
} else {
|
||||
sender.reply(I18nManager.getDmccTranslation("commands.shutdown.failure"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue