Refactor CommandManager to manage command executor lifecycle and add shutdown method

This commit is contained in:
Xujiayao 2026-01-19 15:36:01 +08:00
parent 51e102b343
commit 9bf82012b8
2 changed files with 22 additions and 2 deletions

View file

@ -152,6 +152,8 @@ public class DMCC {
serverInstance.shutdown();
}
CommandManager.shutdown();
// Do NOT clear event handlers here. They are registered once during mod initialization
// and should persist across DMCC reloads.
// EventManager.clear();

View file

@ -22,12 +22,16 @@ import java.util.concurrent.Executors;
public class CommandManager {
private static final Map<String, Command> COMMANDS = new ConcurrentHashMap<>();
private static final ExecutorService COMMAND_EXECUTOR = Executors.newSingleThreadExecutor(ExecutorServiceUtils.newThreadFactory("DMCC-Command"));
private static ExecutorService commandExecutor;
/**
* Initialize and register built-in commands based on the current operating mode.
*/
public static void initialize() {
if (commandExecutor == null || commandExecutor.isShutdown()) {
commandExecutor = Executors.newSingleThreadExecutor(ExecutorServiceUtils.newThreadFactory("DMCC-Command"));
}
COMMANDS.clear();
register(new HelpCommand());
@ -38,6 +42,16 @@ public class CommandManager {
}
}
/**
* Shutdown the command executor.
*/
public static void shutdown() {
if (commandExecutor != null) {
commandExecutor.shutdown();
commandExecutor = null;
}
}
/**
* Register a command.
*
@ -63,7 +77,11 @@ public class CommandManager {
* @param rawInput The raw command line
*/
public static void execute(CommandSender sender, String rawInput) {
COMMAND_EXECUTOR.submit(() -> {
if (commandExecutor == null || commandExecutor.isShutdown()) {
return;
}
commandExecutor.submit(() -> {
String line = rawInput == null ? "" : rawInput.trim();
if (line.isEmpty()) {
sender.reply(I18nManager.getDmccTranslation("terminal.unknown_command", rawInput));