From 379376f0fa4358d53884906ca10898589fe2a57f Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 13 Feb 2013 18:49:08 +1100 Subject: [PATCH] Add player disconnect event, thanks @lazertester --- .../net/md_5/bungee/api/event/LoginEvent.java | 4 +-- .../api/event/PlayerDisconnectEvent.java | 23 +++++++++++++++++ .../md_5/bungee/api/event/ProxyPingEvent.java | 4 +-- .../bungee/api/event/ServerConnectEvent.java | 4 +-- .../api/event/ServerConnectedEvent.java | 4 +-- .../java/net/md_5/bungee/UserConnection.java | 25 +++++++++++++------ 6 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 api/src/main/java/net/md_5/bungee/api/event/PlayerDisconnectEvent.java diff --git a/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java b/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java index 2da25457..b4451a6f 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java @@ -11,8 +11,8 @@ import net.md_5.bungee.api.plugin.Event; * Event called to represent a player logging in. */ @Data -@ToString(callSuper = true) -@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) public class LoginEvent extends Event implements Cancellable { diff --git a/api/src/main/java/net/md_5/bungee/api/event/PlayerDisconnectEvent.java b/api/src/main/java/net/md_5/bungee/api/event/PlayerDisconnectEvent.java new file mode 100644 index 00000000..c39554c2 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/event/PlayerDisconnectEvent.java @@ -0,0 +1,23 @@ +package net.md_5.bungee.api.event; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.plugin.Event; + +/** + * Called when a player has left the proxy, it is not safe to call any methods + * that perform an action on the passed player instance. + */ +@Data +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) +public class PlayerDisconnectEvent extends Event +{ + + /** + * Player disconnecting. + */ + private final ProxiedPlayer player; +} diff --git a/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java index 49c1b5c5..84552852 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/ProxyPingEvent.java @@ -13,8 +13,8 @@ import net.md_5.bungee.api.plugin.Event; */ @Data @AllArgsConstructor -@ToString(callSuper = true) -@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) public class ProxyPingEvent extends Event { diff --git a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectEvent.java index 2637bc32..8a7b8cfe 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectEvent.java @@ -10,8 +10,8 @@ import net.md_5.bungee.api.plugin.Event; @Data @AllArgsConstructor -@ToString(callSuper = true) -@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) public class ServerConnectEvent extends Event { diff --git a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectedEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectedEvent.java index e8370b88..94e53ea4 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/ServerConnectedEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/ServerConnectedEvent.java @@ -14,8 +14,8 @@ import net.md_5.bungee.api.plugin.Event; * information to the server before the player logs in. */ @Data -@ToString(callSuper = true) -@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) public class ServerConnectedEvent extends Event { 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 bbe14b6e..e67937a0 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -21,6 +21,7 @@ import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.ChatEvent; +import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.packet.*; @@ -51,6 +52,7 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer private final Object permMutex = new Object(); // Hack for connect timings private ServerInfo nextServer; + private volatile boolean clientConnected = true; public UserConnection(Socket socket, PendingConnection pendingConnection, PacketStream stream, Packet2Handshake handshake) { @@ -155,16 +157,23 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer } } - private void destroySelf(String reason) + private synchronized void destroySelf(String reason) { - ProxyServer.getInstance().getPlayers().remove( this ); - - disconnect( reason ); - if ( server != null ) + if ( clientConnected ) { - server.getInfo().removePlayer( this ); - server.disconnect( "Quitting" ); - ProxyServer.getInstance().getReconnectHandler().setServer( this ); + PlayerDisconnectEvent event = new PlayerDisconnectEvent( this ); + ProxyServer.getInstance().getPluginManager().callEvent( event ); + ProxyServer.getInstance().getPlayers().remove( this ); + + disconnect( reason ); + if ( server != null ) + { + server.getInfo().removePlayer( this ); + server.disconnect( "Quitting" ); + ProxyServer.getInstance().getReconnectHandler().setServer( this ); + } + + clientConnected = false; } }