Skip to content

Commit

Permalink
Add ServerConnectFailEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta committed Mar 21, 2024
1 parent ffa011c commit 3c888dd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 16 additions & 10 deletions proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() ) ) );
}
}
}
}
Expand Down

0 comments on commit 3c888dd

Please sign in to comment.