From e556fd7150d5b0711cacdda3a77842cb12fdd963 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 16 Jan 2016 12:25:01 +1100 Subject: [PATCH] Add Connection.isConnected()Z API. Idea from kamcio96 in #1693 --- .../md_5/bungee/api/connection/Connection.java | 8 ++++++++ .../java/net/md_5/bungee/ServerConnection.java | 6 ++++++ .../java/net/md_5/bungee/UserConnection.java | 17 ++++++++++------- .../md_5/bungee/connection/InitialHandler.java | 8 +++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/connection/Connection.java b/api/src/main/java/net/md_5/bungee/api/connection/Connection.java index 4e2068b9..eb7e3947 100644 --- a/api/src/main/java/net/md_5/bungee/api/connection/Connection.java +++ b/api/src/main/java/net/md_5/bungee/api/connection/Connection.java @@ -51,6 +51,14 @@ public interface Connection */ void disconnect(BaseComponent reason); + /** + * Gets whether this connection is currently open, ie: not disconnected, and + * able to send / receive data. + * + * @return current connection status + */ + boolean isConnected(); + /** * Get the unsafe methods of this class. * diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index b9e6f0e1..8c1260af 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -77,6 +77,12 @@ public class ServerConnection implements Server return getInfo().getAddress(); } + @Override + public boolean isConnected() + { + return !ch.isClosed(); + } + @Override public Unsafe unsafe() { 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 cd6c5fc1..b843f2ac 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -274,15 +274,12 @@ public final class UserConnection implements ProxiedPlayer { sendMessage( bungee.getTranslation( "fallback_lobby" ) ); connect( def, null, false ); + } else if ( dimensionChange ) + { + disconnect( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) ); } else { - if ( dimensionChange ) - { - disconnect( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) ); - } else - { - sendMessage( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) ); - } + sendMessage( bungee.getTranslation( "fallback_kick", future.cause().getClass().getName() ) ); } } } @@ -592,4 +589,10 @@ public final class UserConnection implements ProxiedPlayer ch.setCompressionThreshold( compressionThreshold ); } } + + @Override + public boolean isConnected() + { + return !ch.isClosed(); + } } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 8dffca38..ce78e198 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -74,7 +74,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection private LoginRequest loginRequest; private EncryptionRequest request; @Getter - private final List registerMessages = new BoundedArrayList<>(128); + private final List registerMessages = new BoundedArrayList<>( 128 ); private State thisState = State.HANDSHAKE; private final Unsafe unsafe = new Unsafe() { @@ -606,4 +606,10 @@ public class InitialHandler extends PacketHandler implements PendingConnection { return "[" + ( ( getName() != null ) ? getName() : getAddress() ) + "] <-> InitialHandler"; } + + @Override + public boolean isConnected() + { + return !ch.isClosed(); + } }