Skip to content

Commit

Permalink
Fix server timeout when server started
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta committed Feb 10, 2024
1 parent b6aa8c9 commit 93d555f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.kamesuta</groupId>
<artifactId>BungeePteroPower</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BungeePteroPower</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/kamesuta/bungeepteropower/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
106 changes: 55 additions & 51 deletions src/main/java/com/kamesuta/bungeepteropower/ServerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}

/**
Expand Down

0 comments on commit 93d555f

Please sign in to comment.