Skip to content

Commit 2b06c83

Browse files
committed
Added the ability to display a message when the lobby server is idle when logging in
1 parent 3e6bf57 commit 2b06c83

12 files changed

+96
-29
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ The `config.yml` file includes the following settings, but not all items need to
139139
- `powerControllerType`: Set the type of power controller to be used.
140140
- The built-in PowerController currently supports only `pterodactyl`, which operates Pterodactyl.
141141
- By adding add-ons, you can add your own custom PowerController.
142+
Certainly! Here's the English translation of the provided description:
143+
- `useSynchronousPing`: This setting determines whether to perform **synchronous** pinging to the server during login. (Experimental feature)
144+
- When enabled, pinging the server during login will happen synchronously rather than asynchronously.
145+
- 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.
146+
- 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.
142147
- `startupJoin`: After server startup, it is used to automatically join players to the server and check the server's status.
143148
- `timeout`: Set the maximum waiting time for players to join after server startup.
144149
- Set this value to the maximum time it takes for the server to start.

README_ja.md

+4
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ https://github.com/Kamesuta/BungeePteroPower/assets/16362824/019fdfc5-f0fc-4532-
139139
- `powerControllerType`: 使用するパワーコントローラーのタイプを設定します。
140140
- ビルトインのPowerControllerは現状 `pterodactyl` のみで、Pterodactylを操作します。
141141
- アドオンを追加することで、独自のPowerControllerを追加することができます。
142+
- `useSynchronousPing`: ログイン時、サーバーにPingを送信する際に同期的に行うかどうかを設定します。 (実験的な機能)
143+
- この設定を有効にすると、ログイン時、サーバーにPingを送信する際に非同期ではなく同期的に行います。
144+
- これによりログイン時に「Could not connect to a default or fallback server」メッセージの代わりにBungeePteroPowerのメッセージ(messages.yml 内の `join_autostart_login`)を表示することができます。
145+
- デフォルトは `false` です。ログイン直後に参加するサーバー(ロビーサーバーなど)をBungeePteroPowerで休止状態にしたい場合にONにすると便利です。
142146
- `startupJoin`: サーバー開始後、プレイヤーを自動的に参加させるため、サーバーのステータスをチェックするために使用されます。
143147
- `timeout`: サーバー起動後、プレイヤーが参加するまでの最大待機時間を設定します。
144148
- この値をサーバーが起動するまでの最大時間を設定してください。

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kamesuta</groupId>
88
<artifactId>BungeePteroPower</artifactId>
9-
<version>1.6-SNAPSHOT</version>
9+
<version>1.7-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>BungeePteroPower</name>

src/main/java/com/kamesuta/bungeepteropower/Config.java

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public class Config {
5353
* (e.g. "pterodactyl")
5454
*/
5555
public final String powerControllerType;
56+
/**
57+
* Send pings to the server synchronously
58+
*/
59+
public final boolean useSynchronousPing;
5660
/**
5761
* The number of seconds the plugin will try to connect the player to the desired server
5862
* Set this to the maximum time the server can take to start
@@ -105,6 +109,7 @@ public Config() {
105109
this.language = configuration.getString("language");
106110
this.startTimeout = configuration.getInt("startTimeout");
107111
this.powerControllerType = configuration.getString("powerControllerType");
112+
this.useSynchronousPing = configuration.getBoolean("useSynchronousPing", false);
108113

109114
// Startup join settings
110115
this.startupJoinTimeout = configuration.getInt("startupJoin.timeout");

src/main/java/com/kamesuta/bungeepteropower/PlayerListener.java

+69-28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import net.md_5.bungee.api.plugin.Listener;
1818
import net.md_5.bungee.event.EventHandler;
1919

20+
import java.util.concurrent.CompletableFuture;
21+
import java.util.logging.Level;
22+
23+
import static com.kamesuta.bungeepteropower.BungeePteroPower.logger;
2024
import static com.kamesuta.bungeepteropower.BungeePteroPower.plugin;
2125

2226
/**
@@ -79,39 +83,76 @@ public void onServerConnect(ServerConnectEvent event) {
7983
return;
8084
}
8185

86+
// Check if the event is a join event
87+
boolean isLogin = event.getReason() == ServerConnectEvent.Reason.JOIN_PROXY;
88+
// Send pings to the server synchronously
89+
boolean useSynchronousPing = isLogin && plugin.config.useSynchronousPing;
90+
8291
// Ping the target server and check if it is offline
92+
CompletableFuture<Void> pingFuture = new CompletableFuture<>();
8393
targetServer.ping((result, error) -> {
84-
if (error != null) { // The server is offline
85-
String serverName = targetServer.getName();
86-
87-
// Start the target server
88-
if (autostart) {
89-
// Send title and message
90-
player.sendTitle(instance.createTitle()
91-
.title(new ComponentBuilder(plugin.messages.getMessage("join_autostart_title", serverName)).color(ChatColor.YELLOW).create())
92-
.subTitle(new ComponentBuilder(plugin.messages.getMessage("join_autostart_subtitle", serverName)).create())
93-
);
94-
95-
// Send power signal
96-
ServerController.sendPowerSignal(player, serverName, serverId, PowerSignal.START);
97-
98-
// Record statistics
99-
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.START_SERVER_AUTOJOIN);
100-
plugin.statistics.startReasonRecorder.recordStart(serverName, Statistics.StartReasonRecorder.StartReason.AUTOJOIN);
101-
102-
} else {
103-
// Send message including the command to start the server
104-
player.sendMessage(plugin.messages.warning("join_start", serverName));
105-
player.sendMessage(new ComponentBuilder()
106-
.append(plugin.messages.success("join_start_button", serverName))
107-
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ptero start " + serverName))
108-
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(plugin.messages.getMessage("join_start_button_tooltip", serverName))))
109-
.color(ChatColor.GREEN)
110-
.create());
111-
94+
try {
95+
// The server is offline
96+
if (error != null) {
97+
String serverName = targetServer.getName();
98+
99+
// Start the target server
100+
if (autostart) {
101+
// 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".
102+
if (useSynchronousPing) {
103+
// Disconnect the player to show custom message
104+
player.disconnect(new ComponentBuilder(plugin.messages.getMessage("join_autostart_login")).color(ChatColor.YELLOW).create());
105+
} else {
106+
// Send title and message
107+
player.sendTitle(instance.createTitle()
108+
.title(new ComponentBuilder(plugin.messages.getMessage("join_autostart_title", serverName)).color(ChatColor.YELLOW).create())
109+
.subTitle(new ComponentBuilder(plugin.messages.getMessage("join_autostart_subtitle", serverName)).create())
110+
);
111+
}
112+
113+
// Send power signal
114+
ServerController.sendPowerSignal(player, serverName, serverId, PowerSignal.START);
115+
116+
// Record statistics
117+
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.START_SERVER_AUTOJOIN);
118+
plugin.statistics.startReasonRecorder.recordStart(serverName, Statistics.StartReasonRecorder.StartReason.AUTOJOIN);
119+
120+
// If synchronous ping is enabled, we can suppress "Could not connect to a default or fallback server" message
121+
if (useSynchronousPing) {
122+
event.setCancelled(true);
123+
}
124+
125+
} else {
126+
// Send message including the command to start the server
127+
player.sendMessage(plugin.messages.warning("join_start", serverName));
128+
player.sendMessage(new ComponentBuilder()
129+
.append(plugin.messages.success("join_start_button", serverName))
130+
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ptero start " + serverName))
131+
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(plugin.messages.getMessage("join_start_button_tooltip", serverName))))
132+
.color(ChatColor.GREEN)
133+
.create());
134+
135+
}
112136
}
137+
138+
} catch (Exception e) {
139+
logger.log(Level.WARNING, "Failed to start server process after ping: " + targetServer.getName(), e);
140+
141+
} finally {
142+
// Complete the future
143+
pingFuture.complete(null);
144+
113145
}
114146
});
147+
148+
// Wait until the ping is finished
149+
if (useSynchronousPing) {
150+
try {
151+
pingFuture.get();
152+
} catch (Exception e) {
153+
logger.log(Level.WARNING, "Failed to wait for the ping of the server: " + targetServer.getName(), e);
154+
}
155+
}
115156
}
116157

117158
@EventHandler(priority = (byte) 1024)

src/main/java/com/kamesuta/bungeepteropower/Statistics.java

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void register() {
3333
// Config charts
3434
metrics.addCustomChart(new SimplePie("powerControllerType", () -> plugin.config.powerControllerType));
3535
metrics.addCustomChart(new SimplePie("language", () -> plugin.config.language));
36+
metrics.addCustomChart(new SimplePie("useSynchronousPing", () -> plugin.config.useSynchronousPing ? "Enabled" : "Disabled"));
3637

3738
// The number of servers managed by BungeePteroPower
3839
// I would like to know the percentage of how many servers are using this plugin.

src/main/resources/config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ startTimeout: 120
2525
# Supported types: pterodactyl
2626
# You can add your own power controller using API.
2727
powerControllerType: pterodactyl
28+
29+
# Perform synchronous pinging to the server during login. (Experimental feature)
30+
# When enabled, pinging the server during login will happen synchronously rather than asynchronously.
31+
# This allows displaying BungeePteroPower messages instead of the "Could not connect to a default or fallback server" message upon login.
32+
# 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.
33+
useSynchronousPing: false
2834

2935
# This is used to check the server status to transfer players after the server starts
3036
startupJoin:

src/main/resources/messages_en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ update_available_tooltip: "Click to download v%2$s!"
55

66
join_autostart_title: "Starting server..."
77
join_autostart_subtitle: "Please wait a moment and try reconnecting."
8+
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."
89
join_start: "The server %s is suspended to reduce server resources, but it can be started by clicking the button below."
910
join_start_button: "[Start Server %s]"
1011
join_start_button_tooltip: "Click to start the server %s!"

src/main/resources/messages_fr.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ update_available_tooltip: "Cliquez pour télécharger v%2$s!"
55

66
join_autostart_title: "Démarrage du serveur..."
77
join_autostart_subtitle: "Veuillez patienter un moment et essayer de vous reconnecter."
8+
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."
89
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."
910
join_start_button: "[Démarrer le serveur %s]"
1011
join_start_button_tooltip: "Cliquez pour démarrer le serveur %s !"

src/main/resources/messages_ja.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ update_available_tooltip: "クリックして v%2$s をダウンロード!"
55

66
join_autostart_title: "サーバーを起動中..."
77
join_autostart_subtitle: "しばらく待ってから再接続してください。"
8+
join_autostart_login: "サーバーを起動中...\n\nサーバー「%s」はサーバーリソースを節約するために休止中です。\n現在起動中ですので、しばらく待ってから再接続してください。"
89
join_start: "サーバー「%s」はサーバーリソースを節約するために休止中ですが、下のボタンをクリックすると起動できます。"
910
join_start_button: "[サーバー「%s」を起動]"
1011
join_start_button_tooltip: "クリックしてサーバー「%s」を起動!"

src/main/resources/messages_ro.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ update_available_tooltip: "Click pentru a descărca v%2$s!"
55

66
join_autostart_title: "Se pornește serverul..."
77
join_autostart_subtitle: "Vă rugăm să așteptați un moment și să încercați să vă reconectați."
8+
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."
89
join_start: "Serverul %s este suspendat pentru a reduce resursele, dar poate fi pornit făcând clic pe butonul de mai jos."
910
join_start_button: "[Pornește serverul %s]"
1011
join_start_button_tooltip: "Faceți clic pentru a porni serverul %s!"

src/main/resources/messages_zh-cn.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ update_available_tooltip: "点击下载 v%2$s!"
55

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

0 commit comments

Comments
 (0)