|
17 | 17 | import net.md_5.bungee.api.plugin.Listener;
|
18 | 18 | import net.md_5.bungee.event.EventHandler;
|
19 | 19 |
|
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.logging.Level; |
| 22 | + |
| 23 | +import static com.kamesuta.bungeepteropower.BungeePteroPower.logger; |
20 | 24 | import static com.kamesuta.bungeepteropower.BungeePteroPower.plugin;
|
21 | 25 |
|
22 | 26 | /**
|
@@ -79,39 +83,76 @@ public void onServerConnect(ServerConnectEvent event) {
|
79 | 83 | return;
|
80 | 84 | }
|
81 | 85 |
|
| 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 | + |
82 | 91 | // Ping the target server and check if it is offline
|
| 92 | + CompletableFuture<Void> pingFuture = new CompletableFuture<>(); |
83 | 93 | 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 | + } |
112 | 136 | }
|
| 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 | + |
113 | 145 | }
|
114 | 146 | });
|
| 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 | + } |
115 | 156 | }
|
116 | 157 |
|
117 | 158 | @EventHandler(priority = (byte) 1024)
|
|
0 commit comments