Actually shutdown Minestom when MinecraftServer.stopCleanly() is called

This commit is contained in:
jglrxavpok 2020-07-01 21:03:53 +02:00
parent 4fb03a8479
commit 82c2af88a0
3 changed files with 37 additions and 9 deletions

View File

@ -21,6 +21,7 @@ import net.minestom.server.scoreboard.TeamManager;
import net.minestom.server.storage.StorageFolder;
import net.minestom.server.storage.StorageManager;
import net.minestom.server.timer.SchedulerManager;
import net.minestom.server.utils.thread.MinestomThread;
import net.minestom.server.world.Difficulty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -227,6 +228,10 @@ public class MinecraftServer {
nettyServer.stop();
schedulerManager.shutdown();
storageManager.getLoadedFolders().forEach(StorageFolder::close);
LOGGER.info("Shutting down all thread pools.");
MinestomThread.shutdownAll();
benchmarkManager.disable();
commandManager.stopConsoleThread();
LOGGER.info("Minestom server stopped successfully.");
}

View File

@ -13,6 +13,7 @@ import java.util.*;
public class CommandManager {
private boolean running;
private String commandPrefix = "/";
private ConsoleSender consoleSender = new ConsoleSender();
@ -21,18 +22,26 @@ public class CommandManager {
private Map<String, CommandProcessor> commandProcessorMap = new HashMap<>();
public CommandManager() {
running = true;
// Setup console thread
new Thread(() -> {
Thread consoleThread = new Thread(() -> {
Scanner scanner = new Scanner(System.in);
while (true) {
String command = scanner.nextLine();
if (!command.startsWith(commandPrefix))
continue;
command = command.replaceFirst(commandPrefix, "");
execute(consoleSender, command);
while (running) {
if(scanner.hasNext()) {
String command = scanner.nextLine();
if (!command.startsWith(commandPrefix))
continue;
command = command.replaceFirst(commandPrefix, "");
execute(consoleSender, command);
}
}
}, "ConsoleCommand-Thread").start();
}, "ConsoleCommand-Thread");
consoleThread.setDaemon(true);
consoleThread.start();
}
public void stopConsoleThread() {
running = false;
}
public void register(Command<CommandSender> command) {

View File

@ -1,16 +1,30 @@
package net.minestom.server.utils.thread;
import net.minestom.server.MinecraftServer;
import net.minestom.server.timer.SchedulerManager;
import net.minestom.server.timer.TaskRunnable;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MinestomThread extends ThreadPoolExecutor {
private static final List<MinestomThread> executors = new LinkedList<>();
public MinestomThread(int nThreads, String name) {
super(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), r -> {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName(thread.getName().replace("Thread", name));
return thread;
});
executors.add(this);
}
public static void shutdownAll() {
executors.forEach(MinestomThread::shutdownNow);
}
}