Add server kick event

This commit is contained in:
md_5 2013-03-16 21:32:11 +11:00
parent ce40391717
commit e681c8906d
3 changed files with 65 additions and 4 deletions

View File

@ -0,0 +1,43 @@
package net.md_5.bungee.api.event;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Cancellable;
import net.md_5.bungee.api.plugin.Event;
/**
* Represents a player getting kicked from a server.
*/
@Data
@ToString(callSuper = false)
@EqualsAndHashCode(callSuper = false)
public class ServerKickEvent extends Event implements Cancellable
{
/**
* Cancelled status.
*/
private boolean cancelled;
/**
* Player being kicked.
*/
private final ProxiedPlayer player;
/**
* Kick reason.
*/
private String kickReason;
/**
* Server to send player to if this event is cancelled.
*/
private ServerInfo cancelServer;
public ServerKickEvent(ProxiedPlayer player, String kickReason, ServerInfo cancelServer)
{
this.player = player;
this.kickReason = kickReason;
this.cancelServer = cancelServer;
}
}

View File

@ -101,6 +101,13 @@ public final class UserConnection implements ProxiedPlayer
connect( target, false ); connect( target, false );
} }
public void connectNow(ServerInfo target)
{
ch.write( Packet9Respawn.DIM1_SWITCH );
ch.write( Packet9Respawn.DIM2_SWITCH );
connect( target );
}
public void connect(ServerInfo info, final boolean retry) public void connect(ServerInfo info, final boolean retry)
{ {
ServerConnectEvent event = new ServerConnectEvent( this, info ); ServerConnectEvent event = new ServerConnectEvent( this, info );

View File

@ -15,6 +15,7 @@ import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.ServerKickEvent;
import net.md_5.bungee.api.scoreboard.Objective; import net.md_5.bungee.api.scoreboard.Objective;
import net.md_5.bungee.api.scoreboard.Position; import net.md_5.bungee.api.scoreboard.Position;
import net.md_5.bungee.api.scoreboard.Score; import net.md_5.bungee.api.scoreboard.Score;
@ -44,9 +45,7 @@ public class DownstreamBridge extends PacketHandler
ServerInfo def = bungee.getServerInfo( con.getPendingConnection().getListener().getDefaultServer() ); ServerInfo def = bungee.getServerInfo( con.getPendingConnection().getListener().getDefaultServer() );
if ( server.getInfo() != def ) if ( server.getInfo() != def )
{ {
con.ch.write( Packet9Respawn.DIM1_SWITCH ); con.connectNow( def );
con.ch.write( Packet9Respawn.DIM2_SWITCH );
con.connect( def );
con.sendMessage( ChatColor.RED + "The server you were previously on went down, you have been connected to the lobby" ); con.sendMessage( ChatColor.RED + "The server you were previously on went down, you have been connected to the lobby" );
} else } else
{ {
@ -276,7 +275,19 @@ public class DownstreamBridge extends PacketHandler
@Override @Override
public void handle(PacketFFKick kick) throws Exception public void handle(PacketFFKick kick) throws Exception
{ {
con.disconnect( "[Kicked] " + kick.message ); ServerInfo def = bungee.getServerInfo( con.getPendingConnection().getListener().getDefaultServer() );
if ( server.getInfo() == def )
{
def = null;
}
ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, kick.message, def ) );
if ( event.isCancelled() && event.getCancelServer() != null )
{
con.connectNow( event.getCancelServer() );
} else
{
con.disconnect( "[Kicked] " + event.getKickReason() );
}
throw new CancelSendSignal(); throw new CancelSendSignal();
} }