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..6761aeeb34 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectFailEvent.java @@ -0,0 +1,41 @@ +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 (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) +@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() ) ) ); + } } } }