2018-12-21 16:24:26 +01:00
From b596e53d71ed3330aba53fb1aa5db81acebd9077 Mon Sep 17 00:00:00 2001
2016-11-06 10:49:42 +01:00
From: Nathan Poirier <nathan@poirier.io>
2016-06-29 06:06:26 +02:00
Date: Tue, 28 Jun 2016 23:00:49 -0500
Subject: [PATCH] Improve ServerKickEvent
ServerKickEvent traditionally will only fire if the server sends a kick packet. During a server shutdown, or server crash this event would not fire for most players. While ServerDisconnectEvent is fired it does not tell us if it was a kick/shutdown/server crash. This improvement fires the ServerKickEvent for server caused disconnections, and adds a Cause to the kick event.
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
2017-08-04 15:57:11 +02:00
index 0e1ef5c4..ee63732d 100644
2016-06-29 06:06:26 +02:00
--- 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
@@ -44,6 +44,12 @@ public class ServerKickEvent extends Event implements Cancellable
* State in which the kick occured.
*/
private State state;
+ // Waterfall start
+ /**
+ * Circumstances which led to the kick.
+ */
+ private Cause cause;
+ // Waterfall end
public enum State
{
@@ -51,6 +57,14 @@ public class ServerKickEvent extends Event implements Cancellable
CONNECTING, CONNECTED, UNKNOWN;
}
+ // Waterfall start
+ public enum Cause
+ {
+
+ SERVER, LOST_CONNECTION, EXCEPTION, UNKNOWN;
+ }
+ // Waterfall end
+
@Deprecated
public ServerKickEvent(ProxiedPlayer player, BaseComponent[] kickReasonComponent, ServerInfo cancelServer)
{
@@ -63,14 +77,23 @@ public class ServerKickEvent extends Event implements Cancellable
this( player, player.getServer().getInfo(), kickReasonComponent, cancelServer, state );
}
+ // Waterfall start
+ @Deprecated
public ServerKickEvent(ProxiedPlayer player, ServerInfo kickedFrom, BaseComponent[] kickReasonComponent, ServerInfo cancelServer, State state)
2018-07-22 18:40:31 +02:00
+ {
2016-06-29 06:06:26 +02:00
+ this( player, kickedFrom, kickReasonComponent, cancelServer, state, Cause.UNKNOWN );
+ }
+
+ public ServerKickEvent(ProxiedPlayer player, ServerInfo kickedFrom, BaseComponent[] kickReasonComponent, ServerInfo cancelServer, State state, Cause cause)
2018-07-22 18:40:31 +02:00
{
2016-06-29 06:06:26 +02:00
this.player = player;
this.kickedFrom = kickedFrom;
this.kickReasonComponent = kickReasonComponent;
this.cancelServer = cancelServer;
this.state = state;
+ this.cause = cause;
}
+ // Waterfall end
@Deprecated
public String getKickReason()
diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
2018-12-21 16:24:26 +01:00
index e7869cfb..c7f081d4 100644
2016-06-29 06:06:26 +02:00
--- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
+++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
2018-12-21 16:24:26 +01:00
@@ -333,7 +333,7 @@ public class ServerConnector extends PacketHandler
2016-06-29 06:06:26 +02:00
public void handle(Kick kick) throws Exception
{
ServerInfo def = user.updateAndGetNextServer( target );
- ServerKickEvent event = new ServerKickEvent( user, target, ComponentSerializer.parse( kick.getMessage() ), def, ServerKickEvent.State.CONNECTING );
+ ServerKickEvent event = new ServerKickEvent( user, target, ComponentSerializer.parse( kick.getMessage() ), def, ServerKickEvent.State.CONNECTING, ServerKickEvent.Cause.SERVER ); // Waterfall
2018-11-04 21:27:05 +01:00
if ( event.getKickReason().toLowerCase( Locale.ROOT ).contains( "outdated" ) && def != null )
2016-06-29 06:06:26 +02:00
{
// Pre cancel the event if we are going to try another server
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
2018-12-21 16:24:26 +01:00
index 54be8954..d0c2ac42 100644
2016-06-29 06:06:26 +02:00
--- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java
+++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java
2018-12-21 16:24:26 +01:00
@@ -9,6 +9,7 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.tree.LiteralCommandNode;
2017-08-10 23:40:40 +02:00
import java.io.DataInput;
+import java.util.Objects; // Waterfall
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
2018-12-21 16:24:26 +01:00
@@ -70,16 +71,19 @@ public class DownstreamBridge extends PacketHandler
2016-06-29 06:06:26 +02:00
return;
}
+ // Waterfall start
2016-11-06 11:17:17 +01:00
ServerInfo def = con.updateAndGetNextServer( server.getInfo() );
- if ( def != null )
2016-06-29 06:06:26 +02:00
+ ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), TextComponent.fromLegacyText( bungee.getTranslation( "server_went_down" ) ), def, ServerKickEvent.State.CONNECTED, ServerKickEvent.Cause.EXCEPTION ) );
+ if ( event.isCancelled() && event.getCancelServer() != null )
{
server.setObsolete( true );
2018-04-02 11:21:24 +02:00
- con.connectNow( def, ServerConnectEvent.Reason.SERVER_DOWN_REDIRECT );
2016-06-29 06:06:26 +02:00
- con.sendMessage( bungee.getTranslation( "server_went_down" ) );
- } else
2018-04-02 11:21:24 +02:00
+ con.connectNow( event.getCancelServer(), ServerConnectEvent.Reason.SERVER_DOWN_REDIRECT );
2016-06-29 06:06:26 +02:00
+ }
+ else
{
- con.disconnect( Util.exception( t ) );
+ con.disconnect0( event.getKickReasonComponent() );
}
+ // Waterfall end
}
@Override
2018-12-21 16:24:26 +01:00
@@ -94,7 +98,19 @@ public class DownstreamBridge extends PacketHandler
2016-06-29 06:06:26 +02:00
if ( !server.isObsolete() )
{
- con.disconnect( bungee.getTranslation( "lost_connection" ) );
+ // Waterfall start
+ ServerInfo def = con.updateAndGetNextServer( server.getInfo() );
+ ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), TextComponent.fromLegacyText( bungee.getTranslation( "lost_connection" ) ), def, ServerKickEvent.State.CONNECTED, ServerKickEvent.Cause.LOST_CONNECTION ) );
+ if ( event.isCancelled() && event.getCancelServer() != null )
+ {
+ server.setObsolete( true );
+ con.connectNow( event.getCancelServer() );
+ }
+ else
+ {
+ con.disconnect0( event.getKickReasonComponent() );
+ }
+ // Waterfall end
}
ServerDisconnectEvent serverDisconnectEvent = new ServerDisconnectEvent( con, server.getInfo() );
2018-12-21 16:24:26 +01:00
@@ -476,7 +492,11 @@ public class DownstreamBridge extends PacketHandler
2016-11-06 11:17:17 +01:00
public void handle(Kick kick) throws Exception
{
ServerInfo def = con.updateAndGetNextServer( server.getInfo() );
2016-06-29 06:06:26 +02:00
- ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), ComponentSerializer.parse( kick.getMessage() ), def, ServerKickEvent.State.CONNECTED ) );
2017-08-10 23:40:40 +02:00
+ if ( Objects.equals( server.getInfo(), def ) )
2016-11-06 11:17:17 +01:00
+ {
+ def = null;
+ }
2016-06-29 06:06:26 +02:00
+ ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), ComponentSerializer.parse( kick.getMessage() ), def, ServerKickEvent.State.CONNECTED, ServerKickEvent.Cause.SERVER ) ); // Waterfall
if ( event.isCancelled() && event.getCancelServer() != null )
{
2018-04-02 11:21:24 +02:00
con.connectNow( event.getCancelServer(), ServerConnectEvent.Reason.KICK_REDIRECT );
2016-06-29 06:06:26 +02:00
--
2018-12-21 16:24:26 +01:00
2.20.1
2016-06-29 06:06:26 +02:00