From 93d555fe0eda6a235778ef46dd882a71345e1412 Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Sun, 11 Feb 2024 05:05:52 +0900 Subject: [PATCH] Fix server timeout when server started --- pom.xml | 2 +- .../com/kamesuta/bungeepteropower/Config.java | 4 +- .../bungeepteropower/ServerController.java | 106 +++++++++--------- 3 files changed, 58 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index f3a0ad7..5700a1e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.kamesuta BungeePteroPower - 1.3-SNAPSHOT + 1.4-SNAPSHOT jar BungeePteroPower diff --git a/src/main/java/com/kamesuta/bungeepteropower/Config.java b/src/main/java/com/kamesuta/bungeepteropower/Config.java index 14f1bfe..16e6386 100644 --- a/src/main/java/com/kamesuta/bungeepteropower/Config.java +++ b/src/main/java/com/kamesuta/bungeepteropower/Config.java @@ -142,8 +142,8 @@ public Config() { * @param serverName The Bungeecord server name * @return The auto stop time */ - public @Nullable Integer getServerTimeout(String serverName) { - return serverTimeoutMap.get(serverName); + public int getServerTimeout(String serverName) { + return serverTimeoutMap.getOrDefault(serverName, 0); } /** diff --git a/src/main/java/com/kamesuta/bungeepteropower/ServerController.java b/src/main/java/com/kamesuta/bungeepteropower/ServerController.java index bb96cd0..fb709cb 100644 --- a/src/main/java/com/kamesuta/bungeepteropower/ServerController.java +++ b/src/main/java/com/kamesuta/bungeepteropower/ServerController.java @@ -31,50 +31,49 @@ public static void sendPowerSignal(CommandSender sender, String serverName, Stri // Send signal plugin.config.getPowerController().sendPowerSignal(serverName, serverId, signalType).thenRun(() -> { + if (signalType == PowerSignal.STOP) { + // When stopping the server + sender.sendMessage(plugin.messages.success("server_stop", serverName)); + } + // Start auto stop task and send warning - if (signalType == PowerSignal.START) { + if (sender instanceof ProxiedPlayer && plugin.config.startupJoinTimeout > 0) { // If auto join is configured, join the server when it is started - if (sender instanceof ProxiedPlayer && plugin.config.startupJoinTimeout > 0) { - // If auto join is configured, join the server when it is started - sender.sendMessage(plugin.messages.success("server_startup_join", serverName)); - - // Get the server info - ServerInfo serverInfo = plugin.getProxy().getServerInfo(serverName); - // ServerInfo is null if the server is not found on bungeecord config - if (serverInfo != null) { - // Wait until the server is started - onceStarted(serverInfo).thenRun(() -> { - // Move player to the started server - ProxiedPlayer player = (ProxiedPlayer) sender; - if (plugin.config.joinDelay > 0) { - // Delay the join - plugin.getProxy().getScheduler().schedule(plugin, () -> player.connect(serverInfo), plugin.config.joinDelay, TimeUnit.SECONDS); - // Send a message - player.sendMessage(plugin.messages.success("server_startup_join_move_delayed", serverName, plugin.config.joinDelay)); - } else { - // Join immediately - player.connect(serverInfo); - // Send a message - player.sendMessage(plugin.messages.success("server_startup_join_move", serverName)); - } - }).exceptionally((Throwable e) -> { - sender.sendMessage(plugin.messages.warning("server_startup_join_failed", serverName)); - return null; - }); - } - - } else { - // Otherwise, just send a message - sender.sendMessage(plugin.messages.success("server_start", serverName)); + sender.sendMessage(plugin.messages.success("server_startup_join", serverName)); + + // Get the server info + ServerInfo serverInfo = plugin.getProxy().getServerInfo(serverName); + // ServerInfo is null if the server is not found on bungeecord config + if (serverInfo != null) { + // Wait until the server is started + onceStarted(serverInfo).thenRun(() -> { + // Move player to the started server + ProxiedPlayer player = (ProxiedPlayer) sender; + if (plugin.config.joinDelay > 0) { + // Delay the join + plugin.getProxy().getScheduler().schedule(plugin, () -> player.connect(serverInfo), plugin.config.joinDelay, TimeUnit.SECONDS); + // Send a message + player.sendMessage(plugin.messages.success("server_startup_join_move_delayed", serverName, plugin.config.joinDelay)); + } else { + // Join immediately + player.connect(serverInfo); + // Send a message + player.sendMessage(plugin.messages.success("server_startup_join_move", serverName)); + } + }).exceptionally((Throwable e) -> { + sender.sendMessage(plugin.messages.warning("server_startup_join_failed", serverName)); + return null; + }); } - // Stop the server if nobody joins after a while - stopAfterWhile(sender, serverName, serverId, signalType); } else { - // When stopping the server - sender.sendMessage(plugin.messages.success("server_stop", serverName)); + // Otherwise, just send a message + sender.sendMessage(plugin.messages.success("server_start", serverName)); } + // Stop the server if nobody joins after a while + stopAfterWhile(sender, serverName, serverId, signalType); + }).exceptionally(e -> { sender.sendMessage(plugin.messages.error("server_" + signal + "_failed", serverName)); return null; @@ -95,21 +94,26 @@ public static void stopAfterWhile(CommandSender sender, String serverName, Strin String signal = signalType.getSignal(); // Get the auto stop time - Integer serverTimeout = plugin.config.getServerTimeout(serverName); - if (serverTimeout != null && serverTimeout >= 0) { - // Stop the server after a while - plugin.delay.stopAfterWhile(serverName, serverTimeout, () -> { - // Stop the server - sendPowerSignal(sender, serverName, serverId, PowerSignal.STOP); - - // Record statistics - plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.STOP_SERVER_NOBODY); - plugin.statistics.startReasonRecorder.recordStop(serverName); - }); - - // Send message - sender.sendMessage(plugin.messages.warning("server_" + signal + "_warning", serverName, serverTimeout)); + int serverTimeout = plugin.config.getServerTimeout(serverName); + if (serverTimeout == 0) return; + + // When on starting, use the start timeout additionally + if (signalType == PowerSignal.START) { + serverTimeout += plugin.config.startTimeout; } + + // Stop the server after a while + plugin.delay.stopAfterWhile(serverName, serverTimeout, () -> { + // Stop the server + sendPowerSignal(sender, serverName, serverId, PowerSignal.STOP); + + // Record statistics + plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.STOP_SERVER_NOBODY); + plugin.statistics.startReasonRecorder.recordStop(serverName); + }); + + // Send message + sender.sendMessage(plugin.messages.warning("server_" + signal + "_warning", serverName, serverTimeout)); } /**