From 2b06c8333ca9b924049f6b72034d71aed9c85477 Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Sat, 9 Mar 2024 13:53:50 +0900 Subject: [PATCH] Added the ability to display a message when the lobby server is idle when logging in --- README.md | 5 + README_ja.md | 4 + pom.xml | 2 +- .../com/kamesuta/bungeepteropower/Config.java | 5 + .../bungeepteropower/PlayerListener.java | 97 +++++++++++++------ .../kamesuta/bungeepteropower/Statistics.java | 1 + src/main/resources/config.yml | 6 ++ src/main/resources/messages_en.yml | 1 + src/main/resources/messages_fr.yml | 1 + src/main/resources/messages_ja.yml | 1 + src/main/resources/messages_ro.yml | 1 + src/main/resources/messages_zh-cn.yml | 1 + 12 files changed, 96 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 2c33eb8..6330b32 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/README_ja.md b/README_ja.md index 98db1ae..1b082d3 100644 --- a/README_ja.md +++ b/README_ja.md @@ -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`: サーバー起動後、プレイヤーが参加するまでの最大待機時間を設定します。 - この値をサーバーが起動するまでの最大時間を設定してください。 diff --git a/pom.xml b/pom.xml index f6bc0a7..f5b8b51 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.kamesuta BungeePteroPower - 1.6-SNAPSHOT + 1.7-SNAPSHOT jar BungeePteroPower diff --git a/src/main/java/com/kamesuta/bungeepteropower/Config.java b/src/main/java/com/kamesuta/bungeepteropower/Config.java index 622bc93..326eff9 100644 --- a/src/main/java/com/kamesuta/bungeepteropower/Config.java +++ b/src/main/java/com/kamesuta/bungeepteropower/Config.java @@ -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 @@ -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"); diff --git a/src/main/java/com/kamesuta/bungeepteropower/PlayerListener.java b/src/main/java/com/kamesuta/bungeepteropower/PlayerListener.java index 53bd26b..6b543aa 100644 --- a/src/main/java/com/kamesuta/bungeepteropower/PlayerListener.java +++ b/src/main/java/com/kamesuta/bungeepteropower/PlayerListener.java @@ -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; /** @@ -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 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) diff --git a/src/main/java/com/kamesuta/bungeepteropower/Statistics.java b/src/main/java/com/kamesuta/bungeepteropower/Statistics.java index 3ca709c..d550765 100644 --- a/src/main/java/com/kamesuta/bungeepteropower/Statistics.java +++ b/src/main/java/com/kamesuta/bungeepteropower/Statistics.java @@ -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. diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3bc10ec..bbc9541 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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: diff --git a/src/main/resources/messages_en.yml b/src/main/resources/messages_en.yml index 4ad2684..a157bf7 100644 --- a/src/main/resources/messages_en.yml +++ b/src/main/resources/messages_en.yml @@ -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!" diff --git a/src/main/resources/messages_fr.yml b/src/main/resources/messages_fr.yml index d2e0ef6..857cbe2 100644 --- a/src/main/resources/messages_fr.yml +++ b/src/main/resources/messages_fr.yml @@ -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 !" diff --git a/src/main/resources/messages_ja.yml b/src/main/resources/messages_ja.yml index 882d202..b123741 100644 --- a/src/main/resources/messages_ja.yml +++ b/src/main/resources/messages_ja.yml @@ -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」を起動!" diff --git a/src/main/resources/messages_ro.yml b/src/main/resources/messages_ro.yml index cf35fc6..1547cbc 100644 --- a/src/main/resources/messages_ro.yml +++ b/src/main/resources/messages_ro.yml @@ -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!" diff --git a/src/main/resources/messages_zh-cn.yml b/src/main/resources/messages_zh-cn.yml index 31901b1..3eace82 100644 --- a/src/main/resources/messages_zh-cn.yml +++ b/src/main/resources/messages_zh-cn.yml @@ -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」!"