#2376 Add ServerConnectEvent Reason API

This commit is contained in:
Mystiflow 2018-03-14 15:13:10 +00:00 committed by md_5
parent d4bbe0d8db
commit 7241eb37c9
8 changed files with 118 additions and 22 deletions

View File

@ -10,6 +10,7 @@ import net.md_5.bungee.api.SkinConfiguration;
import net.md_5.bungee.api.Title;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.score.Scoreboard;
/**
@ -87,6 +88,15 @@ public interface ProxiedPlayer extends Connection, CommandSender
*/
void connect(ServerInfo target);
/**
* Connects / transfers this user to the specified connection, gracefully
* closing the current one. Depending on the implementation, this method
* might return before the user has been connected.
*
* @param reason the reason for connecting to the new server
*/
void connect(ServerInfo target, ServerConnectEvent.Reason reason);
/**
* Connects / transfers this user to the specified connection, gracefully
* closing the current one. Depending on the implementation, this method
@ -99,6 +109,19 @@ public interface ProxiedPlayer extends Connection, CommandSender
*/
void connect(ServerInfo target, Callback<Boolean> callback);
/**
* Connects / transfers this user to the specified connection, gracefully
* closing the current one. Depending on the implementation, this method
* might return before the user has been connected.
*
* @param target the new server to connect to
* @param callback the method called when the connection is complete, or
* when an exception is encountered. The boolean parameter denotes success
* or failure.
* @param reason the reason for connecting to the new server
*/
void connect(ServerInfo target, Callback<Boolean> callback, ServerConnectEvent.Reason reason);
/**
* Gets the server this player is connected to.
*

View File

@ -34,10 +34,57 @@ public class ServerConnectEvent extends Event implements Cancellable
* Cancelled state.
*/
private boolean cancelled;
private final Reason reason;
@Deprecated
public ServerConnectEvent(ProxiedPlayer player, ServerInfo target)
{
this( player, target, Reason.UNKNOWN );
}
public ServerConnectEvent(ProxiedPlayer player, ServerInfo target, Reason reason)
{
this.player = player;
this.target = target;
this.reason = reason;
}
public enum Reason
{
/**
* Redirection to lobby server due to being unable to connect to
* original server
*/
LOBBY_FALLBACK,
/**
* Execution of a command
*/
COMMAND,
/**
* Redirecting to another server when client loses connection to server
* due to an exception.
*/
SERVER_DOWN_REDIRECT,
/**
* Redirecting to another server when kicked from original server.
*/
KICK_REDIRECT,
/**
* Plugin message request.
*/
PLUGIN_MESSAGE,
/**
* Initial proxy connect.
*/
JOIN_PROXY,
/**
* Plugin initiated connect.
*/
PLUGIN,
/**
* Unknown cause.
*/
UNKNOWN
}
}

View File

@ -1,18 +1,17 @@
package net.md_5.bungee.module.cmd.send;
import com.google.common.collect.ImmutableSet;
import java.util.HashSet;
import java.util.Set;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class CommandSend extends Command implements TabExecutor
{
@ -82,7 +81,7 @@ public class CommandSend extends Command implements TabExecutor
{
if ( player.getServer() != null && !player.getServer().getInfo().equals( target ) )
{
player.connect( target );
player.connect( target, ServerConnectEvent.Reason.COMMAND );
player.sendMessage( ChatColor.GOLD + "Summoned to " + target.getName() + " by " + sender.getName() );
}
}

View File

@ -5,17 +5,18 @@ import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import java.util.Collections;
import java.util.Map;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;
/**
* Command to list and switch a player between available servers.
@ -75,7 +76,7 @@ public class CommandServer extends Command implements TabExecutor
player.sendMessage( ProxyServer.getInstance().getTranslation( "no_server_permission" ) );
} else
{
player.connect( server );
player.connect( server, ServerConnectEvent.Reason.COMMAND );
}
}
}

View File

@ -11,6 +11,7 @@ import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.event.ServerConnectedEvent;
import net.md_5.bungee.api.event.ServerKickEvent;
import net.md_5.bungee.api.event.ServerSwitchEvent;
@ -291,7 +292,7 @@ public class ServerConnector extends PacketHandler
if ( event.isCancelled() && event.getCancelServer() != null )
{
obsolete = true;
user.connect( event.getCancelServer() );
user.connect( event.getCancelServer(), ServerConnectEvent.Reason.KICK_REDIRECT );
throw CancelSendSignal.INSTANCE;
}

View File

@ -190,19 +190,37 @@ public final class UserConnection implements ProxiedPlayer
@Override
public void connect(ServerInfo target)
{
connect( target, null );
connect( target, null, ServerConnectEvent.Reason.PLUGIN );
}
@Override
public void connect(ServerInfo target, ServerConnectEvent.Reason reason)
{
connect( target, null, false, reason );
}
@Override
public void connect(ServerInfo target, Callback<Boolean> callback)
{
connect( target, callback, false );
connect( target, callback, false, ServerConnectEvent.Reason.PLUGIN );
}
@Override
public void connect(ServerInfo target, Callback<Boolean> callback, ServerConnectEvent.Reason reason)
{
connect( target, callback, false, reason );
}
@Deprecated
public void connectNow(ServerInfo target)
{
connectNow( target, ServerConnectEvent.Reason.UNKNOWN );
}
public void connectNow(ServerInfo target, ServerConnectEvent.Reason reason)
{
dimensionChange = true;
connect( target );
connect( target, reason );
}
public ServerInfo updateAndGetNextServer(ServerInfo currentTarget)
@ -227,10 +245,15 @@ public final class UserConnection implements ProxiedPlayer
}
public void connect(ServerInfo info, final Callback<Boolean> callback, final boolean retry)
{
connect( info, callback, retry, ServerConnectEvent.Reason.PLUGIN );
}
public void connect(ServerInfo info, final Callback<Boolean> callback, final boolean retry, ServerConnectEvent.Reason reason)
{
Preconditions.checkNotNull( info, "info" );
ServerConnectEvent event = new ServerConnectEvent( this, info );
ServerConnectEvent event = new ServerConnectEvent( this, info, reason );
if ( bungee.getPluginManager().callEvent( event ).isCancelled() )
{
if ( callback != null )
@ -301,7 +324,7 @@ public final class UserConnection implements ProxiedPlayer
if ( retry && def != null && ( getServer() == null || def != getServer().getInfo() ) )
{
sendMessage( bungee.getTranslation( "fallback_lobby" ) );
connect( def, null, true );
connect( def, null, true, ServerConnectEvent.Reason.LOBBY_FALLBACK );
} else if ( dimensionChange )
{
disconnect( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) );

View File

@ -18,6 +18,7 @@ import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.event.ServerKickEvent;
import net.md_5.bungee.api.score.Objective;
import net.md_5.bungee.api.score.Position;
@ -63,7 +64,7 @@ public class DownstreamBridge extends PacketHandler
if ( def != null )
{
server.setObsolete( true );
con.connectNow( def );
con.connectNow( def, ServerConnectEvent.Reason.SERVER_DOWN_REDIRECT );
con.sendMessage( bungee.getTranslation( "server_went_down" ) );
} else
{
@ -325,7 +326,7 @@ public class DownstreamBridge extends PacketHandler
ServerInfo server = bungee.getServerInfo( in.readUTF() );
if ( server != null )
{
con.connect( server );
con.connect( server, ServerConnectEvent.Reason.PLUGIN_MESSAGE );
}
}
if ( subChannel.equals( "ConnectOther" ) )
@ -457,7 +458,7 @@ public class DownstreamBridge extends PacketHandler
ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), ComponentSerializer.parse( kick.getMessage() ), def, ServerKickEvent.State.CONNECTED ) );
if ( event.isCancelled() && event.getCancelServer() != null )
{
con.connectNow( event.getCancelServer() );
con.connectNow( event.getCancelServer(), ServerConnectEvent.Reason.KICK_REDIRECT );
} else
{
con.disconnect0( event.getKickReasonComponent() ); // TODO: Prefix our own stuff.

View File

@ -37,6 +37,7 @@ import net.md_5.bungee.api.event.PlayerHandshakeEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.event.PreLoginEvent;
import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.http.HttpClient;
import net.md_5.bungee.jni.cipher.BungeeCipher;
@ -518,7 +519,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
server = bungee.getServerInfo( listener.getDefaultServer() );
}
userCon.connect( server, null, true );
userCon.connect( server, null, true, ServerConnectEvent.Reason.JOIN_PROXY );
thisState = State.FINISHED;
}