fix the problem that the reference of serverHook in Plugin object is null

This commit is contained in:
Bwly999 2022-05-06 18:16:07 +08:00
parent 744aa478a9
commit 98e1189deb
2 changed files with 22 additions and 16 deletions

View File

@ -92,14 +92,13 @@ public final class Grasscutter {
// Database // Database
DatabaseManager.initialize(); DatabaseManager.initialize();
// Create plugin manager instance.
pluginManager = new PluginManager();
// Create server instances. // Create server instances.
dispatchServer = new DispatchServer(); dispatchServer = new DispatchServer();
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port)); gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
// Create a server hook instance with both servers. // Create a server hook instance with both servers.
new ServerHook(gameServer, dispatchServer); new ServerHook(gameServer, dispatchServer);
// Create plugin manager instance.
pluginManager = new PluginManager();
// Start servers. // Start servers.
if (getConfig().RunMode == ServerRunMode.HYBRID) { if (getConfig().RunMode == ServerRunMode.HYBRID) {

View File

@ -28,6 +28,9 @@ import java.net.InetSocketAddress;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public final class GameServer extends KcpServer { public final class GameServer extends KcpServer {
private final InetSocketAddress address; private final InetSocketAddress address;
@ -67,19 +70,6 @@ public final class GameServer extends KcpServer {
this.taskMap = new TaskMap(true); this.taskMap = new TaskMap(true);
this.dropManager = new DropManager(this); this.dropManager = new DropManager(this);
this.combineManger = new CombineManger(this); this.combineManger = new CombineManger(this);
// Schedule game loop.
Timer gameLoop = new Timer();
gameLoop.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
onTick();
} catch (Exception e) {
Grasscutter.getLogger().error(Grasscutter.getLanguage().An_error_occurred_during_game_update, e);
}
}
}, new Date(), 1000L);
// Hook into shutdown event. // Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown)); Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
@ -212,6 +202,23 @@ public final class GameServer extends KcpServer {
} }
@Override
public synchronized void start() {
// Schedule game loop.
ScheduledExecutorService gameLoop = Executors.newScheduledThreadPool(2);
gameLoop.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
onTick();
} catch (Exception e) {
Grasscutter.getLogger().error(Grasscutter.getLanguage().An_error_occurred_during_game_update, e);
}
}
}, 0L, 1000L, TimeUnit.MILLISECONDS);
super.start();
}
@Override @Override
public void onStartFinish() { public void onStartFinish() {
Grasscutter.getLogger().info(Grasscutter.getLanguage().Grasscutter_is_free); Grasscutter.getLogger().info(Grasscutter.getLanguage().Grasscutter_is_free);