From c12562738fb46f6eba8ede79e60070c5d954c8e8 Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Thu, 21 Mar 2024 11:50:19 +0900 Subject: [PATCH 1/2] Add ServerConnectFailEvent --- .../api/event/ServerConnectFailEvent.java | 37 +++++++++++++++++++ .../java/net/md_5/bungee/UserConnection.java | 26 ++++++++----- 2 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java diff --git a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java new file mode 100644 index 0000000000..4f7350ce5d --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java @@ -0,0 +1,37 @@ +package net.md_5.bungee.api.event; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import net.md_5.bungee.api.ServerConnectRequest; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.api.plugin.Event; + +/** + * This event is called when a connection to a server fails. + * This can occur when the server is offline, the player cannot login due to version mismatch, or the server is full. + */ +@Data +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) +public class ServerConnectFailEvent extends Event +{ + + /** + * Player whom the server is for. + */ + private final ProxiedPlayer player; + /** + * The server itself. + */ + private final Server server; + /** + * Request used to connect to given server. + */ + private final ServerConnectRequest request; + /** + * Cancelled state. + */ + private boolean cancelled; +} diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 9edec4107a..48f9e3faf5 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -39,6 +39,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.PermissionCheckEvent; import net.md_5.bungee.api.event.ServerConnectEvent; +import net.md_5.bungee.api.event.ServerConnectFailEvent; import net.md_5.bungee.api.score.Scoreboard; import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.connection.InitialHandler; @@ -374,17 +375,22 @@ public void operationComplete(ChannelFuture future) throws Exception future.channel().close(); pendingConnects.remove( target ); - ServerInfo def = updateAndGetNextServer( target ); - if ( request.isRetry() && def != null && ( getServer() == null || def != getServer().getInfo() ) ) + ServerConnection server = getServer(); + ServerConnectFailEvent event = new ServerConnectFailEvent( UserConnection.this, server, request ); + if ( !bungee.getPluginManager().callEvent( event ).isCancelled() ) { - sendMessage( bungee.getTranslation( "fallback_lobby" ) ); - connect( def, null, true, ServerConnectEvent.Reason.LOBBY_FALLBACK ); - } else if ( dimensionChange ) - { - disconnect( bungee.getTranslation( "fallback_kick", connectionFailMessage( future.cause() ) ) ); - } else - { - sendMessage( bungee.getTranslation( "fallback_kick", connectionFailMessage( future.cause() ) ) ); + ServerInfo def = updateAndGetNextServer( target ); + if ( request.isRetry() && def != null && ( server == null || def != server.getInfo() ) ) + { + sendMessage( bungee.getTranslation( "fallback_lobby" ) ); + connect( def, null, true, ServerConnectEvent.Reason.LOBBY_FALLBACK ); + } else if ( dimensionChange ) + { + disconnect( bungee.getTranslation( "fallback_kick", connectionFailMessage( future.cause() ) ) ); + } else + { + sendMessage( bungee.getTranslation( "fallback_kick", connectionFailMessage( future.cause() ) ) ); + } } } } From 26c656efe9e577466ef0510cf64b28215979aa38 Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Sun, 24 Mar 2024 13:26:23 +0900 Subject: [PATCH 2/2] Add more documentation to ServerConnectFailEvent --- .../net/md_5/bungee/api/event/ServerConnectFailEvent.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java index 4f7350ce5d..6761aeeb34 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java @@ -9,8 +9,12 @@ import net.md_5.bungee.api.plugin.Event; /** - * This event is called when a connection to a server fails. - * This can occur when the server is offline, the player cannot login due to version mismatch, or the server is full. + * This event is called when a connection to a server fails (e.g. the server is down). + * Only called when the server cannot be connected to, and is not called when kicked from a server (e.g. the server is full). + * Cancelling this event will cancel the transfer to the fallback lobby and prevent the default error message from being shown. + *

+ * If you are using {@link ProxiedPlayer#connect}, this event will be called after the callback is called. + * So, you can't cancel the callback by cancelling this event. */ @Data @ToString(callSuper = false)