From e681c8906d1cac01d86ce42147517ae40b49b480 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 16 Mar 2013 21:32:11 +1100 Subject: [PATCH] Add server kick event --- .../bungee/api/event/ServerKickEvent.java | 43 +++++++++++++++++++ .../java/net/md_5/bungee/UserConnection.java | 7 +++ .../bungee/connection/DownstreamBridge.java | 19 ++++++-- 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 api/src/main/java/net/md_5/bungee/api/event/ServerKickEvent.java diff --git a/api/src/main/java/net/md_5/bungee/api/event/ServerKickEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ServerKickEvent.java new file mode 100644 index 00000000..69029048 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/event/ServerKickEvent.java @@ -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; + } +} 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 a055999d..1869ac59 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -101,6 +101,13 @@ public final class UserConnection implements ProxiedPlayer 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) { ServerConnectEvent event = new ServerConnectEvent( this, info ); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index ec1a05ed..e73df465 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -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.event.ChatEvent; 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.Position; 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() ); if ( server.getInfo() != def ) { - con.ch.write( Packet9Respawn.DIM1_SWITCH ); - con.ch.write( Packet9Respawn.DIM2_SWITCH ); - con.connect( def ); + con.connectNow( def ); con.sendMessage( ChatColor.RED + "The server you were previously on went down, you have been connected to the lobby" ); } else { @@ -276,7 +275,19 @@ public class DownstreamBridge extends PacketHandler @Override 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(); }