Skip to content

Commit

Permalink
Added the ability to display a message when the lobby server is idle …
Browse files Browse the repository at this point in the history
…when logging in
  • Loading branch information
Kamesuta committed Mar 9, 2024
1 parent 3e6bf57 commit 2b06c83
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ The `config.yml` file includes the following settings, but not all items need to
- `powerControllerType`: Set the type of power controller to be used.
- The built-in PowerController currently supports only `pterodactyl`, which operates Pterodactyl.
- By adding add-ons, you can add your own custom PowerController.
Certainly! Here's the English translation of the provided description:
- `useSynchronousPing`: This setting determines whether to perform **synchronous** pinging to the server during login. (Experimental feature)
- When enabled, pinging the server during login will happen synchronously rather than asynchronously.
- This allows displaying BungeePteroPower messages (`join_autostart_login` in messages.yml) instead of the "Could not connect to a default or fallback server" message upon login.
- The default value is `false`. Enabling this can be useful if you want to set servers (such as lobby servers) to a suspended state in BungeePteroPower immediately after login.
- `startupJoin`: After server startup, it is used to automatically join players to the server and check the server's status.
- `timeout`: Set the maximum waiting time for players to join after server startup.
- Set this value to the maximum time it takes for the server to start.
Expand Down
4 changes: 4 additions & 0 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ https://github.com/Kamesuta/BungeePteroPower/assets/16362824/019fdfc5-f0fc-4532-
- `powerControllerType`: 使用するパワーコントローラーのタイプを設定します。
- ビルトインのPowerControllerは現状 `pterodactyl` のみで、Pterodactylを操作します。
- アドオンを追加することで、独自のPowerControllerを追加することができます。
- `useSynchronousPing`: ログイン時、サーバーにPingを送信する際に同期的に行うかどうかを設定します。 (実験的な機能)
- この設定を有効にすると、ログイン時、サーバーにPingを送信する際に非同期ではなく同期的に行います。
- これによりログイン時に「Could not connect to a default or fallback server」メッセージの代わりにBungeePteroPowerのメッセージ(messages.yml 内の `join_autostart_login`)を表示することができます。
- デフォルトは `false` です。ログイン直後に参加するサーバー(ロビーサーバーなど)をBungeePteroPowerで休止状態にしたい場合にONにすると便利です。
- `startupJoin`: サーバー開始後、プレイヤーを自動的に参加させるため、サーバーのステータスをチェックするために使用されます。
- `timeout`: サーバー起動後、プレイヤーが参加するまでの最大待機時間を設定します。
- この値をサーバーが起動するまでの最大時間を設定してください。
Expand Down
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.6-SNAPSHOT</version>
<version>1.7-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BungeePteroPower</name>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/kamesuta/bungeepteropower/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class Config {
* (e.g. "pterodactyl")
*/
public final String powerControllerType;
/**
* Send pings to the server synchronously
*/
public final boolean useSynchronousPing;
/**
* The number of seconds the plugin will try to connect the player to the desired server
* Set this to the maximum time the server can take to start
Expand Down Expand Up @@ -105,6 +109,7 @@ public Config() {
this.language = configuration.getString("language");
this.startTimeout = configuration.getInt("startTimeout");
this.powerControllerType = configuration.getString("powerControllerType");
this.useSynchronousPing = configuration.getBoolean("useSynchronousPing", false);

// Startup join settings
this.startupJoinTimeout = configuration.getInt("startupJoin.timeout");
Expand Down
97 changes: 69 additions & 28 deletions src/main/java/com/kamesuta/bungeepteropower/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;

import static com.kamesuta.bungeepteropower.BungeePteroPower.logger;
import static com.kamesuta.bungeepteropower.BungeePteroPower.plugin;

/**
Expand Down Expand Up @@ -79,39 +83,76 @@ public void onServerConnect(ServerConnectEvent event) {
return;
}

// Check if the event is a join event
boolean isLogin = event.getReason() == ServerConnectEvent.Reason.JOIN_PROXY;
// Send pings to the server synchronously
boolean useSynchronousPing = isLogin && plugin.config.useSynchronousPing;

// Ping the target server and check if it is offline
CompletableFuture<Void> pingFuture = new CompletableFuture<>();
targetServer.ping((result, error) -> {
if (error != null) { // The server is offline
String serverName = targetServer.getName();

// Start the target server
if (autostart) {
// Send title and message
player.sendTitle(instance.createTitle()
.title(new ComponentBuilder(plugin.messages.getMessage("join_autostart_title", serverName)).color(ChatColor.YELLOW).create())
.subTitle(new ComponentBuilder(plugin.messages.getMessage("join_autostart_subtitle", serverName)).create())
);

// Send power signal
ServerController.sendPowerSignal(player, serverName, serverId, PowerSignal.START);

// Record statistics
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.START_SERVER_AUTOJOIN);
plugin.statistics.startReasonRecorder.recordStart(serverName, Statistics.StartReasonRecorder.StartReason.AUTOJOIN);

} else {
// Send message including the command to start the server
player.sendMessage(plugin.messages.warning("join_start", serverName));
player.sendMessage(new ComponentBuilder()
.append(plugin.messages.success("join_start_button", serverName))
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ptero start " + serverName))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(plugin.messages.getMessage("join_start_button_tooltip", serverName))))
.color(ChatColor.GREEN)
.create());

try {
// The server is offline
if (error != null) {
String serverName = targetServer.getName();

// Start the target server
if (autostart) {
// If synchronous ping is enabled, we can disconnect the player to show a custom message instead of "Could not connect to a default or fallback server".
if (useSynchronousPing) {
// Disconnect the player to show custom message
player.disconnect(new ComponentBuilder(plugin.messages.getMessage("join_autostart_login")).color(ChatColor.YELLOW).create());
} else {
// Send title and message
player.sendTitle(instance.createTitle()
.title(new ComponentBuilder(plugin.messages.getMessage("join_autostart_title", serverName)).color(ChatColor.YELLOW).create())
.subTitle(new ComponentBuilder(plugin.messages.getMessage("join_autostart_subtitle", serverName)).create())
);
}

// Send power signal
ServerController.sendPowerSignal(player, serverName, serverId, PowerSignal.START);

// Record statistics
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.START_SERVER_AUTOJOIN);
plugin.statistics.startReasonRecorder.recordStart(serverName, Statistics.StartReasonRecorder.StartReason.AUTOJOIN);

// If synchronous ping is enabled, we can suppress "Could not connect to a default or fallback server" message
if (useSynchronousPing) {
event.setCancelled(true);
}

} else {
// Send message including the command to start the server
player.sendMessage(plugin.messages.warning("join_start", serverName));
player.sendMessage(new ComponentBuilder()
.append(plugin.messages.success("join_start_button", serverName))
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ptero start " + serverName))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(plugin.messages.getMessage("join_start_button_tooltip", serverName))))
.color(ChatColor.GREEN)
.create());

}
}

} catch (Exception e) {
logger.log(Level.WARNING, "Failed to start server process after ping: " + targetServer.getName(), e);

} finally {
// Complete the future
pingFuture.complete(null);

}
});

// Wait until the ping is finished
if (useSynchronousPing) {
try {
pingFuture.get();
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to wait for the ping of the server: " + targetServer.getName(), e);
}
}
}

@EventHandler(priority = (byte) 1024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void register() {
// Config charts
metrics.addCustomChart(new SimplePie("powerControllerType", () -> plugin.config.powerControllerType));
metrics.addCustomChart(new SimplePie("language", () -> plugin.config.language));
metrics.addCustomChart(new SimplePie("useSynchronousPing", () -> plugin.config.useSynchronousPing ? "Enabled" : "Disabled"));

// The number of servers managed by BungeePteroPower
// I would like to know the percentage of how many servers are using this plugin.
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ startTimeout: 120
# Supported types: pterodactyl
# You can add your own power controller using API.
powerControllerType: pterodactyl

# Perform synchronous pinging to the server during login. (Experimental feature)
# When enabled, pinging the server during login will happen synchronously rather than asynchronously.
# This allows displaying BungeePteroPower messages instead of the "Could not connect to a default or fallback server" message upon login.
# The default value is `false`. Enabling this can be useful if you want to set servers (such as lobby servers) to a suspended state in BungeePteroPower immediately after login.
useSynchronousPing: false

# This is used to check the server status to transfer players after the server starts
startupJoin:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ update_available_tooltip: "Click to download v%2$s!"

join_autostart_title: "Starting server..."
join_autostart_subtitle: "Please wait a moment and try reconnecting."
join_autostart_login: "Starting server…\n\nServer %s is currently in hibernation mode to conserve server resources.\nIt is now being started, so please wait a moment and then reconnect."
join_start: "The server %s is suspended to reduce server resources, but it can be started by clicking the button below."
join_start_button: "[Start Server %s]"
join_start_button_tooltip: "Click to start the server %s!"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ update_available_tooltip: "Cliquez pour télécharger v%2$s!"

join_autostart_title: "Démarrage du serveur..."
join_autostart_subtitle: "Veuillez patienter un moment et essayer de vous reconnecter."
join_autostart_login: "Démarrage du serveur en cours...\n\nLe serveur %s est actuellement en mode veille pour économiser des ressources.\nVeuillez attendre un moment avant de vous reconnecter."
join_start: "Le serveur %s est suspendu pour réduire les ressources du serveur, mais il peut être démarré en cliquant sur le bouton ci-dessous."
join_start_button: "[Démarrer le serveur %s]"
join_start_button_tooltip: "Cliquez pour démarrer le serveur %s !"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ update_available_tooltip: "クリックして v%2$s をダウンロード!"

join_autostart_title: "サーバーを起動中..."
join_autostart_subtitle: "しばらく待ってから再接続してください。"
join_autostart_login: "サーバーを起動中...\n\nサーバー「%s」はサーバーリソースを節約するために休止中です。\n現在起動中ですので、しばらく待ってから再接続してください。"
join_start: "サーバー「%s」はサーバーリソースを節約するために休止中ですが、下のボタンをクリックすると起動できます。"
join_start_button: "[サーバー「%s」を起動]"
join_start_button_tooltip: "クリックしてサーバー「%s」を起動!"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_ro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ update_available_tooltip: "Click pentru a descărca v%2$s!"

join_autostart_title: "Se pornește serverul..."
join_autostart_subtitle: "Vă rugăm să așteptați un moment și să încercați să vă reconectați."
join_autostart_login: "Se pornește serverul...\n\nServerul %s este în modul de economisire a resurselor și este în curs de pornire.\nVă rugăm să așteptați puțin înainte de a vă reconecta."
join_start: "Serverul %s este suspendat pentru a reduce resursele, dar poate fi pornit făcând clic pe butonul de mai jos."
join_start_button: "[Pornește serverul %s]"
join_start_button_tooltip: "Faceți clic pentru a porni serverul %s!"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_zh-cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ update_available_tooltip: "点击下载 v%2$s!"

join_autostart_title: "服务器启动中..."
join_autostart_subtitle: "请稍候再尝试重新连接。"
join_autostart_login: "正在启动服务器...\n\n服务器「%s」当前处于休眠状态以节省资源。\n请稍后重新连接。"
join_start: "服务器「%s」处于休眠状态以节省资源,但您可以通过点击下方按钮来启动。"
join_start_button: "[启动服务器「%s」]"
join_start_button_tooltip: "点击以启动服务器「%s」!"
Expand Down

0 comments on commit 2b06c83

Please sign in to comment.