Shut down the game server when Grasscutter is about to shut down

This commit is contained in:
KingRainbow44 2023-06-04 00:26:06 -04:00
parent 494cd3b28c
commit 63ccff8755
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
3 changed files with 24 additions and 22 deletions

View File

@ -1,8 +1,5 @@
package emu.grasscutter; package emu.grasscutter;
import static emu.grasscutter.config.Configuration.SERVER;
import static emu.grasscutter.utils.lang.Language.translate;
import ch.qos.logback.classic.*; import ch.qos.logback.classic.*;
import emu.grasscutter.auth.*; import emu.grasscutter.auth.*;
import emu.grasscutter.command.*; import emu.grasscutter.command.*;
@ -21,16 +18,20 @@ import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.*; import emu.grasscutter.utils.*;
import emu.grasscutter.utils.lang.Language; import emu.grasscutter.utils.lang.Language;
import io.netty.util.concurrent.FastThreadLocalThread; import io.netty.util.concurrent.FastThreadLocalThread;
import java.io.*;
import java.util.Calendar;
import java.util.concurrent.*;
import javax.annotation.Nullable;
import lombok.*; import lombok.*;
import org.jline.reader.*; import org.jline.reader.*;
import org.jline.terminal.*; import org.jline.terminal.*;
import org.reflections.Reflections; import org.reflections.Reflections;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.*;
import java.util.Calendar;
import java.util.concurrent.*;
import static emu.grasscutter.config.Configuration.SERVER;
import static emu.grasscutter.utils.lang.Language.translate;
public final class Grasscutter { public final class Grasscutter {
public static final File configFile = new File("./config.json"); public static final File configFile = new File("./config.json");
public static final Reflections reflector = new Reflections("emu.grasscutter"); public static final Reflections reflector = new Reflections("emu.grasscutter");
@ -183,6 +184,8 @@ public final class Grasscutter {
private static void onShutdown() { private static void onShutdown() {
// Disable all plugins. // Disable all plugins.
if (pluginManager != null) pluginManager.disablePlugins(); if (pluginManager != null) pluginManager.disablePlugins();
// Shutdown the game server.
if (gameServer != null) gameServer.onServerShutdown();
try { try {
// Wait for Grasscutter's thread pool to finish. // Wait for Grasscutter's thread pool to finish.

View File

@ -1,15 +1,15 @@
package emu.grasscutter.command; package emu.grasscutter.command;
import static emu.grasscutter.config.Configuration.SERVER;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.*;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.*;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.util.*;
import static emu.grasscutter.config.Configuration.SERVER;
@SuppressWarnings({"UnusedReturnValue", "unused"}) @SuppressWarnings({"UnusedReturnValue", "unused"})
public final class CommandMap { public final class CommandMap {
private static final int INVALID_UID = Integer.MIN_VALUE; private static final int INVALID_UID = Integer.MIN_VALUE;

View File

@ -1,8 +1,5 @@
package emu.grasscutter.server.game; package emu.grasscutter.server.game;
import static emu.grasscutter.config.Configuration.*;
import static emu.grasscutter.utils.lang.Language.translate;
import emu.grasscutter.*; import emu.grasscutter.*;
import emu.grasscutter.Grasscutter.ServerRunMode; import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
@ -33,13 +30,17 @@ import emu.grasscutter.server.event.types.ServerEvent;
import emu.grasscutter.server.scheduler.ServerTaskScheduler; import emu.grasscutter.server.scheduler.ServerTaskScheduler;
import emu.grasscutter.task.TaskMap; import emu.grasscutter.task.TaskMap;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import kcp.highway.*;
import lombok.*;
import org.jetbrains.annotations.NotNull;
import java.net.*; import java.net.*;
import java.time.*; import java.time.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import kcp.highway.*;
import lombok.*; import static emu.grasscutter.config.Configuration.*;
import org.jetbrains.annotations.NotNull; import static emu.grasscutter.utils.lang.Language.translate;
@Getter @Getter
public final class GameServer extends KcpServer implements Iterable<Player> { public final class GameServer extends KcpServer implements Iterable<Player> {
@ -164,9 +165,6 @@ public final class GameServer extends KcpServer implements Iterable<Player> {
// Chata manager // Chata manager
this.chatManager = new ChatSystem(this); this.chatManager = new ChatSystem(this);
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
} }
private static InetSocketAddress getAdapterInetSocketAddress() { private static InetSocketAddress getAdapterInetSocketAddress() {
@ -330,11 +328,12 @@ public final class GameServer extends KcpServer implements Iterable<Player> {
var event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); var event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now());
event.call(); event.call();
// Save players & the world.
this.getPlayers().forEach((uid, player) -> player.getSession().close()); this.getPlayers().forEach((uid, player) -> player.getSession().close());
this.getWorlds().forEach(World::save); this.getWorlds().forEach(World::save);
Utils.sleep(1000L); // Wait 1 second for operations to finish. Utils.sleep(1000L); // Wait 1 second for operations to finish.
this.stop(); // Stop the server.
try { try {
var threadPool = GameSessionManager.getLogicThread(); var threadPool = GameSessionManager.getLogicThread();