From 3c888dd5e231c1aebf0da12c71c299242b5c7c7e Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Thu, 21 Mar 2024 11:50:19 +0900 Subject: [PATCH] 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 00000000000..4f7350ce5df --- /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 9edec4107a0..48f9e3faf52 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() ) ) ); + } } } }